Files
glpi-neural-brain/internal/graph/store_test.go
groot b3bd3d5ffd
All checks were successful
release-tag / release-image (push) Successful in 2m24s
BugFix
2026-08-05 11:58:25 +02:00

103 lines
4.0 KiB
Go

package graph
import (
"testing"
"github.com/local/glpi-neural-brain/internal/model"
)
func TestReplaceOriginsPreservesUnchangedVector(t *testing.T) {
s, err := Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = s.Close() })
n := model.Node{ID: "n1", Kind: "knowledge", Label: "VPN", Summary: "Gateway", Origin: "knowledge-production"}
s.ReplaceOrigins([]string{"knowledge-production"}, []model.Node{n}, nil)
s.SetVector("n1", []float64{1, 2, 3})
s.ReplaceOrigins([]string{"knowledge-production"}, []model.Node{n}, nil)
v, ok := s.Vector("n1")
if !ok || len(v) != 3 {
t.Fatalf("vector was not preserved: %v %v", ok, v)
}
n.Summary = "changed"
s.ReplaceOrigins([]string{"knowledge-production"}, []model.Node{n}, nil)
if _, ok := s.Vector("n1"); ok {
t.Fatal("changed document retained stale vector")
}
}
func TestRejectedEdgePreventsPairReprocessingButIsHidden(t *testing.T) {
s, _ := Open(t.TempDir())
t.Cleanup(func() { _ = s.Close() })
a := model.Node{ID: "a", Kind: "knowledge", Label: "A", Origin: "knowledge-production"}
b := model.Node{ID: "b", Kind: "knowledge", Label: "B", Origin: "knowledge-production"}
s.UpsertNode(a)
s.UpsertNode(b)
s.SetVector("a", []float64{1, 0})
s.SetVector("b", []float64{.9, .1})
if _, _, _, ok := s.BestPair(.5); !ok {
t.Fatal("expected candidate pair")
}
s.UpsertEdge(model.Edge{Source: "a", Target: "b", Type: "related_to", Origin: "ai-inference", Status: "rejected"})
if _, _, _, ok := s.BestPair(.5); ok {
t.Fatal("rejected pair was selected again")
}
if got := len(s.Snapshot().Edges); got != 0 {
t.Fatalf("rejected edge should not be rendered, got %d", got)
}
}
func TestNextPairUsesBoundedRotatingAnchors(t *testing.T) {
s, _ := Open(t.TempDir())
t.Cleanup(func() { _ = s.Close() })
for _, id := range []string{"a", "b", "c", "d"} {
s.UpsertNode(model.Node{ID: id, Kind: "knowledge", Label: id, Origin: "knowledge-production"})
}
s.SetVector("a", []float64{1, 0})
s.SetVector("b", []float64{.99, .01})
s.SetVector("c", []float64{0, 1})
s.SetVector("d", []float64{.01, .99})
a, b, _, ok, comparisons := s.NextPair(.8, 1)
if !ok || comparisons == 0 {
t.Fatalf("expected bounded candidate search, ok=%v comparisons=%d", ok, comparisons)
}
s.UpsertEdge(model.Edge{Source: a.ID, Target: b.ID, Type: "related_to", Origin: "ai-inference", Status: "rejected"})
_, _, _, _, comparisons = s.NextPair(.8, 1)
if comparisons == 0 {
t.Fatal("rotating anchor did not advance after rejected pair")
}
if _, _, _, ok, comparisons = s.NextPair(.8, 1); !ok || comparisons == 0 {
t.Fatalf("rotating anchor did not reach the next semantic region, ok=%v comparisons=%d", ok, comparisons)
}
}
func TestSourceFiltersLimitEmbeddingAndThinkingCandidates(t *testing.T) {
s, err := Open(t.TempDir())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = s.Close() })
nodes := []model.Node{
{ID: "internal-a", Kind: "knowledge", Label: "Internal A", Categories: []string{"Netzwerk"}, Origin: "test", Metadata: map[string]any{"source": "internal-category"}},
{ID: "internal-b", Kind: "knowledge", Label: "Internal B", Categories: []string{"Applikation"}, Origin: "test", Metadata: map[string]any{"source": "internal-category"}},
{ID: "glpi-a", Kind: "knowledge", Label: "GLPI A", Categories: []string{"Netzwerk"}, Origin: "glpi-kb", Metadata: map[string]any{"source": "GLPI Knowledge Base"}},
{ID: "none", Kind: "knowledge", Label: "Ohne", Origin: "test"},
}
for _, node := range nodes {
s.UpsertNode(node)
}
pending := s.NodesForEmbeddingFiltered([]string{"internal-category"})
if len(pending) != 2 {
t.Fatalf("expected two internal source embeddings, got %d", len(pending))
}
for _, node := range nodes {
s.SetVector(node.ID, []float64{1, .01})
}
a, b, _, ok, _ := s.NextPairFiltered(.5, 8, []string{"internal-category"})
if !ok || NodeSource(a) != "internal-category" || NodeSource(b) != "internal-category" {
t.Fatalf("thinking source filter returned wrong pair: ok=%v a=%+v b=%+v", ok, a, b)
}
}