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
|
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
import {
buildAuthHeaders,
healSuite,
isConfigured,
runAndRepair,
stackTraceToTest,
} from "../lib/gatetest-client";
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("gatetest-client — isConfigured", () => {
it("returns false when no API key is set", () => {
expect(isConfigured()).toBe(false);
});
it("returns true once GATETEST_API_KEY is set", () => {
process.env.GATETEST_API_KEY = "sk-test-abc";
expect(isConfigured()).toBe(true);
});
it("flips back to false when the key is removed mid-process", () => {
process.env.GATETEST_API_KEY = "sk-test-abc";
expect(isConfigured()).toBe(true);
delete process.env.GATETEST_API_KEY;
expect(isConfigured()).toBe(false);
});
});
describe("gatetest-client — buildAuthHeaders", () => {
it("returns only Content-Type when no key is present", () => {
const h = buildAuthHeaders();
expect(h["Content-Type"]).toBe("application/json");
expect(h["Authorization"]).toBeUndefined();
});
it("includes a Bearer token when the key is present", () => {
process.env.GATETEST_API_KEY = "sk-live-xyz";
const h = buildAuthHeaders();
expect(h["Content-Type"]).toBe("application/json");
expect(h["Authorization"]).toBe("Bearer sk-live-xyz");
});
});
describe("gatetest-client — runAndRepair", () => {
it("returns offline result with zeros when no API key is set", async () => {
globalThis.fetch = (() => {
throw new Error("fetch must not be called in offline mode");
}) as unknown as typeof fetch;
const result = await runAndRepair({ repo: "o/r", ref: "main" });
expect(result.offline).toBe(true);
expect(result.passed).toBe(false);
expect(result.totalTests).toBe(0);
expect(result.failedBefore).toBe(0);
expect(result.failedAfter).toBe(0);
expect(result.repairs).toEqual([]);
expect(result.unfixable).toEqual([]);
expect(result.durationMs).toBe(0);
});
it("falls back to offline on a 500 response", async () => {
process.env.GATETEST_API_KEY = "sk-test";
globalThis.fetch = (async () =>
new Response("boom", { status: 500 })) as unknown as typeof fetch;
const result = await runAndRepair({ repo: "o/r", ref: "main" });
expect(result.offline).toBe(true);
expect(result.passed).toBe(false);
expect(result.totalTests).toBe(0);
});
it("parses a healthy 200 response into a structured result", async () => {
process.env.GATETEST_API_KEY = "sk-test";
const payload = {
passed: true,
totalTests: 42,
failedBefore: 3,
failedAfter: 0,
repairs: [
{ file: "a.ts", before: "x", after: "y", reason: "flake" },
],
unfixable: [],
durationMs: 12345,
};
globalThis.fetch = (async () =>
new Response(JSON.stringify(payload), {
status: 200,
headers: { "Content-Type": "application/json" },
})) as unknown as typeof fetch;
const result = await runAndRepair({
repo: "o/r",
ref: "main",
targetGlob: "src/**",
});
expect(result.offline).toBe(false);
expect(result.passed).toBe(true);
expect(result.totalTests).toBe(42);
expect(result.repairs).toHaveLength(1);
});
});
describe("gatetest-client — stackTraceToTest", () => {
it("returns a deterministic offline stub when no key is set", async () => {
const result = await stackTraceToTest({
repo: "o/r",
stackTrace: "TypeError: cannot read 'x' of undefined\n at foo",
language: "typescript",
});
expect(result.offline).toBe(true);
expect(result.framework).toBe("fallback");
expect(result.testCode).toContain("TODO");
expect(result.suggestedPath.endsWith(".test.ts")).toBe(true);
});
it("picks a pytest path for python offline stubs", async () => {
const result = await stackTraceToTest({
repo: "o/r",
stackTrace: "AttributeError: foo",
language: "python",
});
expect(result.offline).toBe(true);
expect(result.suggestedPath.endsWith(".py")).toBe(true);
expect(result.testCode).toContain("def test_");
});
it("falls back to offline on a 500 response", async () => {
process.env.GATETEST_API_KEY = "sk-test";
globalThis.fetch = (async () =>
new Response("down", { status: 500 })) as unknown as typeof fetch;
const result = await stackTraceToTest({
repo: "o/r",
stackTrace: "boom",
});
expect(result.offline).toBe(true);
expect(result.framework).toBe("fallback");
});
});
describe("gatetest-client — healSuite", () => {
it("returns offline zeros when no key is set", async () => {
const result = await healSuite({ repo: "o/r" });
expect(result.offline).toBe(true);
expect(result.flakyFound).toBe(0);
expect(result.deadFound).toBe(0);
expect(result.coverageGapsFound).toBe(0);
expect(result.prDraftBranch).toBeNull();
});
it("falls back to offline on a 500 response", async () => {
process.env.GATETEST_API_KEY = "sk-test";
globalThis.fetch = (async () =>
new Response("fail", { status: 500 })) as unknown as typeof fetch;
const result = await healSuite({ repo: "o/r" });
expect(result.offline).toBe(true);
expect(result.flakyFound).toBe(0);
});
it("falls back to offline when fetch itself throws (network down)", async () => {
process.env.GATETEST_API_KEY = "sk-test";
globalThis.fetch = (async () => {
throw new Error("ECONNREFUSED");
}) as unknown as typeof fetch;
const result = await healSuite({ repo: "o/r" });
expect(result.offline).toBe(true);
expect(result.prDraftBranch).toBeNull();
});
it("parses a healthy 200 response", async () => {
process.env.GATETEST_API_KEY = "sk-test";
globalThis.fetch = (async () =>
new Response(
JSON.stringify({
flakyFound: 2,
deadFound: 1,
coverageGapsFound: 4,
prDraftBranch: "gatetest/heal-42",
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)) as unknown as typeof fetch;
const result = await healSuite({ repo: "o/r" });
expect(result.offline).toBe(false);
expect(result.flakyFound).toBe(2);
expect(result.prDraftBranch).toBe("gatetest/heal-42");
});
});
|