| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { fetchWithStaticInstaller } from "./static-installer"; |
| 3 | |
| 4 | function ctx(): unknown { |
| 5 | return { |
| 6 | waitUntil: vi.fn(), |
| 7 | passThroughOnException: vi.fn(), |
| 8 | }; |
| 9 | } |
| 10 | |
| 11 | describe("static installer route", () => { |
| 12 | const fallbackFetch = vi.fn(async () => new Response("fallback", { status: 200 })); |
| 13 | |
| 14 | beforeEach(() => { |
| 15 | vi.clearAllMocks(); |
| 16 | }); |
| 17 | |
| 18 | it("serves /install.sh from the static asset binding before OpenNext fallback", async () => { |
| 19 | const assetFetch = vi.fn(async () => |
| 20 | new Response("#!/bin/sh\necho codewhale\n", { |
| 21 | headers: { "content-type": "application/octet-stream" }, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | const response = await fetchWithStaticInstaller( |
| 26 | new Request("https://codewhale.net/install.sh"), |
| 27 | { ASSETS: { fetch: assetFetch } }, |
| 28 | ctx(), |
| 29 | fallbackFetch, |
| 30 | ); |
| 31 | |
| 32 | expect(assetFetch).toHaveBeenCalledOnce(); |
| 33 | expect(fallbackFetch).not.toHaveBeenCalled(); |
| 34 | expect(response.headers.get("content-type")).toBe("text/x-shellscript; charset=utf-8"); |
| 35 | expect(response.headers.get("cache-control")).toBe("public, max-age=300"); |
| 36 | expect(await response.text()).toContain("echo codewhale"); |
| 37 | }); |
| 38 | |
| 39 | it("delegates non-installer paths to the OpenNext handler", async () => { |
| 40 | const assetFetch = vi.fn(); |
| 41 | |
| 42 | const response = await fetchWithStaticInstaller( |
| 43 | new Request("https://codewhale.net/install"), |
| 44 | { ASSETS: { fetch: assetFetch } }, |
| 45 | ctx(), |
| 46 | fallbackFetch, |
| 47 | ); |
| 48 | |
| 49 | expect(assetFetch).not.toHaveBeenCalled(); |
| 50 | expect(fallbackFetch).toHaveBeenCalledOnce(); |
| 51 | expect(await response.text()).toBe("fallback"); |
| 52 | }); |
| 53 | }); |
| 54 |