Skip to content

Commit c9ec1de

Browse files
committed
test: dedupe codex shared client mock calls
1 parent f7f9312 commit c9ec1de

1 file changed

Lines changed: 57 additions & 10 deletions

File tree

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,53 @@ async function sendEmptyModelList(harness: ReturnType<typeof createClientHarness
5656
harness.send({ id: modelList.id, result: { data: [] } });
5757
}
5858

59+
function firstMockArg(mock: unknown, label: string): unknown {
60+
const call = (mock as { mock?: { calls?: unknown[][] } }).mock?.calls?.at(0);
61+
if (!call) {
62+
throw new Error(`Expected ${label} first call`);
63+
}
64+
return call[0];
65+
}
66+
67+
function bridgeStartOptionsCall() {
68+
return firstMockArg(mocks.bridgeCodexAppServerStartOptions, "bridge start options") as {
69+
agentDir?: string;
70+
authProfileId?: string;
71+
config?: unknown;
72+
startOptions: { command?: string; commandSource?: string };
73+
};
74+
}
75+
76+
function applyAuthProfileCall() {
77+
return firstMockArg(mocks.applyCodexAppServerAuthProfile, "apply auth profile") as {
78+
agentDir?: string;
79+
authProfileId?: string;
80+
config?: unknown;
81+
};
82+
}
83+
84+
function resolveAuthProfileCall() {
85+
return firstMockArg(mocks.resolveCodexAppServerAuthProfileIdForAgent, "resolve auth profile") as {
86+
agentDir?: string;
87+
authProfileId?: string;
88+
config?: unknown;
89+
};
90+
}
91+
92+
function managedStartOptionsCall() {
93+
return firstMockArg(mocks.resolveManagedCodexAppServerStartOptions, "managed start options") as {
94+
command?: string;
95+
commandSource?: string;
96+
};
97+
}
98+
99+
function clientStartCall(startSpy: unknown) {
100+
return firstMockArg(startSpy, "CodexAppServerClient.start") as {
101+
command?: string;
102+
commandSource?: string;
103+
};
104+
}
105+
59106
describe("shared Codex app-server client", () => {
60107
beforeAll(async () => {
61108
({ listCodexAppServerModels } = await import("./models.js"));
@@ -145,9 +192,9 @@ describe("shared Codex app-server client", () => {
145192
await sendEmptyModelList(harness);
146193

147194
await expect(listPromise).resolves.toEqual({ models: [] });
148-
const [bridgeCall] = mocks.bridgeCodexAppServerStartOptions.mock.calls[0] ?? [];
195+
const bridgeCall = bridgeStartOptionsCall();
149196
expect(bridgeCall?.authProfileId).toBe("openai-codex:work");
150-
const [applyCall] = mocks.applyCodexAppServerAuthProfile.mock.calls[0] ?? [];
197+
const applyCall = applyAuthProfileCall();
151198
expect(applyCall?.authProfileId).toBe("openai-codex:work");
152199
});
153200

@@ -165,16 +212,16 @@ describe("shared Codex app-server client", () => {
165212
await sendEmptyModelList(harness);
166213

167214
await expect(listPromise).resolves.toEqual({ models: [] });
168-
const [resolveCall] = mocks.resolveCodexAppServerAuthProfileIdForAgent.mock.calls[0] ?? [];
215+
const resolveCall = resolveAuthProfileCall();
169216
expect(resolveCall).toStrictEqual({
170217
authProfileId: undefined,
171218
agentDir: "/tmp/openclaw-agent",
172219
config,
173220
});
174-
const [bridgeCall] = mocks.bridgeCodexAppServerStartOptions.mock.calls[0] ?? [];
221+
const bridgeCall = bridgeStartOptionsCall();
175222
expect(bridgeCall?.authProfileId).toBe("openai-codex:work");
176223
expect(bridgeCall?.config).toBe(config);
177-
const [applyCall] = mocks.applyCodexAppServerAuthProfile.mock.calls[0] ?? [];
224+
const applyCall = applyAuthProfileCall();
178225
expect(applyCall?.authProfileId).toBe("openai-codex:work");
179226
expect(applyCall?.config).toBe(config);
180227
});
@@ -192,10 +239,10 @@ describe("shared Codex app-server client", () => {
192239
await sendEmptyModelList(harness);
193240

194241
await expect(listPromise).resolves.toEqual({ models: [] });
195-
const [bridgeCall] = mocks.bridgeCodexAppServerStartOptions.mock.calls[0] ?? [];
242+
const bridgeCall = bridgeStartOptionsCall();
196243
expect(bridgeCall?.agentDir).toBe("/tmp/openclaw-agent-nova");
197244
expect(bridgeCall?.authProfileId).toBe("openai-codex:work");
198-
const [applyCall] = mocks.applyCodexAppServerAuthProfile.mock.calls[0] ?? [];
245+
const applyCall = applyAuthProfileCall();
199246
expect(applyCall?.agentDir).toBe("/tmp/openclaw-agent-nova");
200247
expect(applyCall?.authProfileId).toBe("openai-codex:work");
201248
});
@@ -214,13 +261,13 @@ describe("shared Codex app-server client", () => {
214261
await sendEmptyModelList(harness);
215262

216263
await expect(listPromise).resolves.toEqual({ models: [] });
217-
const [managedCall] = mocks.resolveManagedCodexAppServerStartOptions.mock.calls[0] ?? [];
264+
const managedCall = managedStartOptionsCall();
218265
expect(managedCall?.command).toBe("codex");
219266
expect(managedCall?.commandSource).toBe("managed");
220-
const [bridgeCall] = mocks.bridgeCodexAppServerStartOptions.mock.calls[0] ?? [];
267+
const bridgeCall = bridgeStartOptionsCall();
221268
expect(bridgeCall?.startOptions.command).toBe("/cache/openclaw/codex");
222269
expect(bridgeCall?.startOptions.commandSource).toBe("resolved-managed");
223-
const [startCall] = startSpy.mock.calls[0] ?? [];
270+
const startCall = clientStartCall(startSpy);
224271
expect(startCall?.command).toBe("/cache/openclaw/codex");
225272
expect(startCall?.commandSource).toBe("resolved-managed");
226273
});

0 commit comments

Comments
 (0)