Files
hyperframes/skills/media-use/scripts/resolve.test.mjs
T
Miguel ÁngelandClaude Opus 4.8 57b3c78987 feat(media-use): color grading — grade/lut resolve, smart-grade, grade-compare + compare (#2041)
* feat(media-use): color grading — grade/lut resolve, smart-grade, grade-compare CLI

Add color grading to media-use as first-class resolve types plus a faithful
comparison command. All local, offline, deterministic — no model, no GPU.

- resolve -t grade / -t lut: produce a data-color-grading block (or a frozen
  .cube). Look cascade: core preset (no file) -> bundled .cube library ->
  parametric buildCube. Emitted .cube is Rec.709 and validated against core's
  colorLuts constraints (LUT_3D_SIZE <= 64) before it is frozen.
- smart grade (grade --for <media>): ffmpeg signalstats -> adjust suggestion
  (exposure / contrast / white balance), surfaced with the measured evidence on
  stderr as a starting point; never auto-applied.
- hyperframes grade-compare: renders N candidate grades onto a reference frame
  through the real runtime shader into one labeled comparison PNG, so an agent
  picks a look without opening Studio. Prepends an "original" baseline cell by
  default (--no-baseline to omit). Shares the headless-capture pipeline with
  snapshot via capture/captureCompositionFrame.
- media-use SKILL: proactive "media opportunity pass" guidance (grounded
  signal -> offer, ask once, surface don't mutate).

Verified: media-use 116/116, grade-compare 7/7, snapshot 9/9, lint + format
clean, full build green, comparison renders end to end.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* test(cli): narrow grade-compare baseline assertion off unknown-typed grading

Assert the whole cell via toEqual instead of reaching into .grading.preset /
.grading.lut on the unknown-typed field, keeping the test typecheck-clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(media-use): agent-authored LUTs via --params + validate --from cube; never-read-.cube guardrail

- resolve -t lut / -t grade --params '<json>': build a parametric .cube from
  explicit params (bypassing the intent cascade), validate, and freeze in one
  step. --intent becomes the optional description. Lets an agent commit a look
  it computed itself.
- --from <file.cube> now validates the ingested LUT for lut/grade types and
  rejects an invalid/oversized cube (no partial write) — the escape hatch for a
  LUT the agent generated with its own code.
- SKILL.md: hard rule to never read a .cube body into context (~size^3 lines,
  zero legible signal) — inspect via grade-compare (see it) or cube-validate
  (ok/size), read the manifest description for meaning; plus both authoring
  paths and the parametric-vs-film-stock ceiling note.

Verified: media-use 116/116, lint + format clean; smokes — --params builds a
valid frozen cube, grade --params returns a lut block, bad JSON and an oversized
--from cube are both rejected with no stray file.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(cli): grade-compare validates referenced LUTs, warns on no-op cells, caps candidates

Bug-bash follow-ups — grade-compare silently accepted bad input:

- Validate LUT *content*, not just existence: each referenced .cube is parsed
  with core's parseCubeLut (now exported from @hyperframes/core) and rejected
  with a per-cell error ("LUT for \"<label>\" is not a valid .cube: ..."). A
  file that exists but isn't a valid cube no longer renders a silent no-op cell.
- Warn on inactive cells: a grading that normalizes to inactive (e.g. a
  malformed {lut:12345}) emits a stderr warning naming the cell; the
  auto-prepended "original" baseline is intentionally inactive and stays silent.
  stdout remains valid JSON.
- Cap candidates at 16 (excluding baseline): over-cap input renders the first N
  and reports {truncated:true, total:M} on stdout + a stderr note — no silent
  drop, no unbounded giant sheet.

Verified: grade-compare 10/10; non-cube LUT → clear error; {lut:12345} → warning
+ ok; 20 cells → cells=17 truncated total=20; valid runs unchanged. Lint/format
clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(cli): general `hyperframes compare` visual-variant primitive

Generalize grade-compare's "render N variants → one labeled sheet → the agent
looks and picks" loop into a standalone command that works on ANY variation
(font, layout, motion, grade, whole compositions) — the tool never needs to
know what differs.

- `hyperframes compare <path...> [--at <sec>] [--labels a,b,c] [--out] [--cols]
  [--json]`: renders each agent-authored composition variant through the real
  runtime (captureCompositionFrame) and stitches one labeled comparison sheet +
  JSON ({ok, sheet, rendered, variants, truncated?/total?}). 2+ paths required;
  caps at 16 with loud truncation. It presents, it does not judge — choosing is
  the caller's job.
- Factored the shared "render a labeled set → contact sheet" path so compare,
  grade-compare, and snapshot all sit on it (no duplication). grade-compare is
  now the first color-specific specialization of this primitive.
- New pathArgs util + contactSheet test; hyperframes-cli SKILL documents compare
  as the agent's "see your own renders and choose" primitive.

Verified: 26/26 across compare + grade-compare + snapshot + contactSheet (no
regressions); compare renders 3 variants into one visibly-distinct labeled
sheet; 2+-path error path clean; lint/format clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(ci): green the skills CI — skip ffmpeg tests when absent, oxfmt markdown

The "Test: skills" CI job runs bare `node --test` with no ffmpeg on PATH (by
design — skills tests are meant to be node-builtin-only). The grade-analyzer +
smart-grade tests shell to ffmpeg and were failing there with ENOENT. Guard
them to skip when ffmpeg isn't on PATH; they still run locally / where it is.

Also oxfmt README.md + hyperframes/media-use SKILL.md (the whole-repo
`oxfmt --check .` Format job caught markdown left unformatted by the rebase
conflict resolution).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(ci): skip core-conformance test when tsx is unavailable

The "Test: skills" CI job installs no deps, so the normalizeHfColorGrading
conformance test (which imports core's TS via `node --import tsx`) failed there.
Guard it to skip when tsx can't resolve; runs locally / in the deps-installed
Test job. Completes the skills-CI greening (the ffmpeg guards handled the rest).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(cli): escape grade-compare src double-quotes (CodeQL XSS) + Windows-safe compare test

- grade-compare built `<img src="...">` (double-quoted) with the single-quote
  escaper, leaving `"` unescaped — a `"` in the frame path could break out
  (CodeQL: incomplete HTML attribute sanitization). Use escapeXml for src.
- compare label test hard-coded POSIX paths that can't match on Windows; assert
  the derived labels (the subject); path resolution is covered elsewhere.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* refactor(media-use): generate LUT library from params (drop committed .cube files)

The 3 bundled .cube files were 733 lines each (2,199 total) and were themselves
buildCube output — pure repo bloat. Replace with compact per-look params in
luts/index.json, generated on resolve; add an optional `url` for future scanned
LUTs to be CDN-hosted + downloaded on demand (freezeUrl) instead of committed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(media-use): serve library LUTs from CDN on-demand (static.heygen.ai/luts), params fallback

Looks now carry a CDN `url` (hosted at s3://heygen-public/luts → static.heygen.ai/luts/<id>.cube);
resolve downloads + validates + freezes on demand, like bgm/image. `params` stays
as the deterministic offline fallback (--local-only, or if the download fails), so
resolution is never blocked on the network. Provider prefers url, falls back to params.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(media-use): address #2041 review — atomic LUT writes, compare telemetry, follow-ups

- Atomic .cube writes: library provider (url + params) and the parametric
  generator now write to a .tmp path, validate, then rename, so a crash can
  never orphan an invalid .cube at the final path (was validate-after-write).
- track("media_use_resolve") now emits provenance.via (url/params-fallback/params).
- grade-compare + compare: --timeout flag (was hardcoded 5000) and a
  media_use_compare event (cells, truncated, total, render_ready_timed_out);
  openSettledCompositionPage now surfaces the render-ready timeout.
- compare staging skips node_modules/.git; --for gets an upfront existence check.
- Rec.709 luma comment; HYPERFRAMES_ANALYZE_TIMEOUT_MS override; measured note
  uses basename; LUT s3 hosting moved from index.json into luts/README.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 22:20:16 -04:00

596 lines
18 KiB
JavaScript

import { strict as assert } from "node:assert";
import {
mkdtempSync,
rmSync,
writeFileSync,
readFileSync,
mkdirSync,
existsSync,
readdirSync,
} from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { execFileSync, spawnSync } from "node:child_process";
import { appendRecord, readManifest } from "./lib/manifest.mjs";
import { regenerateIndex } from "./lib/index-gen.mjs";
import { getProvider } from "./lib/providers.mjs";
import { freezeLocalFile } from "./lib/freeze.mjs";
import { cachePut, cacheGet, importFromCache } from "./lib/cache.mjs";
import { validateCubeFile } from "./lib/cube-validate.mjs";
const REPO_ROOT = join(import.meta.dirname, "..", "..", "..");
const RESOLVE_CLI = join(import.meta.dirname, "resolve.mjs");
// The "Test: skills" CI job has no ffmpeg on PATH (by design). The smart-grade
// test shells to ffmpeg, so it's skipped there and runs where ffmpeg exists.
const HAS_FFMPEG = spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status === 0;
// The core-conformance test imports core's TypeScript via tsx. The dependency-free
// "Test: skills" CI job has neither tsx nor installed deps, so skip it there; it
// runs wherever the workspace is installed (locally, the main Test job).
const CAN_TSX =
spawnSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", "0"], {
stdio: "ignore",
}).status === 0;
let tmp;
function setup() {
tmp = mkdtempSync(join(tmpdir(), "mu-resolve-test-"));
}
function cleanup() {
if (tmp) rmSync(tmp, { recursive: true, force: true });
}
function makeRecord(overrides = {}) {
return {
id: "bgm_001",
type: "bgm",
path: ".media/audio/bgm/bgm_001.wav",
source: "search",
description: "soft minimal ambient",
duration: 11,
provenance: { provider: "test", prompt: "test prompt" },
...overrides,
};
}
// Run resolve.mjs with argv passed as a literal array (no shell). Each token is
// a separate argv entry, so a value with spaces or shell metacharacters can't
// break out — never build a command string and hand it to a shell.
function runResolve(args, opts = {}) {
return execFileSync(process.execPath, [RESOLVE_CLI, ...args], {
cwd: REPO_ROOT,
encoding: "utf8",
...opts,
});
}
function spawnResolve(args, opts = {}) {
return spawnSync(process.execPath, [RESOLVE_CLI, ...args], {
cwd: REPO_ROOT,
encoding: "utf8",
...opts,
});
}
function makeFrame(dir, name, color) {
const out = join(dir, name);
execFileSync(
"ffmpeg",
[
"-hide_banner",
"-loglevel",
"error",
"-f",
"lavfi",
"-i",
`color=c=${color}:s=64x64`,
"-frames:v",
"1",
"-y",
out,
],
{ stdio: "pipe" },
);
return out;
}
function normalizeWithCoreSource(grading) {
const sourcePath = join(REPO_ROOT, "packages/core/src/colorGrading.ts");
const code = `
import { normalizeHfColorGrading } from ${JSON.stringify(sourcePath)};
const grading = JSON.parse(process.env.HF_GRADING_JSON);
const normalized = normalizeHfColorGrading(grading);
if (!normalized) process.exit(2);
console.log(JSON.stringify({
preset: normalized.preset,
intensity: normalized.intensity,
adjust: normalized.adjust,
lut: normalized.lut,
colorSpace: normalized.colorSpace
}));
`;
return JSON.parse(
execFileSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", code], {
cwd: REPO_ROOT,
encoding: "utf8",
env: { ...process.env, HF_GRADING_JSON: JSON.stringify(grading) },
}),
);
}
const tests = [];
function test(name, fn) {
tests.push({ name, fn });
}
// --- manifest cache hit ---
test("project manifest hit skips providers", () => {
setup();
const record = makeRecord({ provenance: { prompt: "cached query", provider: "test" } });
appendRecord(tmp, record);
const filePath = join(tmp, record.path);
mkdirSync(join(filePath, ".."), { recursive: true });
writeFileSync(filePath, "cached audio");
const out = runResolve(["--type", "bgm", "--intent", "cached query", "--project", tmp, "--json"]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.id, "bgm_001");
assert.equal(parsed._source, "cached");
cleanup();
});
test("entity hit matches across icon/image (figma-imported brand marks)", () => {
setup();
const record = makeRecord({
id: "image_001",
type: "image",
path: ".media/images/image_001.svg",
description: "Acme logo",
entity: "Acme logo",
provenance: { source: "figma", fileKey: "KEY", nodeId: "1:2", version: "1", format: "svg" },
});
delete record.duration;
appendRecord(tmp, record);
const filePath = join(tmp, record.path);
mkdirSync(join(filePath, ".."), { recursive: true });
writeFileSync(filePath, "<svg/>");
const out = runResolve([
"--type",
"icon",
"--intent",
"acme brand mark",
"--entity",
"Acme logo",
"--project",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.id, "image_001");
assert.equal(parsed._source, "cached");
cleanup();
});
// --- global cache hit ---
test("global cache hit copies to project and registers", () => {
setup();
const sourceFile = join(tmp, "source.wav");
writeFileSync(sourceFile, "cached globally for resolve");
const record = makeRecord({ provenance: { prompt: "global resolve test" } });
cachePut(sourceFile, record);
const cached = cacheGet("global resolve test", "bgm");
assert.ok(cached);
const projectDir = mkdtempSync(join(tmpdir(), "mu-resolve-proj-"));
const imported = importFromCache(cached, projectDir, "bgm_001", ".media/audio/bgm/bgm_001.wav");
assert.ok(imported);
assert.ok(existsSync(join(projectDir, ".media/audio/bgm/bgm_001.wav")));
appendRecord(projectDir, imported);
regenerateIndex(projectDir);
const manifest = readManifest(projectDir);
assert.equal(manifest.length, 1);
assert.equal(manifest[0].provenance.imported_from, cached.sha);
rmSync(projectDir, { recursive: true, force: true });
cleanup();
});
// --- provider interface ---
test("getProvider returns provider with type", () => {
const p = getProvider("bgm");
assert.equal(p.type, "bgm");
assert.ok(typeof p.search === "function");
});
test("getProvider throws for unknown type", () => {
assert.throws(() => getProvider("unknown_type"), /unknown media type/);
});
// --- freeze ---
test("freezeLocalFile creates parent dirs and copies", () => {
setup();
const src = join(tmp, "src.bin");
writeFileSync(src, "freeze test data");
const dest = join(tmp, "deep/nested/dir/file.bin");
freezeLocalFile(src, dest);
assert.ok(existsSync(dest));
assert.equal(readFileSync(dest, "utf8"), "freeze test data");
cleanup();
});
// --- adopt existing assets ---
test("--adopt registers existing assets/ files", () => {
setup();
mkdirSync(join(tmp, "assets/bgm"), { recursive: true });
mkdirSync(join(tmp, "assets/icons"), { recursive: true });
writeFileSync(join(tmp, "assets/bgm/track.mp3"), "fake mp3");
writeFileSync(join(tmp, "assets/icons/logo.svg"), "fake svg");
const out = runResolve(["--adopt", "--project", tmp, "--json"]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.adopted, 2);
assert.ok(parsed.assets.some((a) => a.path === "assets/bgm/track.mp3"));
assert.ok(parsed.assets.some((a) => a.path === "assets/icons/logo.svg"));
const manifest = readManifest(tmp);
assert.equal(manifest.length, 2);
cleanup();
});
test("--adopt skips already-registered assets", () => {
setup();
mkdirSync(join(tmp, "assets/bgm"), { recursive: true });
writeFileSync(join(tmp, "assets/bgm/track.mp3"), "fake mp3");
runResolve(["--adopt", "--project", tmp, "--json"]);
const out = runResolve(["--adopt", "--project", tmp, "--json"]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.adopted, 0);
const manifest = readManifest(tmp);
assert.equal(manifest.length, 1);
cleanup();
});
test("resolve finds existing unregistered asset before hitting providers", () => {
setup();
mkdirSync(join(tmp, "assets/bgm"), { recursive: true });
writeFileSync(join(tmp, "assets/bgm/ambient-track.mp3"), "existing bgm");
const out = runResolve([
"--type",
"bgm",
"--intent",
"ambient track",
"--project",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.path, "assets/bgm/ambient-track.mp3");
assert.equal(parsed._source, "existing");
cleanup();
});
// --- CLI interface ---
test("--help exits 0", () => {
const out = runResolve(["--help"]);
assert.ok(out.includes("media-use resolve"));
assert.ok(out.includes("--type"));
assert.ok(out.includes("--for"));
assert.ok(out.includes("--from"));
assert.ok(out.includes("--local-only"));
});
test("unknown type error lists grade and lut", () => {
try {
runResolve(["--type", "bogus", "--intent", "x"], { stdio: "pipe" });
assert.fail("should have exited");
} catch (err) {
assert.equal(err.status, 2);
assert.match(String(err.stderr), /known: .*grade.*lut/);
}
});
test("missing required args exits 2", () => {
try {
runResolve([], { stdio: "pipe" });
assert.fail("should have exited");
} catch (err) {
assert.equal(err.status, 2);
}
});
test("--json returns error JSON on stub provider failure", () => {
setup();
try {
runResolve(["--type", "bgm", "--intent", "stub fail", "--project", tmp, "--json"], {
stdio: "pipe",
});
assert.fail("should have exited");
} catch (err) {
const output = err.stdout || "";
const parsed = JSON.parse(output.trim());
assert.equal(parsed.ok, false);
assert.ok(parsed.error.includes("no provider"));
}
cleanup();
});
test("one-line output format matches contract", () => {
setup();
const record = makeRecord({ provenance: { prompt: "format test", provider: "test" } });
appendRecord(tmp, record);
const filePath = join(tmp, record.path);
mkdirSync(join(filePath, ".."), { recursive: true });
writeFileSync(filePath, "format check");
const out = runResolve(["--type", "bgm", "--intent", "format test", "--project", tmp]);
assert.match(out.trim(), /^resolved bgm_001 → .media\/audio\/bgm\/bgm_001\.wav \(bgm/);
cleanup();
});
// --- color grading ---
test("grade resolves a preset-only look with no cube file", () => {
setup();
const out = runResolve([
"--type",
"grade",
"--intent",
"warm daylight",
"--project",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.type, "grade");
assert.equal(parsed.grading.preset, "warm-daylight");
assert.equal(parsed.grading.lut, undefined);
assert.equal(parsed.path, undefined);
assert.equal(readManifest(tmp).length, 1);
cleanup();
});
test("grade resolves a library LUT look and freezes a validated cube", () => {
setup();
const out = runResolve([
"--type",
"grade",
"--intent",
"teal orange blockbuster",
"--project",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.match(parsed.grading.lut.src, /^\.media\/luts\/grade_001\.cube$/);
assert.equal(parsed.path, parsed.grading.lut.src);
assert.ok(existsSync(join(tmp, parsed.grading.lut.src)));
assert.equal(validateCubeFile(join(tmp, parsed.grading.lut.src)).ok, true);
cleanup();
});
test("smart grade merges measured adjust and keeps stdout valid JSON", () => {
if (!HAS_FFMPEG) {
console.log(" (skipped: ffmpeg not on PATH)");
return;
}
setup();
const frame = makeFrame(tmp, "under.png", "0x202020");
const proc = spawnResolve([
"--type",
"grade",
"--intent",
"warm cinematic",
"--for",
frame,
"--project",
tmp,
"--json",
]);
assert.equal(proc.status, 0, proc.stderr);
const parsed = JSON.parse(proc.stdout);
assert.equal(parsed.ok, true);
assert.ok(parsed.grading.adjust.exposure > 0, "under-exposed frame should suggest lift");
assert.match(proc.stderr, /media-use: measured/);
cleanup();
});
test("emitted grading block survives the core normalizeHfColorGrading contract", () => {
if (!CAN_TSX) {
console.log(" (skipped: tsx / core source unavailable)");
return;
}
setup();
const out = runResolve([
"--type",
"grade",
"--intent",
"teal orange blockbuster",
"--project",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
const normalized = normalizeWithCoreSource(parsed.grading);
assert.equal(normalized.lut.src, parsed.grading.lut.src);
assert.equal(normalized.lut.intensity, parsed.grading.lut.intensity);
assert.equal(normalized.colorSpace, "rec709");
cleanup();
});
test("lut resolves only the frozen cube path", () => {
setup();
const out = runResolve([
"--type",
"lut",
"--intent",
"teal orange blockbuster",
"--project",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.type, "lut");
assert.match(parsed.path, /^\.media\/luts\/lut_001\.cube$/);
assert.equal(parsed.grading, undefined);
assert.equal(validateCubeFile(join(tmp, parsed.path)).ok, true);
cleanup();
});
test("lut --params builds, validates, and freezes a cube", () => {
setup();
const params = { contrast: 0.2, temperature: -0.3 };
const out = runResolve(["-t", "lut", "--params", JSON.stringify(params), "-p", tmp, "--json"]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.type, "lut");
assert.match(parsed.path, /^\.media\/luts\/lut_001\.cube$/);
assert.equal(parsed.description, "custom parametric lut");
assert.equal(parsed.provenance.provider, "cube_lut.builder");
assert.deepEqual(parsed.provenance.params, params);
assert.ok(existsSync(join(tmp, parsed.path)));
assert.equal(validateCubeFile(join(tmp, parsed.path)).ok, true);
cleanup();
});
test("grade --params returns a grading block with a frozen valid cube", () => {
setup();
const out = runResolve([
"-t",
"grade",
"--params",
JSON.stringify({ exposure: 0.2 }),
"-p",
tmp,
"--json",
]);
const parsed = JSON.parse(out.trim());
assert.equal(parsed.ok, true);
assert.equal(parsed.type, "grade");
assert.equal(parsed.grading.intensity, 1);
assert.match(parsed.grading.lut.src, /^\.media\/luts\/grade_001\.cube$/);
assert.equal(parsed.lut.src, parsed.grading.lut.src);
assert.equal(parsed.path, parsed.grading.lut.src);
assert.equal(validateCubeFile(join(tmp, parsed.grading.lut.src)).ok, true);
cleanup();
});
test("--params malformed JSON errors cleanly without freezing a cube", () => {
setup();
const proc = spawnResolve(["-t", "lut", "--params", "{not json", "-p", tmp, "--json"]);
assert.equal(proc.status, 1, proc.stderr);
const parsed = JSON.parse(proc.stdout);
assert.equal(parsed.ok, false);
assert.match(parsed.error, /^invalid --params JSON:/);
assert.equal(readManifest(tmp).length, 0);
assert.equal(existsSync(join(tmp, ".media/luts")), false);
cleanup();
});
// buildCube clamps every accepted parameter and resolve.mjs does not expose
// the size argument, so there is no CLI input that can make --params emit a
// structurally invalid cube. Invalid cube cleanup is covered through --from.
test("--from rejects invalid lut cube without registering or leaving a frozen file", () => {
setup();
const broken = join(tmp, "broken.cube");
writeFileSync(broken, "LUT_3D_SIZE 999\n");
const proc = spawnResolve(["--from", broken, "-t", "lut", "-p", tmp, "--json"]);
assert.equal(proc.status, 1, proc.stderr);
const parsed = JSON.parse(proc.stdout);
assert.equal(parsed.ok, false);
assert.match(parsed.error, /^ingested LUT is invalid: LUT_3D_SIZE 999 exceeds max 64/);
assert.equal(readManifest(tmp).length, 0);
const lutDir = join(tmp, ".media/luts");
assert.deepEqual(existsSync(lutDir) ? readdirSync(lutDir) : [], []);
cleanup();
});
test("grade miss exits explicitly with no partial file", () => {
setup();
const missIntent = `zqxv imaginary neutron ${process.pid}`;
try {
runResolve(["--type", "grade", "--intent", missIntent, "--project", tmp, "--json"]);
assert.fail("should have exited");
} catch (err) {
assert.equal(err.status, 1);
const parsed = JSON.parse(String(err.stdout));
assert.equal(parsed.ok, false);
assert.match(parsed.error, /no local color grade could resolve/);
assert.equal(readManifest(tmp).length, 0);
assert.equal(existsSync(join(tmp, ".media/luts")), false);
}
cleanup();
});
test("identical grade resolve hits the project cache without re-freezing", () => {
setup();
const first = JSON.parse(
runResolve([
"--type",
"grade",
"--intent",
"teal orange blockbuster",
"--project",
tmp,
"--json",
]),
);
const second = JSON.parse(
runResolve([
"--type",
"grade",
"--intent",
"teal orange blockbuster",
"--project",
tmp,
"--json",
]),
);
assert.equal(second._source, "cached");
assert.equal(second.id, first.id);
assert.equal(second.path, first.path);
assert.equal(readManifest(tmp).length, 1);
cleanup();
});
// --- run ---
async function main() {
console.log("media-use · resolve engine tests\n");
let passed = 0;
let failed = 0;
for (const { name, fn } of tests) {
try {
await fn();
passed++;
console.log(` \x1b[32m✓\x1b[0m ${name}`);
} catch (err) {
failed++;
console.log(` \x1b[31m✗\x1b[0m ${name}`);
console.log(` ${err.message}`);
}
}
console.log(`\n${passed} passed, ${failed} failed`);
if (failed > 0) process.exit(1);
}
main();