Skip to content

Commit fad203d

Browse files
committed
fix tests
1 parent 0a31c3c commit fad203d

3 files changed

Lines changed: 132 additions & 68 deletions

File tree

apps/sim/app/api/copilot/api-keys/route.test.ts

Lines changed: 84 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ vi.mock('@/lib/core/config/env', () => createEnvMock({ COPILOT_API_KEY: 'test-ap
2020

2121
import { DELETE, GET } from '@/app/api/copilot/api-keys/route'
2222

23+
// `fetchGo` reads `response.status` and `response.headers.get('content-length')`
24+
// to stamp span attributes, so mock responses need both fields or the call
25+
// path throws before the route handler sees the body.
26+
function buildMockResponse(init: {
27+
ok: boolean
28+
status?: number
29+
json: () => Promise<unknown>
30+
}): Record<string, unknown> {
31+
return {
32+
ok: init.ok,
33+
status: init.status ?? (init.ok ? 200 : 500),
34+
headers: new Headers(),
35+
json: init.json,
36+
}
37+
}
38+
2339
describe('Copilot API Keys API Route', () => {
2440
beforeEach(() => {
2541
vi.clearAllMocks()
@@ -60,10 +76,12 @@ describe('Copilot API Keys API Route', () => {
6076
},
6177
]
6278

63-
mockFetch.mockResolvedValueOnce({
64-
ok: true,
65-
json: () => Promise.resolve(mockApiKeys),
66-
})
79+
mockFetch.mockResolvedValueOnce(
80+
buildMockResponse({
81+
ok: true,
82+
json: () => Promise.resolve(mockApiKeys),
83+
})
84+
)
6785

6886
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
6987
const response = await GET(request)
@@ -83,10 +101,12 @@ describe('Copilot API Keys API Route', () => {
83101
user: { id: 'user-123', email: 'test@example.com' },
84102
})
85103

86-
mockFetch.mockResolvedValueOnce({
87-
ok: true,
88-
json: () => Promise.resolve([]),
89-
})
104+
mockFetch.mockResolvedValueOnce(
105+
buildMockResponse({
106+
ok: true,
107+
json: () => Promise.resolve([]),
108+
})
109+
)
90110

91111
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
92112
const response = await GET(request)
@@ -101,10 +121,12 @@ describe('Copilot API Keys API Route', () => {
101121
user: { id: 'user-123', email: 'test@example.com' },
102122
})
103123

104-
mockFetch.mockResolvedValueOnce({
105-
ok: true,
106-
json: () => Promise.resolve([]),
107-
})
124+
mockFetch.mockResolvedValueOnce(
125+
buildMockResponse({
126+
ok: true,
127+
json: () => Promise.resolve([]),
128+
})
129+
)
108130

109131
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
110132
await GET(request)
@@ -127,11 +149,13 @@ describe('Copilot API Keys API Route', () => {
127149
user: { id: 'user-123', email: 'test@example.com' },
128150
})
129151

130-
mockFetch.mockResolvedValueOnce({
131-
ok: false,
132-
status: 503,
133-
json: () => Promise.resolve({ error: 'Service unavailable' }),
134-
})
152+
mockFetch.mockResolvedValueOnce(
153+
buildMockResponse({
154+
ok: false,
155+
status: 503,
156+
json: () => Promise.resolve({ error: 'Service unavailable' }),
157+
})
158+
)
135159

136160
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
137161
const response = await GET(request)
@@ -146,10 +170,12 @@ describe('Copilot API Keys API Route', () => {
146170
user: { id: 'user-123', email: 'test@example.com' },
147171
})
148172

149-
mockFetch.mockResolvedValueOnce({
150-
ok: true,
151-
json: () => Promise.resolve({ invalid: 'response' }),
152-
})
173+
mockFetch.mockResolvedValueOnce(
174+
buildMockResponse({
175+
ok: true,
176+
json: () => Promise.resolve({ invalid: 'response' }),
177+
})
178+
)
153179

154180
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
155181
const response = await GET(request)
@@ -189,10 +215,12 @@ describe('Copilot API Keys API Route', () => {
189215
},
190216
]
191217

192-
mockFetch.mockResolvedValueOnce({
193-
ok: true,
194-
json: () => Promise.resolve(mockApiKeys),
195-
})
218+
mockFetch.mockResolvedValueOnce(
219+
buildMockResponse({
220+
ok: true,
221+
json: () => Promise.resolve(mockApiKeys),
222+
})
223+
)
196224

197225
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
198226
const response = await GET(request)
@@ -207,10 +235,12 @@ describe('Copilot API Keys API Route', () => {
207235
user: { id: 'user-123', email: 'test@example.com' },
208236
})
209237

210-
mockFetch.mockResolvedValueOnce({
211-
ok: true,
212-
json: () => Promise.reject(new Error('Invalid JSON')),
213-
})
238+
mockFetch.mockResolvedValueOnce(
239+
buildMockResponse({
240+
ok: true,
241+
json: () => Promise.reject(new Error('Invalid JSON')),
242+
})
243+
)
214244

215245
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys')
216246
const response = await GET(request)
@@ -251,10 +281,12 @@ describe('Copilot API Keys API Route', () => {
251281
user: { id: 'user-123', email: 'test@example.com' },
252282
})
253283

254-
mockFetch.mockResolvedValueOnce({
255-
ok: true,
256-
json: () => Promise.resolve({ success: true }),
257-
})
284+
mockFetch.mockResolvedValueOnce(
285+
buildMockResponse({
286+
ok: true,
287+
json: () => Promise.resolve({ success: true }),
288+
})
289+
)
258290

259291
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys?id=key-123')
260292
const response = await DELETE(request)
@@ -281,11 +313,13 @@ describe('Copilot API Keys API Route', () => {
281313
user: { id: 'user-123', email: 'test@example.com' },
282314
})
283315

284-
mockFetch.mockResolvedValueOnce({
285-
ok: false,
286-
status: 404,
287-
json: () => Promise.resolve({ error: 'Key not found' }),
288-
})
316+
mockFetch.mockResolvedValueOnce(
317+
buildMockResponse({
318+
ok: false,
319+
status: 404,
320+
json: () => Promise.resolve({ error: 'Key not found' }),
321+
})
322+
)
289323

290324
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys?id=non-existent')
291325
const response = await DELETE(request)
@@ -300,10 +334,12 @@ describe('Copilot API Keys API Route', () => {
300334
user: { id: 'user-123', email: 'test@example.com' },
301335
})
302336

303-
mockFetch.mockResolvedValueOnce({
304-
ok: true,
305-
json: () => Promise.resolve({ success: false }),
306-
})
337+
mockFetch.mockResolvedValueOnce(
338+
buildMockResponse({
339+
ok: true,
340+
json: () => Promise.resolve({ success: false }),
341+
})
342+
)
307343

308344
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys?id=key-123')
309345
const response = await DELETE(request)
@@ -333,10 +369,12 @@ describe('Copilot API Keys API Route', () => {
333369
user: { id: 'user-123', email: 'test@example.com' },
334370
})
335371

336-
mockFetch.mockResolvedValueOnce({
337-
ok: true,
338-
json: () => Promise.reject(new Error('Invalid JSON')),
339-
})
372+
mockFetch.mockResolvedValueOnce(
373+
buildMockResponse({
374+
ok: true,
375+
json: () => Promise.reject(new Error('Invalid JSON')),
376+
})
377+
)
340378

341379
const request = new NextRequest('http://localhost:3000/api/copilot/api-keys?id=key-123')
342380
const response = await DELETE(request)

apps/sim/app/api/copilot/confirm/route.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe('Copilot Confirm API Route', () => {
206206
})
207207
})
208208

209-
it('returns 400 when the durable write fails before publish', async () => {
209+
it('returns 500 when the durable write fails before publish', async () => {
210210
completeAsyncToolCall.mockRejectedValueOnce(new Error('db down'))
211211

212212
const response = await POST(
@@ -216,7 +216,7 @@ describe('Copilot Confirm API Route', () => {
216216
})
217217
)
218218

219-
expect(response.status).toBe(400)
219+
expect(response.status).toBe(500)
220220
expect(publishToolConfirmation).not.toHaveBeenCalled()
221221
})
222222
})

apps/sim/app/api/copilot/stats/route.test.ts

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ vi.mock('@/lib/core/config/env', () => createEnvMock({ COPILOT_API_KEY: 'test-ap
2222

2323
import { POST } from '@/app/api/copilot/stats/route'
2424

25+
// `fetchGo` reads `response.status` and `response.headers.get('content-length')`
26+
// to stamp span attributes, so mock responses need both fields or the call
27+
// path throws before the route handler sees the body.
28+
function buildMockResponse(init: {
29+
ok: boolean
30+
status?: number
31+
json: () => Promise<unknown>
32+
}): Record<string, unknown> {
33+
return {
34+
ok: init.ok,
35+
status: init.status ?? (init.ok ? 200 : 500),
36+
headers: new Headers(),
37+
json: init.json,
38+
}
39+
}
40+
2541
describe('Copilot Stats API Route', () => {
2642
beforeEach(() => {
2743
vi.clearAllMocks()
@@ -58,10 +74,12 @@ describe('Copilot Stats API Route', () => {
5874
isAuthenticated: true,
5975
})
6076

61-
mockFetch.mockResolvedValueOnce({
62-
ok: true,
63-
json: () => Promise.resolve({ success: true }),
64-
})
77+
mockFetch.mockResolvedValueOnce(
78+
buildMockResponse({
79+
ok: true,
80+
json: () => Promise.resolve({ success: true }),
81+
})
82+
)
6583

6684
const req = createMockRequest('POST', {
6785
messageId: 'message-123',
@@ -152,10 +170,12 @@ describe('Copilot Stats API Route', () => {
152170
isAuthenticated: true,
153171
})
154172

155-
mockFetch.mockResolvedValueOnce({
156-
ok: false,
157-
json: () => Promise.resolve({ error: 'Invalid message ID' }),
158-
})
173+
mockFetch.mockResolvedValueOnce(
174+
buildMockResponse({
175+
ok: false,
176+
json: () => Promise.resolve({ error: 'Invalid message ID' }),
177+
})
178+
)
159179

160180
const req = createMockRequest('POST', {
161181
messageId: 'invalid-message',
@@ -176,10 +196,12 @@ describe('Copilot Stats API Route', () => {
176196
isAuthenticated: true,
177197
})
178198

179-
mockFetch.mockResolvedValueOnce({
180-
ok: false,
181-
json: () => Promise.resolve({ message: 'Rate limit exceeded' }),
182-
})
199+
mockFetch.mockResolvedValueOnce(
200+
buildMockResponse({
201+
ok: false,
202+
json: () => Promise.resolve({ message: 'Rate limit exceeded' }),
203+
})
204+
)
183205

184206
const req = createMockRequest('POST', {
185207
messageId: 'message-123',
@@ -200,10 +222,12 @@ describe('Copilot Stats API Route', () => {
200222
isAuthenticated: true,
201223
})
202224

203-
mockFetch.mockResolvedValueOnce({
204-
ok: false,
205-
json: () => Promise.reject(new Error('Not JSON')),
206-
})
225+
mockFetch.mockResolvedValueOnce(
226+
buildMockResponse({
227+
ok: false,
228+
json: () => Promise.reject(new Error('Not JSON')),
229+
})
230+
)
207231

208232
const req = createMockRequest('POST', {
209233
messageId: 'message-123',
@@ -266,10 +290,12 @@ describe('Copilot Stats API Route', () => {
266290
isAuthenticated: true,
267291
})
268292

269-
mockFetch.mockResolvedValueOnce({
270-
ok: true,
271-
json: () => Promise.resolve({ success: true }),
272-
})
293+
mockFetch.mockResolvedValueOnce(
294+
buildMockResponse({
295+
ok: true,
296+
json: () => Promise.resolve({ success: true }),
297+
})
298+
)
273299

274300
const req = createMockRequest('POST', {
275301
messageId: 'message-456',

0 commit comments

Comments
 (0)