Files
hyperframes/scripts/check-media-use-copy-parity.test.mjs
T
Michael Wang bc5259d2d3 fix(media-use): audio scripts judge a received .env by the CLI's rule (#8086)
* fix(cli): a project .env provider key prints a note when it is used

A provider key from a project .env is still used when no own credential
competes. Capture vision (OpenRouter, Gemini, Google and both Vertex parts),
snapshot --describe, Gemini speech and Lyria music now print one note per key
before it is first used, naming the variable and the escaped .env path, as
the ElevenLabs note does. The ignoring notes escape the path too, and the
media-use skill gets a generated copy of the CLI's terminal-text helper.

* fix(media-use): the audio scripts read a received .env by the CLI's full rule

The audio scripts judged a project .env as received only from
received-files.json at or above it. They now use a generated copy of the
CLI's rule, which also reads the host's received-state file, nested records
and the received content hashes, and treats an unreadable one as received.
A parity test runs the CLI and the copy over the same projects.

A key the CLI unset from a .env is now passed to the scripts it launches,
so they do not load it again from the same file.

* fix(media-use): match the CLI's folder walk and judge a parent .env by itself

The received-rule copy now skips folders by the CLI's exact private-path rule,
so a hidden Studio folder with an asset-like name is walked as the CLI walks
it. A .env the scripts find in a folder above the working one is judged only
by records and host state at or above that folder, so a received sibling or
a large parent folder cannot flip the person's own key. Keys the CLI unset
are matched by the folder's real path.

* fix(media-use): match the received rule's narrowed private-file check

The CLI's private-file rule now treats a hidden file as private whatever its type,
and framework build-output folders and service-account key names as private.
The media-use copy of the rule follows it, so the parity cases for hidden
asset-named Studio folders agree again.
2026-11-17 19:26:39 +00:00

119 lines
4.2 KiB
JavaScript

import assert from "node:assert/strict";
import {
existsSync,
mkdirSync,
mkdtempSync,
readdirSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { basename, dirname, join, relative, resolve } from "node:path";
import { tmpdir } from "node:os";
import { describe, it } from "node:test";
import { generateSkillModuleCopies } from "./generate-skill-module-copies.mjs";
const skillLibDir = resolve("skills/media-use/scripts/lib");
const cliLibDir = resolve("packages/cli/src/media-use/lib");
export const MEDIA_USE_COPY_NAMES = [
"config-lock.mjs",
"credit-limit.mjs",
"cutlist.mjs",
"duck.mjs",
"error-diffusion.mjs",
"index-gen.mjs",
"manifest.mjs",
"media-fetch.mjs",
"media-home.mjs",
"npx-sync.mjs",
"parakeet-words.mjs",
"prefs-store.mjs",
"received-env.mjs",
"recipe-store.mjs",
"telemetry.mjs",
"terminal-text.mjs",
"transcriptCutFade.mjs",
"words.mjs",
];
export const INTENTIONAL_MEDIA_USE_DIVERGENCES = new Map([
[
"npx-sync.mjs",
"the standalone skill stays self-contained while the CLI copy uses the shared audio helper",
],
]);
function copyPaths(name, skillDir, cliDir) {
return { skillPath: join(skillDir, name), cliPath: join(cliDir, name) };
}
export function findMediaUseCopyParityIssues({ skillDir = skillLibDir, cliDir = cliLibDir } = {}) {
const missingNames = MEDIA_USE_COPY_NAMES.filter((name) => {
const { skillPath, cliPath } = copyPaths(name, skillDir, cliDir);
return !existsSync(skillPath) || !existsSync(cliPath);
});
const missing = missingNames.map((name) => `${name}: both media-use copies must exist`);
const drifted = MEDIA_USE_COPY_NAMES.filter(
(name) => !missingNames.includes(name) && !INTENTIONAL_MEDIA_USE_DIVERGENCES.has(name),
)
.filter((name) => {
const { skillPath, cliPath } = copyPaths(name, skillDir, cliDir);
return !readFileSync(skillPath).equals(readFileSync(cliPath));
})
.map((name) => `${name}: standalone and CLI copies differ without an allowlist reason`);
return [...missing, ...drifted];
}
describe("media-use source parity", () => {
it("keeps every standalone copy equal or explicitly allowlisted", () => {
assert.deepEqual(findMediaUseCopyParityIssues(), []);
});
it("reports a missing copy without reading it as drift", () => {
const root = mkdtempSync(join(tmpdir(), "media-use-parity-"));
const skillDir = join(root, "skill");
const cliDir = join(root, "cli");
try {
const name = MEDIA_USE_COPY_NAMES[0];
mkdirSync(skillDir, { recursive: true });
mkdirSync(cliDir, { recursive: true });
writeFileSync(join(skillDir, name), "same");
const issues = findMediaUseCopyParityIssues({ skillDir, cliDir });
assert.equal(issues[0], `${name}: both media-use copies must exist`);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
});
it("keeps generated standalone modules equal to their owners", () => {
assert.deepEqual(generateSkillModuleCopies({ check: true }), []);
});
function publishedScripts(dir) {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const path = join(dir, entry.name);
if (entry.isDirectory()) {
return entry.name === "node_modules" || path === skillLibDir ? [] : publishedScripts(path);
}
return /\.mjs$/.test(entry.name) && !/\.test\.mjs$/.test(entry.name) ? [path] : [];
});
}
// The CLI build swaps the published media-use scripts/lib for the CLI's lib folder.
it("finds every scripts/lib module a published media-use script imports in the CLI lib", () => {
const skillRoot = resolve("skills/media-use");
const engineEntry = join(skillRoot, "scripts", "resolve.mjs");
const missing = publishedScripts(skillRoot)
.filter((file) => file !== engineEntry)
.flatMap((file) =>
[...readFileSync(file, "utf8").matchAll(/(?:from\s+|import\s*\(?\s*)["'](\.[^"']+)["']/g)]
.map((match) => resolve(dirname(file), match[1]))
.filter((target) => dirname(target) === skillLibDir)
.filter((target) => !existsSync(join(cliLibDir, basename(target))))
.map((target) => `${relative(skillRoot, file)} -> ${basename(target)}`),
);
assert.deepEqual(missing, []);
});