From bf4ab7ecf0998dfb73bc75a77bd84d376a1f6bd6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 12 May 2026 14:57:30 +0100 Subject: [PATCH] test: dedupe models list runtime mock calls --- src/commands/models.list.e2e.test.ts | 35 ++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/commands/models.list.e2e.test.ts b/src/commands/models.list.e2e.test.ts index a8f8fe79dba..363a6bd81e0 100644 --- a/src/commands/models.list.e2e.test.ts +++ b/src/commands/models.list.e2e.test.ts @@ -159,13 +159,38 @@ function makeRuntime() { }; } +function firstMockArg(mockFn: ReturnType, label: string): unknown { + const call = mockFn.mock.calls.at(0); + if (!call) { + throw new Error(`Expected ${label} call`); + } + return call.at(0); +} + +function runtimeLogText(runtime: ReturnType): string { + const value = firstMockArg(runtime.log, "runtime.log"); + if (typeof value !== "string") { + throw new Error("Expected runtime.log text"); + } + return value; +} + +function runtimeErrorText(runtime: ReturnType): string { + const value = firstMockArg(runtime.error, "runtime.error"); + if (typeof value !== "string") { + throw new Error("Expected runtime.error text"); + } + return value; +} + function expectModelRegistryUnavailable( runtime: ReturnType, expectedDetail: string, ) { expect(runtime.error).toHaveBeenCalledTimes(1); - expect(runtime.error.mock.calls[0]?.[0]).toContain("Model registry unavailable:"); - expect(runtime.error.mock.calls[0]?.[0]).toContain(expectedDetail); + const errorText = runtimeErrorText(runtime); + expect(errorText).toContain("Model registry unavailable:"); + expect(errorText).toContain(expectedDetail); expect(runtime.log).not.toHaveBeenCalled(); expect(process.exitCode).toBe(1); } @@ -312,7 +337,7 @@ describe("models list/status", () => { function parseJsonLog(runtime: ReturnType) { expect(runtime.log).toHaveBeenCalledTimes(1); - return JSON.parse(String(runtime.log.mock.calls[0]?.[0])); + return JSON.parse(runtimeLogText(runtime)); } async function expectZaiProviderFilter(provider: string) { @@ -397,7 +422,7 @@ describe("models list/status", () => { await modelsListCommand({ plain: true }, runtime); expect(runtime.log).toHaveBeenCalledTimes(1); - expect(runtime.log.mock.calls[0]?.[0]).toBe("zai/glm-4.7"); + expect(runtimeLogText(runtime)).toBe("zai/glm-4.7"); }); it("models list plain keeps canonical OpenRouter native ids", async () => { @@ -420,7 +445,7 @@ describe("models list/status", () => { await modelsListCommand({ plain: true }, runtime); expect(runtime.log).toHaveBeenCalledTimes(1); - expect(runtime.log.mock.calls[0]?.[0]).toBe("openrouter/hunter-alpha"); + expect(runtimeLogText(runtime)).toBe("openrouter/hunter-alpha"); }); it.each(["z.ai", "Z.AI", "z-ai"] as const)(