FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
opencode/packages/opencode/test/plugin/codex.test.ts at dev · argszero/opencode · GitHub
argszero
/
opencode
Public
forked from
anomalyco/opencode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
opencode
/
packages
/
opencode
/
test
/
plugin
/
codex.test.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
299 lines (267 loc) · 10.3 KB
Breadcrumbs
opencode
/
packages
/
opencode
/
test
/
plugin
/
codex.test.ts
Copy path
File metadata and controls
299 lines (267 loc) · 10.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
288
289
290
291
292
293
294
295
296
297
298
299
import
{
describe
,
expect
,
test
}
from
"bun:test"
import
{
CodexAuthPlugin
,
parseJwtClaims
,
extractAccountIdFromClaims
,
extractAccountId
,
renderOAuthError
,
type
IdTokenClaims
,
}
from
"../../src/plugin/openai/codex"
function
createTestJwt
(
payload
:
object
)
:
string
{
const
header
=
Buffer
.
from
(
JSON
.
stringify
(
{
alg
:
"none"
}
)
)
.
toString
(
"base64url"
)
const
body
=
Buffer
.
from
(
JSON
.
stringify
(
payload
)
)
.
toString
(
"base64url"
)
return
`
${
header
}
.
${
body
}
.sig`
}
describe
(
"plugin.codex"
,
(
)
=>
{
test
(
"escapes provider errors in callback HTML"
,
(
)
=>
{
const
error
=
`</div><script>alert("xss" & 'more')</script>`
const
html
=
renderOAuthError
(
error
)
expect
(
html
)
.
toContain
(
"</div><script>alert("xss" & 'more')</script>"
)
expect
(
html
)
.
not
.
toContain
(
error
)
}
)
describe
(
"parseJwtClaims"
,
(
)
=>
{
test
(
"parses valid JWT with claims"
,
(
)
=>
{
const
payload
=
{
email
:
"test@example.com"
,
chatgpt_account_id
:
"acc-123"
}
const
jwt
=
createTestJwt
(
payload
)
const
claims
=
parseJwtClaims
(
jwt
)
expect
(
claims
)
.
toEqual
(
payload
)
}
)
test
(
"returns undefined for JWT with less than 3 parts"
,
(
)
=>
{
expect
(
parseJwtClaims
(
"invalid"
)
)
.
toBeUndefined
(
)
expect
(
parseJwtClaims
(
"only.two"
)
)
.
toBeUndefined
(
)
}
)
test
(
"returns undefined for invalid base64"
,
(
)
=>
{
expect
(
parseJwtClaims
(
"a.!!!invalid!!!.b"
)
)
.
toBeUndefined
(
)
}
)
test
(
"returns undefined for invalid JSON payload"
,
(
)
=>
{
const
header
=
Buffer
.
from
(
"{}"
)
.
toString
(
"base64url"
)
const
invalidJson
=
Buffer
.
from
(
"not json"
)
.
toString
(
"base64url"
)
expect
(
parseJwtClaims
(
`
${
header
}
.
${
invalidJson
}
.sig`
)
)
.
toBeUndefined
(
)
}
)
}
)
describe
(
"extractAccountIdFromClaims"
,
(
)
=>
{
test
(
"extracts chatgpt_account_id from root"
,
(
)
=>
{
const
claims
:
IdTokenClaims
=
{
chatgpt_account_id
:
"acc-root"
}
expect
(
extractAccountIdFromClaims
(
claims
)
)
.
toBe
(
"acc-root"
)
}
)
test
(
"extracts chatgpt_account_id from nested https://api.openai.com/auth"
,
(
)
=>
{
const
claims
:
IdTokenClaims
=
{
"https://api.openai.com/auth"
:
{
chatgpt_account_id
:
"acc-nested"
}
,
}
expect
(
extractAccountIdFromClaims
(
claims
)
)
.
toBe
(
"acc-nested"
)
}
)
test
(
"prefers root over nested"
,
(
)
=>
{
const
claims
:
IdTokenClaims
=
{
chatgpt_account_id
:
"acc-root"
,
"https://api.openai.com/auth"
:
{
chatgpt_account_id
:
"acc-nested"
}
,
}
expect
(
extractAccountIdFromClaims
(
claims
)
)
.
toBe
(
"acc-root"
)
}
)
test
(
"extracts from organizations array as fallback"
,
(
)
=>
{
const
claims
:
IdTokenClaims
=
{
organizations
:
[
{
id
:
"org-123"
}
,
{
id
:
"org-456"
}
]
,
}
expect
(
extractAccountIdFromClaims
(
claims
)
)
.
toBe
(
"org-123"
)
}
)
test
(
"returns undefined when no accountId found"
,
(
)
=>
{
const
claims
:
IdTokenClaims
=
{
email
:
"test@example.com"
}
expect
(
extractAccountIdFromClaims
(
claims
)
)
.
toBeUndefined
(
)
}
)
}
)
describe
(
"extractAccountId"
,
(
)
=>
{
test
(
"extracts from id_token first"
,
(
)
=>
{
const
idToken
=
createTestJwt
(
{
chatgpt_account_id
:
"from-id-token"
}
)
const
accessToken
=
createTestJwt
(
{
chatgpt_account_id
:
"from-access-token"
}
)
expect
(
extractAccountId
(
{
id_token
:
idToken
,
access_token
:
accessToken
,
refresh_token
:
"rt"
,
}
)
,
)
.
toBe
(
"from-id-token"
)
}
)
test
(
"falls back to access_token when id_token has no accountId"
,
(
)
=>
{
const
idToken
=
createTestJwt
(
{
email
:
"test@example.com"
}
)
const
accessToken
=
createTestJwt
(
{
"https://api.openai.com/auth"
:
{
chatgpt_account_id
:
"from-access"
}
,
}
)
expect
(
extractAccountId
(
{
id_token
:
idToken
,
access_token
:
accessToken
,
refresh_token
:
"rt"
,
}
)
,
)
.
toBe
(
"from-access"
)
}
)
test
(
"returns undefined when no tokens have accountId"
,
(
)
=>
{
const
token
=
createTestJwt
(
{
email
:
"test@example.com"
}
)
expect
(
extractAccountId
(
{
id_token
:
token
,
access_token
:
token
,
refresh_token
:
"rt"
,
}
)
,
)
.
toBeUndefined
(
)
}
)
test
(
"handles missing id_token"
,
(
)
=>
{
const
accessToken
=
createTestJwt
(
{
chatgpt_account_id
:
"acc-123"
}
)
expect
(
extractAccountId
(
{
id_token
:
""
,
access_token
:
accessToken
,
refresh_token
:
"rt"
,
}
)
,
)
.
toBe
(
"acc-123"
)
}
)
}
)
test
(
"installs websocket transport only when experimental websockets are enabled"
,
async
(
)
=>
{
const
disabled
=
await
CodexAuthPlugin
(
{
}
as
never
)
const
enabled
=
await
CodexAuthPlugin
(
{
}
as
never
,
{
experimentalWebSockets
:
true
}
)
const
disabledOptions
=
await
disabled
.
auth
!
.
loader
!
(
async
(
)
=>
(
{
type
:
"api"
,
key
:
"sk-test"
}
)
as
never
,
{
}
as
never
,
)
const
enabledOptions
=
await
enabled
.
auth
!
.
loader
!
(
async
(
)
=>
(
{
type
:
"api"
,
key
:
"sk-test"
}
)
as
never
,
{
}
as
never
,
)
expect
(
disabledOptions
.
fetch
)
.
toBeUndefined
(
)
expect
(
enabledOptions
.
fetch
)
.
toBeFunction
(
)
await
enabled
.
dispose
?.
(
)
}
)
test
(
"filters unsupported modes and uses Codex context limits for OAuth GPT models"
,
async
(
)
=>
{
const
hooks
=
await
CodexAuthPlugin
(
{
}
as
never
)
const
limit
=
{
context
:
1_050_000
,
input
:
922_000
,
output
:
128_000
}
const
provider
=
{
models
:
{
...
Object
.
fromEntries
(
[
"gpt-5.4"
,
"gpt-5.5"
,
"gpt-5.6-sol"
,
"gpt-5.6-terra"
,
"gpt-5.6-luna"
,
"gpt-5.7-pro"
]
.
map
(
(
id
)
=>
[
id
,
{
id
,
api
:
{
id
}
,
limit
,
cost
:
{
}
,
options
:
{
}
}
,
]
)
,
)
,
"gpt-5.4-pro"
:
{
id
:
"gpt-5.4-pro"
,
api
:
{
id
:
"gpt-5.4"
}
,
limit
,
cost
:
{
}
,
options
:
{
reasoningMode
:
"pro"
}
,
}
,
"gpt-5.6-sol-high"
:
{
id
:
"gpt-5.6-sol-high"
,
api
:
{
id
:
"gpt-5.6-sol"
}
,
limit
,
cost
:
{
}
,
options
:
{
reasoningEffort
:
"high"
}
,
}
,
}
,
}
const
models
=
await
hooks
.
provider
!
.
models
!
(
provider
as
never
,
{
auth
:
{
type
:
"oauth"
}
}
as
never
)
expect
(
models
[
"gpt-5.4"
]
?.
limit
)
.
toEqual
(
limit
)
expect
(
models
[
"gpt-5.5"
]
?.
limit
)
.
toEqual
(
{
context
:
400_000
,
input
:
272_000
,
output
:
128_000
}
)
expect
(
models
[
"gpt-5.6-sol"
]
?.
limit
)
.
toEqual
(
{
context
:
500_000
,
input
:
372_000
,
output
:
128_000
}
)
expect
(
models
[
"gpt-5.6-terra"
]
?.
limit
)
.
toEqual
(
{
context
:
500_000
,
input
:
372_000
,
output
:
128_000
}
)
expect
(
models
[
"gpt-5.6-luna"
]
?.
limit
)
.
toEqual
(
{
context
:
500_000
,
input
:
372_000
,
output
:
128_000
}
)
expect
(
models
[
"gpt-5.4-pro"
]
)
.
toBeUndefined
(
)
expect
(
models
[
"gpt-5.7-pro"
]
)
.
toBeDefined
(
)
expect
(
models
[
"gpt-5.6-sol-high"
]
)
.
toBeDefined
(
)
expect
(
await
hooks
.
provider
!
.
models
!
(
provider
as
never
,
{
auth
:
{
type
:
"api"
}
}
as
never
)
)
.
toBe
(
provider
.
models
as
never
,
)
}
)
test
(
"deduplicates concurrent Codex token refreshes"
,
async
(
)
=>
{
let
auth
=
{
type
:
"oauth"
as
const
,
refresh
:
"refresh-old"
,
access
:
""
,
expires
:
0
,
}
const
authUpdates
:
Array
<
{
body
:
{
refresh
:
string
;
access
:
string
;
expires
:
number
;
accountId
?:
string
}
}
>
=
[
]
let
resolveRefresh
:
(
(
)
=>
void
)
|
undefined
const
refreshReady
=
new
Promise
<
void
>
(
(
resolve
)
=>
{
resolveRefresh
=
resolve
}
)
let
refreshRequests
=
0
const
apiRequests
:
{
authorization
:
string
|
null
;
accountId
:
string
|
null
}
[
]
=
[
]
using
server
=
Bun
.
serve
(
{
port
:
0
,
async
fetch
(
request
)
{
const
url
=
new
URL
(
request
.
url
)
if
(
url
.
pathname
===
"/oauth/token"
)
{
expect
(
await
request
.
text
(
)
)
.
toContain
(
"refresh_token=refresh-old"
)
refreshRequests
+=
1
await
refreshReady
return
Response
.
json
(
{
id_token
:
createTestJwt
(
{
chatgpt_account_id
:
"acc-123"
}
)
,
access_token
:
"access-new"
,
refresh_token
:
"refresh-new"
,
expires_in
:
3600
,
}
)
}
if
(
url
.
pathname
===
"/backend-api/codex/responses"
)
{
apiRequests
.
push
(
{
authorization
:
request
.
headers
.
get
(
"authorization"
)
,
accountId
:
request
.
headers
.
get
(
"ChatGPT-Account-Id"
)
,
}
)
return
new
Response
(
"{}"
,
{
status
:
200
}
)
}
return
new
Response
(
"unexpected request"
,
{
status
:
500
}
)
}
,
}
)
const
hooks
=
await
CodexAuthPlugin
(
{
client
:
{
auth
:
{
async
set
(
input
:
{
body
:
{
refresh
:
string
;
access
:
string
;
expires
:
number
;
accountId
?:
string
}
}
)
{
authUpdates
.
push
(
input
)
auth
=
{
type
:
"oauth"
,
refresh
:
input
.
body
.
refresh
,
access
:
input
.
body
.
access
,
expires
:
input
.
body
.
expires
,
...
(
input
.
body
.
accountId
&&
{
accountId
:
input
.
body
.
accountId
}
)
,
}
}
,
}
,
}
as
never
,
project
:
{
}
as
never
,
directory
:
""
,
worktree
:
""
,
experimental_workspace
:
{
register
(
)
{
}
,
}
,
serverUrl
:
new
URL
(
"https://example.com"
)
,
$
:
{
}
as
never
,
}
,
{
issuer
:
server
.
url
.
origin
,
codexApiEndpoint
:
new
URL
(
"/backend-api/codex/responses"
,
server
.
url
)
.
toString
(
)
,
}
,
)
const
loaded
=
await
hooks
.
auth
!
.
loader
!
(
async
(
)
=>
auth
as
never
,
{
}
as
never
)
const
first
=
loaded
.
fetch
!
(
"https://api.openai.com/v1/responses"
)
const
second
=
loaded
.
fetch
!
(
"https://api.openai.com/v1/responses"
)
await
waitFor
(
(
)
=>
refreshRequests
===
1
)
expect
(
apiRequests
)
.
toHaveLength
(
0
)
resolveRefresh
!
(
)
await
Promise
.
all
(
[
first
,
second
]
)
expect
(
refreshRequests
)
.
toBe
(
1
)
expect
(
authUpdates
)
.
toHaveLength
(
1
)
expect
(
authUpdates
[
0
]
?.
body
.
refresh
)
.
toBe
(
"refresh-new"
)
expect
(
authUpdates
[
0
]
?.
body
.
access
)
.
toBe
(
"access-new"
)
expect
(
authUpdates
[
0
]
?.
body
.
accountId
)
.
toBe
(
"acc-123"
)
expect
(
apiRequests
)
.
toEqual
(
[
{
authorization
:
"Bearer access-new"
,
accountId
:
"acc-123"
}
,
{
authorization
:
"Bearer access-new"
,
accountId
:
"acc-123"
}
,
]
)
}
)
}
)
async
function
waitFor
(
predicate
:
(
)
=>
boolean
)
{
const
started
=
Date
.
now
(
)
while
(
!
predicate
(
)
)
{
if
(
Date
.
now
(
)
-
started
>
1_000
)
throw
new
Error
(
"timed out waiting for condition"
)
await
new
Promise
(
(
resolve
)
=>
setTimeout
(
resolve
,
1
)
)
}
}
Back
|
FazBrowse Home
|
New Git URL