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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
import {
afterEach,
beforeEach,
describe,
expect,
it,
} from "bun:test";
import {
renderFixAgentComment,
buildFixAgentSummary,
runFixAgent,
FIX_AGENT_BOT_USERNAME,
FIX_AGENT_SLUG,
FIX_AGENT_COST_CENTS,
FIX_AGENT_MAX_REPAIRS_IN_COMMENT,
} from "../../lib/agents/fix-agent";
const ENV_KEYS = ["GATETEST_API_KEY", "GATETEST_BASE_URL"] as const;
let savedEnv: Record<string, string | undefined> = {};
let savedFetch: typeof fetch;
beforeEach(() => {
savedEnv = {};
for (const k of ENV_KEYS) savedEnv[k] = process.env[k];
for (const k of ENV_KEYS) delete process.env[k];
savedFetch = globalThis.fetch;
});
afterEach(() => {
for (const k of ENV_KEYS) {
const v = savedEnv[k];
if (v === undefined) delete process.env[k];
else process.env[k] = v;
}
globalThis.fetch = savedFetch;
});
describe("fix-agent — identity constants", () => {
it("uses the agent- prefixed slug", () => {
expect(FIX_AGENT_SLUG).toBe("agent-fix");
});
it("uses the [bot] suffixed username", () => {
expect(FIX_AGENT_BOT_USERNAME).toBe("agent-fix[bot]");
expect(FIX_AGENT_BOT_USERNAME.endsWith("[bot]")).toBe(true);
});
it("cost is flat 3¢", () => {
expect(FIX_AGENT_COST_CENTS).toBe(3);
});
});
describe("fix-agent — renderFixAgentComment", () => {
it("renders the passing case with all-clear wording", () => {
const body = renderFixAgentComment({
passed: true,
totalTests: 47,
failedBefore: 3,
failedAfter: 0,
repairs: [
{ file: "src/foo.ts", before: "a", after: "b", reason: "null check" },
],
unfixable: [],
});
expect(body).toContain("Fix Agent");
expect(body).toContain("47");
expect(body).toContain("all passing");
expect(body).toContain("src/foo.ts");
expect(body).toContain(FIX_AGENT_BOT_USERNAME);
});
it("renders the failing case with before/after counts", () => {
const body = renderFixAgentComment({
passed: false,
totalTests: 10,
failedBefore: 5,
failedAfter: 2,
repairs: [
{ file: "a.ts", before: "", after: "", reason: "x" },
{ file: "b.ts", before: "", after: "", reason: "y" },
{ file: "c.ts", before: "", after: "", reason: "z" },
],
unfixable: [{ file: "d.ts", reason: "unsupported language" }],
});
expect(body).toContain("**3** repairs");
expect(body).toContain("5 → 2");
expect(body).toContain("Unfixable");
expect(body).toContain("d.ts");
});
it("caps repairs list at FIX_AGENT_MAX_REPAIRS_IN_COMMENT and shows overflow count", () => {
const many = Array.from({ length: 30 }, (_, i) => ({
file: `file${i}.ts`,
before: "",
after: "",
reason: "r",
}));
const body = renderFixAgentComment({
passed: true,
totalTests: 30,
failedBefore: 30,
failedAfter: 0,
repairs: many,
unfixable: [],
});
expect(body).toContain(`file${FIX_AGENT_MAX_REPAIRS_IN_COMMENT - 1}.ts`);
expect(body).not.toContain(`file${FIX_AGENT_MAX_REPAIRS_IN_COMMENT}.ts`);
expect(body).toContain(
`…and ${30 - FIX_AGENT_MAX_REPAIRS_IN_COMMENT} more`
);
});
it("omits repairs section when there are no repairs but unfixable entries", () => {
const body = renderFixAgentComment({
passed: false,
totalTests: 1,
failedBefore: 1,
failedAfter: 1,
repairs: [],
unfixable: [{ file: "x.ts", reason: "too complex" }],
});
expect(body).not.toContain("### Repairs");
expect(body).toContain("### Unfixable");
expect(body).toContain("x.ts");
});
it("uses singular 'repair' for exactly one proposed", () => {
const body = renderFixAgentComment({
passed: false,
totalTests: 2,
failedBefore: 2,
failedAfter: 1,
repairs: [{ file: "f.ts", before: "", after: "", reason: "r" }],
unfixable: [],
});
expect(body).toContain("**1** repair ");
expect(body).not.toContain("**1** repairs");
});
});
describe("fix-agent — buildFixAgentSummary", () => {
it("returns offline message when gatetest is offline", () => {
const s = buildFixAgentSummary({
offline: true,
passed: false,
failedBefore: 0,
failedAfter: 0,
repairs: 0,
});
expect(s).toBe("gatetest offline; skipped");
});
it("returns healthy message when nothing to fix", () => {
const s = buildFixAgentSummary({
offline: false,
passed: true,
failedBefore: 0,
failedAfter: 0,
repairs: 0,
});
expect(s).toContain("suite healthy");
});
it("reports failing-with-no-repairs case", () => {
const s = buildFixAgentSummary({
offline: false,
passed: false,
failedBefore: 5,
failedAfter: 5,
repairs: 0,
});
expect(s).toContain("5 failing");
expect(s).toContain("0 repairs");
});
it("reports full-repair success", () => {
const s = buildFixAgentSummary({
offline: false,
passed: true,
failedBefore: 3,
failedAfter: 0,
repairs: 3,
});
expect(s).toBe("repaired 3 (3 → 0)");
});
it("reports partial-repair case with plural repairs", () => {
const s = buildFixAgentSummary({
offline: false,
passed: false,
failedBefore: 5,
failedAfter: 2,
repairs: 3,
});
expect(s).toContain("3 repairs");
expect(s).toContain("5 → 2");
});
it("reports singular 'repair' for one", () => {
const s = buildFixAgentSummary({
offline: false,
passed: false,
failedBefore: 1,
failedAfter: 0,
repairs: 1,
});
expect(s).toContain("1 repair ");
expect(s).not.toContain("1 repairs");
});
});
describe("fix-agent — runFixAgent", () => {
it("rejects missing repositoryId without throwing", async () => {
const r = await runFixAgent({
repositoryId: "",
pullRequestId: "00000000-0000-0000-0000-000000000000",
});
expect(r.ok).toBe(false);
expect(r.runId).toBeNull();
expect(r.summary.toLowerCase()).toContain("invalid args");
});
it("rejects missing pullRequestId without throwing", async () => {
const r = await runFixAgent({
repositoryId: "00000000-0000-0000-0000-000000000000",
pullRequestId: "",
});
expect(r.ok).toBe(false);
expect(r.runId).toBeNull();
});
it("returns documented failure when DB cannot open a run", async () => {
globalThis.fetch = (() => {
throw new Error("fetch must not be called when run cannot be opened");
}) as unknown as typeof fetch;
const r = await runFixAgent({
repositoryId: "00000000-0000-0000-0000-000000000000",
pullRequestId: "00000000-0000-0000-0000-000000000000",
});
expect(r.ok).toBe(false);
expect(r.runId).toBeNull();
expect(r.summary.toLowerCase()).toMatch(/agent_runs|could not/);
});
});
|