Files
memoryweave/go/internal/api/routes/entity_resolve_test.go
T
小唯 f26126765c graph: 跨层实体并集 + PathEnds 双键容错 + 邻域展开上限(修 CPU 打爆)
- entity_resolve.go: 新增 ResolveEntityIDs / MergePaths / PathEnds / ExtractEntities
- graph_sqlite.go: 新增 NodeIDsByName 精确同名查询(SearchNodes 是 name LIKE '%x%'
  LIMIT 100,'织忆' 有 834 条子串命中会把精确同名挤出去 → 并集静默失效)
- graph_cache.go: 转发 NodeIDsByName(包装器不转发 → 类型断言失败 → 静默降级回 LIKE 路径)
- server.go: notes 路径接并集(union=1 opt-in / 0 关闭,响应带 union + entity_ids_used);
  navigate 结果统一走 routes.PathEnds —— governance.Navigate 返回的是 from/to,
  此前读 source/target 导致并集路径与「邻居进笔记匹配」被整条静默丢弃;
  邻域进笔记匹配加 maxMatchEntities=24 上限(2026-09-27 事故:无上限邻域展开
  → 数千实体 × 全库笔记扫描 → CPU 159% / HTTP 超时)
- 单测 5 条,含 from/to 键容错回归
2026-09-27 09:23:03 +08:00

