Files
Enterprise-Knowledge-Base/internal/agent/tools/wiki_write_page_test.go
T
wizardchen 5d0326f987 fix(wiki): harden slug integrity across ingest and agent write paths
Weak LLMs routinely mangle high-entropy wiki slugs (especially UUID-based
summary/<id> slugs), producing dead cross-links and cross-item dedup
hallucinations. This tightens integrity end to end:

- Ingest dedup now groups each new item with only its own trigram
  candidates and validates merges against that per-item scope, rejecting
  cross-item mispairings; candidate top-K narrowed 20 -> 5.
- Editor path aliases out-link slugs behind low-entropy ref-N tokens and
  translates them back after generation, removing the chance to mangle.
- Agent write tool normalizes/validates slugs, blocks manual creation of
  system-owned summary pages, and auto-repairs dead [[slug]] links
  (rewrite-only, never strips) via bigram/display-text resolution.
- llmresource registry aliases summary/<uuid> slugs the same way as
  storage handles so they round-trip safely through chat.

Adds unit tests for slug validation, aliasing round-trip, dead-link
repair, and dedup rejection rules.
2026-07-24 15:57:08 +08:00

54 lines
1.8 KiB
Go

package tools
import "testing"
func TestNormalizeAndValidateWikiSlug(t *testing.T) {
tests := []struct {
name string
in string
want string
wantErr bool
}{
{name: "already valid", in: "entity/acme-corp", want: "entity/acme-corp"},
{name: "lowercased and spaces", in: "Entity/Acme Corp", want: "entity/acme-corp"},
{name: "trimmed", in: " concept/rag ", want: "concept/rag"},
{name: "cjk kept", in: "entity/上海中心大厦", want: "entity/上海中心大厦"},
{name: "uuid summary", in: "summary/07a20bb1-a662-47cf-9929-06fb5d5b5b5e", want: "summary/07a20bb1-a662-47cf-9929-06fb5d5b5b5e"},
{name: "empty", in: " ", wantErr: true},
{name: "leading slash", in: "/entity/x", wantErr: true},
{name: "trailing slash", in: "entity/x/", wantErr: true},
{name: "double slash", in: "entity//x", wantErr: true},
{name: "invalid char", in: "entity/x!y", wantErr: true},
{name: "invalid space-only becomes empty", in: " ", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := normalizeAndValidateWikiSlug(tt.in)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error for %q, got slug %q", tt.in, got)
}
return
}
if err != nil {
t.Fatalf("unexpected error for %q: %v", tt.in, err)
}
if got != tt.want {
t.Fatalf("normalizeAndValidateWikiSlug(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}
func TestIsSummaryNamespace(t *testing.T) {
if !isSummaryNamespace("summary/abc") {
t.Fatal("summary/abc must be in the summary namespace")
}
if isSummaryNamespace("summary") {
t.Fatal("bare 'summary' (no slash) must not count as the summary namespace")
}
if isSummaryNamespace("entity/summary-of-x") {
t.Fatal("entity/summary-of-x must not count as the summary namespace")
}
}