|
| 1 | +package graphrag |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// edgeIDs collects from→to keys so two []*Edge slices can be compared |
| 12 | +// order-independently. |
| 13 | +func edgeIDs(edges []*Edge) []string { |
| 14 | + out := make([]string, 0, len(edges)) |
| 15 | + for _, e := range edges { |
| 16 | + out = append(out, e.FromID+"->"+e.ToID) |
| 17 | + } |
| 18 | + sort.Strings(out) |
| 19 | + return out |
| 20 | +} |
| 21 | + |
| 22 | +func fullScanFrom(s *ServiceStore, service string) []*Edge { |
| 23 | + var out []*Edge |
| 24 | + for _, e := range s.Edges { |
| 25 | + if e.Type == EdgeCalls && e.FromID == service { |
| 26 | + out = append(out, e) |
| 27 | + } |
| 28 | + } |
| 29 | + return out |
| 30 | +} |
| 31 | + |
| 32 | +func fullScanTo(s *ServiceStore, service string) []*Edge { |
| 33 | + var out []*Edge |
| 34 | + for _, e := range s.Edges { |
| 35 | + if e.Type == EdgeCalls && e.ToID == service { |
| 36 | + out = append(out, e) |
| 37 | + } |
| 38 | + } |
| 39 | + return out |
| 40 | +} |
| 41 | + |
| 42 | +// TestAdjacencyIndexMatchesFullScan asserts the indexed CallEdgesFrom/To return |
| 43 | +// exactly the same CALLS edges a full Edges-map scan would, across both the |
| 44 | +// aggregating UpsertCallEdge path and the existence-only EnsureCallEdge path. |
| 45 | +func TestAdjacencyIndexMatchesFullScan(t *testing.T) { |
| 46 | + g := newTestGraphRAG(t) |
| 47 | + now := time.Now() |
| 48 | + s := g.storesForTenant("t").service |
| 49 | + |
| 50 | + // Aggregating path. |
| 51 | + s.UpsertCallEdge("a", "b", 5, false, now) |
| 52 | + s.UpsertCallEdge("a", "c", 5, true, now) |
| 53 | + s.UpsertCallEdge("b", "c", 5, false, now) |
| 54 | + // Existence-only (pre-sample topology observer) path. |
| 55 | + s.EnsureService("d", now) |
| 56 | + s.EnsureCallEdge("a", "d", now) |
| 57 | + s.EnsureCallEdge("d", "c", now) |
| 58 | + // Repeat upserts must not duplicate index entries. |
| 59 | + s.UpsertCallEdge("a", "b", 5, false, now) |
| 60 | + s.EnsureCallEdge("a", "d", now) |
| 61 | + |
| 62 | + for _, svc := range []string{"a", "b", "c", "d"} { |
| 63 | + gotFrom := edgeIDs(s.CallEdgesFrom(svc)) |
| 64 | + wantFrom := edgeIDs(fullScanFrom(s, svc)) |
| 65 | + if fmt.Sprint(gotFrom) != fmt.Sprint(wantFrom) { |
| 66 | + t.Errorf("CallEdgesFrom(%q) = %v, full scan = %v", svc, gotFrom, wantFrom) |
| 67 | + } |
| 68 | + gotTo := edgeIDs(s.CallEdgesTo(svc)) |
| 69 | + wantTo := edgeIDs(fullScanTo(s, svc)) |
| 70 | + if fmt.Sprint(gotTo) != fmt.Sprint(wantTo) { |
| 71 | + t.Errorf("CallEdgesTo(%q) = %v, full scan = %v", svc, gotTo, wantTo) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // a→b should be a single edge despite the repeated upsert. |
| 76 | + if from := s.CallEdgesFrom("a"); len(from) != 3 { // a→b, a→c, a→d |
| 77 | + t.Fatalf("CallEdgesFrom(a) = %d edges, want 3", len(from)) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// TestOperationsForServiceIndex asserts the per-service operations index matches |
| 82 | +// a filter over the full Operations map. |
| 83 | +func TestOperationsForServiceIndex(t *testing.T) { |
| 84 | + g := newTestGraphRAG(t) |
| 85 | + now := time.Now() |
| 86 | + s := g.storesForTenant("t").service |
| 87 | + |
| 88 | + s.UpsertOperation("a", "GET /x", 1, false, now) |
| 89 | + s.UpsertOperation("a", "GET /y", 1, false, now) |
| 90 | + s.UpsertOperation("b", "POST /z", 1, false, now) |
| 91 | + s.UpsertOperation("a", "GET /x", 1, true, now) // repeat: no new index entry |
| 92 | + |
| 93 | + if ops := s.OperationsForService("a"); len(ops) != 2 { |
| 94 | + t.Fatalf("OperationsForService(a) = %d, want 2", len(ops)) |
| 95 | + } |
| 96 | + if ops := s.OperationsForService("b"); len(ops) != 1 { |
| 97 | + t.Fatalf("OperationsForService(b) = %d, want 1", len(ops)) |
| 98 | + } |
| 99 | + if ops := s.OperationsForService("missing"); ops != nil { |
| 100 | + t.Fatalf("OperationsForService(missing) = %v, want nil", ops) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// TestServiceMapAroundBounded asserts the focused service map only returns the |
| 105 | +// subgraph reachable within depth hops, while the unfocused full map returns all |
| 106 | +// services. |
| 107 | +func TestServiceMapAroundBounded(t *testing.T) { |
| 108 | + g := newTestGraphRAG(t) |
| 109 | + now := time.Now() |
| 110 | + ctx := context.Background() |
| 111 | + s := g.storesFor(ctx).service |
| 112 | + |
| 113 | + // Chain a→b→c→d plus an isolated island e→f. |
| 114 | + for _, sv := range []string{"a", "b", "c", "d", "e", "f"} { |
| 115 | + s.UpsertService(sv, 1, false, now) |
| 116 | + } |
| 117 | + s.UpsertCallEdge("a", "b", 1, false, now) |
| 118 | + s.UpsertCallEdge("b", "c", 1, false, now) |
| 119 | + s.UpsertCallEdge("c", "d", 1, false, now) |
| 120 | + s.UpsertCallEdge("e", "f", 1, false, now) |
| 121 | + |
| 122 | + // depth 1 from b → {b, a, c}. |
| 123 | + got := g.ServiceMapAround(ctx, "b", 1) |
| 124 | + if len(got) != 3 { |
| 125 | + t.Fatalf("ServiceMapAround(b, 1) = %d entries, want 3", len(got)) |
| 126 | + } |
| 127 | + |
| 128 | + // depth 2 from a → {a, b, c} (downstream only reaches c at 2 hops). |
| 129 | + got = g.ServiceMapAround(ctx, "a", 2) |
| 130 | + if len(got) != 3 { |
| 131 | + t.Fatalf("ServiceMapAround(a, 2) = %d entries, want 3", len(got)) |
| 132 | + } |
| 133 | + |
| 134 | + // Unknown seed → nil. |
| 135 | + if got := g.ServiceMapAround(ctx, "nope", 3); got != nil { |
| 136 | + t.Fatalf("ServiceMapAround(nope) = %v, want nil", got) |
| 137 | + } |
| 138 | + |
| 139 | + // Full map returns every service (island included). |
| 140 | + if full := g.ServiceMap(ctx, 0); len(full) != 6 { |
| 141 | + t.Fatalf("ServiceMap = %d entries, want 6", len(full)) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// BenchmarkServiceMap exercises the full topology dump at 200 services / ~800 |
| 146 | +// edges to confirm the adjacency index keeps it linear. |
| 147 | +func BenchmarkServiceMap(b *testing.B) { |
| 148 | + g := newTestGraphRAG(b) |
| 149 | + now := time.Now() |
| 150 | + ctx := context.Background() |
| 151 | + s := g.storesFor(ctx).service |
| 152 | + |
| 153 | + const n = 200 |
| 154 | + for i := 0; i < n; i++ { |
| 155 | + s.UpsertService(fmt.Sprintf("svc-%d", i), 1, false, now) |
| 156 | + } |
| 157 | + // ~4 outgoing edges per service. |
| 158 | + for i := 0; i < n; i++ { |
| 159 | + for k := 1; k <= 4; k++ { |
| 160 | + s.UpsertCallEdge(fmt.Sprintf("svc-%d", i), fmt.Sprintf("svc-%d", (i+k)%n), 1, false, now) |
| 161 | + } |
| 162 | + s.UpsertOperation(fmt.Sprintf("svc-%d", i), "GET /", 1, false, now) |
| 163 | + } |
| 164 | + |
| 165 | + b.ResetTimer() |
| 166 | + for i := 0; i < b.N; i++ { |
| 167 | + _ = g.ServiceMap(ctx, 0) |
| 168 | + } |
| 169 | +} |
0 commit comments