|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { createStudioServer, type StudioServer } from "./studioServer.js"; |
| 5 | +import { cleanupStudioServerRoot, makeStudioServerRoot } from "./studioServerTestFixture.js"; |
| 6 | + |
| 7 | +const COMPOSITION = `<!doctype html> |
| 8 | +<html><head><style>@font-face{font-family:Brand;src:url(assets/brand.ttf)}</style></head> |
| 9 | +<body><div id="root" data-composition-id="main" data-width="320" data-height="180"> |
| 10 | +<img id="pic" src="assets/pic.png" data-start="0" data-duration="1"> |
| 11 | +<img id="again" src="assets/pic.png" data-start="1" data-duration="1"> |
| 12 | +<img id="responsive" src="assets/pic.png" srcset="assets/pic.png 1x, assets/pic@2x.png 2x" data-start="0" data-duration="1"> |
| 13 | +<video id="clip" src="assets/clip.mp4" poster="assets/pic.png" muted data-start="0" data-duration="1"></video> |
| 14 | +<div id="scene-host" data-composition-id="scene" data-composition-src="compositions/scene.html" data-start="0" data-duration="1"></div> |
| 15 | +</div></body></html>`; |
| 16 | + |
| 17 | +const SCENE = `<template id="scene-template"> |
| 18 | + <div data-composition-id="scene" data-width="320" data-height="180"> |
| 19 | + <img id="nested" src="../assets/pic@2x.png"> |
| 20 | + </div> |
| 21 | +</template>`; |
| 22 | + |
| 23 | +const PIC = Buffer.alloc(4096, 7); |
| 24 | +const PIC_2X = Buffer.alloc(8192, 9); |
| 25 | + |
| 26 | +let root: string; |
| 27 | +let projectDir: string; |
| 28 | +let server: StudioServer; |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + ({ root, projectDir } = makeStudioServerRoot("hf-preview-assets-")); |
| 32 | + fs.mkdirSync(path.join(projectDir, "assets")); |
| 33 | + fs.mkdirSync(path.join(projectDir, "compositions")); |
| 34 | + fs.writeFileSync(path.join(projectDir, "index.html"), COMPOSITION); |
| 35 | + fs.writeFileSync(path.join(projectDir, "compositions", "scene.html"), SCENE); |
| 36 | + fs.writeFileSync(path.join(projectDir, "assets", "pic.png"), PIC); |
| 37 | + fs.writeFileSync(path.join(projectDir, "assets", "pic@2x.png"), PIC_2X); |
| 38 | + fs.writeFileSync(path.join(projectDir, "assets", "brand.ttf"), Buffer.alloc(1024, 3)); |
| 39 | + fs.writeFileSync(path.join(projectDir, "assets", "clip.mp4"), Buffer.alloc(1024, 5)); |
| 40 | + server = createStudioServer({ projectDir, projectName: "film" }); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + cleanupStudioServerRoot(server, root); |
| 45 | +}); |
| 46 | + |
| 47 | +async function fetchAsset(url: string): Promise<Buffer> { |
| 48 | + const response = await server.app.request(`/api/projects/film/preview/${url}`); |
| 49 | + expect(response.status).toBe(200); |
| 50 | + return Buffer.from(await response.arrayBuffer()); |
| 51 | +} |
| 52 | + |
| 53 | +describe("studio preview document", () => { |
| 54 | + it("keeps every asset URL instead of inlining it as base64", async () => { |
| 55 | + const response = await server.app.request("/api/projects/film/preview"); |
| 56 | + const html = await response.text(); |
| 57 | + |
| 58 | + expect(response.status).toBe(200); |
| 59 | + // Served by the bundler, not the raw-HTML fallback, so the assertions below mean something. |
| 60 | + expect(html).toContain('data-hyperframes-preview-runtime="1" src="/api/runtime.js"'); |
| 61 | + expect(html).not.toContain("base64,"); |
| 62 | + expect(html.match(/src="assets\/pic\.png"/g)?.length).toBeGreaterThanOrEqual(3); |
| 63 | + expect(html).toContain("url(assets/brand.ttf)"); |
| 64 | + expect(html).toContain('poster="assets/pic.png"'); |
| 65 | + expect(html).toContain('srcset="assets/pic.png 1x, assets/pic@2x.png 2x"'); |
| 66 | + // Kept URLs resolve against the preview's <base href> through the asset route. |
| 67 | + expect(html).toContain('<base href="/api/projects/film/preview/">'); |
| 68 | + expect(await fetchAsset("assets/pic.png")).toEqual(PIC); |
| 69 | + const nested = html.match(/<img [^>]*id="nested" src="([^"]+)"/)?.[1]; |
| 70 | + expect(nested).toBe("assets/pic@2x.png"); |
| 71 | + expect(await fetchAsset(nested ?? "")).toEqual(PIC_2X); |
| 72 | + }); |
| 73 | + |
| 74 | + it("tags no asset a copy tool just rewrote with its old mtime", async () => { |
| 75 | + const clip = path.join(projectDir, "assets", "clip.mp4"); |
| 76 | + fs.writeFileSync(clip, Buffer.alloc(1024, 6)); |
| 77 | + const copied = new Date("2026-01-01T00:00:00Z"); |
| 78 | + fs.utimesSync(clip, copied, copied); |
| 79 | + |
| 80 | + const response = await server.app.request("/api/projects/film/preview/assets/clip.mp4"); |
| 81 | + |
| 82 | + expect(response.status).toBe(200); |
| 83 | + expect(response.headers.get("ETag")).toBeNull(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments