diff --git a/internal/app/completion_test.go b/internal/app/completion_test.go index e63db33..0488d33 100644 --- a/internal/app/completion_test.go +++ b/internal/app/completion_test.go @@ -72,3 +72,25 @@ func TestHoverTranslationInProgress(t *testing.T) { 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 应被初始化") + } +} diff --git a/internal/app/translate.go b/internal/app/translate.go index f61c21b..bba318d 100644 --- a/internal/app/translate.go +++ b/internal/app/translate.go @@ -36,8 +36,13 @@ func (a *App) docTranslation(text string) string { if a.transInflight[key] { return text } + // 两个 map 分别判空:loadTranslations 只装 a.trans(有历史缓存时它 + // 非 nil),transInflight 仍是 nil——之前合并判空导致往 nil map 写, + // 渲染帧里直接 panic 闪退(crash.log 实锤)。 if a.trans == nil { a.trans = map[string]string{} + } + if a.transInflight == nil { a.transInflight = map[string]bool{} } a.transInflight[key] = true @@ -98,6 +103,7 @@ func (a *App) loadTranslations() { return } a.trans = map[string]string{} + a.transInflight = map[string]bool{} for k, v := range m { if k != "" && v != "" { // 旧版缓存 key 是原文,统一规范化,能被预热与懒翻译共同命中。