/** * The dev seed's media PUT (`putOnce`), against a stubbed fetch: a 5xx is sent * again until it lands, at most PUT_TRIES times; a 4xx refuses at once; a 5xx * every try refuses, saying how often it was tried. */import { afterEach, describe, expect, it, vi } from"vitest";import { putOnce, PUT_TRIES, SeedRefusal, typeSession } from"../dev-seed/client.ts";const SESSION: Session = { base: "https://dev.example", databaseId: "db", credential: "c", token: "t", tokenId: "i" };const MEDIA = { key: "db/photos/invented.png", contentType: "image/png", bytes: newUint8Array([1, 2, 3]) };const noWait = async () => {};function answering(...statuses: number[]) {const asked: number[] = []; vi.stubGlobal("fetch", async () => {const status = statuses[Math.min(asked.length, statuses.length - 1)]!; asked.push(status);returnnewResponse(JSON.stringify(status >= 500 ? { code: "internal_error", message: "the business did not answer" } : { stored: true }), { status }); });return asked;}afterEach(() => vi.unstubAllGlobals());describe("the seed's media PUT", () => { it("sends a PUT the Worker answered 5xx again, and it lands", async () => { vi.spyOn(console, "error").mockImplementation(() => {});const asked = answering(500, 201);const r = await putOnce(SESSION, "/v1/media/db/photos/invented.png", MEDIA as never, 0, noWait); expect(r.status).toBe(201); expect(asked).toEqual([500, 201]); }); it("refuses a 4xx at once", async () => {const asked = answering(413);await expect(putOnce(SESSION, "/v1/media/db/photos/invented.png", MEDIA as never, 0, noWait)).rejects.toBeInstanceOf(SeedRefusal); expect(asked).toEqual([413]); }); it("refuses a 5xx every try, saying how often it was tried", async () => { vi.spyOn(console, "error").mockImplementation(() => {});const asked = answering(500);await expect(putOnce(SESSION, "/v1/media/db/photos/invented.png", MEDIA as never, 0, noWait)).rejects.toThrow(`(tried ${PUT_TRIES} times)`); expect(asked.length).toBe(PUT_TRIES); });});