Files
memoryweave/go/internal/api/routes/nsdebug_ghost_test.go
T
小唯 fba425af80 fix(recall/debug): 空结果分支补齐 query/count,响应契约两分支一致 (t_nsdebug-20261109)
诊断端点的两条早期返回(encode 失败 / 0 候选)只回 steps+results+total_ms,
缺 query 与 count ⇒ 消费方读 count 得 nil,与正常分支形状不同(又一次「诊断自成一派」)。
测试:ghost ns 用例改为断言 count 键**必须存在**且为 0。
2026-11-09 19:49:02 +08:00

92 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package routes
// 2026-11-09 t_nsdebug:/api/v1/recall/debug 的 ghost-ns 判据回归。
// 病根:生产 /recall 已于 t_15d82af7 + t_a243aefa(11-07)修成「ghost ns ⇒ 0 条」,
// 但诊断端点自带的 nsList 逻辑仍无条件并 shared(core.go「五修」段),
// 实测 ghost ns 报 "5 candidates from 2 namespaces" —— 诊断端点撒谎比没有诊断更危险。
// 判据:diagnose 与生产同判据(storage.NamespaceHasRowsFor),真实 ns 的 [ns, shared] 语义不变。
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/xiaoxue/memoryweave/internal/governance"
"github.com/xiaoxue/memoryweave/internal/models"
"github.com/xiaoxue/memoryweave/internal/storage"
)
func runRecallDebugHTTP(t *testing.T, db storage.LanceDB, body string) (int, map[string]interface{}) {
t.Helper()
saved := storage.SearchCacheInstance
storage.SearchCacheInstance = nil
t.Cleanup(func() { storage.SearchCacheInstance = saved })
srv := fakeEmbedServer(t)
emb := storage.NewEmbedder(srv.URL)
rerank := storage.NewReranker("", emb)
api := NewAPI(db, emb, rerank, nil, governance.NewInMemoryGraph())
req := httptest.NewRequest("POST", "/api/v1/recall/debug", strings.NewReader(body))
w := httptest.NewRecorder()
api.RecallDebug(w, req)
var m map[string]interface{}
_ = json.Unmarshal(w.Body.Bytes(), &m)
return w.Code, m
}
func annDetail(m map[string]interface{}) string {
steps, _ := m["steps"].([]interface{})
for _, x := range steps {
s, _ := x.(map[string]interface{})
if s["step"] == "ann_search" {
d, _ := s["detail"].(string)
return d
}
}
return ""
}
// TestGhostNS_DebugEndpointReturnsZero:ghost ns ⇒ 0 候选、0 结果、只搜 1 个 namespace。
func TestGhostNS_DebugEndpointReturnsZero(t *testing.T) {
db := &nsAwareRecallLance{rows: []models.MemoryRecord{
{ID: "sh_1", Namespace: "shared", Category: "distilled", Content: "共享事实一"},
{ID: "sh_2", Namespace: "shared", Category: "distilled", Content: "共享事实二"},
{ID: "wiki_1", Namespace: "wiki-curator-main", Category: "distilled", Content: "笔记知识一"},
}}
code, m := runRecallDebugHTTP(t, db, `{"query":"ghost ns 诊断端点探针","limit":5,"namespace":"ghost-ns-debug-2026-11"}`)
if code != 200 {
t.Fatalf("HTTP=%d body=%v", code, m)
}
if n, ok := m["count"]; !ok {
t.Fatalf("✗ 空结果分支也必须带 count 键(诊断响应契约一致性),实际 body=%v", m)
} else if n.(float64) != 0 {
t.Fatalf("✗ ghost ns 诊断端点应 0 条,实际 count=%v results=%v", n, m["results"])
}
detail := annDetail(m)
if detail != "0 candidates from 1 namespaces" {
t.Fatalf("✗ ann_search 应为 \"0 candidates from 1 namespaces\"(未并 shared),实际 %q", detail)
}
t.Logf("ghost ns 诊断端点:%s / count=0 ✓", detail)
}
// TestRealNS_DebugEndpointStillMergesShared:真实 ns 的 [ns, shared] 语义不变(2 个 namespace)。
func TestRealNS_DebugEndpointStillMergesShared(t *testing.T) {
db := &nsAwareRecallLance{rows: []models.MemoryRecord{
{ID: "own_1", Namespace: "hermes-main", Category: "distilled", Content: "本域事实一"},
{ID: "sh_1", Namespace: "shared", Category: "distilled", Content: "共享事实一"},
}}
code, m := runRecallDebugHTTP(t, db, `{"query":"真实 ns 诊断端点对照","limit":5,"namespace":"hermes-main"}`)
if code != 200 {
t.Fatalf("HTTP=%d body=%v", code, m)
}
detail := annDetail(m)
if detail != "2 candidates from 2 namespaces" {
t.Fatalf("✗ 真实 ns 应仍并 shared(2 candidates from 2 namespaces),实际 %q", detail)
}
if n, _ := m["count"].(float64); n == 0 {
t.Fatalf("✗ 真实 ns 诊断端点不应 0 条:%v", m)
}
t.Logf("真实 ns 诊断端点:%s / count=%v ✓", detail, m["count"])
}