crash.log 实锤:panic: assignment to entry in nil map, docTranslation(hover 渲染帧内)。 合并判空是错的:loadTranslations 只装 a.trans,缓存有数据后 a.trans 永远非 nil,跳过了 transInflight 初始化——hover 一个 未缓存符号就往 nil map 写,主线程渲染帧直接炸。两次崩溃同因。 修复:两个 map 分别判空;loadTranslations 同时初始化两个。 回归测试:TestDocTranslationNilInflightWithLoadedCache。
97 lines
3.9 KiB
Go
97 lines
3.9 KiB
Go
package app
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const hoverSample = "func fmt.Println(a ...any) (n int, err error)\n" +
|
|
"Println formats using the default formats for its operands and writes to standard output.\n" +
|
|
"Spaces are always added between operands and a newline is appended.\n" +
|
|
"\n" +
|
|
"[`fmt.Println` on pkg.go.dev](https://pkg.go.dev/fmt#Println)"
|
|
|
|
// TestSplitHoverDoc 验证悬停文本的拆行保护:签名行与链接行不进翻译段。
|
|
// 结构对齐 gopls 的真实 hover 输出(markdownToText 之后)。
|
|
func TestSplitHoverDoc(t *testing.T) {
|
|
joined, first, hs := splitHoverDoc(hoverSample)
|
|
|
|
want := "Println formats using the default formats for its operands and writes to standard output.\n" +
|
|
"Spaces are always added between operands and a newline is appended."
|
|
if joined != want {
|
|
t.Fatalf("翻译段不对:\n got %q\nwant %q", joined, want)
|
|
}
|
|
if first != 1 {
|
|
t.Fatalf("第一个正文行 = %d, want 1", first)
|
|
}
|
|
wantMask := []bool{false, true, true, false, false}
|
|
if len(hs.mask) != len(wantMask) {
|
|
t.Fatalf("行数 = %d, want %d", len(hs.mask), len(wantMask))
|
|
}
|
|
for i, m := range wantMask {
|
|
if hs.mask[i] != m {
|
|
t.Errorf("行 %d (%q) mask = %v, want %v", i, hs.lines[i], hs.mask[i], m)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestHoverTranslationAssembly 验证拼回:译文插到正文位置,签名与链接原样。
|
|
// 预填翻译缓存绕过 LLM 与窗口依赖;key 用 normDocKey(与运行时一致)。
|
|
func TestHoverTranslationAssembly(t *testing.T) {
|
|
docJoined, _, _ := splitHoverDoc(hoverSample)
|
|
a := &App{trans: map[string]string{
|
|
normDocKey(docJoined): "Println 用其操作数的默认格式进行格式化并写入标准输出,操作数之间总会添加空格,并附加换行。",
|
|
}, transInflight: map[string]bool{}, set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"}}
|
|
|
|
got := a.hoverTranslation(hoverSample)
|
|
want := "func fmt.Println(a ...any) (n int, err error)\n" +
|
|
"Println 用其操作数的默认格式进行格式化并写入标准输出,操作数之间总会添加空格,并附加换行。\n" +
|
|
"\n" +
|
|
"[`fmt.Println` on pkg.go.dev](https://pkg.go.dev/fmt#Println)"
|
|
if got != want {
|
|
t.Fatalf("拼回结果不对:\n got %q\nwant %q", got, want)
|
|
}
|
|
}
|
|
|
|
// TestHoverTranslationUnconfigured 未配置时必须原样返回。
|
|
func TestHoverTranslationUnconfigured(t *testing.T) {
|
|
a := &App{trans: map[string]string{}, transInflight: map[string]bool{}}
|
|
if got := a.hoverTranslation(hoverSample); got != hoverSample {
|
|
t.Fatalf("未配置应返回原文, got %q", got)
|
|
}
|
|
}
|
|
|
|
// TestHoverTranslationInProgress 翻译中(缓存未命中且已在请求里)返回原文。
|
|
func TestHoverTranslationInProgress(t *testing.T) {
|
|
a := &App{trans: map[string]string{}, transInflight: map[string]bool{}, set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"}}
|
|
joined, _, _ := splitHoverDoc(hoverSample)
|
|
a.transInflight[joined] = true
|
|
a.win = nil // 若误触后台请求立即暴露;docTranslation 发请求前不应用到 win
|
|
got := a.hoverTranslation(hoverSample)
|
|
if !strings.Contains(got, "Println formats") {
|
|
t.Fatalf("翻译中应显示原文, got %q", got)
|
|
}
|
|
}
|
|
|
|
// TestDocTranslationNilInflightWithLoadedCache 复现闪退:启动时装载了历史
|
|
// 缓存(a.trans 非 nil)但 transInflight 仍为 nil,此时 hover 一个未缓存的
|
|
// 文档——不得往 nil map 写(crash.log: assignment to entry in nil map)。
|
|
func TestDocTranslationNilInflightWithLoadedCache(t *testing.T) {
|
|
a := &App{
|
|
trans: map[string]string{"cached": "已缓存译文"}, // 只有 trans 被装载
|
|
set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"},
|
|
}
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Fatalf("不得 panic: %v", r)
|
|
}
|
|
}()
|
|
got := a.docTranslation("some uncached doc text")
|
|
if got != "some uncached doc text" {
|
|
t.Fatalf("未缓存应返回原文, got %q", got)
|
|
}
|
|
if a.transInflight == nil {
|
|
t.Fatal("transInflight 应被初始化")
|
|
}
|
|
}
|