From 5c59bd69876a53da5eccbe6dd9a720efb9e76580 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 23:12:57 +0100 Subject: [PATCH] test: guard qa and telnyx fetch mock calls --- .../src/qa-credentials-admin.runtime.test.ts | 27 ++++++++++++++---- .../voice-call/src/providers/telnyx.test.ts | 28 ++++++++++--------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/extensions/qa-lab/src/qa-credentials-admin.runtime.test.ts b/extensions/qa-lab/src/qa-credentials-admin.runtime.test.ts index 358201dbab0..3ec3fab6c64 100644 --- a/extensions/qa-lab/src/qa-credentials-admin.runtime.test.ts +++ b/extensions/qa-lab/src/qa-credentials-admin.runtime.test.ts @@ -16,14 +16,30 @@ function jsonResponse(payload: unknown, status = 200) { }); } +function requireFirstFetchCall(fetchImpl: ReturnType) { + const [call] = fetchImpl.mock.calls as unknown[][]; + if (!call) { + throw new Error("expected fetch call"); + } + return call; +} + function requireFirstFetchInput(fetchImpl: ReturnType): RequestInfo | URL { - const input = fetchImpl.mock.calls[0]?.[0] as RequestInfo | URL | undefined; + const input = requireFirstFetchCall(fetchImpl)[0] as RequestInfo | URL | undefined; if (!input) { throw new Error("expected fetch input"); } return input; } +function requireFirstFetchInit(fetchImpl: ReturnType): RequestInit { + const init = requireFirstFetchCall(fetchImpl)[1]; + if (!init || typeof init !== "object" || Array.isArray(init)) { + throw new Error("expected fetch init"); + } + return init as RequestInit; +} + async function expectQaCredentialAdminError(promise: Promise, code: string) { const error = await promise.then( () => undefined, @@ -69,8 +85,10 @@ describe("qa credential admin runtime", () => { }); expect(result.credential.credentialId).toBe("cred-1"); - const [url, init] = fetchImpl.mock.calls[0] ?? []; - expect(url).toBe("https://first-schnauzer-821.convex.site/qa-credentials/v1/admin/add"); + expect(requireFirstFetchInput(fetchImpl)).toBe( + "https://first-schnauzer-821.convex.site/qa-credentials/v1/admin/add", + ); + const init = requireFirstFetchInit(fetchImpl); const headers = init?.headers as Record; expect(headers.authorization).toBe("Bearer maint-secret"); const bodyText = init?.body; @@ -207,8 +225,7 @@ describe("qa credential admin runtime", () => { }); expect(result.credentials).toHaveLength(1); - const [, init] = fetchImpl.mock.calls[0] ?? []; - const bodyText = init?.body; + const bodyText = requireFirstFetchInit(fetchImpl).body; expect(typeof bodyText).toBe("string"); const body = JSON.parse(bodyText as string) as Record; expect(body).toEqual({ diff --git a/extensions/voice-call/src/providers/telnyx.test.ts b/extensions/voice-call/src/providers/telnyx.test.ts index 35c94c9b9c5..a8518183bd8 100644 --- a/extensions/voice-call/src/providers/telnyx.test.ts +++ b/extensions/voice-call/src/providers/telnyx.test.ts @@ -28,21 +28,23 @@ function createCtx(params?: Partial): WebhookContext { } function requireFetchRequest() { - const request = apiMocks.fetchWithSsrFGuard.mock.calls[0]?.[0] as - | { - url?: string; - auditContext?: string; - policy?: unknown; - init?: { - method?: string; - body?: unknown; - }; - } - | undefined; - if (!request) { + const [call] = apiMocks.fetchWithSsrFGuard.mock.calls; + if (!call) { throw new Error("expected Telnyx provider to call fetchWithSsrFGuard"); } - return request; + const [request] = call; + if (!request || typeof request !== "object" || Array.isArray(request)) { + throw new Error("expected Telnyx provider to call fetchWithSsrFGuard"); + } + return request as { + url?: string; + auditContext?: string; + policy?: unknown; + init?: { + method?: string; + body?: unknown; + }; + }; } function decodeBase64Url(input: string): Buffer {