懒翻译要等 1-2 秒。真 gopls 实证 go/doc 从本地源码提取的文档与 gopls 补全/悬停正文规范化空白后完全一致,批量预热方案成立: - internal/godoc: go/doc 提取器 + 36 个常用标准库清单 + NormKey - app.PreheatDocs: 6 并发翻译,进度走输出面板,已缓存自动跳过; 菜单手动触发 + 打开项目 8 秒后自动跑(每会话一次) - tools/preheat: 命令行批量工具,直接写用户缓存文件 - 缓存 key 统一 NormKey,预热/懒翻译/旧缓存三方互命中 - 已实测灌入 10 包 394 条译文(272 秒),常用包全部秒出
75 lines
3.1 KiB
Go
75 lines
3.1 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)
|
|
}
|
|
}
|