Skip to content

Commit ca95525

Browse files
committed
Stabilize chat completions fetch-path tests
1 parent baf5f0b commit ca95525

1 file changed

Lines changed: 181 additions & 161 deletions

File tree

web/src/app/api/v1/chat/completions/__tests__/completions.test.ts

Lines changed: 181 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -520,108 +520,120 @@ describe('/api/v1/chat/completions POST endpoint', () => {
520520
expect(body.message).not.toContain(nextQuotaReset)
521521
})
522522

523-
it('lets a new account with no paid relationship through for non-free mode', async () => {
524-
const req = new NextRequest(
525-
'http://localhost:3000/api/v1/chat/completions',
526-
{
527-
method: 'POST',
528-
headers: { Authorization: 'Bearer test-api-key-new-free' },
529-
body: JSON.stringify({
530-
model: 'test/test-model',
531-
stream: false,
532-
codebuff_metadata: {
533-
run_id: 'run-123',
534-
client_id: 'test-client-id-123',
535-
},
536-
}),
537-
},
538-
)
523+
it(
524+
'lets a new account with no paid relationship through for non-free mode',
525+
async () => {
526+
const req = new NextRequest(
527+
'http://localhost:3000/api/v1/chat/completions',
528+
{
529+
method: 'POST',
530+
headers: { Authorization: 'Bearer test-api-key-new-free' },
531+
body: JSON.stringify({
532+
model: 'test/test-model',
533+
stream: false,
534+
codebuff_metadata: {
535+
run_id: 'run-123',
536+
client_id: 'test-client-id-123',
537+
},
538+
}),
539+
},
540+
)
539541

540-
const response = await postChatCompletions({
541-
req,
542-
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
543-
logger: mockLogger,
544-
trackEvent: mockTrackEvent,
545-
getUserUsageData: mockGetUserUsageData,
546-
getAgentRunFromId: mockGetAgentRunFromId,
547-
fetch: mockFetch,
548-
insertMessageBigquery: mockInsertMessageBigquery,
549-
loggerWithContext: mockLoggerWithContext,
550-
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
551-
})
542+
const response = await postChatCompletions({
543+
req,
544+
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
545+
logger: mockLogger,
546+
trackEvent: mockTrackEvent,
547+
getUserUsageData: mockGetUserUsageData,
548+
getAgentRunFromId: mockGetAgentRunFromId,
549+
fetch: mockFetch,
550+
insertMessageBigquery: mockInsertMessageBigquery,
551+
loggerWithContext: mockLoggerWithContext,
552+
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
553+
})
552554

553-
expect(response.status).toBe(200)
554-
})
555+
expect(response.status).toBe(200)
556+
},
557+
FETCH_PATH_TEST_TIMEOUT_MS,
558+
)
555559

