|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + anchorLoad, |
| 4 | + packBatches, |
| 5 | + shardCandidate, |
| 6 | + shardKeyOf, |
| 7 | + toLocations, |
| 8 | +} from "../src/anchors.js"; |
| 9 | +import type { AgentCandidate } from "../src/detect.js"; |
| 10 | +import type { PreFilterHit } from "../src/pre-filter.js"; |
| 11 | + |
| 12 | +function hit(line: number, label = "r", endLine?: number): PreFilterHit { |
| 13 | + return { line, label, snippet: `line ${line}`, ...(endLine !== undefined ? { endLine } : {}) }; |
| 14 | +} |
| 15 | + |
| 16 | +function candidate(filePath: string, hits: PreFilterHit[]): AgentCandidate { |
| 17 | + return { filePath, content: "x\n".repeat(600), hits }; |
| 18 | +} |
| 19 | + |
| 20 | +describe("toLocations", () => { |
| 21 | + it("collapses hits that point at the same line", () => { |
| 22 | + const locs = toLocations([hit(7, "regex-a"), hit(7, "semgrep-b"), hit(9)]); |
| 23 | + expect(locs.length).toBe(2); |
| 24 | + expect(locs[0].hits.length).toBe(2); |
| 25 | + }); |
| 26 | + |
| 27 | + it("keeps a ranged anchor separate from a single-line one", () => { |
| 28 | + const locs = toLocations([hit(7), hit(7, "r", 12)]); |
| 29 | + expect(locs.length).toBe(2); |
| 30 | + }); |
| 31 | + |
| 32 | + it("sorts by line regardless of build order", () => { |
| 33 | + // Real hits arrive pattern-major, then semgrep appended — never sorted. |
| 34 | + const locs = toLocations([hit(40), hit(2), hit(17)]); |
| 35 | + expect(locs.map((l) => l.line)).toEqual([2, 17, 40]); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("anchorLoad", () => { |
| 40 | + it("charges once for several patterns matching one line", () => { |
| 41 | + expect(anchorLoad(candidate("a.ts", [hit(3, "a"), hit(3, "b"), hit(3, "c")]))).toBe(1); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("shardCandidate", () => { |
| 46 | + const many = Array.from({ length: 500 }, (_, i) => hit(i + 1)); |
| 47 | + |
| 48 | + it("returns the same object untouched when the file is under the cap", () => { |
| 49 | + const c = candidate("a.ts", [hit(5), hit(1)]); |
| 50 | + const shards = shardCandidate(c, 150); |
| 51 | + expect(shards.length).toBe(1); |
| 52 | + // Identity matters: an unsharded scan must render byte-identical prompts, |
| 53 | + // which means the original hit order survives. |
| 54 | + expect(shards[0]).toBe(c); |
| 55 | + }); |
| 56 | + |
| 57 | + it("splits an over-cap file into contiguous line ranges", () => { |
| 58 | + const shards = shardCandidate(candidate("a.ts", many), 50); |
| 59 | + expect(shards.length).toBe(10); |
| 60 | + expect(shards[0].hits[0].line).toBe(1); |
| 61 | + expect(shards[0].hits.at(-1)?.line).toBe(50); |
| 62 | + expect(shards[1].hits[0].line).toBe(51); |
| 63 | + expect(shards.at(-1)?.hits.at(-1)?.line).toBe(500); |
| 64 | + }); |
| 65 | + |
| 66 | + it("covers every anchor exactly once", () => { |
| 67 | + const shards = shardCandidate(candidate("a.ts", many), 33); |
| 68 | + const lines = shards.flatMap((s) => s.hits.map((h) => h.line)); |
| 69 | + expect(lines.length).toBe(500); |
| 70 | + expect(new Set(lines).size).toBe(500); |
| 71 | + }); |
| 72 | + |
| 73 | + it("never splits when the cap is disabled", () => { |
| 74 | + expect(shardCandidate(candidate("a.ts", many), Number.POSITIVE_INFINITY).length).toBe(1); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("shardKeyOf", () => { |
| 79 | + it("gives disjoint shards of one file distinct keys", () => { |
| 80 | + const shards = shardCandidate( |
| 81 | + candidate( |
| 82 | + "a.ts", |
| 83 | + Array.from({ length: 120 }, (_, i) => hit(i + 1)), |
| 84 | + ), |
| 85 | + 50, |
| 86 | + ); |
| 87 | + const keys = shards.map(shardKeyOf); |
| 88 | + expect(new Set(keys).size).toBe(keys.length); |
| 89 | + expect(keys[0]).toBe("1-50:50"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("is stable across rebuilds of the same candidate", () => { |
| 93 | + const hits = [hit(9), hit(2), hit(2, "other")]; |
| 94 | + expect(shardKeyOf(candidate("a.ts", hits))).toBe(shardKeyOf(candidate("a.ts", [...hits]))); |
| 95 | + }); |
| 96 | + |
| 97 | + it("changes when the anchor set shifts, so a stale cut cannot resume", () => { |
| 98 | + const before = shardKeyOf(candidate("a.ts", [hit(1), hit(2)])); |
| 99 | + const after = shardKeyOf(candidate("a.ts", [hit(1), hit(2), hit(3)])); |
| 100 | + expect(before).not.toBe(after); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("packBatches", () => { |
| 105 | + const light = (name: string) => candidate(name, [hit(1), hit(2)]); |
| 106 | + |
| 107 | + it("matches the fixed slice it replaces when the anchor cap is off", () => { |
| 108 | + const items = ["a", "b", "c", "d", "e", "f", "g"].map(light); |
| 109 | + const batches = packBatches(items, 5, Number.POSITIVE_INFINITY); |
| 110 | + expect(batches.map((b) => b.length)).toEqual([5, 2]); |
| 111 | + }); |
| 112 | + |
| 113 | + it("closes a batch early when the anchor ceiling binds first", () => { |
| 114 | + const dense = (name: string) => |
| 115 | + candidate( |
| 116 | + name, |
| 117 | + Array.from({ length: 40 }, (_, i) => hit(i + 1)), |
| 118 | + ); |
| 119 | + const batches = packBatches([dense("a"), dense("b"), dense("c")], 5, 50); |
| 120 | + expect(batches.map((b) => b.length)).toEqual([1, 1, 1]); |
| 121 | + }); |
| 122 | + |
| 123 | + it("keeps two shards of one file out of the same batch", () => { |
| 124 | + const shards = shardCandidate( |
| 125 | + candidate( |
| 126 | + "big.ts", |
| 127 | + Array.from({ length: 300 }, (_, i) => hit(i + 1)), |
| 128 | + ), |
| 129 | + 100, |
| 130 | + ); |
| 131 | + const batches = packBatches(shards, 5, 100); |
| 132 | + expect(batches.length).toBe(3); |
| 133 | + for (const b of batches) expect(b.length).toBe(1); |
| 134 | + }); |
| 135 | + |
| 136 | + it("still emits an oversized candidate rather than dropping it", () => { |
| 137 | + const huge = candidate( |
| 138 | + "huge.ts", |
| 139 | + Array.from({ length: 80 }, (_, i) => hit(i + 1)), |
| 140 | + ); |
| 141 | + const batches = packBatches([huge], 5, 10); |
| 142 | + expect(batches.length).toBe(1); |
| 143 | + expect(batches[0][0]).toBe(huge); |
| 144 | + }); |
| 145 | +}); |
0 commit comments