package engine import ( "strings" "testing" "time" "github.com/local/glpi-neural-brain/internal/sourceagent" "github.com/local/glpi-neural-brain/internal/vectorgraph" ) func testVectorAgentRequest() sourceagent.VectorGraphComputeRequest { return sourceagent.VectorGraphComputeRequest{ Header: sourceagent.VectorGraphComputeHeader{ GraphVersion: 7, Primary: vectorgraph.Config{Neighbors: 1, Layout: false}, OrphanPass: true, Orphan: vectorgraph.Config{Neighbors: 1}, OrphanFocusIDs: []string{"c"}, }, Entries: []vectorgraph.Entry{{ID: "a", Vector: []float32{1, 0}}, {ID: "b", Vector: []float32{.9, .1}}, {ID: "c", Vector: []float32{.8, .2}}}, } } func TestValidateAgentVectorGraphResultRejectsInvalidEndpoint(t *testing.T) { req := testVectorAgentRequest() result := sourceagent.VectorGraphComputeResult{Kind: sourceagent.ComputeKindVectorGraph, GraphVersion: 7, Primary: vectorgraph.Result{Links: []vectorgraph.Link{{Source: "a", Target: "missing", Similarity: .9, Affinity: .8, Confidence: .85}}}} if err := validateAgentVectorGraphResult(req, result); err == nil || !strings.Contains(err.Error(), "endpoint") { t.Fatalf("expected endpoint validation error, got %v", err) } } func TestValidateAgentVectorGraphResultRejectsOrphanLinkOutsideFocus(t *testing.T) { req := testVectorAgentRequest() result := sourceagent.VectorGraphComputeResult{Kind: sourceagent.ComputeKindVectorGraph, GraphVersion: 7, Orphan: vectorgraph.Result{Links: []vectorgraph.Link{{Source: "a", Target: "b", Similarity: .9, Affinity: .8, Confidence: .85}}}} if err := validateAgentVectorGraphResult(req, result); err == nil || !strings.Contains(err.Error(), "orphan focus") { t.Fatalf("expected orphan focus validation error, got %v", err) } } func TestValidateAgentVectorGraphResultRejectsUnexpectedPositions(t *testing.T) { req := testVectorAgentRequest() result := sourceagent.VectorGraphComputeResult{Kind: sourceagent.ComputeKindVectorGraph, GraphVersion: 7, Primary: vectorgraph.Result{Positions: []vectorgraph.Position{{ID: "a", X: .1, Y: .2, Z: .3}}}} if err := validateAgentVectorGraphResult(req, result); err == nil || !strings.Contains(err.Error(), "unexpected positions") { t.Fatalf("expected unexpected position validation error, got %v", err) } } func TestValidateAgentVectorGraphResultAcceptsBoundedResult(t *testing.T) { req := testVectorAgentRequest() result := sourceagent.VectorGraphComputeResult{ Kind: sourceagent.ComputeKindVectorGraph, GraphVersion: 7, Primary: vectorgraph.Result{Links: []vectorgraph.Link{{Source: "a", Target: "b", Similarity: .9, Affinity: .8, Confidence: .85}}}, Orphan: vectorgraph.Result{Links: []vectorgraph.Link{{Source: "c", Target: "b", Similarity: .82, Affinity: .5, Confidence: .7}}}, } if err := validateAgentVectorGraphResult(req, result); err != nil { t.Fatalf("expected valid result, got %v", err) } } func TestVectorAgentStartupGraceUsesConfiguredWaitOnlyDuringBootstrap(t *testing.T) { if got := vectorAgentStartupGrace(true, 2*time.Minute); got != 2*time.Minute { t.Fatalf("expected configured startup grace during bootstrap, got %s", got) } if got := vectorAgentStartupGrace(false, 2*time.Minute); got != 0 { t.Fatalf("steady-state vector rebuild must not wait for late agent registration, got %s", got) } if got := vectorAgentStartupGrace(true, 0); got != 0 { t.Fatalf("disabled wait must remain disabled, got %s", got) } }