Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/packages/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ npx hyperframes media-treatment --selector '#hero' --clear
| `--capability <id>` | Inspect one family, control, effect, preset, or palette |
| `--all` | Print the exhaustive capability catalog |
| `--project <dir>` | Project directory (default: current directory) |
| `--file <path>` | Composition file (default `index.html`) |
| `--file <path>` | Composition file; omit to find the selector across mounted scenes |
| `--selector <css>` | CSS selector; every matched `<img>`, `<video>` or `<canvas>` gets the patch |
| `--selector-index <n>` | Zero-based match index to patch only one of several matches |
| `--grading <json>` | Validated colour-grading patch |
Expand All @@ -630,6 +630,11 @@ npx hyperframes media-treatment --selector '#hero' --clear
| `--dry-run` | Validate and report without writing |
| `--json` | Agent-readable JSON |

Without `--file`, the command searches `index.html` and every scene it mounts,
including scenes nested inside another scene's `<template>`. It edits the file
that contains the matching clip. If the selector matches in more than one file,
pass `--file` to choose one.

`--dry-run` accepts `--grading` without `--apply` and writes nothing. JSON includes `count`, `attribute`, the exact normalized `value` that apply would write (or `null` to remove it), and `lint`, the color-grading lint verdict and findings for the proposed HTML. When the selector matches several elements, each gets the patch merged into its own current grading, and JSON lists them under `targets` (each with its own `value`) instead of a single `value`; run `--clear` first for an identical look. Matches come from the main document or, when it has none, from the first `<template>` that has any, the same scope rule as Studio's source edits.

The command writes `data-color-grading` — the same contract Studio, preview, and
Expand Down Expand Up @@ -1011,6 +1016,10 @@ npx hyperframes normalize-audio --reference target-audio --target user-audio --j
```

`--reference` and `--target` are `<audio>` element ids, with or without `#`.
The command finds them in `index.html` and its mounted scenes, including scenes
nested inside another scene's `<template>`. An id declared in more than one file
is rejected; `--write` changes only the file that contains the target clip.

The reference stays unchanged; the command accounts for both clips' current
`data-volume` and writes the target's absolute matched gain. It measures
`data-media-start` and `data-duration` and rejects remote or out-of-project sources.
Expand Down
95 changes: 95 additions & 0 deletions packages/cli/src/commands/media-treatment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,101 @@ describe("applyMediaTreatmentToHtml", () => {
}
});

it("previews and grades a clip in a nested template scene without changing its parents", async () => {
const project = mkdtempSync(join(tmpdir(), "hf-media-nested-scene-"));
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
const root = '<div data-composition-src="compositions/outer.html"></div>';
const outer = '<template><div data-composition-src="compositions/inner.html"></div></template>';
const inner = '<template><video id="hero" src="hero.mp4"></video></template>';
mkdirSync(join(project, "compositions"));
writeFileSync(join(project, "index.html"), root);
writeFileSync(join(project, "compositions/outer.html"), outer);
writeFileSync(join(project, "compositions/inner.html"), inner);
const args = [
"--project",
project,
"--selector",
"#hero",
"--grading",
'{"preset":"warm-daylight"}',
"--json",
];
try {
await runCommand(mediaTreatmentCommand, { rawArgs: [...args, "--dry-run"] });
expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
ok: true,
file: "compositions/inner.html",
dryRun: true,
changed: true,
});
expect(readFileSync(join(project, "compositions/inner.html"), "utf8")).toBe(inner);

await runCommand(mediaTreatmentCommand, { rawArgs: [...args, "--apply"] });
expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
ok: true,
file: "compositions/inner.html",
changed: true,
});
expect(readFileSync(join(project, "compositions/inner.html"), "utf8")).toContain(
"data-color-grading=",
);
expect(readFileSync(join(project, "index.html"), "utf8")).toBe(root);
expect(readFileSync(join(project, "compositions/outer.html"), "utf8")).toBe(outer);
} finally {
log.mockRestore();
rmSync(project, { recursive: true, force: true });
}
});

