Files
hyperframes/packages/cli/src/utils/catalogUsage.test.ts
T
Michael WangandClaude Opus 5.5 5a88c9938e fix(parsers,lint,producer,cli): confine composition path lookups to the project (#7534)
* fix(parsers,lint,producer,cli): confine composition path lookups to the project

- The local asset resolver offers only files inside the project: a path
  that is still absolute after its leading `../` is clamped (an encoded
  `//`, a drive letter, a leading backslash) yields no candidate.
- Lint, the catalog-usage walk and `keyframes` read a
  data-composition-src only inside the project. Lint and the catalog
  walk resolve it with the render's own resolver, now shared from
  `@hyperframes/parsers/asset-resolution`, and lint reports one outside
  the project as the render pre-flight does.
- The render compile step does not look at a media, gsap script or image
  path on another UNC or device root, and the GIF and HEIC preparation
  treats a `//host/...` image src as a URL rather than a share.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(parsers,lint): keep Windows absolute asset paths that render copies in

The shared asset resolver now offers only files inside the project. On
Windows a drive-letter src such as C:\media\clip.mp4 used to reach the
real file through the old fallback, and render still copies that file in.
Lint now accepts such a file for media and for CSS written in the page,
so it raises no new missing_local_asset or texture_mask_asset_not_found
error. The media-treatment and normalize-audio commands find it again.

The check never looks at another computer's share, and on macOS and
Linux a src that starts with / is still a project URL, so nothing
changes there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(parsers): expect a drive-letter sub-composition src to be refused on Windows

On Windows an absolute src outside the project is a drive-letter path,
which resolveCompositionSrc refuses, as the render pre-flight already
does. On POSIX the same src is a project URL and resolves inside the
project. The test now expects each platform's result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(lint): do not report a missing timeline registry a script outside the project may hold

Lint no longer reads a script outside the project. When a root
composition loads such a script, lint cannot know whether the script
registers the timeline, so it no longer reports missing_timeline_registry
for that composition. Render still loads the script. A composition whose
scripts are all in the project is checked as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 04:14:45 +00:00

235 lines
8.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { summarizeCatalogUsage, type CatalogUsage } from "./catalogUsage.js";
import type { RegistryItemRecord } from "./projectConfig.js";
/**
* Materialize a throwaway project, summarize it, and clean up. Every case here
* needs the same fixture, so the shape lives once.
*/
function usageOf(
files: Record<string, string>,
registryItems?: RegistryItemRecord[],
entry = "index.html",
): CatalogUsage {
const dir = mkdtempSync(join(tmpdir(), "hf-catalog-test-"));
try {
writeFileSync(
join(dir, "hyperframes.json"),
JSON.stringify({
registry: "https://example.test",
...(registryItems ? { registryItems } : {}),
}),
);
for (const [rel, html] of Object.entries(files)) {
const path = join(dir, rel);
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, html);
}
return summarizeCatalogUsage(dir, join(dir, entry));
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
/** Same as {@link usageOf}, but writes `hyperframes.json` verbatim. */
function usageOfRawConfig(configText: string | null): CatalogUsage {
const dir = mkdtempSync(join(tmpdir(), "hf-catalog-test-"));
try {
if (configText !== null) writeFileSync(join(dir, "hyperframes.json"), configText);
writeFileSync(join(dir, "index.html"), entryDoc());
return summarizeCatalogUsage(dir, join(dir, "index.html"));
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
function mountTag(src: string): string {
return `<div data-composition-src="${src}" data-duration="3" data-width="1920" data-height="1080"></div>`;
}
/** Render entry: a plain document, never `<template>`-wrapped (lint forbids it). */
function entryDoc(...srcs: string[]): string {
return `<!doctype html><html><body><div id="root" data-composition-id="main" data-width="1920" data-height="1080">${srcs
.map(mountTag)
.join("")}</div></body></html>`;
}
/**
* A sub-composition as they are actually authored everywhere in this repo:
* wrapped in `<template>`. Template content is inert, so a DOM scan of this
* file finds nothing — the fixture exists to keep that failure from returning.
*/
function subCompDoc(id: string, ...srcs: string[]): string {
return `<template id="${id}-template"><div data-composition-id="${id}" data-width="1920" data-height="1080">${srcs
.map(mountTag)
.join("")}</div></template>`;
}
const BLOCK = (name: string): RegistryItemRecord => ({
name,
type: "hyperframes:block",
target: `compositions/${name}.html`,
});
describe("summarizeCatalogUsage", () => {
it("reports nothing for a project that never added a catalog item", () => {
expect(usageOf({ "index.html": entryDoc() })).toEqual({
installed: [],
usedBlocks: [],
manifestUnreadable: false,
});
});
// The whole point of the manifest: an item that was installed and then not
// mounted is a rejection, and no add-time event can say so.
it("separates an installed block that the entry mounts from one it dropped", () => {
expect(
usageOf(
{
"index.html": entryDoc("compositions/kept.html"),
"compositions/kept.html": subCompDoc("kept"),
"compositions/dropped.html": subCompDoc("dropped"),
},
[BLOCK("kept"), BLOCK("dropped")],
),
).toEqual({
installed: ["dropped", "kept"],
usedBlocks: ["kept"],
manifestUnreadable: false,
});
});
// Regression for two invariants that a DOM scan of raw files gets wrong, each
// of which reports a block that renders in every video as abandoned:
// `<template>` content is invisible to `querySelectorAll`, and nested
// `data-composition-src` is root-relative, not relative to its own file.
it("follows a root-relative mount from inside a template-wrapped sub-composition", () => {
expect(
usageOf(
{
"index.html": entryDoc("compositions/outer.html"),
"compositions/outer.html": subCompDoc("outer", "compositions/inner.html"),
"compositions/inner.html": subCompDoc("inner"),
},
[BLOCK("outer"), BLOCK("inner")],
).usedBlocks,
).toEqual(["inner", "outer"]);
});
// The render refuses a mount outside the project, so a walk must not read one either.
it("does not follow a mount that leaves the project", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-catalog-outside-"));
try {
const project = join(dir, "project");
mkdirSync(join(project, "compositions"), { recursive: true });
writeFileSync(join(dir, "outside.html"), subCompDoc("outside", "compositions/kept.html"));
writeFileSync(join(project, "compositions", "kept.html"), subCompDoc("kept"));
writeFileSync(join(project, "index.html"), entryDoc("../outside.html"));
writeFileSync(
join(project, "hyperframes.json"),
JSON.stringify({ registry: "https://example.test", registryItems: [BLOCK("kept")] }),
);
expect(summarizeCatalogUsage(project, join(project, "index.html")).usedBlocks).toEqual([]);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// A cyclic project must not wedge a render that already produced a video.
it("terminates on a mount cycle", () => {
expect(
usageOf(
{
"index.html": entryDoc("compositions/a.html"),
"compositions/a.html": subCompDoc("a", "compositions/b.html"),
"compositions/b.html": subCompDoc("b", "compositions/a.html"),
},
[BLOCK("a"), BLOCK("b")],
).usedBlocks,
).toEqual(["a", "b"]);
});
// A mount the author commented out does not render, so it is not "used".
it("ignores a commented-out mount", () => {
expect(
usageOf(
{
"index.html": `<!doctype html><html><body><!-- ${mountTag("compositions/kept.html")} --></body></html>`,
"compositions/kept.html": subCompDoc("kept"),
},
[BLOCK("kept")],
),
).toEqual({ installed: ["kept"], usedBlocks: [], manifestUnreadable: false });
});
// Components are pasted inline, so there is no src to match. Reporting one as
// "used" would be a guess; reporting it as installed is a fact.
it("counts a component as installed but never as used", () => {
expect(
usageOf({ "index.html": entryDoc() }, [
{
name: "film-grain",
type: "hyperframes:component",
target: "compositions/components/film-grain.html",
},
]),
).toEqual({ installed: ["film-grain"], usedBlocks: [], manifestUnreadable: false });
});
it("drops a manifest name that is not a safe slug rather than sending it", () => {
expect(
usageOf({ "index.html": entryDoc() }, [
{ name: "/Users/someone/secret", type: "hyperframes:block", target: "compositions/x.html" },
BLOCK("fine"),
]).installed,
).toEqual(["fine"]);
});
it("never matches a manifest target that escapes the project directory", () => {
expect(
usageOf({ "index.html": entryDoc("compositions/kept.html") }, [
{ name: "escaping", type: "hyperframes:block", target: "../outside.html" },
]).usedBlocks,
).toEqual([]);
});
it("survives an entry file that does not exist", () => {
expect(usageOf({}, [BLOCK("kept")], "missing.html")).toEqual({
installed: ["kept"],
usedBlocks: [],
manifestUnreadable: false,
});
});
it("contributes nothing for a remote mount, which has no local file to match", () => {
expect(
usageOf({ "index.html": entryDoc("https://example.test/compositions/kept.html") }, [
BLOCK("kept"),
]).usedBlocks,
).toEqual([]);
});
// A degraded read must not enrol a catalog user into the no-catalog control
// group, which would bias the comparison toward "the catalog changes nothing".
it("distinguishes a corrupt manifest from a project that never used the catalog", () => {
expect(usageOfRawConfig("{ not valid json")).toEqual({
installed: [],
usedBlocks: [],
manifestUnreadable: true,
});
expect(usageOfRawConfig('{ "registry": "https://example.test" }')).toEqual({
installed: [],
usedBlocks: [],
manifestUnreadable: false,
});
expect(usageOfRawConfig(null)).toEqual({
installed: [],
usedBlocks: [],
manifestUnreadable: false,
});
});
});