171 lines
5.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
import (
"strings"
"testing"
)
// fakeSearcher 模拟 SearchNodes 的 LIKE 语义返回,用于验证精确匹配过滤。
type fakeSearcher struct {
nodes []map[string]interface{}
calls int
}
func (f *fakeSearcher) SearchNodes(label, namespace string) []map[string]interface{} {
f.calls++
out := []map[string]interface{}{}
for _, n := range f.nodes {
nm, _ := n["name"].(string)
id, _ := n["id"].(string)
if strings.Contains(nm, label) || strings.Contains(id, label) {
out = append(out, n)
}
}
return out
}
// 同名两层:旧 entity(id=名字)+ 新 concept(id=n_名字)→ 都应返回,且顺序稳定。
func TestResolveEntityIDs_TwoLayers(t *testing.T) {
gs := &fakeSearcher{nodes: []map[string]interface{}{
{"id": "织忆", "name": "织忆", "type": "entity"},
{"id": "n_织忆", "name": "织忆", "type": "concept"},
}}
ids := ResolveEntityIDs(gs, "织忆")
if len(ids) != 2 {
t.Fatalf("want 2 ids (两层), got %v", ids)
}
if ids[0] != "织忆" || ids[1] != "n_织忆" {
t.Fatalf("unexpected order: %v", ids)
}
}
// LIKE 子串噪声必须被挡掉:查「织忆」不应带出「织忆召回」。
func TestResolveEntityIDs_RejectsSubstringNoise(t *testing.T) {
gs := &fakeSearcher{nodes: []map[string]interface{}{
{"id": "织忆", "name": "织忆"},
{"id": "织忆召回机制", "name": "织忆召回机制"},
}}
ids := ResolveEntityIDs(gs, "织忆")
for _, id := range ids {
if id == "织忆召回机制" {
t.Fatalf("子串噪声未被过滤: %v", ids)
}
}
if len(ids) != 1 {
t.Fatalf("want 1 (仅精确), got %v", ids)
}
}
// 图谱查不到时回退为名字本身(按 id 直查仍可用)。
func TestResolveEntityIDs_FallbackToName(t *testing.T) {
gs := &fakeSearcher{}
ids := ResolveEntityIDs(gs, "不存在的实体")
if len(ids) != 1 || ids[0] != "不存在的实体" {
t.Fatalf("want fallback to name, got %v", ids)
}
}
// nil store / 空名字不外抛。
func TestResolveEntityIDs_NilAndEmpty(t *testing.T) {
if got := ResolveEntityIDs(nil, "织忆"); len(got) != 1 || got[0] != "织忆" {
t.Fatalf("nil store 应回退名字: %v", got)
}
if got := ResolveEntityIDs(nil, " "); got != nil {
t.Fatalf("空名字应返回 nil, got %v", got)
}
}
// 去重:SearchNodes 同时按 name 与 id 命中同一节点时只返回一次。
func TestResolveEntityIDs_Dedup(t *testing.T) {
gs := &fakeSearcher{nodes: []map[string]interface{}{
{"id": "n_织忆", "name": "织忆"},
{"id": "n_织忆", "name": "n_织忆"},
}}
ids := ResolveEntityIDs(gs, "织忆")
// n_织忆 命中(name 相等);名字回退也加入 ⇒ 共 2 条且不重复
if len(ids) != 2 || ids[0] != "n_织忆" || ids[1] != "织忆" {
t.Fatalf("dedup 结果异常: %v", ids)
}
}
// fakeExact 同时实现 NodeSearcher 与 ExactNodeFinder:验证优先走精确 SQL。
// 模拟真实场景:SearchNodes(LIKE)被 834 条子串命中淹没、精确改名拿不到 ⇒ 必须用精确路径。
type fakeExact struct {
exactIDs []string
likeRows []map[string]interface{}
}
func (f *fakeExact) SearchNodes(label, namespace string) []map[string]interface{} { return f.likeRows }
func (f *fakeExact) NodeIDsByName(name string) []string { return f.exactIDs }
func TestResolveEntityIDs_PrefersExactFinder(t *testing.T) {
gs := &fakeExact{
exactIDs: []string{"织忆", "n_织忆"},
likeRows: []map[string]interface{}{ // LIKE 噪声:精确同名的 n_织忆 不在其中
{"id": "n_织忆召回机制", "name": "织忆召回机制"},
{"id": "n_织忆架构", "name": "织忆架构"},
},
}
ids := ResolveEntityIDs(gs, "织忆")
want := []string{"织忆", "n_织忆"}
if len(ids) != len(want) || ids[0] != want[0] || ids[1] != want[1] {
t.Fatalf("want %v, got %v", want, ids)
}
}
// MergePaths:按 source|relation|target 去重,并尊重上限。
func TestMergePaths_TolerantToFromToKeys(t *testing.T) {
// governance.Navigate 返回 from/to;MergePaths 必须认,否则路径被静默丢弃
nav := []map[string]interface{}{
{"from": "织忆", "to": "Hermes", "relation": "USES"},
{"from": "织忆", "to": "LanceDB", "relation": "DEPENDS_ON"},
}
out := MergePaths([]map[string]interface{}{}, nav, nil, 0)
if len(out) != 2 {
t.Fatalf("Navigate 风格 from/to 路径被丢弃:want 2, got %d", len(out))
}
// 去重:同一 from|relation|to 再来一次不重复
out = MergePaths(out, nav, nil, 0)
if len(out) != 2 {
t.Fatalf("去重失败:want 2, got %d", len(out))
}
// limit 生效
out = MergePaths([]map[string]interface{}{}, nav, nil, 1)
if len(out) != 1 {
t.Fatalf("limit 未生效:want 1, got %d", len(out))
}
}
func TestPathEnds_BothKeySets(t *testing.T) {
if s, tt, r := PathEnds(map[string]interface{}{"from": "a", "to": "b", "relation": "R"}); s != "a" || tt != "b" || r != "R" {
t.Fatalf("from/to 未识别: %s %s %s", s, tt, r)
}
if s, tt, _ := PathEnds(map[string]interface{}{"source": "a", "target": "b"}); s != "a" || tt != "b" {
t.Fatalf("source/target 未识别: %s %s", s, tt)
}
}
func TestMergePaths_DedupAndLimit(t *testing.T) {
p := func(s, r, tg string) map[string]interface{} {
return map[string]interface{}{"source": s, "relation": r, "target": tg}
}
dst := []map[string]interface{}{p("a", "is", "b")}
seen := map[string]bool{}
dst = MergePaths(dst, []map[string]interface{}{
p("a", "is", "b"), // 重复
p("n_a", "is", "c"),
p("n_a", "is", "d"),
}, seen, 2)
if len(dst) != 2 {
t.Fatalf("want limit 2, got %d: %v", len(dst), dst)
}
if dst[1]["target"] != "c" {
t.Fatalf("第二条应为新路径 c, got %v", dst[1])
}
// 再次合并应识别已有 key(含 dst 内建索引)
dst = MergePaths(dst, []map[string]interface{}{p("n_a", "is", "c")}, seen, 0)
if len(dst) != 2 {
t.Fatalf("重复路径未被去重: %v", dst)
}
}