fix(media-use): recipe, prefs and SFX lines show control characters as escapes (#8202)

recipe.mjs and prefs.mjs printed recipe.json and preferences.json values
raw: names, workflows, approval dates, brief values, confirmed-in project
names, and the known-recipe list in an unknown-name error. Both files
travel with a project, so an escape sequence in them could rewrite or
hide earlier output, set the window title or write the clipboard in
terminals that allow it. The SFX provider's empty-search line repeats the
intent, and its Openverse fallback line repeats a network error.

Each of those lines now goes through terminalText, like the resolve and
adopt lines beside them. --json output is unchanged.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WaterrrForever
2026-11-19 05:10:03 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent e5283f63b9
commit 36b60d18b4
6 changed files with 192 additions and 16 deletions
@@ -1,5 +1,6 @@
import { heygenSearch } from "./heygen-search.mjs";
import { OpenverseUnavailableError, searchOpenverseSfx } from "./openverse.mjs";
import { terminalText } from "./terminal-text.mjs";
export const OPENVERSE_SFX_PROVIDER = "openverse.audio";
@@ -24,7 +25,9 @@ export const heygenSfxProvider = {
minScore: SFX_MIN_SCORE,
onEmpty: () =>
console.error(
`media-use: HeyGen sound search matched nothing for "${intent}" at min score ${SFX_MIN_SCORE}; trying the next source`,
terminalText(
`media-use: HeyGen sound search matched nothing for "${intent}" at min score ${SFX_MIN_SCORE}; trying the next source`,
),
),
});
if (!results) return null;
@@ -50,7 +53,7 @@ export const sfxProvider = {
sound = await searchOpenverseSfx(intent);
} catch (error) {
if (!(error instanceof OpenverseUnavailableError)) throw error;
console.error(`media-use: ${error.message}; using the bundled sound effects`);
console.error(terminalText(`media-use: ${error.message}; using the bundled sound effects`));
return null;
}
if (!sound) return null;
@@ -0,0 +1,52 @@
import { strict as assert } from "node:assert";
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { delimiter, join } from "node:path";
import { test } from "node:test";
import { heygenSfxProvider, sfxProvider } from "./sfx-provider.mjs";
const HOSTILE = "\x1b]52;c;aGk=\x07\x1b[2J";
const SHOWN = "\\x1b]52;c;aGk=\\x07\\x1b[2J";
function captureStderr(t) {
const lines = [];
t.mock.method(console, "error", (...args) => lines.push(args.join(" ")));
return lines;
}
function assertEscaped(lines) {
const out = lines.join("\n");
assert.ok(out.includes(SHOWN), out);
for (const raw of ["\x1b", "\x07"]) assert.ok(!out.includes(raw), JSON.stringify(out));
}
test(
"an empty HeyGen sound search repeats the intent with control characters as visible escapes",
{
skip: process.platform === "win32" && "the fake heygen binary is a shell script",
},
async (t) => {
const bin = mkdtempSync(join(tmpdir(), "sfx-provider-heygen-"));
const savedPath = process.env.PATH;
try {
writeFileSync(join(bin, "heygen"), `#!/bin/sh\necho '{"data":[]}'\n`);
chmodSync(join(bin, "heygen"), 0o755);
process.env.PATH = `${bin}${delimiter}${savedPath}`;
const lines = captureStderr(t);
assert.equal(await heygenSfxProvider.search(`whoosh${HOSTILE}`), null);
assertEscaped(lines);
} finally {
process.env.PATH = savedPath;
rmSync(bin, { recursive: true, force: true });
}
},
);
test("an unreachable Openverse error prints with control characters as visible escapes", async (t) => {
t.mock.method(globalThis, "fetch", async () => {
throw new Error(`proxy said ${HOSTILE}`);
});
const lines = captureStderr(t);
assert.equal(await sfxProvider.search("whoosh"), null);
assertEscaped(lines);
});