diff --git a/packages/producer/src/services/distributed/plan.test.ts b/packages/producer/src/services/distributed/plan.test.ts index 9e149a35282..782bb6bb753 100644 --- a/packages/producer/src/services/distributed/plan.test.ts +++ b/packages/producer/src/services/distributed/plan.test.ts @@ -1624,3 +1624,73 @@ describe("plan() — webm format (distributed VP9)", () => { expect((caught as Error).message).toMatch(/codec.*only valid for format="mp4"/); }); }); + +describe("plan() — template sub-composition entry", () => { + const TIMEOUT_MS = 30_000; + const INDEX_HTML = ` + + + +
+
+
+ +`; + const STING_TEMPLATE = ``; + + function writeMountedProject(name: string): string { + const dir = join(runRoot, name); + mkdirSync(join(dir, "compositions"), { recursive: true }); + writeFileSync(join(dir, "index.html"), INDEX_HTML, "utf-8"); + writeFileSync(join(dir, "compositions", "sting.html"), STING_TEMPLATE, "utf-8"); + return dir; + } + + it( + "plans a template-wrapped sub-composition rendered alone with its own duration", + async () => { + const dir = writeMountedProject("template-entry-project"); + const planDir = join(runRoot, "template-entry-plan"); + mkdirSync(planDir, { recursive: true }); + const result = await plan( + dir, + { fps: 30, width: 320, height: 240, format: "mp4", entryFile: "compositions/sting.html" }, + planDir, + ); + expect(result.totalFrames).toBe(90); + }, + TIMEOUT_MS, + ); + + it( + "rejects a template entry that index.html does not mount", + async () => { + const dir = writeMountedProject("template-entry-unmounted"); + writeFileSync(join(dir, "compositions", "orphan.html"), STING_TEMPLATE, "utf-8"); + const planDir = join(runRoot, "template-entry-unmounted-plan"); + mkdirSync(planDir, { recursive: true }); + let caught: unknown; + try { + await plan( + dir, + { + fps: 30, + width: 320, + height: 240, + format: "mp4", + entryFile: "compositions/orphan.html", + }, + planDir, + ); + } catch (err) { + caught = err; + } + expect((caught as Error).message).toMatch(/not mounted from index\.html/); + }, + TIMEOUT_MS, + ); +}); diff --git a/packages/producer/src/services/distributed/plan.ts b/packages/producer/src/services/distributed/plan.ts index dad295c4c73..6fd08c25566 100644 --- a/packages/producer/src/services/distributed/plan.ts +++ b/packages/producer/src/services/distributed/plan.ts @@ -1302,7 +1302,6 @@ async function compilePlanComposition( urlByAbsentLocalPath, projectDir, workDir, - htmlPath, entryFile, job, cfg, diff --git a/packages/producer/src/services/render/stages/compileStage.mediaType.test.ts b/packages/producer/src/services/render/stages/compileStage.mediaType.test.ts index 973e6a61dce..6047947d2cc 100644 --- a/packages/producer/src/services/render/stages/compileStage.mediaType.test.ts +++ b/packages/producer/src/services/render/stages/compileStage.mediaType.test.ts @@ -61,9 +61,8 @@ describe("runCompileStage — asset media-type preflight", () => { workDir = mkdtempSync(join(tmpdir(), "compile-stage-media-type-")); const projectDir = join(workDir, "project"); mkdirSync(projectDir); - const htmlPath = join(projectDir, "index.html"); writeFileSync( - htmlPath, + join(projectDir, "index.html"), `
@@ -92,7 +91,6 @@ describe("runCompileStage — asset media-type preflight", () => { runCompileStage({ projectDir, workDir, - htmlPath, entryFile: "index.html", job: { id: "media-type-test", diff --git a/packages/producer/src/services/render/stages/compileStage.test.ts b/packages/producer/src/services/render/stages/compileStage.test.ts index 245124eac48..4aa158509b7 100644 --- a/packages/producer/src/services/render/stages/compileStage.test.ts +++ b/packages/producer/src/services/render/stages/compileStage.test.ts @@ -18,12 +18,13 @@ */ import { afterEach, describe, expect, it } from "vitest"; -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { tmpdir } from "node:os"; import type { EngineConfig } from "@hyperframes/engine"; import type { CanvasResolution } from "@hyperframes/core"; import { + resolveEntryHtmlPath, runCompileStage, type CompileStageInput, type CompileStageResult, @@ -164,7 +165,6 @@ async function runWith( const input: CompileStageInput = { projectDir, workDir: fixture.workDir, - htmlPath: fixture.htmlPath, entryFile: "index.html", job: createJob(), cfg, @@ -260,7 +260,6 @@ describe("runCompileStage — master bus", () => { const result = await runCompileStage({ projectDir: join(fixture.workDir, "project"), workDir: fixture.workDir, - htmlPath: fixture.htmlPath, entryFile: "index.html", job: createJob(), cfg: createCfg(), @@ -294,7 +293,6 @@ describe("runCompileStage — font fetch cancellation", () => { const result = runCompileStage({ projectDir, workDir: fixture.workDir, - htmlPath: fixture.htmlPath, entryFile: "index.html", job: createJob(), cfg: createCfg(), @@ -345,7 +343,6 @@ describe("runCompileStage — aspect-agnostic --resolution re-map", () => { const stageInput: CompileStageInput = { projectDir, workDir: fixture.workDir, - htmlPath: fixture.htmlPath, entryFile: "index.html", job: createJob({ outputResolution: input.outputResolution, @@ -472,3 +469,69 @@ describe("runCompileStage — aspect-agnostic --resolution re-map", () => { ).rejects.toThrow(/aspect ratio|--resolution portrait/i); }); }); + +describe("resolveEntryHtmlPath", () => { + const TEMPLATE = ``; + let fixture: CompileFixture | null = null; + + afterEach(() => { + fixture?.cleanup(); + fixture = null; + }); + + function resolve(entryFile: string): string { + if (!fixture) throw new Error("fixture not set up"); + return resolveEntryHtmlPath({ + projectDir: join(fixture.workDir, "project"), + workDir: fixture.workDir, + entryFile, + log: noopLog, + }); + } + + it("returns a plain entry untouched and writes no wrapper", () => { + fixture = setupFixture(PLAIN_HTML); + expect(resolve("index.html")).toBe(fixture.htmlPath); + expect(existsSync(join(fixture.workDir, "standalone-entry.html"))).toBe(false); + }); + + it("returns a non-template sub-composition file untouched", () => { + fixture = setupFixture(PLAIN_HTML); + const subPath = join(fixture.workDir, "project", "full.html"); + writeFileSync(subPath, PLAIN_HTML, "utf-8"); + expect(resolve("full.html")).toBe(subPath); + }); + + it("isolates a mounted template entry inside the index shell", () => { + fixture = setupFixture( + ` +
+
+
+
+ `, + ); + mkdirSync(join(fixture.workDir, "project", "compositions")); + writeFileSync(join(fixture.workDir, "project", "compositions", "scene.html"), TEMPLATE); + const wrapper = resolve("compositions/scene.html"); + expect(wrapper).toBe(join(fixture.workDir, "standalone-entry.html")); + const html = readFileSync(wrapper, "utf-8"); + expect(html).toContain('id="scene"'); + expect(html).toContain('data-duration="2"'); + expect(html).not.toContain('id="other"'); + }); + + it("refuses a template entry that index.html does not mount", () => { + fixture = setupFixture(PLAIN_HTML); + mkdirSync(join(fixture.workDir, "project", "compositions")); + writeFileSync(join(fixture.workDir, "project", "compositions", "orphan.html"), TEMPLATE); + expect(() => resolve("compositions/orphan.html")).toThrow(/not mounted from index\.html/); + }); + + it("requires an index.html to extract a template entry's shell", () => { + fixture = setupFixture(PLAIN_HTML); + rmSync(fixture.htmlPath); + writeFileSync(join(fixture.workDir, "project", "scene.html"), TEMPLATE); + expect(() => resolve("scene.html")).toThrow(/requires a project index\.html/); + }); +}); diff --git a/packages/producer/src/services/render/stages/compileStage.ts b/packages/producer/src/services/render/stages/compileStage.ts index 08957fcdb36..9440b67c273 100644 --- a/packages/producer/src/services/render/stages/compileStage.ts +++ b/packages/producer/src/services/render/stages/compileStage.ts @@ -35,9 +35,12 @@ * same behavior as before this PR. */ +import { existsSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import type { EngineConfig } from "@hyperframes/engine"; import { suggestMatchingPreset, type CanvasResolution } from "@hyperframes/core"; +import { extractStandaloneEntryFromIndex, isTemplateEntry } from "@hyperframes/core/compiler"; +import { readTextFile } from "@hyperframes/parsers/asset-resolution"; import type { CompiledComposition } from "../../htmlCompiler.js"; import { compileForRender } from "../../htmlCompiler.js"; import type { ProducerLogger } from "../../../logger.js"; @@ -53,9 +56,7 @@ import { preflightCompositionAssetMediaTypes } from "../../assetMediaType.js"; export interface CompileStageInput { projectDir: string; workDir: string; - /** Absolute path to the entry HTML (already resolved to standalone-entry if needed). */ - htmlPath: string; - /** The relative `entryFile` string, used only for log payloads. */ + /** Entry HTML relative to `projectDir`; a `