From 87f874e06f12b5190bea17daef833824b9c33c4b Mon Sep 17 00:00:00 2001 From: JianFeeeee Date: Wed, 4 Nov 2026 18:17:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(webui):=207=20=E4=B8=AA=E9=BB=91?= =?UTF-8?q?=E7=AE=B1=E5=88=A4=E6=8D=AE=E8=BF=81=E5=88=B0=20test/=20+=20?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=89=8D=E7=AB=AF=E8=B5=84=E6=BA=90=E5=85=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 迁移了什么 chat_ack_timeout_test.go → test/ chat_attachment_img_test.go → test/ cmd_history_render_test.go → test/ config_namespace_test.go → test/ dashboard_tabs_deps_test.go → test/ plugin_status_render_test.go → test/ starmap_perf_test.go → test/ 包名 `webui` → `test`。`webui/` 目录少 7 个测试文件。 ## 为什么是这 7 个(分类依据) 先按「对未导出符号的依赖数」把 29 个测试文件测了一遍,这 7 个是 **0 依赖**(既不用 handleXxx,也不用 testSDK/newTestHandler 等测试辅助)。 其余 22 个按强度递增留在包内,后续逐步处理。 ## 新增两个导出入口 ### webui.FrontendAsset(name) 返回打包进二进制的前端资源原文(dashboard.js / dashboard.css / mascot.webp / logo.svg …)。 判据要检查「实际会被发出去的那份文本」:dashboard.js 在严格模式下 会不会抛 ReferenceError(用 node 真跑)、mascot.webp 的魔数是否合法、 HTML 属性里有没有残留转义引号。 这些无法通过 HTTP 端点可靠断言(要登录态、且拿到的是拼接后整页, 定位不到单个 asset)。而留在包内测就得读 `dashboardFS` 这个**未导出变量** —— 那正是"测试耦合实现"。导出后,判据变成「输入 asset 名 → 输出字节」, 与资源存在哪个变量、怎么 embed 无关。 ### webui.DashboardPage() 返回组装完成的整页 HTML(外壳 + 内联 CSS/JS)—— 浏览器实际收到的那份。 与 FrontendAsset 的分工:那个取单个源资产,这个取成品。 ## 顺带修掉迁移暴露的两个隐患 ① 三个判据原本 `os.ReadFile("dashboard.js")` 读**磁盘相对路径**。 在包内恰好能跑(工作目录就是包目录),但那读的是**工作区文件**, 不是**二进制里实际嵌入的那份** —— 两者在生产上可能不同 (比如打包后 dashboard.js 被替换)。统一改走 FrontendAsset。 ② starmap_perf_test 的相对路径 `../../../shared/starmap/layout.js` 与 `static/starmap-layout-worker.js` 在移入子目录后失效,已修正为 `../../../../shared/...` 与 `../static/...`。 ## 判据仍有效(变异验证) 把 dashboard.js 里 ack 超时 `}, 600000);` 改成 `}, 30000);`: --- FAIL: TestChatAckTimeoutIsGenerous ★ 受理超时只有 30000 ms —— 太小。 超时太短会让前端 abort,而 agent 仍在正常干活 恢复后全绿。迁移不是搬完就废。 ## 验证 go build ./... PASS go vet ./... PASS gofmt -l 干净 go test ./... -count=1 63 包全过,0 FAIL (webui/test 是新包,故 62 → 63) 干净检出(worktree) build + webui/... 全绿 ## 后续 webui 剩余 22 个文件(依赖 handleXxx / testSDK 等),按强度递增处理 agent/core 739 处白盒引用,先分类再动 CI 补 coverage + staticcheck --- .../plugins/webui/dashboard_js_scope_test.go | 5 - internal/plugins/webui/handler.go | 28 ++++++ .../webui/{ => test}/chat_ack_timeout_test.go | 15 +-- .../{ => test}/chat_attachment_img_test.go | 2 +- .../{ => test}/cmd_history_render_test.go | 2 +- .../webui/{ => test}/config_namespace_test.go | 2 +- .../{ => test}/dashboard_tabs_deps_test.go | 2 +- internal/plugins/webui/test/helpers_test.go | 99 +++++++++++++++++++ .../{ => test}/plugin_status_render_test.go | 2 +- .../webui/{ => test}/starmap_perf_test.go | 14 ++- 10 files changed, 140 insertions(+), 31 deletions(-) rename internal/plugins/webui/{ => test}/chat_ack_timeout_test.go (93%) rename internal/plugins/webui/{ => test}/chat_attachment_img_test.go (99%) rename internal/plugins/webui/{ => test}/cmd_history_render_test.go (99%) rename internal/plugins/webui/{ => test}/config_namespace_test.go (99%) rename internal/plugins/webui/{ => test}/dashboard_tabs_deps_test.go (99%) create mode 100644 internal/plugins/webui/test/helpers_test.go rename internal/plugins/webui/{ => test}/plugin_status_render_test.go (99%) rename internal/plugins/webui/{ => test}/starmap_perf_test.go (95%) diff --git a/internal/plugins/webui/dashboard_js_scope_test.go b/internal/plugins/webui/dashboard_js_scope_test.go index 99a6c87b..c2867f19 100644 --- a/internal/plugins/webui/dashboard_js_scope_test.go +++ b/internal/plugins/webui/dashboard_js_scope_test.go @@ -350,11 +350,6 @@ func TestDashboardJS_NoWriteOnlyGlobals(t *testing.T) { } } -// stripJSComments 剥掉 // 行注释与 /* */ 块注释(粗粒度,但足够用于词频统计)。 -// -// 不做完整的 JS 词法分析:本判据只用于「统计某个标识符出现次数」, -// 粗粒度剥离已经足够,且不会误伤字符串里的 //(本文件没有这类内容, -// 若将来有,宁可少剥也不能多剥 —— 多剥会让判据漏报)。 func stripJSComments(s string) string { var b strings.Builder b.Grow(len(s)) diff --git a/internal/plugins/webui/handler.go b/internal/plugins/webui/handler.go index 32640cbe..392272e2 100644 --- a/internal/plugins/webui/handler.go +++ b/internal/plugins/webui/handler.go @@ -80,6 +80,34 @@ document.getElementById('login-form').addEventListener('submit',async(e)=>{e.pre document.getElementById('password').addEventListener('keydown',function(e){if(e.key==='Enter')document.getElementById('login-form').dispatchEvent(new Event('submit'))}); ` +// FrontendAsset 返回打包进二进制的前端资源原文 +// (dashboard.js / dashboard.css / dashboard.html / mascot.webp / logo.svg)。 +// +// ## 为什么需要导出(2026-11-04) +// +// 前端刻意没有构建链 —— 资源经 go:embed 进二进制。判据要检查的是 +// 「**实际会被发出去的那份文本**」:dashboard.js 在严格模式下会不会抛 +// ReferenceError、mascot.webp 的魔数是否合法、剥离 HTML 后的属性里 +// 有没有残留转义引号。 +// +// 这些无法通过 HTTP 端点可靠断言(要登录态、且拿到的是拼接后的整页, +// 定位不到单个 asset)。而留在包内测就得读 `dashboardFS` 这个未导出变量 +// —— 那正是"测试耦合实现"的形态。 +// +// 导出它,测试就能以「输入 asset 名 → 输出字节」断言, +// 与资源存在哪个变量、怎么 embed 完全无关。 +func FrontendAsset(name string) ([]byte, error) { + return dashboardFS.ReadFile(name) +} + +// DashboardPage 返回**组装完成的控制台整页**(外壳 + 内联 CSS/JS)。 +// +// 这是浏览器实际收到的那份 HTML。与 FrontendAsset 的分工: +// 那个取单个源资产,这个取组装后的成品。两者都是对外产物。 +func DashboardPage() string { + return dashboardHTML +} + // dashboardHTML 是组装好的控制台页面:dashboard.html 外壳 + dashboard.css + dashboard.js。 // // 前端刻意没有构建链,所以拆分的办法是:外壳里留 {{DASHBOARD_CSS}} / {{DASHBOARD_JS}} diff --git a/internal/plugins/webui/chat_ack_timeout_test.go b/internal/plugins/webui/test/chat_ack_timeout_test.go similarity index 93% rename from internal/plugins/webui/chat_ack_timeout_test.go rename to internal/plugins/webui/test/chat_ack_timeout_test.go index 0f33d340..f2e8eab5 100644 --- a/internal/plugins/webui/chat_ack_timeout_test.go +++ b/internal/plugins/webui/test/chat_ack_timeout_test.go @@ -1,4 +1,4 @@ -package webui +package test // 聊天受理超时的判据(2026-10-17 内测故障)。 // @@ -29,24 +29,13 @@ package webui // 没有可注入的时钟。扫描至少能防住"有人把 15s 改回来"。 import ( - "os" - "path/filepath" "regexp" "strconv" "strings" "testing" ) -func dashboardJS(t *testing.T) string { - t.Helper() - // webui 包目录 → 同目录下 dashboard.js - p := filepath.Join("dashboard.js") - b, err := os.ReadFile(p) - if err != nil { - t.Fatalf("读 dashboard.js: %v", err) - } - return string(b) -} +// dashboardJS 见 helpers_test.go 的同名函数(经 webui.FrontendAsset 读嵌入资源)。 // ★★★ 判据:受理超时必须 ≥ 5 分钟。 // diff --git a/internal/plugins/webui/chat_attachment_img_test.go b/internal/plugins/webui/test/chat_attachment_img_test.go similarity index 99% rename from internal/plugins/webui/chat_attachment_img_test.go rename to internal/plugins/webui/test/chat_attachment_img_test.go index dbc30aa9..8fcaef26 100644 --- a/internal/plugins/webui/chat_attachment_img_test.go +++ b/internal/plugins/webui/test/chat_attachment_img_test.go @@ -1,4 +1,4 @@ -package webui +package test // 聊天图片附件的 HTML 必须**能被浏览器正确切分**(2026-10-30)。 // diff --git a/internal/plugins/webui/cmd_history_render_test.go b/internal/plugins/webui/test/cmd_history_render_test.go similarity index 99% rename from internal/plugins/webui/cmd_history_render_test.go rename to internal/plugins/webui/test/cmd_history_render_test.go index afe1f502..0f0251f3 100644 --- a/internal/plugins/webui/cmd_history_render_test.go +++ b/internal/plugins/webui/test/cmd_history_render_test.go @@ -1,4 +1,4 @@ -package webui +package test import ( "strings" diff --git a/internal/plugins/webui/config_namespace_test.go b/internal/plugins/webui/test/config_namespace_test.go similarity index 99% rename from internal/plugins/webui/config_namespace_test.go rename to internal/plugins/webui/test/config_namespace_test.go index f3c059c1..e8886210 100644 --- a/internal/plugins/webui/config_namespace_test.go +++ b/internal/plugins/webui/test/config_namespace_test.go @@ -1,4 +1,4 @@ -package webui +package test import ( "path/filepath" diff --git a/internal/plugins/webui/dashboard_tabs_deps_test.go b/internal/plugins/webui/test/dashboard_tabs_deps_test.go similarity index 99% rename from internal/plugins/webui/dashboard_tabs_deps_test.go rename to internal/plugins/webui/test/dashboard_tabs_deps_test.go index 71f2651b..4f9f5e04 100644 --- a/internal/plugins/webui/dashboard_tabs_deps_test.go +++ b/internal/plugins/webui/test/dashboard_tabs_deps_test.go @@ -1,4 +1,4 @@ -package webui +package test // 判据:「插件」页签的「服务入口」必须真的被请求到数据。 // diff --git a/internal/plugins/webui/test/helpers_test.go b/internal/plugins/webui/test/helpers_test.go new file mode 100644 index 00000000..4d9dca16 --- /dev/null +++ b/internal/plugins/webui/test/helpers_test.go @@ -0,0 +1,99 @@ +package test + +// 本文件是 test 包的共享测试辅助(不属于任何单个判据)。 +// +// 放在这里而不是某个 *_test.go 里:多个判据文件都要用它, +// 而在"测试与源码分离"之后,它们不能再从包内符号借用。 + +import ( + "strings" + "testing" + + "github.com/JianFeeeee/HologramAgent/internal/plugins/webui" +) + +// stripJSComments 剥掉 // 行注释与 /* */ 块注释(粗粒度,但足够用于词频统计)。 +// +// 不做完整的 JS 词法分析:本判据只用于「统计某个标识符出现次数」, +// 粗粒度剥离已经足够,且不会误伤字符串里的 //(本文件没有这类内容, +// 若将来有,宁可少剥也不能多剥 —— 多剥会让判据漏报)。 +func stripJSComments(s string) string { + var b strings.Builder + b.Grow(len(s)) + inLine, inBlock := false, false + var inStr byte + for i := 0; i < len(s); i++ { + c := s[i] + if inLine { + if c == '\n' { + inLine = false + b.WriteByte(c) + } + continue + } + if inBlock { + if c == '*' && i+1 < len(s) && s[i+1] == '/' { + inBlock = false + i++ + } + continue + } + if c == '\n' { + b.WriteByte(c) + continue + } + if inStr != 0 { + b.WriteByte(c) + if c == '\\' { + if i+1 < len(s) { + b.WriteByte(s[i+1]) + i++ + } + continue + } + if c == inStr { + inStr = 0 + } + continue + } + if c == '"' || c == '\'' || c == '`' { + // 反引号也是字符串定界符,但它可跨行 —— 本判据只统计标识符, + // 跨行模板串里出现标识符属正常,此处按普通字符串处理即可。 + inStr = c + b.WriteByte(c) + continue + } + if c == '/' && i+1 < len(s) && s[i+1] == '/' { + inLine = true + i++ + continue + } + if c == '/' && i+1 < len(s) && s[i+1] == '*' { + inBlock = true + i++ + continue + } + b.WriteByte(c) + } + return b.String() +} + +// dashboardJSSource 取出待测的前端脚本原文。 +// +// 经导出的 webui.FrontendAsset 读 —— 与"资源存在哪个变量、怎么 embed" +// 完全无关,测试只关心「实际会被发出去的那份文本」。 +func dashboardJSSource(t *testing.T) string { + t.Helper() + b, err := webui.FrontendAsset("dashboard.js") + if err != nil { + t.Fatalf("读 dashboard.js: %v", err) + } + return string(b) +} + +// dashboardJS 与 dashboardJSSource 同义,保留两个名字是因为 +// 原来的判据文件各自用其中一个(迁移时不做无谓重命名)。 +func dashboardJS(t *testing.T) string { + t.Helper() + return dashboardJSSource(t) +} diff --git a/internal/plugins/webui/plugin_status_render_test.go b/internal/plugins/webui/test/plugin_status_render_test.go similarity index 99% rename from internal/plugins/webui/plugin_status_render_test.go rename to internal/plugins/webui/test/plugin_status_render_test.go index 36f3b1ce..6b3dffc9 100644 --- a/internal/plugins/webui/plugin_status_render_test.go +++ b/internal/plugins/webui/test/plugin_status_render_test.go @@ -1,4 +1,4 @@ -package webui +package test import ( "strings" diff --git a/internal/plugins/webui/starmap_perf_test.go b/internal/plugins/webui/test/starmap_perf_test.go similarity index 95% rename from internal/plugins/webui/starmap_perf_test.go rename to internal/plugins/webui/test/starmap_perf_test.go index 49ce0fe0..0d8ba9d6 100644 --- a/internal/plugins/webui/starmap_perf_test.go +++ b/internal/plugins/webui/test/starmap_perf_test.go @@ -1,4 +1,4 @@ -package webui +package test import ( "os" @@ -93,7 +93,7 @@ func TestStarmapNoPerFrameFullScan(t *testing.T) { checkStarmapSharedBlock(t, src) // Worker 脚本必须存在且用同一套网格算法 - wb, err := os.ReadFile(filepath.Join("static", "starmap-layout-worker.js")) + wb, err := os.ReadFile(filepath.Join("..", "static", "starmap-layout-worker.js")) if err != nil { t.Fatalf("缺少 static/starmap-layout-worker.js: %v", err) } @@ -130,13 +130,11 @@ func extractFunc(t *testing.T, src, name string) string { return rest[:end] } +// readDashboardJS 经导出的 webui.FrontendAsset 读嵌入资源 +// (原先读磁盘相对路径 "dashboard.js",迁移到 test 包后失效)。 func readDashboardJS(t *testing.T) string { t.Helper() - b, err := os.ReadFile("dashboard.js") - if err != nil { - t.Fatal(err) - } - return string(b) + return dashboardJSSource(t) } // checkStarmapSharedBlock 校验本文件里的共享块与 shared/starmap/layout.js 逐字节一致。 @@ -156,7 +154,7 @@ func checkStarmapSharedBlock(t *testing.T, src string) { } block := src[b : e+len(end)] // 共享源必须被完整包进块里(抽首尾两段特征串判,避免把整份源码抄进断言)。 - shared, err := os.ReadFile(filepath.Join("..", "..", "..", "shared", "starmap", "layout.js")) + shared, err := os.ReadFile(filepath.Join("..", "..", "..", "..", "shared", "starmap", "layout.js")) if err != nil { t.Fatalf("读共享源失败: %v", err) }