|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { registerProjectRoutes } from "./projects"; |
| 7 | +import type { StudioApiAdapter } from "../types"; |
| 8 | + |
| 9 | +const tempDirs: string[] = []; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + for (const dir of tempDirs.splice(0)) { |
| 13 | + rmSync(dir, { recursive: true, force: true }); |
| 14 | + } |
| 15 | +}); |
| 16 | + |
| 17 | +const COMPOSITION_HTML = '<html><body><div data-composition-id="main"></div></body></html>'; |
| 18 | + |
| 19 | +// Project layout for #1384: real compositions at the root and under |
| 20 | +// compositions/, plus vendored example HTML inside dot-directories that |
| 21 | +// must not surface as compositions. |
| 22 | +function createProjectDir(): string { |
| 23 | + const projectDir = mkdtempSync(join(tmpdir(), "hf-projects-test-")); |
| 24 | + tempDirs.push(projectDir); |
| 25 | + writeFileSync(join(projectDir, "index.html"), COMPOSITION_HTML); |
| 26 | + mkdirSync(join(projectDir, "compositions")); |
| 27 | + writeFileSync(join(projectDir, "compositions", "scene.html"), COMPOSITION_HTML); |
| 28 | + mkdirSync(join(projectDir, ".hyperframes", "examples"), { recursive: true }); |
| 29 | + writeFileSync(join(projectDir, ".hyperframes", "examples", "preset.html"), COMPOSITION_HTML); |
| 30 | + return projectDir; |
| 31 | +} |
| 32 | + |
| 33 | +function createAdapter(projectDir: string): StudioApiAdapter { |
| 34 | + return { |
| 35 | + listProjects: () => [], |
| 36 | + resolveProject: async (id: string) => ({ id, dir: projectDir }), |
| 37 | + bundle: async () => null, |
| 38 | + lint: async () => ({ findings: [] }), |
| 39 | + runtimeUrl: "/api/runtime.js", |
| 40 | + rendersDir: () => "/tmp/renders", |
| 41 | + startRender: () => ({ |
| 42 | + id: "job-1", |
| 43 | + status: "rendering", |
| 44 | + progress: 0, |
| 45 | + outputPath: "/tmp/out.mp4", |
| 46 | + }), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +describe("registerProjectRoutes — composition discovery (#1384)", () => { |
| 51 | + it("excludes HTML inside dot-directories from compositions", async () => { |
| 52 | + const projectDir = createProjectDir(); |
| 53 | + const app = new Hono(); |
| 54 | + registerProjectRoutes(app, createAdapter(projectDir)); |
| 55 | + |
| 56 | + const response = await app.request("http://localhost/projects/demo"); |
| 57 | + const payload = (await response.json()) as { compositions?: string[] }; |
| 58 | + |
| 59 | + expect(response.status).toBe(200); |
| 60 | + expect(payload.compositions).toContain("index.html"); |
| 61 | + expect(payload.compositions).toContain("compositions/scene.html"); |
| 62 | + expect(payload.compositions).not.toContain(".hyperframes/examples/preset.html"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("keeps dot-directory files visible in the file tree", async () => { |
| 66 | + const projectDir = createProjectDir(); |
| 67 | + const app = new Hono(); |
| 68 | + registerProjectRoutes(app, createAdapter(projectDir)); |
| 69 | + |
| 70 | + const response = await app.request("http://localhost/projects/demo"); |
| 71 | + const payload = (await response.json()) as { files?: string[] }; |
| 72 | + |
| 73 | + expect(payload.files).toContain(".hyperframes/examples/preset.html"); |
| 74 | + }); |
| 75 | +}); |
0 commit comments