// @vitest-environment node import { afterAll, describe, expect, it, vi } from "vitest"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createStudioApi } from "../createStudioApi"; import type { StudioApiAdapter } from "../types"; vi.mock("../helpers/subCompositionBuild.js", async (real) => ({ ...(await real()), ROOT_PREVIEW_DEADLINE_MS: 200, })); const projectDir = mkdtempSync(join(tmpdir(), "hf-preview-root-deadline-")); writeFileSync( join(projectDir, "index.html"), '

saved film

', ); afterAll(() => rmSync(projectDir, { recursive: true, force: true })); const adapter: StudioApiAdapter = { listProjects: () => [], resolveProject: async (id: string) => ({ id, dir: projectDir }), bundle: () => new Promise(() => {}), lint: async () => ({ findings: [] }), runtimeUrl: "/api/runtime.js", rendersDir: () => join(projectDir, "renders"), startRender: () => ({ id: "job", status: "rendering", progress: 0, outputPath: "/tmp/out.mp4" }), }; describe("the root preview page's deadline", () => { // fails without the fix: a host bundle that never answered kept the page request open, past every deadline. it("covers the host's own bundle: past it, the page built from the saved file is served", async () => { const api = createStudioApi(adapter); const late = new Promise<"no answer">((done) => setTimeout(done, 5_000, "no answer")); const res = await Promise.race([api.request("http://localhost/projects/demo/preview"), late]); expect(res).not.toBe("no answer"); const page = res as Response; expect(page.status).toBe(200); expect(await page.text()).toContain("saved film"); }, 30_000); });