From a130dd080b8a73143ac6f6ebe19be98a29cfd7e5 Mon Sep 17 00:00:00 2001 From: Shakker Date: Fri, 8 May 2026 18:34:10 +0100 Subject: [PATCH] test: tighten image completion call assertions --- src/media-understanding/image.test.ts | 39 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/media-understanding/image.test.ts b/src/media-understanding/image.test.ts index 43a9a2b593a..65a4d700662 100644 --- a/src/media-understanding/image.test.ts +++ b/src/media-understanding/image.test.ts @@ -382,8 +382,12 @@ describe("describeImageWithModel", () => { }), expect.any(Object), ); - const [, context] = completeMock.mock.calls[0] ?? []; - expect(context?.messages?.[0]?.content).toHaveLength(1); + const firstCall = completeMock.mock.calls[0]; + if (!firstCall) { + throw new Error("Expected image completion call"); + } + const [, context] = firstCall; + expect(context.messages[0]?.content).toHaveLength(1); }); it("places OpenRouter image prompts in user content before images", async () => { @@ -422,9 +426,13 @@ describe("describeImageWithModel", () => { text: "openrouter ok", model: "google/gemini-2.5-flash", }); - const [, context] = completeMock.mock.calls[0] ?? []; - expect(context?.systemPrompt).toBeUndefined(); - expect(context?.messages?.[0]?.content).toEqual([ + const firstCall = completeMock.mock.calls[0]; + if (!firstCall) { + throw new Error("Expected OpenRouter image completion call"); + } + const [, context] = firstCall; + expect(context.systemPrompt).toBeUndefined(); + expect(context.messages[0]?.content).toEqual([ { type: "text", text: "Describe the image." }, expect.objectContaining({ type: "image", @@ -536,7 +544,11 @@ describe("describeImageWithModel", () => { model: model.id, }); expect(completeMock).toHaveBeenCalledTimes(2); - const [, , retryOptions] = completeMock.mock.calls[1] ?? []; + const retryCall = completeMock.mock.calls[1]; + if (!retryCall) { + throw new Error("Expected retry image completion call"); + } + const [retryModel, , retryOptions] = retryCall; if (!retryOptions?.onPayload) { throw new Error("expected retry payload mapper"); } @@ -546,7 +558,7 @@ describe("describeImageWithModel", () => { reasoning_effort: "high", include: ["reasoning.encrypted_content"], }, - completeMock.mock.calls[1]?.[0], + retryModel, ); expect(retryPayload).toEqual(expectedRetryPayload); }, @@ -580,9 +592,16 @@ describe("describeImageWithModel", () => { const assertion = expect(result).rejects.toThrow("image description timed out after 25ms"); await vi.advanceTimersByTimeAsync(25); await assertion; - const [, , options] = completeMock.mock.calls[0] ?? []; - expect(options?.signal?.aborted).toBe(true); - expect(options?.timeoutMs).toBe(25); + const firstCall = completeMock.mock.calls[0]; + if (!firstCall) { + throw new Error("Expected timed image completion call"); + } + const [, , options] = firstCall; + if (!options?.signal) { + throw new Error("Expected image completion abort signal"); + } + expect(options.signal.aborted).toBe(true); + expect(options.timeoutMs).toBe(25); }); it("rejects when image runtime setup exceeds the request timeout", async () => {