it("requires --file for a selector shared by the root and a nested template scene", async () => {
const project = mkdtempSync(join(tmpdir(), "hf-media-nested-ambiguous-"));
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
const root =
'<video id="hero" src="root.mp4"></video><div data-composition-src="outer.html"></div>';
const outer = '<template><div data-composition-src="inner.html"></div></template>';
const inner = '<template><video id="hero" src="inner.mp4"></video></template>';
for (const [file, html] of Object.entries({
"index.html": root,
"outer.html": outer,
"inner.html": inner,
})) {
writeFileSync(join(project, file), html);
}
const args = [
"--project",
project,
"--selector",
"#hero",
"--grading",
'{"preset":"warm-daylight"}',
"--apply",
"--json",
];
try {
await expect(runCommand(mediaTreatmentCommand, { rawArgs: args })).rejects.toThrow(
CliRuntimeError,
);
expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
ok: false,
error: { message: expect.stringContaining("index.html, inner.html; pass --file") },
});
expect(readFileSync(join(project, "index.html"), "utf8")).toBe(root);
expect(readFileSync(join(project, "outer.html"), "utf8")).toBe(outer);
expect(readFileSync(join(project, "inner.html"), "utf8")).toBe(inner);

await runCommand(mediaTreatmentCommand, { rawArgs: [...args, "--file", "inner.html"] });
expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
ok: true,
file: "inner.html",
});
expect(readFileSync(join(project, "inner.html"), "utf8")).toContain("data-color-grading=");
expect(readFileSync(join(project, "index.html"), "utf8")).toBe(root);
} finally {
log.mockRestore();
rmSync(project, { recursive: true, force: true });
}
});

it("reports how many elements an --apply treated", async () => {
const project = mkdtempSync(join(tmpdir(), "hf-media-all-"));
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
Expand Down
82 changes: 82 additions & 0 deletions packages/cli/src/commands/normalize-audio.command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vi.mock("@hyperframes/studio-server/loudness", async (importOriginal) => ({
}));

import normalizeAudioCommand from "./normalize-audio.js";
import { CliRuntimeError } from "../utils/commandResult.js";

describe("absolute normalize-audio command", () => {
it.each([
Expand Down Expand Up @@ -127,6 +128,87 @@ describe("normalize-audio on a clip inside a mounted scene", () => {
});
});

describe("normalize-audio on nested template scenes", () => {
it("writes the gain only into the nested scene that declares the target", async () => {
const dir = mkdtempSync(join(tmpdir(), "hf-normalize-nested-test-"));
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const root = `<div data-composition-id="main"><div data-composition-src="compositions/outer.html"></div></div>`;
const outer = `<template><div data-composition-id="outer"><div data-composition-src="compositions/inner.html"></div></div></template>`;
const inner = `<template><div data-composition-id="inner"><audio id="voice" src="voice.wav" data-volume="0" data-duration="2"></audio></div></template>`;
try {
mkdirSync(join(dir, "compositions"));
writeFileSync(join(dir, "index.html"), root);
writeFileSync(join(dir, "compositions", "outer.html"), outer);
writeFileSync(join(dir, "compositions", "inner.html"), inner);
writeFileSync(join(dir, "voice.wav"), "audio bytes");
measureAudio.mockReset();
measureAudio.mockResolvedValue({ integratedLufs: -16, truePeakDbfs: -12.3 });

await runCommand(normalizeAudioCommand, {
rawArgs: [dir, "--target", "voice", "--json", "--write"],
});

expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
ok: true,
targetId: "voice",
wrote: true,
});
expect(measureAudio).toHaveBeenCalledTimes(1);
expect(readFileSync(join(dir, "index.html"), "utf8")).toBe(root);
expect(readFileSync(join(dir, "compositions", "outer.html"), "utf8")).toBe(outer);
expect(readFileSync(join(dir, "compositions", "inner.html"), "utf8")).toBe(
inner.replace('data-volume="0"', 'data-volume="1"'),
);
expect(readFileSync(join(dir, "voice.wav"), "utf8")).toBe("audio bytes");
} finally {
log.mockRestore();
measureAudio.mockReset();
rmSync(dir, { recursive: true, force: true });
}
});

it("rejects a target declared in the root and a nested scene before measuring or writing", async () => {
const dir = mkdtempSync(join(tmpdir(), "hf-normalize-nested-duplicate-test-"));
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const audio = `<audio id="voice" src="voice.wav" data-volume="0" data-duration="2"></audio>`;
const root = `<div data-composition-id="main">${audio}<div data-composition-src="compositions/outer.html"></div></div>`;
const outer = `<template><div data-composition-id="outer"><div data-composition-src="compositions/inner.html"></div></div></template>`;
const inner = `<template><div data-composition-id="inner">${audio}</div></template>`;
try {
mkdirSync(join(dir, "compositions"));
writeFileSync(join(dir, "index.html"), root);
writeFileSync(join(dir, "compositions", "outer.html"), outer);
writeFileSync(join(dir, "compositions", "inner.html"), inner);
writeFileSync(join(dir, "voice.wav"), "audio bytes");
measureAudio.mockReset();
measureAudio.mockResolvedValue({ integratedLufs: -16, truePeakDbfs: -12.3 });

await expect(
runCommand(normalizeAudioCommand, {
rawArgs: [dir, "--target", "voice", "--json", "--write"],
}),
).rejects.toBeInstanceOf(CliRuntimeError);

expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
ok: false,
error: {
code: "runtime_error",
message: "Target audio #voice is declared in index.html, compositions/inner.html",
},
});
expect(measureAudio).not.toHaveBeenCalled();
expect(readFileSync(join(dir, "index.html"), "utf8")).toBe(root);
expect(readFileSync(join(dir, "compositions", "outer.html"), "utf8")).toBe(outer);
expect(readFileSync(join(dir, "compositions", "inner.html"), "utf8")).toBe(inner);
expect(readFileSync(join(dir, "voice.wav"), "utf8")).toBe("audio bytes");
} finally {
log.mockRestore();
measureAudio.mockReset();
rmSync(dir, { recursive: true, force: true });
}
});
});

