Repository navigation
Expand file tree
/
Copy pathspec-store.test.ts
More file actions
161 lines (148 loc) · 4.86 KB
/
Copy pathspec-store.test.ts
File metadata and controls
161 lines (148 loc) · 4.86 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
SpecStore,
buildSpecStorePath,
buildLeanStorePath,
hashImplementation,
suggestionToStoredSpec,
} from "../src/spec-store";
describe("spec-store", () => {
test("buildSpecStorePath encodes file path and function", () => {
expect(buildSpecStorePath("src/math.ts", "add")).toBe(
".specsync/specs/src__math.ts__add.json"
);
});
test("buildLeanStorePath uses SPECS_DIR", () => {
expect(buildLeanStorePath("add")).toBe("specs/add_spec.lean");
});
test("hashImplementation is stable", () => {
expect(hashImplementation("fn() {}")).toBe(hashImplementation("fn() {}"));
expect(hashImplementation("a")).not.toBe(hashImplementation("b"));
});
test("suggestionToStoredSpec normalizes confidence", () => {
const stored = suggestionToStoredSpec(
{
functionName: "add",
filePath: "src/a.ts",
lineNumber: 1,
preconditions: ["a"],
postconditions: ["b"],
invariants: [],
confidence: 85,
reasoning: "ok",
},
{ pr: 1, sha: "abc" }
);
expect(stored.confidence).toBe(0.85);
expect(stored.version).toBe(1);
expect(stored.pr).toBe(1);
});
test("storeSpec writes via createOrUpdateFileContents", async () => {
const store = new SpecStore();
const writes: Array<Record<string, unknown>> = [];
const octokit = {
repos: {
getContent: jest.fn().mockRejectedValue({ status: 404 }),
createOrUpdateFileContents: jest.fn().mockImplementation(async (params) => {
writes.push(params);
return { data: { content: { sha: "newsha" } } };
}),
},
};
const stored = suggestionToStoredSpec(
{
functionName: "add",
filePath: "src/math.ts",
lineNumber: 3,
preconditions: ["x"],
postconditions: ["y"],
invariants: [],
confidence: 0.9,
reasoning: "r",
},
{ pr: 9, sha: "deadbeef" }
);
const result = await store.storeSpec(
octokit,
{ owner: "o", repo: "r", ref: "feature" },
stored
);
expect(result.path).toContain(".specsync/specs/");
expect(writes[0].branch).toBe("feature");
expect(writes[0].path).toBe(result.path);
const decoded = Buffer.from(writes[0].content as string, "base64").toString("utf8");
expect(JSON.parse(decoded).functionName).toBe("add");
});
test("computeCoverageForFunctions uses accepted store", async () => {
const store = new SpecStore();
const octokit = {
repos: {
getContent: jest.fn().mockImplementation(async ({ path }: { path: string }) => {
if (path === ".specsync/specs") {
return {
data: [
{
type: "file",
name: "src__math.ts__add.json",
path: ".specsync/specs/src__math.ts__add.json",
},
],
};
}
if (path === ".specsync/specs/src__math.ts__add.json") {
return {
data: {
content: Buffer.from(
JSON.stringify({
version: 1,
functionName: "add",
filePath: "src/math.ts",
confidence: 0.9,
acceptedAt: new Date().toISOString(),
contract: { preconditions: [], postconditions: [], invariants: [] },
})
).toString("base64"),
},
};
}
throw { status: 404 };
}),
createOrUpdateFileContents: jest.fn(),
},
};
const coverage = await store.computeCoverageForFunctions(
octokit,
{ owner: "o", repo: "r", ref: "main" },
[
{ functionName: "add", filePath: "src/math.ts", lineNumber: 1 },
{ functionName: "sub", filePath: "src/math.ts", lineNumber: 2 },
]
);
expect(coverage.totalFunctions).toBe(2);
expect(coverage.coveredFunctions).toBe(1);
expect(coverage.coverage).toBe(50);
expect(coverage.formula).toContain("accepted");
});
test("in-memory cache expires via TTL", async () => {
const store = new SpecStore();
const getContent = jest.fn().mockResolvedValue({
data: {
content: Buffer.from(
JSON.stringify({
version: 1,
functionName: "add",
filePath: "a.ts",
confidence: 1,
acceptedAt: "t",
contract: { preconditions: [], postconditions: [], invariants: [] },
})
).toString("base64"),
},
});
const octokit = {
repos: { getContent, createOrUpdateFileContents: jest.fn() },
};
await store.retrieveSpec(octokit, { owner: "o", repo: "r", ref: "main" }, "a.ts", "add");
await store.retrieveSpec(octokit, { owner: "o", repo: "r", ref: "main" }, "a.ts", "add");
expect(getContent).toHaveBeenCalledTimes(1);
});
});