



















@@ -11,6 +11,32 @@ function jsonResponse(payload: unknown, status = 200) {
1111});
1212}
131314+type FetchMock = { mock: { calls: Parameters<typeof fetch>[] } };
15+16+function fetchCall(fetchImpl: FetchMock, index = 0): Parameters<typeof fetch> {
17+const call = fetchImpl.mock.calls.at(index);
18+if (!call) {
19+throw new Error(`expected fetch call ${index}`);
20+}
21+return call;
22+}
23+24+function fetchUrl(fetchImpl: FetchMock, index = 0): string {
25+const url = fetchCall(fetchImpl, index)[0];
26+if (typeof url !== "string") {
27+throw new Error(`expected fetch call ${index} URL`);
28+}
29+return url;
30+}
31+32+function fetchInit(fetchImpl: FetchMock, index = 0): RequestInit {
33+const init = fetchCall(fetchImpl, index)[1];
34+if (!init || typeof init !== "object") {
35+throw new Error(`expected fetch call ${index} init`);
36+}
37+return init;
38+}
39+1440describe("credential lease runtime", () => {
1541afterEach(() => {
1642vi.restoreAllMocks();
@@ -73,9 +99,8 @@ describe("credential lease runtime", () => {
7399await lease.release();
7410075101expect(fetchImpl).toHaveBeenCalledTimes(3);
76-const firstCall = fetchImpl.mock.calls[0];
77-expect(firstCall?.[0]).toContain("/qa-credentials/v1/acquire");
78-const firstInit = firstCall?.[1];
102+expect(fetchUrl(fetchImpl)).toContain("/qa-credentials/v1/acquire");
103+const firstInit = fetchInit(fetchImpl);
79104const headers = firstInit?.headers as Record<string, string>;
80105expect(headers.authorization).toBe("Bearer maintainer-secret");
81106});
@@ -123,10 +148,10 @@ describe("credential lease runtime", () => {
123148sutToken: "sut",
124149});
125150expect(fetchImpl).toHaveBeenCalledTimes(3);
126-expect(fetchImpl.mock.calls[1]?.[0]).toBe(
151+expect(fetchUrl(fetchImpl, 1)).toBe(
127152"https://qa-cred.example.convex.site/qa-credentials/v1/payload-chunk",
128153);
129-const chunkRequestBody = fetchImpl.mock.calls[1]?.[1]?.body;
154+const chunkRequestBody = fetchInit(fetchImpl, 1).body;
130155expect(chunkRequestBody).toBeTypeOf("string");
131156const chunkRequest = JSON.parse(chunkRequestBody as string) as {
132157credentialId?: string;
@@ -161,8 +186,7 @@ describe("credential lease runtime", () => {
161186payload as { groupId: string; driverToken: string; sutToken: string },
162187});
163188164-const firstCall = fetchImpl.mock.calls[0];
165-const firstInit = firstCall?.[1];
189+const firstInit = fetchInit(fetchImpl);
166190const headers = firstInit?.headers as Record<string, string>;
167191expect(headers.authorization).toBe("Bearer maintainer-secret");
168192});
@@ -191,8 +215,7 @@ describe("credential lease runtime", () => {
191215payload as { groupId: string; driverToken: string; sutToken: string },
192216});
193217194-const firstCall = fetchImpl.mock.calls[0];
195-const firstInit = firstCall?.[1];
218+const firstInit = fetchInit(fetchImpl);
196219const headers = firstInit?.headers as Record<string, string>;
197220expect(headers.authorization).toBe("Bearer ci-secret");
198221});
@@ -294,8 +317,7 @@ describe("credential lease runtime", () => {
294317payload as { groupId: string; driverToken: string; sutToken: string },
295318});
296319297-const firstCall = fetchImpl.mock.calls[0];
298-expect(firstCall?.[0]).toBe("http://127.0.0.1:3210/qa-credentials/v1/acquire");
320+expect(fetchUrl(fetchImpl)).toBe("http://127.0.0.1:3210/qa-credentials/v1/acquire");
299321});
300322301323it("rejects unsafe endpoint prefix overrides", async () => {
@@ -346,7 +368,7 @@ describe("credential lease runtime", () => {
346368).rejects.toThrow("bad payload shape");
347369348370expect(fetchImpl).toHaveBeenCalledTimes(2);
349-expect(fetchImpl.mock.calls[1]?.[0]).toBe(
371+expect(fetchUrl(fetchImpl, 1)).toBe(
350372"https://qa-cred.example.convex.site/qa-credentials/v1/release",
351373);
352374});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。