11// Codex tests cover auth profile runtime contract plugin behavior.
2- import fs from "node:fs/promises" ;
3- import os from "node:os" ;
42import path from "node:path" ;
5- import {
6- abortAndDrainAgentHarnessRun ,
7- type EmbeddedRunAttemptParams ,
8- } from "openclaw/plugin-sdk/agent-harness" ;
3+ import type { EmbeddedRunAttemptParams } from "openclaw/plugin-sdk/agent-harness" ;
94import { AUTH_PROFILE_RUNTIME_CONTRACT } from "openclaw/plugin-sdk/agent-runtime-test-contracts" ;
10- import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
11- import { createCodexRuntimePlanFixture } from "./run-attempt-test-harness.js" ;
12- import { runCodexAppServerAttempt as runCodexAppServerAttemptImpl } from "./run-attempt.js" ;
5+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
6+ import {
7+ createAppServerHarness ,
8+ createCodexRuntimePlanFixture ,
9+ createParams as createSharedParams ,
10+ runCodexAppServerAttempt as runSharedCodexAppServerAttempt ,
11+ setupRunAttemptTestHooks ,
12+ tempDir ,
13+ threadStartResult ,
14+ turnStartResult ,
15+ } from "./run-attempt-test-harness.js" ;
1316import {
1417 readCodexAppServerBinding ,
15- registerCodexTestSessionIdentity ,
16- resetCodexTestBindingStore ,
17- testCodexAppServerBindingStore ,
1818 writeCodexAppServerBinding as writeRawCodexAppServerBinding ,
1919} from "./session-binding.test-helpers.js" ;
20- import {
21- adaptCodexTestClientFactory ,
22- createCodexTestModel ,
23- type CodexTestAppServerClientFactory ,
24- } from "./test-support.js" ;
25- import { CODEX_APP_SERVER_VERSION } from "./version.js" ;
26-
27- let codexAppServerClientFactoryForTest : CodexTestAppServerClientFactory | undefined ;
28-
29- type RunCodexAppServerAttemptOptions = Omit <
30- NonNullable < Parameters < typeof runCodexAppServerAttemptImpl > [ 1 ] > ,
31- "bindingStore"
32- > ;
33-
34- function setCodexAppServerClientFactoryForTest ( factory : CodexTestAppServerClientFactory ) : void {
35- codexAppServerClientFactoryForTest = factory ;
36- }
37-
38- function resetCodexAppServerClientFactoryForTest ( ) : void {
39- codexAppServerClientFactoryForTest = undefined ;
40- }
20+ import type { CodexAppServerClientOptions } from "./shared-client.js" ;
4121
4222/** Keeps native Codex bindings reusable while omitting OpenClaw tools and search. */
4323function withPersistentCodexTestToolPolicy (
@@ -71,42 +51,23 @@ function withPersistentCodexTestToolPolicy(
7151
7252function runCodexAppServerAttempt (
7353 params : EmbeddedRunAttemptParams ,
74- options : RunCodexAppServerAttemptOptions = { } ,
54+ options : Parameters < typeof runSharedCodexAppServerAttempt > [ 1 ] = { } ,
7555) {
76- const clientFactory =
77- options . clientFactory ??
78- ( codexAppServerClientFactoryForTest
79- ? adaptCodexTestClientFactory ( codexAppServerClientFactoryForTest )
80- : undefined ) ;
81- return runCodexAppServerAttemptImpl ( withPersistentCodexTestToolPolicy ( params ) , {
82- ...options ,
83- bindingStore : testCodexAppServerBindingStore ,
84- ...( clientFactory ? { clientFactory } : { } ) ,
85- } ) ;
56+ return runSharedCodexAppServerAttempt ( withPersistentCodexTestToolPolicy ( params ) , options ) ;
8657}
8758
8859function createParams ( sessionFile : string , workspaceDir : string ) : EmbeddedRunAttemptParams {
89- registerCodexTestSessionIdentity (
90- sessionFile ,
91- AUTH_PROFILE_RUNTIME_CONTRACT . sessionId ,
92- AUTH_PROFILE_RUNTIME_CONTRACT . sessionKey ,
93- ) ;
94- return {
60+ const params = createSharedParams ( sessionFile , workspaceDir , {
9561 prompt : AUTH_PROFILE_RUNTIME_CONTRACT . workspacePrompt ,
9662 sessionId : AUTH_PROFILE_RUNTIME_CONTRACT . sessionId ,
9763 sessionKey : AUTH_PROFILE_RUNTIME_CONTRACT . sessionKey ,
98- sessionFile,
99- workspaceDir,
10064 runId : AUTH_PROFILE_RUNTIME_CONTRACT . runId ,
10165 provider : AUTH_PROFILE_RUNTIME_CONTRACT . codexHarnessProvider ,
102- modelId : "gpt-5.4-codex" ,
103- model : createCodexTestModel ( AUTH_PROFILE_RUNTIME_CONTRACT . codexHarnessProvider ) ,
104- thinkLevel : "medium" ,
105- timeoutMs : 5_000 ,
106- authStorage : { } as never ,
107- authProfileStore : { version : 1 , profiles : { } } ,
108- modelRegistry : { } as never ,
109- } as EmbeddedRunAttemptParams ;
66+ } ) ;
67+ delete params . contextTokenBudget ;
68+ delete params . contextWindowInfo ;
69+ delete params . observeToolTerminal ;
70+ return params ;
11071}
11172
11273function setPreparedOpenAIRoute (
@@ -156,121 +117,37 @@ function writeCodexAppServerBinding(...args: Parameters<typeof writeRawCodexAppS
156117 ) ;
157118}
158119
159- function threadStartResult ( threadId = "thread-auth-contract" ) {
160- return {
161- thread : {
162- id : threadId ,
163- sessionId : "session-1" ,
164- forkedFromId : null ,
165- preview : "" ,
166- ephemeral : false ,
167- modelProvider : "openai" ,
168- createdAt : 1 ,
169- updatedAt : 1 ,
170- status : { type : "idle" } ,
171- path : null ,
172- cwd : "" ,
173- cliVersion : "0.146.1" ,
174- source : "unknown" ,
175- agentNickname : null ,
176- agentRole : null ,
177- gitInfo : null ,
178- name : null ,
179- turns : [ ] ,
180- } ,
181- model : "gpt-5.4-codex" ,
182- modelProvider : "openai" ,
183- serviceTier : null ,
184- cwd : "" ,
185- instructionSources : [ ] ,
186- approvalPolicy : "never" ,
187- approvalsReviewer : "user" ,
188- sandbox : { type : "dangerFullAccess" } ,
189- permissionProfile : null ,
190- reasoningEffort : null ,
191- } ;
192- }
193-
194- function turnStartResult ( turnId = "turn-auth-contract" ) {
195- return {
196- turn : {
197- id : turnId ,
198- status : "inProgress" ,
199- items : [ ] ,
200- error : null ,
201- startedAt : null ,
202- completedAt : null ,
203- durationMs : null ,
204- } ,
205- } ;
206- }
207-
208- function getMockServerVersion ( ) {
209- return CODEX_APP_SERVER_VERSION ;
210- }
211-
212- function getMockRuntimeIdentity ( ) {
213- return { serverVersion : getMockServerVersion ( ) } ;
214- }
215-
216- function mockClientRuntimeMethods ( ) {
217- return {
218- getInstanceId : ( ) => "test-client-1" ,
219- getRuntimeIdentity : getMockRuntimeIdentity ,
220- getServerVersion : getMockServerVersion ,
221- } ;
222- }
223-
224120function createCodexAuthProfileHarness ( params : { startMethod : "thread/start" | "thread/resume" } ) {
225121 const seenAuthProfileIds : Array < string | undefined > = [ ] ;
226122 const seenAgentDirs : Array < string | undefined > = [ ] ;
227- const seenClientOptions : Array < NonNullable < Parameters < CodexTestAppServerClientFactory > [ 4 ] > > = [ ] ;
228- const requests : Array < { method : string ; params : unknown } > = [ ] ;
229- const notificationHandlers = new Set < ( notification : unknown ) => Promise < void > | void > ( ) ;
230- const notify = async ( notification : unknown ) => {
231- await Promise . all (
232- [ ...notificationHandlers ] . map ( ( handler ) => Promise . resolve ( handler ( notification ) ) ) ,
233- ) ;
234- } ;
235- setCodexAppServerClientFactoryForTest (
236- async ( _startOptions , authProfileId , agentDir , _config , options ) => {
237- seenAuthProfileIds . push ( authProfileId ) ;
238- seenAgentDirs . push ( agentDir ) ;
239- if ( options ) {
240- seenClientOptions . push ( options ) ;
123+ const seenClientOptions : CodexAppServerClientOptions [ ] = [ ] ;
124+ const harness = createAppServerHarness (
125+ async ( method ) => {
126+ if ( method === params . startMethod ) {
127+ return threadStartResult ( "thread-auth-contract" , { cwd : "" } ) ;
241128 }
242- return {
243- ...mockClientRuntimeMethods ( ) ,
244- request : vi . fn ( async ( method : string , requestParams ?: unknown ) => {
245- requests . push ( { method, params : requestParams } ) ;
246- if ( method === params . startMethod ) {
247- return threadStartResult ( ) ;
248- }
249- if ( method === "turn/start" ) {
250- return turnStartResult ( ) ;
251- }
252- throw new Error ( `unexpected method: ${ method } ` ) ;
253- } ) ,
254- addNotificationHandler : ( handler : ( notification : unknown ) => Promise < void > | void ) => {
255- notificationHandlers . add ( handler ) ;
256- return ( ) => notificationHandlers . delete ( handler ) ;
257- } ,
258- addRequestHandler : ( ) => ( ) => undefined ,
259- addCloseHandler : ( ) => ( ) => undefined ,
260- } as never ;
129+ if ( method === "turn/start" ) {
130+ return turnStartResult ( "turn-auth-contract" ) ;
131+ }
132+ throw new Error ( `unexpected method: ${ method } ` ) ;
133+ } ,
134+ {
135+ onStart ( authProfileId , agentDir , options ) {
136+ seenAuthProfileIds . push ( authProfileId ) ;
137+ seenAgentDirs . push ( agentDir ) ;
138+ if ( options ) {
139+ seenClientOptions . push ( options ) ;
140+ }
141+ } ,
261142 } ,
262143 ) ;
263144 return {
145+ ...harness ,
264146 seenAuthProfileIds,
265147 seenAgentDirs,
266148 seenClientOptions,
267- async waitForMethod ( method : string ) {
268- await vi . waitFor ( ( ) => expect ( requests . map ( ( entry ) => entry . method ) ) . toContain ( method ) , {
269- ...APP_SERVER_START_WAIT ,
270- } ) ;
271- } ,
272149 async completeTurn ( ) {
273- await notify ( {
150+ await harness . notify ( {
274151 method : "turn/completed" ,
275152 params : {
276153 threadId : "thread-auth-contract" ,
@@ -282,26 +159,13 @@ function createCodexAuthProfileHarness(params: { startMethod: "thread/start" | "
282159 } ;
283160}
284161
162+ setupRunAttemptTestHooks ( ) ;
163+
285164describe ( "Auth profile runtime contract - Codex app-server adapter" , ( ) => {
286165 let tmpDir : string ;
287166
288- beforeEach ( async ( ) => {
289- resetCodexTestBindingStore ( ) ;
290- vi . useRealTimers ( ) ;
291- tmpDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-codex-auth-contract-" ) ) ;
292- } ) ;
293-
294- afterEach ( async ( ) => {
295- vi . useRealTimers ( ) ;
296- await abortAndDrainAgentHarnessRun ( {
297- sessionId : AUTH_PROFILE_RUNTIME_CONTRACT . sessionId ,
298- sessionKey : AUTH_PROFILE_RUNTIME_CONTRACT . sessionKey ,
299- settleMs : 1_000 ,
300- forceClear : true ,
301- reason : "test_cleanup" ,
302- } ) ;
303- resetCodexAppServerClientFactoryForTest ( ) ;
304- await fs . rm ( tmpDir , { recursive : true , force : true } ) ;
167+ beforeEach ( ( ) => {
168+ tmpDir = tempDir ;
305169 } ) ;
306170
307171 it ( "passes the exact OpenAI Codex auth profile into app-server startup" , async ( ) => {
0 commit comments