556-
it('lets a BYOK free-tier new account through the paid-plan gate', async () => {
557-
const req = new NextRequest(
558-
'http://localhost:3000/api/v1/chat/completions',
559-
{
560-
method: 'POST',
561-
headers: {
562-
Authorization: 'Bearer test-api-key-new-free',
563-
'x-openrouter-api-key': 'sk-or-byok-test',
564-
},
565-
body: JSON.stringify({
566-
model: 'test/test-model',
567-
stream: false,
568-
codebuff_metadata: {
569-
run_id: 'run-123',
570-
client_id: 'test-client-id-123',
560+
it(
561+
'lets a BYOK free-tier new account through the paid-plan gate',
562+
async () => {
563+
const req = new NextRequest(
564+
'http://localhost:3000/api/v1/chat/completions',
565+
{
566+
method: 'POST',
567+
headers: {
568+
Authorization: 'Bearer test-api-key-new-free',
569+
'x-openrouter-api-key': 'sk-or-byok-test',
571570
},
572-
}),
573-
},
574-
)
571+
body: JSON.stringify({
572+
model: 'test/test-model',
573+
stream: false,
574+
codebuff_metadata: {
575+
run_id: 'run-123',
576+
client_id: 'test-client-id-123',
577+
},
578+
}),
579+
},
580+
)
575581

576-
const response = await postChatCompletions({
577-
req,
578-
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
579-
logger: mockLogger,
580-
trackEvent: mockTrackEvent,
581-
getUserUsageData: mockGetUserUsageData,
582-
getAgentRunFromId: mockGetAgentRunFromId,
583-
fetch: mockFetch,
584-
insertMessageBigquery: mockInsertMessageBigquery,
585-
loggerWithContext: mockLoggerWithContext,
586-
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
587-
})
582+
const response = await postChatCompletions({
583+
req,
584+
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
585+
logger: mockLogger,
586+
trackEvent: mockTrackEvent,
587+
getUserUsageData: mockGetUserUsageData,
588+
getAgentRunFromId: mockGetAgentRunFromId,
589+
fetch: mockFetch,
590+
insertMessageBigquery: mockInsertMessageBigquery,
591+
loggerWithContext: mockLoggerWithContext,
592+
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
593+
})
588594

589-
expect(response.status).toBe(200)
590-
})
595+
expect(response.status).toBe(200)
596+
},
597+
FETCH_PATH_TEST_TIMEOUT_MS,
598+
)
591599

592-
it('lets a freebuff/free-mode request through even for a brand-new unpaid account', async () => {
593-
const req = new NextRequest(
594-
'http://localhost:3000/api/v1/chat/completions',
595-
{
596-
method: 'POST',
597-
headers: allowedFreeModeHeaders('test-api-key-new-free'),
598-
body: JSON.stringify({
599-
model: 'minimax/minimax-m2.7',
600-
stream: false,
601-
codebuff_metadata: {
602-
run_id: 'run-free',
603-
client_id: 'test-client-id-123',
604-
cost_mode: 'free',
605-
},
606-
}),
607-
},
608-
)
600+
it(
601+
'lets a freebuff/free-mode request through even for a brand-new unpaid account',
602+
async () => {
603+
const req = new NextRequest(
604+
'http://localhost:3000/api/v1/chat/completions',
605+
{
606+
method: 'POST',
607+
headers: allowedFreeModeHeaders('test-api-key-new-free'),
608+
body: JSON.stringify({
609+
model: 'minimax/minimax-m2.7',
610+
stream: false,
611+
codebuff_metadata: {
612+
run_id: 'run-free',
613+
client_id: 'test-client-id-123',
614+
cost_mode: 'free',
615+
},
616+
}),
617+
},
618+
)
609619

610-
const response = await postChatCompletions({
611-
req,
612-
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
613-
logger: mockLogger,
614-
trackEvent: mockTrackEvent,
615-
getUserUsageData: mockGetUserUsageData,
616-
getAgentRunFromId: mockGetAgentRunFromId,
617-
fetch: mockFetch,
618-
insertMessageBigquery: mockInsertMessageBigquery,
619-
loggerWithContext: mockLoggerWithContext,
620-
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
621-
})
620+
const response = await postChatCompletions({
621+
req,
622+
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
623+
logger: mockLogger,
624+
trackEvent: mockTrackEvent,
625+
getUserUsageData: mockGetUserUsageData,
626+
getAgentRunFromId: mockGetAgentRunFromId,
627+
fetch: mockFetch,
628+
insertMessageBigquery: mockInsertMessageBigquery,
629+
loggerWithContext: mockLoggerWithContext,
630+
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
631+
})
622632

623-
expect(response.status).toBe(200)
624-
})
633+
expect(response.status).toBe(200)
634+
},
635+
FETCH_PATH_TEST_TIMEOUT_MS,
636+
)
625637

626638
it('rejects free-mode requests when location is unknown', async () => {
627639
const req = new NextRequest(
@@ -1033,39 +1045,43 @@ describe('/api/v1/chat/completions POST endpoint', () => {
10331045
expect(body.error).toBe('free_mode_invalid_agent_model')
10341046
})
10351047

1036-
it('allows browser-use as a free-mode subagent under a freebuff root', async () => {
1037-
const req = new NextRequest(
1038-
'http://localhost:3000/api/v1/chat/completions',
1039-
{
1040-
method: 'POST',
1041-
headers: allowedFreeModeHeaders('test-api-key-new-free-gemini'),
1042-
body: JSON.stringify({
1043-
model: 'google/gemini-3.1-flash-lite-preview',
1044-
stream: false,
1045-
codebuff_metadata: {
1046-
run_id: 'run-browser-use-child',
1047-
client_id: 'test-client-id-123',
1048-
cost_mode: 'free',
1049-
},
1050-
}),
1051-
},
1052-
)
1048+
it(
1049+
'allows browser-use as a free-mode subagent under a freebuff root',
1050+
async () => {
1051+
const req = new NextRequest(
1052+
'http://localhost:3000/api/v1/chat/completions',
1053+
{
1054+
method: 'POST',
1055+
headers: allowedFreeModeHeaders('test-api-key-new-free-gemini'),
1056+
body: JSON.stringify({
1057+
model: 'google/gemini-3.1-flash-lite-preview',
1058+
stream: false,
1059+
codebuff_metadata: {
1060+
run_id: 'run-browser-use-child',
1061+
client_id: 'test-client-id-123',
1062+
cost_mode: 'free',
1063+
},
1064+
}),
1065+
},
1066+
)
10531067

1054-
const response = await postChatCompletions({
1055-
req,
1056-
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
1057-
logger: mockLogger,
1058-
trackEvent: mockTrackEvent,
1059-
getUserUsageData: mockGetUserUsageData,
1060-
getAgentRunFromId: mockGetAgentRunFromId,
1061-
fetch: mockFetch,
1062-
insertMessageBigquery: mockInsertMessageBigquery,
1063-
loggerWithContext: mockLoggerWithContext,
1064-
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
1065-
})
1068+
const response = await postChatCompletions({
1069+
req,
1070+
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
1071+
logger: mockLogger,
1072+
trackEvent: mockTrackEvent,
1073+
getUserUsageData: mockGetUserUsageData,
1074+
getAgentRunFromId: mockGetAgentRunFromId,
1075+
fetch: mockFetch,
1076+
insertMessageBigquery: mockInsertMessageBigquery,
1077+
loggerWithContext: mockLoggerWithContext,
1078+
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
1079+
})
10661080

1067-
expect(response.status).toBe(200)
1068-
})
1081+
expect(response.status).toBe(200)
1082+
},
1083+
FETCH_PATH_TEST_TIMEOUT_MS,
1084+
)
10691085

10701086
it('rejects standalone free-mode reviewer runs even when the model is allowlisted', async () => {
10711087
const req = new NextRequest(
@@ -1402,45 +1418,49 @@ describe('/api/v1/chat/completions POST endpoint', () => {
14021418
})
14031419

14041420
describe('Successful responses', () => {
1405-
it('returns stream with correct headers', async () => {
1406-
const req = new NextRequest(
1407-
'http://localhost:3000/api/v1/chat/completions',
1408-
{
1409-
method: 'POST',
1410-
headers: { Authorization: 'Bearer test-api-key-123' },
1411-
body: JSON.stringify({
1412-
stream: true,
1413-
codebuff_metadata: {
1414-
run_id: 'run-123',
1415-
client_id: 'test-client-id-123',
1416-
client_request_id: 'test-client-session-id-123',
1417-
},
1418-
}),
1419-
},
1420-
)
1421+
it(
1422+
'returns stream with correct headers',
1423+
async () => {
1424+
const req = new NextRequest(
1425+
'http://localhost:3000/api/v1/chat/completions',
1426+
{
1427+
method: 'POST',
1428+
headers: { Authorization: 'Bearer test-api-key-123' },
1429+
body: JSON.stringify({
1430+
stream: true,
1431+
codebuff_metadata: {
1432+
run_id: 'run-123',
1433+
client_id: 'test-client-id-123',
1434+
client_request_id: 'test-client-session-id-123',
1435+
},
1436+
}),
1437+
},
1438+
)
14211439

1422-
const response = await postChatCompletions({
1423-
req,
1424-
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
1425-
logger: mockLogger,
1426-
trackEvent: mockTrackEvent,
1427-
getUserUsageData: mockGetUserUsageData,
1428-
getAgentRunFromId: mockGetAgentRunFromId,
1429-
fetch: mockFetch,
1430-
insertMessageBigquery: mockInsertMessageBigquery,
1431-
loggerWithContext: mockLoggerWithContext,
1432-
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
1433-
})
1440+
const response = await postChatCompletions({
1441+
req,
1442+
getUserInfoFromApiKey: mockGetUserInfoFromApiKey,
1443+
logger: mockLogger,
1444+
trackEvent: mockTrackEvent,
1445+
getUserUsageData: mockGetUserUsageData,
1446+
getAgentRunFromId: mockGetAgentRunFromId,
1447+
fetch: mockFetch,
1448+
insertMessageBigquery: mockInsertMessageBigquery,
1449+
loggerWithContext: mockLoggerWithContext,
1450+
checkSessionAdmissible: mockCheckSessionAdmissibleAllow,
1451+
})
14341452

1435-
if (response.status !== 200) {
1436-
const errorBody = await response.json()
1437-
console.log('Error response:', errorBody)
1438-
}
1439-
expect(response.status).toBe(200)
1440-
expect(response.headers.get('Content-Type')).toBe('text/event-stream')
1441-
expect(response.headers.get('Cache-Control')).toBe('no-cache')
1442-
expect(response.headers.get('Connection')).toBe('keep-alive')
1443-
})
1453+
if (response.status !== 200) {
1454+
const errorBody = await response.json()
1455+
console.log('Error response:', errorBody)
1456+
}
1457+
expect(response.status).toBe(200)
1458+
expect(response.headers.get('Content-Type')).toBe('text/event-stream')
1459+
expect(response.headers.get('Cache-Control')).toBe('no-cache')
1460+
expect(response.headers.get('Connection')).toBe('keep-alive')
1461+
},
1462+
FETCH_PATH_TEST_TIMEOUT_MS,
1463+
)
14441464

14451465
it(
14461466
'returns JSON response for non-streaming requests',

0 commit comments

Comments
 (0)