-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathcodexGateway.test.ts
More file actions
287 lines (252 loc) · 9.23 KB
/
Copy pathcodexGateway.test.ts
File metadata and controls
287 lines (252 loc) · 9.23 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
278
279
280
281
282
283
284
285
286
287
import { afterEach, describe, expect, it, vi } from 'vitest'
import { getAvailableModelIds, getThreadDetail, listDirectoryComposioConnectors, logoutDirectoryComposioCli, resumeThread, startThreadTurn } from './codexGateway'
function mockRpcFetch(): { requests: Array<{ method: string, params: Record<string, unknown> }> } {
const requests: Array<{ method: string, params: Record<string, unknown> }> = []
vi.stubGlobal('fetch', vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
const body = typeof init?.body === 'string'
? JSON.parse(init.body) as { method: string, params: Record<string, unknown> }
: { method: '', params: {} }
requests.push(body)
return new Response(JSON.stringify({
result: {
turn: {
id: `turn-${requests.length}`,
},
},
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
})
}))
return { requests }
}
describe('startThreadTurn collaboration mode payloads', () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it('sends default collaboration mode explicitly after a plan turn', async () => {
const { requests } = mockRpcFetch()
await startThreadTurn('thread-1', 'make a plan', [], 'gpt-5.4', 'medium', undefined, [], 'plan')
await startThreadTurn('thread-1', 'implement it', [], 'gpt-5.4', 'medium', undefined, [], 'default')
expect(requests).toHaveLength(2)
expect(requests[0].method).toBe('turn/start')
expect(requests[0].params.collaborationMode).toEqual({
mode: 'plan',
settings: {
model: 'gpt-5.4',
reasoning_effort: 'medium',
developer_instructions: null,
},
})
expect(requests[1].method).toBe('turn/start')
expect(requests[1].params.collaborationMode).toEqual({
mode: 'default',
settings: {
model: 'gpt-5.4',
reasoning_effort: 'medium',
developer_instructions: null,
},
})
})
})
describe('listDirectoryComposioConnectors', () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it('sends search queries as query params expected by the server', async () => {
const requests: string[] = []
vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL) => {
requests.push(String(input))
return new Response(JSON.stringify({
data: [],
nextCursor: null,
total: 0,
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
})
}))
await listDirectoryComposioConnectors('instagram', '50', 25)
expect(requests).toEqual(['/codex-api/composio/connectors?query=instagram&cursor=50&limit=25'])
})
it('posts to the logout endpoint', async () => {
const requests: Array<{ input: string, method: string }> = []
vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
requests.push({
input: String(input),
method: String(init?.method ?? 'GET'),
})
return new Response(JSON.stringify({
ok: true,
command: 'composio logout',
output: '',
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
})
}))
await logoutDirectoryComposioCli()
expect(requests).toEqual([{ input: '/codex-api/composio/logout', method: 'POST' }])
})
})
describe('getAvailableModelIds', () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it('uses provider models without waiting for model/list when provider models are required', async () => {
const requests: string[] = []
vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL) => {
requests.push(String(input))
if (String(input) === '/codex-api/provider-models') {
return new Response(JSON.stringify({
data: ['big-pickle', 'deepseek-v4-flash-free'],
exclusive: true,
}), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}
throw new Error(`unexpected request ${String(input)}`)
}))
await expect(getAvailableModelIds({
includeProviderModels: true,
requireProviderModels: true,
})).resolves.toEqual(['big-pickle', 'deepseek-v4-flash-free'])
expect(requests).toEqual(['/codex-api/provider-models'])
})
it('requests models for an explicit thread provider', async () => {
const requests: string[] = []
vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL) => {
requests.push(String(input))
if (String(input) === '/codex-api/provider-models?provider=opencode-zen') {
return new Response(JSON.stringify({
data: ['big-pickle', 'ring-2.6-1t-free'],
exclusive: true,
}), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}
throw new Error(`unexpected request ${String(input)}`)
}))
await expect(getAvailableModelIds({
includeProviderModels: true,
requireProviderModels: true,
providerId: 'opencode-zen',
})).resolves.toEqual(['big-pickle', 'ring-2.6-1t-free'])
expect(requests).toEqual(['/codex-api/provider-models?provider=opencode-zen'])
})
it('falls back to model/list when provider models are optional and unavailable', async () => {
const requests: string[] = []
vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
requests.push(String(input))
if (String(input) === '/codex-api/provider-models') {
return new Response(JSON.stringify({ data: [] }), {
status: 503,
headers: { 'Content-Type': 'application/json' },
})
}
const body = typeof init?.body === 'string'
? JSON.parse(init.body) as { method: string }
: { method: '' }
expect(body.method).toBe('model/list')
return new Response(JSON.stringify({
result: {
data: [
{ id: 'gpt-5.5' },
{ model: 'gpt-5.4-mini' },
],
},
}), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}))
await expect(getAvailableModelIds({
includeProviderModels: true,
})).resolves.toEqual(['gpt-5.5', 'gpt-5.4-mini'])
expect(requests).toEqual(['/codex-api/provider-models', '/codex-api/rpc'])
})
})
describe('getThreadDetail', () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it('reads modelProvider from nested thread payloads returned by thread/read', async () => {
vi.stubGlobal('fetch', vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
const body = typeof init?.body === 'string'
? JSON.parse(init.body) as { method: string; params: Record<string, unknown> }
: { method: '', params: {} }
expect(body.method).toBe('thread/read')
return new Response(JSON.stringify({
result: {
thread: {
id: body.params.threadId,
modelProvider: 'opencode_zen',
turns: [],
},
},
}), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}))
await expect(getThreadDetail('legacy-thread')).resolves.toMatchObject({
modelProvider: 'opencode_zen',
})
})
})
describe('resumeThread', () => {
afterEach(() => {
vi.useRealTimers()
vi.unstubAllGlobals()
})
it('coalesces repeated resume failures for the same thread', async () => {
const requests: Array<{ method: string; params: Record<string, unknown> }> = []
vi.stubGlobal('fetch', vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
const body = typeof init?.body === 'string'
? JSON.parse(init.body) as { method: string; params: Record<string, unknown> }
: { method: '', params: {} }
requests.push(body)
return new Response(JSON.stringify({ error: 'no rollout found for thread id missing-thread' }), {
status: 502,
headers: { 'Content-Type': 'application/json' },
})
}))
const results = await Promise.allSettled([
resumeThread('missing-thread'),
resumeThread('missing-thread'),
])
expect(results.every((result) => result.status === 'rejected')).toBe(true)
expect(requests).toEqual([
{ method: 'thread/resume', params: { threadId: 'missing-thread' } },
])
})
it('evicts a stalled resume so later resume attempts are not pinned forever', async () => {
vi.useFakeTimers()
const requests: Array<{ method: string; params: Record<string, unknown> }> = []
vi.stubGlobal('fetch', vi.fn((_input: RequestInfo | URL, init?: RequestInit) => {
const body = typeof init?.body === 'string'
? JSON.parse(init.body) as { method: string; params: Record<string, unknown> }
: { method: '', params: {} }
requests.push(body)
return new Promise<Response>(() => undefined)
}))
const first = resumeThread('stalled-thread')
void resumeThread('stalled-thread')
expect(requests).toHaveLength(1)
await vi.advanceTimersByTimeAsync(30_000)
const retried = resumeThread('stalled-thread')
expect(retried).not.toBe(first)
expect(requests).toEqual([
{ method: 'thread/resume', params: { threadId: 'stalled-thread' } },
{ method: 'thread/resume', params: { threadId: 'stalled-thread' } },
])
})
})