test(cli): count print calls whose value is not plainly the CLI's own (#8608)

* test(cli): count print calls whose value is not plainly the CLI's own

Each CLI source file's count of such print calls is kept in a baseline,
so a new one shows up in the change that adds it. Print calls whose
output another program reads are named in the test.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(cli): tighten the print count rule

A name the file declares itself is no longer taken for the colour helper
or a converter, a local form of a value counts unless the value is a
number, JSON text inside a line for people counts, and spinner lines and
clack's framing calls are counted too. A file listed with more calls than
it has now gets a message that names the list.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(cli): name the lines other programs parse

The count rule now also sees writes to a bare `stdout` or `stderr`, and
the `@hf-*` lines and the `--json` failure body are listed as output for
another program instead of sitting in the baseline.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-23 19:45:31 +00:00
committed by GitHub
co-authored by Claude Fable 5.2
parent 590617475f
commit c03897fc11
4 changed files with 408 additions and 2 deletions
+4
View File
@@ -53,6 +53,10 @@ bun run format:check # Check formatting without writing
Git hooks (via [lefthook](https://github.com/evilmartians/lefthook)) run automatically after `bun install` and enforce linting + formatting on staged files before each commit.
#### Printing a value in the CLI
When a CLI line for people repeats a value the CLI did not write (a name from a project file, a path, another program's output), pass it through `terminalText` or `terminalLines` from `@hyperframes/core`, so a control character in it shows as visible text. A test counts, per file, the print calls whose value is not plainly the CLI's own text (`packages/cli/src/printedFailureText.wiring.test.ts`). If it fails on your change, it says which file and what to do: wrap the value, or, when the CLI wrote the value itself, change that file's number in `packages/cli/src/printedValues.baseline.json` in the same PR. Output meant for another program (`--json`, a path printed as a command's whole answer) is not wrapped; such a print call is named in the list at the top of that test.
#### Type-safety conventions
We aim for honest types — code that lies to the compiler eventually lies to users. The underlying convention is:
+3
View File
@@ -69,6 +69,9 @@ out-of-date skills notices, the path in the FFmpeg install hint, the
`data-fps` warning of a cloud render, the font summary of `capture`, the notice
that a key comes from a folder's `.env`, and the plain progress lines of a run
with no terminal attached.
Output a command writes for another program to read is left exactly as it is:
`--json` output, the path `browser path` answers with, and the file contents
`history peek` writes.
The same lines show the Unicode marks that switch text direction
(U+202A to U+202E and U+2066 to U+2069) and the line and paragraph separators
(U+2028 and U+2029) as visible text such as `\u202e`. Right-to-left letters, the
@@ -1,4 +1,4 @@
import { readdirSync, readFileSync } from "node:fs";
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";
import ts from "typescript";
@@ -36,13 +36,16 @@ const calleeName = (call: ts.CallExpression) =>
? call.expression.name.text
: call.expression.getText();
/** The process's own streams, also under the bare names a file takes them out of `process` by. */
const PROCESS_STREAM = /^(process\.)?(stdout|stderr)$/;
function printsToTerminal(call: ts.CallExpression): boolean {
if (!ts.isPropertyAccessExpression(call.expression)) return false;
const target = call.expression.expression.getText();
const method = call.expression.name.text;
if (target === "console") return CONSOLE_PRINTS.has(method);
if (target === "clack.log") return true;
return method === "write" && (target === "process.stderr" || target === "process.stdout");
return method === "write" && PROCESS_STREAM.test(target);
}
const shownAsWritten = (node: ts.Node) =>
@@ -244,3 +247,309 @@ it("finds a value printed as it came in a file that runs before a command", () =
);
expect(found).toEqual(["sample.ts:1", "sample.ts:5", "sample.ts:7"]);
});
// The CLI's print calls, counted per file: `console` prints, `clack.log` lines, `clack.intro`,
// `outro`, `note` and `cancel`, the lines of a spinner (a name that says so: `s`, `spin`, `spinner`,
// `…Spinner`), and writes to the process's own stdout and stderr (as `process.stdout` or a bare
// `stdout`). A print made any other way is not seen here.
//
// A print call is "plain" when all it prints is text the CLI wrote, by shape: literals, counts,
// numbers and their formatting, constants set from those, colour helpers around those, and anything
// passed through a text helper. JSON text is plain as a print's whole argument, and counted when it
// sits inside a line for people, since it leaves some marks as they are. A name the file declares
// itself (a parameter, a loop variable) is never taken for the colour helper or a converter. Every
// other print call is counted, and the counts are kept in
// `printedValues.baseline.json` so a count only moves in a reviewed change.
//
// To write the baseline again after a deliberate change, from packages/cli:
// UPDATE_PRINTED_VALUES_BASELINE=1 npx vitest run src/printedFailureText.wiring.test.ts
const BASELINE = join(SRC, "printedValues.baseline.json");
/** Print calls whose output another program reads, so it stays byte for byte as it is. */
const PRINTS_FOR_ANOTHER_PROGRAM: Record<string, { calls: number; why: string }> = {
"browser/installRun.ts": { calls: 3, why: "progress lines the parent process parses" },
"commands/browser.ts": { calls: 2, why: "`browser path` answers with the path alone" },
"commands/history.ts": { calls: 1, why: "`history peek` writes the stored file's own bytes" },
"commands/render.ts": { calls: 1, why: "passes on the container's stderr as it arrives" },
"fontLocalizeCli.ts": { calls: 2, why: "a pipe tool: what it was handed goes back out" },
"renderSetupWorker.ts": { calls: 2, why: "the result and failure lines its parent parses" },
"ui/progress.ts": { calls: 1, why: "the `@hf-progress` line a parent process parses" },
"ui/renderDiagnostic.ts": { calls: 1, why: "the `@hf-diagnostic` line a parent process parses" },
"ui/renderResult.ts": { calls: 1, why: "the `@hf-result` line a parent process parses" },
"utils/cancellableProcess.ts": { calls: 2, why: "passes on its own worker's stderr" },
"utils/jsonError.ts": { calls: 1, why: "the `--json` failure body, one JSON line" },
"whisper/sherpaWorker.ts": { calls: 3, why: "prefixed lines `whisper/sherpa.ts` parses" },
};
const KEEPS_TEXT_PLAIN = new Set([
"join",
"toLocaleString",
"padEnd",
"padStart",
"repeat",
"toLowerCase",
"toUpperCase",
"trim",
"trimEnd",
]);
const GIVES_A_NUMBER_AS_TEXT = new Set(["toFixed", "toPrecision"]);
const GIVES_JSON_TEXT = new Set(["stringify", "formatJson"]);
const PRINTS_A_CLACK_LINE = new Set(["intro", "outro", "note", "cancel"]);
const PRINTS_A_SPINNER_LINE = new Set(["start", "stop", "message"]);
const NAMES_A_SPINNER = /^(s|spin|spinner|\w+Spinner)$/;
const CONVERTS = new Set(["Boolean", "Number", "String", "parseFloat", "parseInt"]);
const GIVES_A_NUMBER_OR_A_TRUTH = new Set([
ts.SyntaxKind.MinusToken,
ts.SyntaxKind.AsteriskToken,
ts.SyntaxKind.SlashToken,
ts.SyntaxKind.PercentToken,
ts.SyntaxKind.EqualsEqualsEqualsToken,
ts.SyntaxKind.ExclamationEqualsEqualsToken,
ts.SyntaxKind.GreaterThanToken,
ts.SyntaxKind.LessThanToken,
]);
type Constants = Map<string, ts.Expression[] | null>;
const constInitializer = (node: ts.VariableDeclaration) =>
(node.parent.flags & ts.NodeFlags.Const) !== 0 ? node.initializer : undefined;
/** Each name's initializers when every declaration of it in the file is a `const`; else null. */
function constantsOf(file: ts.SourceFile): Constants {
const found: Constants = new Map();
const declare = (name: ts.Node, initializer: ts.Expression | undefined) => {
if (!ts.isIdentifier(name)) return;
const before = found.get(name.text);
found.set(name.text, initializer && before !== null ? [...(before ?? []), initializer] : null);
};
const visit = (node: ts.Node): void => {
if (ts.isParameter(node) || ts.isBindingElement(node)) declare(node.name, undefined);
if (ts.isVariableDeclaration(node)) declare(node.name, constInitializer(node));
ts.forEachChild(node, visit);
};
visit(file);
return found;
}
/** The colour helper or `Math`, and not a name the file declares itself (a loop variable `c`). */
const namesAHelper = (target: ts.Expression, constants: Constants) =>
ts.isIdentifier(target) &&
(target.text === "c" || target.text === "Math") &&
!constants.has(target.text);
function methodCallIsNotPlain(
target: ts.Expression,
name: string,
constants: Constants,
seen: Set<string>,
): boolean {
if (GIVES_A_NUMBER_AS_TEXT.has(name)) return false;
return !KEEPS_TEXT_PLAIN.has(name) || isNotPlain(target, constants, seen);
}
function callIsNotPlain(call: ts.CallExpression, constants: Constants, seen: Set<string>): boolean {
const anyArgument = () => call.arguments.some((each) => isNotPlain(each, constants, seen));
const name = calleeName(call);
if (!ts.isPropertyAccessExpression(call.expression))
return !CONVERTS.has(name) || constants.has(name) || anyArgument();
const target = call.expression.expression;
if (namesAHelper(target, constants)) return anyArgument();
return methodCallIsNotPlain(target, name, constants, seen) || anyArgument();
}
const insideALineForPeople = (node: ts.Node) =>
ts.isTemplateSpan(node.parent) ||
(ts.isBinaryExpression(node.parent) &&
node.parent.operatorToken.kind === ts.SyntaxKind.PlusToken);
const jsonInALineForPeople = (node: ts.Node) =>
ts.isCallExpression(node) && GIVES_JSON_TEXT.has(calleeName(node)) && insideALineForPeople(node);
function printsForPeople(call: ts.CallExpression): boolean {
if (printsToTerminal(call)) return true;
if (!ts.isPropertyAccessExpression(call.expression)) return false;
const target = call.expression.expression.getText();
const method = call.expression.name.text;
if (target === "clack") return PRINTS_A_CLACK_LINE.has(method);
return PRINTS_A_SPINNER_LINE.has(method) && NAMES_A_SPINNER.test(target);
}
const plainByShape = (node: ts.Node) =>
(shownAsWritten(node) && !jsonInALineForPeople(node)) ||
handsOverAHelper(node) ||
countOf(node) ||
(ts.isBinaryExpression(node) && GIVES_A_NUMBER_OR_A_TRUTH.has(node.operatorToken.kind));
function nameIsNotPlain(name: string, constants: Constants, seen: Set<string>): boolean {
if (name === "undefined") return false;
const initializers = constants.get(name);
if (!initializers || seen.has(name)) return true;
const within = new Set(seen).add(name);
return initializers.some((each) => isNotPlain(each, constants, within));
}
function isNotPlain(node: ts.Node, constants: Constants, seen = new Set<string>()): boolean {
if (plainByShape(node)) return false;
if (ts.isCallExpression(node)) return callIsNotPlain(node, constants, seen);
if (ts.isConditionalExpression(node))
return (
isNotPlain(node.whenTrue, constants, seen) || isNotPlain(node.whenFalse, constants, seen)
);
if (ts.isPropertyAccessExpression(node)) return true;
if (ts.isIdentifier(node)) return nameIsNotPlain(node.text, constants, seen);
return !!ts.forEachChild(node, (child) => isNotPlain(child, constants, seen) || undefined);
}
function countedPrints(name: string, text: string): number {
const file = ts.createSourceFile(name, text, ts.ScriptTarget.Latest, true);
const constants = constantsOf(file);
let count = 0;
const visit = (node: ts.Node): void => {
if (
ts.isCallExpression(node) &&
printsForPeople(node) &&
node.arguments.some((argument) => isNotPlain(argument, constants))
)
count += 1;
ts.forEachChild(node, visit);
};
visit(file);
return count;
}
type Counts = Record<string, number>;
/** What to tell the person whose change moved a count, one line per file. */
function movedCounts(counted: Counts, baseline: Counts, forAnotherProgram: Counts): string[] {
const files = [
...new Set([
...Object.keys(counted),
...Object.keys(baseline),
...Object.keys(forAnotherProgram),
]),
].sort();
return files.flatMap((file) => {
const listed = forAnotherProgram[file] ?? 0;
const all = counted[file] ?? 0;
if (all < listed)
return [
`${file}: ${all} print call(s) counted, fewer than the ${listed} listed for it in ` +
`PRINTS_FOR_ANOTHER_PROGRAM. Lower \`calls\` there to ${all}, or drop the entry at 0, in the same change.`,
];
const now = all - listed;
const kept = baseline[file] ?? 0;
if (now > kept)
return [
`${file}: ${now - kept} more print call(s) with a value that is not plainly the CLI's own text. ` +
"Pass the value through terminalText or terminalLines. If the CLI wrote the value itself, " +
`raise this file's number to ${now} in printedValues.baseline.json in the same change.`,
];
if (now < kept)
return [
`${file}: ${kept - now} fewer such print call(s) than recorded. ` +
`Lower this file's number to ${now} in printedValues.baseline.json in the same change.`,
];
return [];
});
}
function countsOfEveryFile(): Counts {
const counts: Counts = {};
for (const path of sources(SRC)) {
const name = relative(SRC, path).split(sep).join("/");
const count = countedPrints(name, readFileSync(path, "utf8"));
if (count > 0) counts[name] = count;
}
return counts;
}
const callsOf = (list: typeof PRINTS_FOR_ANOTHER_PROGRAM): Counts =>
Object.fromEntries(Object.entries(list).map(([file, { calls }]) => [file, calls]));
it("keeps each file's count of print calls with a value that is not plainly the CLI's own", () => {
const counted = countsOfEveryFile();
const forAnotherProgram = callsOf(PRINTS_FOR_ANOTHER_PROGRAM);
if (process.env.UPDATE_PRINTED_VALUES_BASELINE) {
const kept = Object.entries(counted)
.map(([file, count]) => [file, count - (forAnotherProgram[file] ?? 0)] as const)
.filter(([, count]) => count > 0)
.sort(([a], [b]) => a.localeCompare(b));
writeFileSync(BASELINE, `${JSON.stringify(Object.fromEntries(kept), null, 2)}\n`);
}
const baseline = JSON.parse(readFileSync(BASELINE, "utf8")) as Counts;
expect(movedCounts(counted, baseline, forAnotherProgram)).toEqual([]);
});
it("counts a print call by what it prints", () => {
const count = (line: string) => countedPrints("sample.ts", `const total = 3;\n${line}`);
expect(
count("console.log(`${total} of ${files.length} done in ${(ms / 1000).toFixed(1)}s`);"),
).toBe(0);
expect(count("console.log(c.dim(`Saved ${terminalText(path)}`));")).toBe(0);
expect(count("console.log(c.dim(`Saved ${path}`));")).toBe(1);
expect(count("console.log(row.name);")).toBe(1);
expect(count("console.log(describe(total));")).toBe(1);
expect(count("process.stdout.write(bytes);")).toBe(1);
expect(count("stderr.write(bytes);")).toBe(1);
expect(count("child.stdout.write(bytes);")).toBe(0);
expect(count("function show(total: string) { console.log(total); }")).toBe(1);
});
it("does not take a name the file declares for the colour helper or a converter", () => {
const count = (text: string) => countedPrints("sample.ts", text);
expect(count("console.log(c.dim(rows.length));")).toBe(0);
expect(count("for (const c of rows) console.log(c.trim());")).toBe(1);
expect(count("rows.forEach((c) => console.log(c.toString()));")).toBe(1);
expect(count("function show(Math: Texts) { console.log(Math.pick(1)); }")).toBe(1);
expect(count("console.log(String(rows.length));")).toBe(0);
expect(count("function show(String: Maker) { console.log(String(3)); }")).toBe(1);
});
it("counts a local form of a value that is not a number", () => {
const count = (line: string) => countedPrints("sample.ts", `const total = 3;\n${line}`);
expect(count('console.log(total.toLocaleString("en-US"));')).toBe(0);
expect(count("console.log((ms / 1000).toLocaleString());")).toBe(0);
expect(count("console.log(name.toLocaleString());")).toBe(1);
expect(count("console.log(rows.toLocaleString());")).toBe(1);
});
it("counts JSON text inside a line for people, and not as a print's whole argument", () => {
const count = (line: string) => countedPrints("sample.ts", line);
expect(count("console.log(JSON.stringify({ name }));")).toBe(0);
expect(count("console.log(`Name: ${JSON.stringify(name)}`);")).toBe(1);
expect(count('console.log("Name: " + JSON.stringify(name));')).toBe(1);
});
it("counts the lines of a spinner and of clack's framing calls", () => {
const count = (line: string) => countedPrints("sample.ts", line);
expect(count("spinner.stop(`Saved ${path}`);")).toBe(1);
expect(count("publishSpinner.message(`Sending ${name}`);")).toBe(1);
expect(count("s.start(`Reading ${terminalText(path)}`);")).toBe(0);
expect(count("clack.outro(`Done: ${url}`);")).toBe(1);
expect(count('clack.outro("Done");')).toBe(0);
expect(count("timer.stop(label);")).toBe(0);
});
it("says what to do when a file's count moves, and counts output meant for another program", () => {
const moved = (counted: Counts, forAnotherProgram: Counts = {}) =>
movedCounts(counted, { "a.ts": 2 }, forAnotherProgram);
expect(moved({ "a.ts": 2 })).toEqual([]);
expect(moved({ "a.ts": 3 })).toEqual([
expect.stringMatching(/^a\.ts: 1 more .*terminalText.*raise this file's number to 3 /),
]);
expect(moved({ "a.ts": 1 })).toEqual([
expect.stringMatching(/^a\.ts: 1 fewer .*Lower this file's number to 1 /),
]);
expect(moved({})).toEqual([expect.stringMatching(/^a\.ts: 2 fewer /)]);
expect(moved({ "a.ts": 2, "tool.ts": 1 })).toEqual([expect.stringMatching(/^tool\.ts: 1 more /)]);
expect(moved({ "a.ts": 2, "tool.ts": 1 }, { "tool.ts": 1 })).toEqual([]);
expect(moved({ "a.ts": 2, "tool.ts": 1 }, { "tool.ts": 2 })).toEqual([
expect.stringMatching(
/^tool\.ts: 1 print call\(s\) counted, fewer than the 2 listed .*Lower `calls` there to 1/,
),
]);
expect(moved({ "a.ts": 2 }, { "tool.ts": 2 })).toEqual([
expect.stringMatching(/^tool\.ts: 0 print call\(s\) counted, fewer than the 2 listed /),
]);
});
@@ -0,0 +1,90 @@
{
"auth/oauth.ts": 3,
"auth/resolver.ts": 1,
"browser/manager.ts": 2,
"capture/captureAttempt.ts": 1,
"capture/captureCompositionFrame.ts": 2,
"cli.ts": 4,
"commands/audio.ts": 2,
"commands/auth/logout.ts": 1,
"commands/auth/status.ts": 2,
"commands/batchRender.ts": 4,
"commands/beats.ts": 2,
"commands/benchmark.ts": 7,
"commands/billing.ts": 1,
"commands/browser.ts": 4,
"commands/capture.ts": 10,
"commands/capture/video.ts": 7,
"commands/catalog.ts": 4,
"commands/catch-up.ts": 5,
"commands/check.ts": 13,
"commands/clean.ts": 3,
"commands/cloud/get.ts": 1,
"commands/cloud/list.ts": 1,
"commands/cloud/render.ts": 10,
"commands/cloudrun.ts": 7,
"commands/compositions.ts": 1,
"commands/diff.ts": 14,
"commands/docs.ts": 6,
"commands/doctor.ts": 1,
"commands/figma/asset.ts": 1,
"commands/figma/component.ts": 1,
"commands/figma/tokens.ts": 1,
"commands/info.ts": 6,
"commands/init.ts": 18,
"commands/keyframes.ts": 12,
"commands/lambda.ts": 3,
"commands/lambda/deploy.ts": 1,
"commands/lambda/policies.ts": 1,
"commands/lambda/progress.ts": 4,
"commands/lambda/render-batch.ts": 2,
"commands/lambda/render.ts": 3,
"commands/lambda/sites.ts": 1,
"commands/layout.ts": 4,
"commands/lint.ts": 2,
"commands/media-treatment.ts": 1,
"commands/models.ts": 3,
"commands/normalize-audio.ts": 2,
"commands/open.ts": 6,
"commands/play.ts": 3,
"commands/present.ts": 3,
"commands/preview.ts": 9,
"commands/publish.ts": 3,
"commands/remove-background.ts": 7,
"commands/render.ts": 12,
"commands/render/execute.ts": 6,
"commands/render/present.ts": 4,
"commands/share.ts": 9,
"commands/skills.ts": 4,
"commands/snapshot.ts": 6,
"commands/telemetry.ts": 2,
"commands/timeline.ts": 1,
"commands/transcribe.ts": 4,
"commands/tts.ts": 4,
"commands/update.ts": 4,
"commands/upgrade.ts": 9,
"commands/validate.ts": 5,
"measure/face.ts": 1,
"renderSetupWorker.ts": 1,
"server/studioServer.ts": 2,
"share/publishTransition.ts": 1,
"timeline/a2MutationCommand.ts": 2,
"ui/banner.ts": 1,
"ui/diagnostics.ts": 2,
"ui/format.ts": 1,
"ui/progress.ts": 3,
"utils/autoUpdate.ts": 1,
"utils/checkBrowser.ts": 4,
"utils/checkDeadline.ts": 2,
"utils/checkPipeline.ts": 2,
"utils/checkProgress.ts": 1,
"utils/compositionFps.ts": 1,
"utils/dotEnv.ts": 1,
"utils/jsonError.ts": 1,
"utils/reject-unknown-flags.ts": 1,
"utils/render-success-state.ts": 1,
"utils/skillsUpdateCheck.ts": 1,
"utils/updateCheck.ts": 4,
"utils/webmAlphaCheck.ts": 1,
"warmCheckClient.ts": 1
}