@@ -11,6 +11,28 @@ type RetryOptions = {
|
11 | 11 | shouldRetry: (err: unknown) => boolean; |
12 | 12 | }; |
13 | 13 | |
| 14 | +type PostJsonParams = { |
| 15 | +url?: unknown; |
| 16 | +headers?: unknown; |
| 17 | +body?: unknown; |
| 18 | +errorPrefix?: unknown; |
| 19 | +attachStatus?: unknown; |
| 20 | +}; |
| 21 | + |
| 22 | +function requirePostJsonParams( |
| 23 | +postJsonMock: ReturnType<typeof vi.mocked<typeof import("./post-json.js").postJson>>, |
| 24 | +): PostJsonParams { |
| 25 | +const [call] = postJsonMock.mock.calls; |
| 26 | +if (!call) { |
| 27 | +throw new Error("expected postJson call"); |
| 28 | +} |
| 29 | +const [params] = call; |
| 30 | +if (typeof params !== "object" || params === null || Array.isArray(params)) { |
| 31 | +throw new Error("expected postJson params to be an object"); |
| 32 | +} |
| 33 | +return params; |
| 34 | +} |
| 35 | + |
14 | 36 | function requireRetryOptions(call: unknown[] | undefined): RetryOptions { |
15 | 37 | const options = call?.[1] as RetryOptions | undefined; |
16 | 38 | if (!options) { |
@@ -49,12 +71,12 @@ describe("postJsonWithRetry", () => {
|
49 | 71 | }); |
50 | 72 | |
51 | 73 | expect(result).toEqual({ ok: true, ids: [1, 2] }); |
52 | | -const postJsonParams = postJsonMock.mock.calls[0]?.[0]; |
53 | | -expect(postJsonParams?.url).toBe("https://memory.example/v1/batch"); |
54 | | -expect(postJsonParams?.headers).toEqual({ Authorization: "Bearer test" }); |
55 | | -expect(postJsonParams?.body).toEqual({ chunks: ["a", "b"] }); |
56 | | -expect(postJsonParams?.errorPrefix).toBe("memory batch failed"); |
57 | | -expect(postJsonParams?.attachStatus).toBe(true); |
| 74 | +const postJsonParams = requirePostJsonParams(postJsonMock); |
| 75 | +expect(postJsonParams.url).toBe("https://memory.example/v1/batch"); |
| 76 | +expect(postJsonParams.headers).toEqual({ Authorization: "Bearer test" }); |
| 77 | +expect(postJsonParams.body).toEqual({ chunks: ["a", "b"] }); |
| 78 | +expect(postJsonParams.errorPrefix).toBe("memory batch failed"); |
| 79 | +expect(postJsonParams.attachStatus).toBe(true); |
58 | 80 | |
59 | 81 | const retryOptions = requireRetryOptions(retryAsyncMock.mock.calls[0]); |
60 | 82 | expect(retryOptions.attempts).toBe(3); |
|