fix(test): 合并两个 TestMain 为单一合并体(t_e5997ca4 落盘点护栏 + t_b7a8ed9c 打分缓存护栏)

两卡分支各自在 routes 包加了 TestMain,合入 main 后 multiple definitions of TestMain
⇒ routes 包 setup failed(全套测试拦下)。按 t_b7a8ed9c 文件顶部的合并提示,把两个
TestMain 体合进 testmain_test.go 并删除 scorecache_guard_test.go。

验收(小怡实跑):
- go test ./internal/api/routes/ → ok 50.344s(修前 setup failed)
- E2E TestCommitConflictGate_E2E → PASS 3.70s;日志 '[scorecache] 已禁用(ZHIYI_SCORE_CACHE=0)'
- 测试不再污染生产 Redis:distill-score 键数 3096 → 3096(不变)
This commit is contained in:
小唯
2026-10-17 22:53:43 +08:00
parent 42fafc495f
commit 25f68831fd
2 changed files with 32 additions and 32 deletions
@@ -1,28 +0,0 @@
// 织忆 MemoryWeave — routes 包测试护栏(t_b7a8ed9c):默认关闭 distill 打分缓存。
//
// 背景:distill 打分记忆化(0dc571d)+ Redis L2(36db9bb,键 zhiyi:distill-score:*,
// TTL 168h)后,**本包 E2E 测试(zz_commit_conflict_gate_test.go)会读写生产 Redis**:
// 第一次 run L2 miss → 真调 mock LLM 并把「测试内容哈希 → 结果」写进生产;
// 7 天内后续 run L2 hit → LLM 被静默短路 → 数 LLM 调用次数的断言(base+1)必挂。
// 实测 2026-10-17:TestCommitConflictGate_E2E 16.5s 固定 FAIL,
// 「expected >= 1 LLM distill calls, got 0」,生产键 TTL≈40h 残留即污染源。
//
// 治法(对齐项目门禁规矩③:加缓存必须给该包 TestMain 加关闭开关):
// TestMain 里 Setenv ZHIYI_SCORE_CACHE=0 —— distill.getScoreCache 的 Once 闭包
// 活读 env(包级变量在 init 时已定型,跨包设不了),本进程内缓存整体禁用:
// 不读 L2(LLM 真被调用,计数断言有覆盖)、不写 L2(不再污染生产)。
//
// ⚠️ 合并提示:t_e5997ca4 分支在 routes 加了 testmain_test.go(ZHIYI_DASHBOARD_PATH
// 护栏,另一个 TestMain)。两卡分支合并到 main 后同包两个 TestMain 编译冲突,
// 合并时必须把两个 TestMain 体合并进一个文件。
package routes
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
_ = os.Setenv("ZHIYI_SCORE_CACHE", "0")
os.Exit(m.Run())
}
+32 -4
View File
@@ -1,8 +1,32 @@
// 织忆 MemoryWeave — 路由层测试隔离(t_e5997ca4 起)
// 织忆 MemoryWeave — routes 包测试隔离(两个护栏合并 · t_e5997ca4 + t_b7a8ed9c)
//
// 单元测试不得写到生产落盘点。selfoptimize 包已有同款 TestMain 先例
// (曾实测:整套测试跑完把 /var/lib/memoryweave/dashboard.json 写成测试数字)。
// ⚠️ 本文件是**两个 TestMain 的合并体**。两卡分支各自加了一个 TestMain:
// · t_e5997ca4 → testmain_test.go(落盘点护栏)
// · t_b7a8ed9c → scorecache_guard_test.go(打分缓存护栏)
// 在各自分支上互不干扰,合并到 main 后同包两个 TestMain **编译冲突**
// (multiple definitions of TestMain)⇒ 必须合进一个文件。
// (此点由 t_b7a8ed9c 在 scorecache_guard_test.go 顶部预先标注;小怡合并时未读文件
// 而漏看,由全套测试的 setup failed 拦下 —— 见 worklog 教训。)
//
// 护栏①:单元测试不得写到生产落盘点(t_e5997ca4)
// ---------------------------------------------------------------
// selfoptimize 包已有同款 TestMain 先例(曾实测:整套测试跑完把
// /var/lib/memoryweave/dashboard.json 写成测试数字)。
// gap close/repair 端点会触发 Dash.persist()(本地 JSON 兜底)⇒ 统一重定向到临时目录。
//
// 护栏②:默认关闭 distill 打分缓存(t_b7a8ed9c)
// ---------------------------------------------------------------
// 背景:distill 打分记忆化(0dc571d)+ Redis L2(36db9bb,键 zhiyi:distill-score:*,
// TTL 168h)后,**本包 E2E 测试(zz_commit_conflict_gate_test.go)会读写生产 Redis**:
// 第一次 run L2 miss → 真调 mock LLM 并把「测试内容哈希 → 结果」写进生产;
// 7 天内后续 run L2 hit → LLM 被静默短路 → 数 LLM 调用次数的断言(base+1)必挂。
// 实测 2026-10-17:TestCommitConflictGate_E2E 16.5s 固定 FAIL,
// 「expected >= 1 LLM distill calls, got 0」。
//
// 治法(对齐项目门禁规矩③:加缓存必须给该包 TestMain 加关闭开关):
// Setenv ZHIYI_SCORE_CACHE=0 —— distill.getScoreCache 的 Once 闭包**活读 env**
// (包级变量在 init 时已定型,跨包设不了),本进程内缓存整体禁用:
// 不读 L2(LLM 真被调用,计数断言有覆盖)、不写 L2(不再污染生产)。
package routes
import (
@@ -12,12 +36,16 @@ import (
)
func TestMain(m *testing.M) {
// 护栏①:落盘点重定向到临时目录(宁可测试直接失败,也不写生产落盘点)
dir, err := os.MkdirTemp("", "routes-test-dash-*")
if err != nil {
// 宁可测试直接失败,也不写生产落盘点
panic(err)
}
_ = os.Setenv("ZHIYI_DASHBOARD_PATH", filepath.Join(dir, "dashboard.json"))
// 护栏②:关闭 distill 打分缓存(不读/不写生产 Redis L2)
_ = os.Setenv("ZHIYI_SCORE_CACHE", "0")
code := m.Run()
_ = os.RemoveAll(dir)
os.Exit(code)