From a1ff0e476755aae04673026c116ffe3dfeee9f8a Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 22:12:02 +0000 Subject: [PATCH] refactor(test): dedupe sessions_spawn thinking assertions --- ...spawn-applies-thinking-default.e2e.test.ts | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/agents/openclaw-tools.subagents.sessions-spawn-applies-thinking-default.e2e.test.ts b/src/agents/openclaw-tools.subagents.sessions-spawn-applies-thinking-default.e2e.test.ts index c9b717571..ecd32cab7 100644 --- a/src/agents/openclaw-tools.subagents.sessions-spawn-applies-thinking-default.e2e.test.ts +++ b/src/agents/openclaw-tools.subagents.sessions-spawn-applies-thinking-default.e2e.test.ts @@ -33,21 +33,37 @@ vi.mock("../gateway/call.js", () => { }; }); +type GatewayCall = { method: string; params?: Record }; + +async function getGatewayCalls(): Promise { + const { callGateway } = await import("../gateway/call.js"); + return (callGateway as unknown as ReturnType).mock.calls.map( + (call) => call[0] as GatewayCall, + ); +} + +function findLastCall(calls: GatewayCall[], predicate: (call: GatewayCall) => boolean) { + for (let i = calls.length - 1; i >= 0; i -= 1) { + const call = calls[i]; + if (call && predicate(call)) { + return call; + } + } + return undefined; +} + describe("sessions_spawn thinking defaults", () => { it("applies agents.defaults.subagents.thinking when thinking is omitted", async () => { const tool = createSessionsSpawnTool({ agentSessionKey: "agent:test:main" }); const result = await tool.execute("call-1", { task: "hello" }); expect(result.details).toMatchObject({ status: "accepted" }); - const { callGateway } = await import("../gateway/call.js"); - const calls = (callGateway as unknown as ReturnType).mock.calls; - - const agentCall = calls - .map((call) => call[0] as { method: string; params?: Record }) - .findLast((call) => call.method === "agent"); - const thinkingPatch = calls - .map((call) => call[0] as { method: string; params?: Record }) - .findLast((call) => call.method === "sessions.patch" && call.params?.thinkingLevel); + const calls = await getGatewayCalls(); + const agentCall = findLastCall(calls, (call) => call.method === "agent"); + const thinkingPatch = findLastCall( + calls, + (call) => call.method === "sessions.patch" && call.params?.thinkingLevel !== undefined, + ); expect(agentCall?.params?.thinking).toBe("high"); expect(thinkingPatch?.params?.thinkingLevel).toBe("high"); @@ -58,15 +74,12 @@ describe("sessions_spawn thinking defaults", () => { const result = await tool.execute("call-2", { task: "hello", thinking: "low" }); expect(result.details).toMatchObject({ status: "accepted" }); - const { callGateway } = await import("../gateway/call.js"); - const calls = (callGateway as unknown as ReturnType).mock.calls; - - const agentCall = calls - .map((call) => call[0] as { method: string; params?: Record }) - .findLast((call) => call.method === "agent"); - const thinkingPatch = calls - .map((call) => call[0] as { method: string; params?: Record }) - .findLast((call) => call.method === "sessions.patch" && call.params?.thinkingLevel); + const calls = await getGatewayCalls(); + const agentCall = findLastCall(calls, (call) => call.method === "agent"); + const thinkingPatch = findLastCall( + calls, + (call) => call.method === "sessions.patch" && call.params?.thinkingLevel !== undefined, + ); expect(agentCall?.params?.thinking).toBe("low"); expect(thinkingPatch?.params?.thinkingLevel).toBe("low");