Skip to content

Commit 4e33639

Browse files
authored
test(codex): await shared-client transport writes (#131300)
1 parent 6ac9e3e commit 4e33639

2 files changed

Lines changed: 46 additions & 14 deletions

File tree

‎extensions/codex/src/app-server/shared-client.test.ts‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ async function sendInitializeResult(
149149
harness: ReturnType<typeof createClientHarness>,
150150
userAgent: string,
151151
): Promise<void> {
152-
await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(1));
153-
const initialize = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
152+
const initialize = JSON.parse(await harness.waitForWrite(0)) as { id: number; method: string };
153+
expect(initialize.method).toBe("initialize");
154154
harness.send({ id: initialize.id, result: { userAgent } });
155155
}
156156

157157
async function sendEmptyModelList(harness: ReturnType<typeof createClientHarness>): Promise<void> {
158-
await vi.waitFor(() => expect(harness.writes.length).toBeGreaterThanOrEqual(3));
159-
const modelList = JSON.parse(harness.writes[2] ?? "{}") as { id?: number };
158+
const modelList = JSON.parse(await harness.waitForWrite(2)) as { id: number; method: string };
159+
expect(modelList.method).toBe("model/list");
160160
harness.send({ id: modelList.id, result: { data: [] } });
161161
}
162162

@@ -411,8 +411,8 @@ describe("shared Codex app-server client", () => {
411411
secretFreeCacheKey: "replacement-account",
412412
});
413413
const nextAcquire = getLeasedSharedCodexAppServerClient(options);
414-
await vi.waitFor(() => expect(startSpy).toHaveBeenCalledTimes(2));
415414
await sendInitializeResult(replacement, "openclaw/0.149.0 (Linux; test)");
415+
expect(startSpy).toHaveBeenCalledTimes(2);
416416
await expect(nextAcquire).resolves.toBe(replacement.client);
417417
releaseLeasedSharedCodexAppServerClient(replacement.client);
418418
expect(mocks.applyCodexAppServerAuthProfile).toHaveBeenLastCalledWith(
@@ -1374,25 +1374,33 @@ describe("shared Codex app-server client", () => {
13741374
chatgptPlanType: null,
13751375
});
13761376
}
1377+
const responseIndex = first.writes.length;
13771378
first.send({
13781379
id: "failed-refresh",
13791380
method: "account/chatgptAuthTokens/refresh",
13801381
params: { reason: "unauthorized", previousAccountId: "original-account" },
13811382
});
1382-
await vi.waitFor(() =>
1383-
expect(first.writes.map((line) => JSON.parse(line))).toContainEqual(
1384-
expect.objectContaining({ id: "failed-refresh", error: expect.any(Object) }),
1385-
),
1386-
);
1383+
expect(JSON.parse(await first.waitForWrite(responseIndex))).toEqual({
1384+
id: "failed-refresh",
1385+
error: {
1386+
code: -32603,
1387+
message:
1388+
failure === "failure"
1389+
? "refresh failed"
1390+
: "ChatGPT workspace changed during Codex token refresh. Retry to start a client for the selected workspace.",
1391+
},
1392+
});
13871393
expect(first.stdinDestroyed).toBe(false);
13881394
const nextAcquire = getLeasedSharedCodexAppServerClient(options);
1389-
await vi.waitFor(() => expect(startSpy).toHaveBeenCalledTimes(2));
13901395
await sendInitializeResult(replacement, "openclaw/0.149.0 (Linux; test)");
1396+
expect(startSpy).toHaveBeenCalledTimes(2);
13911397
await expect(nextAcquire).resolves.toBe(replacement.client);
13921398
expect(releaseLeasedSharedCodexAppServerClient(first.client)).toBe(true);
13931399
expect(first.stdinDestroyed).toBe(true);
13941400
expect(replacement.stdinDestroyed).toBe(false);
1395-
releaseLeasedSharedCodexAppServerClient(replacement.client);
1401+
expect(releaseLeasedSharedCodexAppServerClient(replacement.client)).toBe(true);
1402+
expect(clearSharedCodexAppServerClientIfCurrent(replacement.client)).toBe(true);
1403+
expect(replacement.stdinDestroyed).toBe(true);
13961404
},
13971405
);
13981406

@@ -1543,8 +1551,8 @@ describe("shared Codex app-server client", () => {
15431551
}
15441552
: { authProfileId: "openai:scoped", authProfileStore: secondStore }),
15451553
});
1546-
await vi.waitFor(() => expect(startSpy).toHaveBeenCalledTimes(2));
15471554
await sendInitializeResult(secondHarness, "openclaw/0.149.0 (macOS; test)");
1555+
expect(startSpy).toHaveBeenCalledTimes(2);
15481556
await expect(secondPromise).resolves.toBe(secondHarness.client);
15491557

15501558
expect(resolvedCacheKeys).toEqual(["account:sha256:first", "account:sha256:second"]);
@@ -1693,8 +1701,8 @@ describe("shared Codex app-server client", () => {
16931701
timeoutMs: 1000,
16941702
preparedAuth: { kind: "api-key", apiKey: "second-platform-key" },
16951703
});
1696-
await vi.waitFor(() => expect(startSpy).toHaveBeenCalledTimes(2));
16971704
await sendInitializeResult(secondHarness, "openclaw/0.149.0 (macOS; test)");
1705+
expect(startSpy).toHaveBeenCalledTimes(2);
16981706
await expect(secondPromise).resolves.toBe(secondHarness.client);
16991707

17001708
expect(cacheKeys).toEqual(["api_key:sha256:first", "api_key:sha256:second"]);

‎extensions/codex/src/app-server/test-support.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function createCodexTestModel(provider = "openai", input = ["text"]): Mod
9393
export function createClientHarness(options: { autoEmitExit?: boolean } = {}) {
9494
const stdout = new PassThrough();
9595
const writes: string[] = [];
96+
const writeEvents = new EventEmitter();
9697
let stdinDestroyed = false;
9798
let exitEmitted = false;
9899
let emitProcessExit: () => void = () => undefined;
@@ -113,6 +114,7 @@ export function createClientHarness(options: { autoEmitExit?: boolean } = {}) {
113114
write(chunk, _encoding, callback) {
114115
writes.push(chunk.toString());
115116
callback();
117+
writeEvents.emit("write");
116118
},
117119
});
118120
const destroyStdin = stdin.destroy.bind(stdin);
@@ -143,6 +145,28 @@ export function createClientHarness(options: { autoEmitExit?: boolean } = {}) {
143145
client,
144146
process,
145147
writes,
148+
async waitForWrite(index: number): Promise<string> {
149+
if (writes[index] !== undefined) {
150+
return writes[index];
151+
}
152+
return await new Promise<string>((resolve, reject) => {
153+
const cleanup = () => {
154+
clearTimeout(timer);
155+
writeEvents.off("write", onWrite);
156+
};
157+
const onWrite = () => {
158+
if (writes[index] !== undefined) {
159+
cleanup();
160+
resolve(writes[index]);
161+
}
162+
};
163+
const timer = setTimeout(() => {
164+
cleanup();
165+
reject(new Error(`Timed out waiting for app-server harness write ${index}`));
166+
}, 1_000);
167+
writeEvents.on("write", onWrite);
168+
});
169+
},
146170
get stdinDestroyed() {
147171
return stdinDestroyed;
148172
},

0 commit comments

Comments
 (0)