mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-05 16:57:32 +00:00
refactor(webui): 图谱判据迁到 test/ —— 导出 GraphDataForVisual 一层转换
graph_source_test.go → test/
handler_graph_pulse_test.go → test/ (7 条判据)
webui/ 174 → 167
webui/test/ 28 → 35
合计 202 不变
## 这一条必须动生产代码,理由要写清
判据想测的是「GraphData 的 raw map 被裁成可视化视图时,稠密向量必须被剥掉」。
两条路都不通:
走 HTTP 端点 /api/v1/memory/graph
→ 端点输入来自 h.memory.GraphData(),h.memory 是私有字段,
包外无法注入合成数据。测不了。
包内测试直接调 graphDataForVisual
→ 这正是要摆脱的白盒写法。
所以导出这一层转换 `graphDataForVisual → GraphDataForVisual`。它不是
为测试造出来的形状:函数的输入输出本来就是本包唯一的「raw map →
visual map」语义,端点在 handler_memory.go:199 调它。导出的是真实边界,
不是内部细节。(判据 §5.3「不要为满足测试导出内部」指的是后者。)
顺带把 `graphBlockView`(私有 JSON 结构)在测试侧改成匿名结构体 ——
它只用于反序列化断言,没有必要引用包内类型。
## 改走真实路由暴露的两个事实
### ① `newGraphHandler` 直接设 `&Handler{memory: m}`
改成 `NewHandler(sdk.New("webui", sdk.SDKConfig{Memory: m, ...}))` ——
`Memory` 确实能经 SDKConfig 注入(plugin.go:457 `base.SetMemoryAPI(cfg.Memory)`)。
### ② 503 暴露了鉴权要求
改走路由后立刻:
status = 503, want 200
`requireAPI` 需要 api_key,而最小装配没配。补 `seedWebUIConfig` 后 200。
之前直接调 handler 的写法**从来不知道有这个要求** —— 路由层挡掉的东西,
绕过路由就看不见。
## 判据有效性(变异验证)
在 `GraphDataForVisual` 里把裁剪短路掉(`if blocks != nil { return out }`),
实测红,且输出直接暴露泄漏内容:
--- FAIL: TestHandleMemoryGraph_StripsVector
response still contains vector field:
{"data":{"memory_blocks":[{"id":"b_old",...,"vector":[0.1,0.2,0.3],...
"success":true}
稠密向量确实会被下发到前端。恢复后全绿。
## 验证
go build ./... PASS
go vet ./... PASS
gofmt -l 干净
go test ./... -count=1 67 包全过,0 FAIL
This commit is contained in:
@ -143,12 +143,18 @@ type graphBlockView struct {
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// graphDataForVisual 把 GraphData 的原始 map 裁成可视化视图(去掉稠密向量)。
|
||||
// GraphDataForVisual 把 GraphData 的原始 map 裁成可视化视图(去掉稠密向量)。
|
||||
//
|
||||
// ★ 导出(2026-11-04):这是本包唯一「输入 raw map → 输出 visual map」的
|
||||
// 纯转换,也是判据 `TestGraphDataExposesBlocksForStarmap` 想测的东西。
|
||||
// 走 HTTP 端点测不了它 —— 端点的输入来自 h.memory.GraphData(),而 h.memory
|
||||
// 是私有字段,包外无法注入合成数据。导出这层转换比导出整个 Handler 的
|
||||
// 装配更小、也更贴合它真实的对外语义。
|
||||
//
|
||||
// 用 map 断言而不是泛型/反射:GraphData 返回 map[string]interface{},
|
||||
// 里面的具体类型是包内私有的 graphEntity/[]*memory.MemoryBlock,
|
||||
// 断言不中就原样透传(宁可多发也不让接口挂掉)。
|
||||
func graphDataForVisual(data map[string]interface{}) map[string]interface{} {
|
||||
func GraphDataForVisual(data map[string]interface{}) map[string]interface{} {
|
||||
out := make(map[string]interface{}, len(data))
|
||||
for k, v := range data {
|
||||
out[k] = v
|
||||
@ -196,7 +202,7 @@ func (h *Handler) handleMemoryGraph(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"success": true, "data": graphDataForVisual(data)})
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"success": true, "data": GraphDataForVisual(data)})
|
||||
}
|
||||
|
||||
// handleMemoryGraphPulse 是给星图「跟随 agent 动」用的**轻量**活动端点。
|
||||
|
||||
Reference in New Issue
Block a user