-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi-client.seer.test.ts
More file actions
277 lines (238 loc) · 7.99 KB
/
Copy pathapi-client.seer.test.ts
File metadata and controls
277 lines (238 loc) · 7.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
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
270
271
272
273
274
275
276
277
/**
* Seer API Client Tests
*
* Tests for the seer-related API functions by mocking fetch.
*/
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import {
getAutofixState,
triggerRootCauseAnalysis,
triggerSolutionPlanning,
} from "../../src/lib/api-client.js";
import { setAuthToken } from "../../src/lib/db/auth.js";
import { setOrgRegion } from "../../src/lib/db/regions.js";
import { useTestConfigDir } from "../helpers.js";
useTestConfigDir("test-seer-api-");
let originalFetch: typeof globalThis.fetch;
beforeEach(async () => {
// Save original fetch
originalFetch = globalThis.fetch;
// Set up auth token (manual token, no refresh)
await setAuthToken("test-token");
// Pre-populate region cache to avoid region resolution API calls
setOrgRegion("test-org", "https://sentry.io");
});
afterEach(() => {
// Restore original fetch
globalThis.fetch = originalFetch;
});
describe("triggerRootCauseAnalysis", () => {
test("sends POST request to autofix endpoint with explorer mode", async () => {
let capturedRequest: Request | undefined;
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
capturedRequest = new Request(input, init);
return new Response(JSON.stringify({ run_id: 12_345 }), {
status: 202,
headers: { "Content-Type": "application/json" },
});
};
await triggerRootCauseAnalysis("test-org", "123456789");
expect(capturedRequest?.method).toBe("POST");
expect(capturedRequest?.url).toContain(
"/organizations/test-org/issues/123456789/autofix/"
);
expect(capturedRequest?.url).toContain("mode=explorer");
});
test("includes step and stopping_point in request body", async () => {
let capturedBody: unknown;
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
const req = new Request(input, init);
capturedBody = await req.json();
return new Response(JSON.stringify({ run_id: 12_345 }), {
status: 202,
headers: { "Content-Type": "application/json" },
});
};
await triggerRootCauseAnalysis("test-org", "123456789");
expect(capturedBody).toEqual({
step: "root_cause",
referrer: "api.cli",
});
});
test("throws ApiError on 402 response", async () => {
globalThis.fetch = async () =>
new Response(JSON.stringify({ detail: "No budget for Seer Autofix" }), {
status: 402,
headers: { "Content-Type": "application/json" },
});
await expect(
triggerRootCauseAnalysis("test-org", "123456789")
).rejects.toThrow();
});
test("throws ApiError on 403 response", async () => {
globalThis.fetch = async () =>
new Response(JSON.stringify({ detail: "AI Autofix is not enabled" }), {
status: 403,
headers: { "Content-Type": "application/json" },
});
await expect(
triggerRootCauseAnalysis("test-org", "123456789")
).rejects.toThrow();
});
});
describe("getAutofixState", () => {
test("sends GET request to autofix endpoint with explorer mode", async () => {
let capturedRequest: Request | undefined;
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
capturedRequest = new Request(input, init);
return new Response(
JSON.stringify({
autofix: {
run_id: 12_345,
status: "processing",
blocks: [],
updated_at: "2025-01-01T00:00:00Z",
},
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
};
const result = await getAutofixState("test-org", "123456789");
expect(result?.run_id).toBe(12_345);
expect(result?.status).toBe("PROCESSING");
expect(capturedRequest?.method).toBe("GET");
expect(capturedRequest?.url).toContain(
"/organizations/test-org/issues/123456789/autofix/"
);
expect(capturedRequest?.url).toContain("mode=explorer");
});
test("normalizes agent status values to uppercase", async () => {
globalThis.fetch = async () =>
new Response(
JSON.stringify({
autofix: {
run_id: 1,
status: "awaiting_user_input",
blocks: [],
updated_at: "2025-01-01T00:00:00Z",
},
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
const result = await getAutofixState("test-org", "123456789");
expect(result?.status).toBe("WAITING_FOR_USER_RESPONSE");
});
test("normalizes US spelling 'canceled' to CANCELLED for terminal status match", async () => {
globalThis.fetch = async () =>
new Response(
JSON.stringify({
autofix: {
run_id: 1,
status: "canceled",
blocks: [],
updated_at: "2025-01-01T00:00:00Z",
},
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
const result = await getAutofixState("test-org", "123456789");
expect(result?.status).toBe("CANCELLED");
});
test("normalizes need_more_information to NEED_MORE_INFORMATION", async () => {
globalThis.fetch = async () =>
new Response(
JSON.stringify({
autofix: {
run_id: 1,
status: "need_more_information",
blocks: [],
updated_at: "2025-01-01T00:00:00Z",
},
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
const result = await getAutofixState("test-org", "123456789");
expect(result?.status).toBe("NEED_MORE_INFORMATION");
});
test("returns null when autofix is null", async () => {
globalThis.fetch = async () =>
new Response(JSON.stringify({ autofix: null }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
const result = await getAutofixState("test-org", "123456789");
expect(result).toBeNull();
});
test("returns completed state with blocks", async () => {
globalThis.fetch = async () =>
new Response(
JSON.stringify({
autofix: {
run_id: 12_345,
status: "completed",
updated_at: "2025-01-01T00:00:00Z",
blocks: [
{
id: "block-1",
message: { role: "assistant", content: "Found the root cause" },
timestamp: "2025-01-01T00:00:00Z",
artifacts: [
{
key: "root_cause",
data: {
one_line_description: "Test cause",
five_whys: ["Why 1"],
},
reason: "",
},
],
},
],
},
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
const result = await getAutofixState("test-org", "123456789");
expect(result?.status).toBe("COMPLETED");
});
});
describe("triggerSolutionPlanning", () => {
test("sends POST request to autofix endpoint with explorer mode", async () => {
let capturedRequest: Request | undefined;
let capturedBody: unknown;
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
capturedRequest = new Request(input, init);
capturedBody = await new Request(input, init).json();
return new Response(JSON.stringify({ run_id: 12_345 }), {
status: 202,
headers: { "Content-Type": "application/json" },
});
};
await triggerSolutionPlanning("test-org", "123456789", 12_345);
expect(capturedRequest?.method).toBe("POST");
expect(capturedRequest?.url).toContain(
"/organizations/test-org/issues/123456789/autofix/"
);
expect(capturedRequest?.url).toContain("mode=explorer");
expect(capturedBody).toEqual({
step: "solution",
run_id: 12_345,
referrer: "api.cli",
});
});
});