describe("absolute normalize-audio command held by a limit", () => {
it("prints the level the clip plays at, its data-volume included", async () => {
const dir = mkdtempSync(join(tmpdir(), "hf-normalize-absolute-held-test-"));
Expand Down
72 changes: 72 additions & 0 deletions packages/cli/src/utils/mountedCompositions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { projectCompositionFiles } from "./mountedCompositions.js";

describe("projectCompositionFiles", () => {
let directory: string;
let project: string;

beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), "hf-mounted-compositions-"));
project = join(directory, "project");
});

afterEach(() => rmSync(directory, { recursive: true, force: true }));

function write(file: string, html: string): void {
const path = join(project, file);
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, html);
}

it("follows template-wrapped scenes recursively without revisiting cycles or duplicate mounts", () => {
write(
"index.html",
'<div data-composition-src="compositions/outer.html"></div>' +
'<div data-composition-src="./compositions/outer.html"></div>',
);
write(
"compositions/outer.html",
'<template><div data-composition-src="compositions/inner.html"></div></template>',
);
write(
"compositions/inner.html",
'<template><div data-composition-src="index.html"></div></template>',
);
write("compositions/unmounted.html", "<template>Unused scene</template>");

expect(projectCompositionFiles(project)).toEqual([
"index.html",
"compositions/outer.html",
"compositions/inner.html",
]);
});

it("ignores mounts in comments and scripts while finding active template content", () => {
write(
"index.html",
'<template><!-- <div data-composition-src="commented.html"></div> -->' +
"<script>const example = '<div data-composition-src=\"scripted.html\"></div>';</script>" +
'<div data-composition-src="active.html"></div></template>',
);
for (const file of ["commented.html", "scripted.html", "active.html"]) write(file, "");

expect(projectCompositionFiles(project)).toEqual(["index.html", "active.html"]);
});

it("keeps nested mounts inside the project and skips missing files", () => {
write("../outside.html", "<template>Outside scene</template>");
write("index.html", '<div data-composition-src="outer.html"></div>');
write(
"outer.html",
'<template><div data-composition-src="../outside.html"></div>' +
'<div data-composition-src="missing.html"></div>' +
'<div data-composition-src="inner.html"></div></template>',
);
write("inner.html", "<template>Inner scene</template>");

expect(projectCompositionFiles(project)).toEqual(["index.html", "outer.html", "inner.html"]);
});
});
8 changes: 3 additions & 5 deletions packages/cli/src/utils/mountedCompositions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readFileSync } from "node:fs";
import { relative, resolve } from "node:path";
import { parseHTML } from "linkedom";
import { collectSubCompositionSrcs } from "@hyperframes/parsers/asset-resolution";
import { isPathInside } from "@hyperframes/core";

/** The entry file plus every sub-composition file it mounts, nested ones included, as project-relative paths. */
Expand All @@ -11,10 +11,8 @@ export function projectCompositionFiles(projectDir: string, entry = "index.html"
const path = resolve(projectDir, file);
if (!isPathInside(path, projectDir) || !existsSync(path)) return;
files.push(file);
const { document } = parseHTML(readFileSync(path, "utf8"));
for (const host of document.querySelectorAll("[data-composition-src]")) {
const src = host.getAttribute("data-composition-src");
if (src) visit(relative(projectDir, resolve(projectDir, src)).split("\\").join("/"));
for (const src of collectSubCompositionSrcs(readFileSync(path, "utf8"))) {
visit(relative(projectDir, resolve(projectDir, src)).split("\\").join("/"));
}
};
visit(entry);
Expand Down
Loading