Repository navigation
Expand file tree
/
Copy pathspec-analyzer.test.ts
More file actions
136 lines (126 loc) · 3.99 KB
/
Copy pathspec-analyzer.test.ts
File metadata and controls
136 lines (126 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { SpecAnalyzer } from "../src/spec-analyzer";
import { hashImplementation } from "../src/spec-store";
describe("SpecAnalyzer budgets and drift", () => {
test("analyzeChanges caps at SPECSYNC_MAX_SPECS_PER_PR", async () => {
process.env.SPECSYNC_MAX_SPECS_PER_PR = "2";
process.env.SPECSYNC_LLM_CONCURRENCY = "2";
const analyzer = new SpecAnalyzer({
llmClient: {
generateSpec: jest.fn().mockResolvedValue({
preconditions: ["p"],
postconditions: ["q"],
invariants: [],
edgeCases: [],
confidence: 0.7,
reasoning: "mock",
}),
},
}) as SpecAnalyzer & {
lastJobCost: {
cappedAt: number;
skippedByBudget: number;
llmCalls: number;
};
};
const analyses = [1, 2, 3, 4].map((i) => ({
functionName: `fn${i}`,
filePath: `src/f${i}.ts`,
lineNumber: i,
functionBody: `function fn${i}() { return ${i}; }`,
comments: [],
ast: {
inputTypes: [],
outputType: "number",
conditionalGuards: [],
loops: [],
branching: [],
earlyReturns: [],
controlFlow: {},
complexity: 1,
},
}));
const context = {
payload: {
repository: { owner: { login: "o" }, name: "r", full_name: "o/r" },
pull_request: { head: { sha: "abc" } },
},
octokit: {
repos: {
getContent: jest.fn().mockRejectedValue({ status: 404 }),
},
},
};
const suggestions = await analyzer.analyzeChanges(analyses, context);
expect(suggestions).toHaveLength(2);
expect(analyzer.lastJobCost.cappedAt).toBe(2);
expect(analyzer.lastJobCost.skippedByBudget).toBe(2);
expect(analyzer.lastJobCost.llmCalls).toBe(2);
delete process.env.SPECSYNC_MAX_SPECS_PER_PR;
delete process.env.SPECSYNC_LLM_CONCURRENCY;
});
test("detectDrift reports hash mismatch against stored specs", async () => {
const analyzer = new SpecAnalyzer();
const oldBody = "function add(a,b){return a+b;}";
const newBody = "function add(a,b){return a+b+1;}";
const context = {
payload: {
repository: { owner: { login: "o" }, name: "r", full_name: "o/r" },
ref: "refs/heads/main",
after: "newsha",
},
octokit: {
repos: {
getContent: jest.fn().mockImplementation(async ({ path }: { path: string }) => {
if (path === ".specsync/specs") {
return {
data: [
{
type: "file",
name: "src__math.js__add.json",
path: ".specsync/specs/src__math.js__add.json",
},
],
};
}
if (path.endsWith(".json")) {
return {
data: {
content: Buffer.from(
JSON.stringify({
version: 1,
functionName: "add",
filePath: "src/math.js",
confidence: 0.9,
acceptedAt: "t",
implementationHash: hashImplementation(oldBody),
contract: {
preconditions: [],
postconditions: [],
invariants: [],
},
})
).toString("base64"),
},
};
}
if (path === "src/math.js") {
return {
data: {
content: Buffer.from(newBody, "utf8").toString("base64"),
},
};
}
throw { status: 404 };
}),
},
},
};
const drift = await analyzer.detectDrift(
[{ modified: ["src/math.js"], added: [], removed: [] }],
context
);
expect(drift).toHaveLength(1);
expect(drift[0].functionName).toBe("add");
expect(drift[0].reason).toMatch(/hash/i);
});
});