Files
reverse-engineering-anything/tests/process-global/updateNoticeEnvironment.test.ts
T
a10037ef10 refactor(cli): report unknown commands and the update notice from REA (#11692)
* refactor(cli): render help pages with REA's help owner

REA now renders the root page, every command page, help after an unknown command, the mcp group page, the mcp doctor builtin page and the completions page before incur sees argv, with incur's precedence (update check, --update without help, --mcp and the LLM manifests first; then the integrations; then --version; then root and command pages). help.ts gains arguments, custom global options, the root-only flags and integrations, and a command-list page.

The --format row on the root and command pages now lists csv and tsv (#10250), the approved difference; the mcp and mcp add pages keep the document formats they accept. Every other help byte is unchanged: 397 pages compared with incur's Help for all 196 registered commands.

Fixes #10250

* test(cli): elide the root command list wherever the root page is printed

The golden renderer chose root-page elision by case name, so help after an unknown command froze the whole command list. It now elides any stdout that opens with the root page's rea@<version> title.

* refactor(cli): answer --version, --schema and the LLM manifests in REA

src/cli/discovery.ts answers help, --version, --schema, --llms and --llms-full with incur's precedence, reading the arguments as the dispatcher receives them (after result output has adapted them, so token counts and buffering are unchanged). commandManifests.ts renders the Markdown index and full manifest, the structured manifests and the --schema document; commandSuggestion.ts ranks Did-you-mean candidates. Both are adapted from incur under its MIT licence.

--schema --format md stays with incur: incur renders it without Markdown escaping, unlike REA's renderer, and that difference awaits a decision on #11654. Compared with incur's serve for every registered command and format, 3746 of 3945 discovery invocations are identical; the rest are the 196 Markdown schemas left to incur and the three root help pages with the approved csv/tsv row.

* test(cli): pin which discovery document wins when several are asked for

Goldens for --llms with --help, --llms with --version, --llms with --llms-full, --schema with --version and --schema with --help, recorded with incur's rendering on the base branch.

* refactor(cli): report unknown commands and the update notice from REA

src/cli/updateNotice.ts owns the terminal update notice: the same trigger (terminal stdout, no explicit format), text, cache file ($XDG_CACHE_HOME/incur/updates/<package>.json), daily detached refresh through rea --incur-update-check, and opt-outs (NO_UPDATE_NOTIFIER, npm_config_update_notifier=false, CI). Discovery answers the detached refresh first, and reports an unknown command with incur's Did-you-mean suggestion, corrected invocation, help command and notice, as text on a terminal or as the error document, full-output envelope, token count or token window otherwise. outputEnvelope.ts renders these dispatcher failures.

Unknown commands with --format md stay with incur pending the Markdown escaping decision on #11654. Compared with incur's serve, 224 of 224 unknown-command invocations are identical, piped and on a terminal with a due notice.

* fix(cli): redact unknown-command guidance before rendering

Share the producing reply boundary for root lookup, unknown schemas and MCP subcommands. Preserve ordinary argument identity and existing MCP suggestion policies; omit sensitive or unsafe replay commands and redact full-output command metadata.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

---------

Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-16 14:46:48 +08:00

95 lines
3.4 KiB
TypeScript

import childProcess, { ChildProcess } from "node:child_process";
import { syncBuiltinESMExports } from "node:module";
import { readFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { expect, it, vi } from "vitest";
import { createCliShell } from "../../src/cli/cliShell.js";
import { PRODUCT_IDENTITY } from "../../src/identity.js";
import { HOST_PLATFORM } from "../../src/platform/HostPlatform.js";
import { snapshotEnvironment } from "../../src/process/snapshotEnvironment.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
it.each(["XDG cache", "home fallback"] as const)(
"schedules a stale notice refresh with the selected environment and %s",
async (source) => {
const root = await createTestTempDirectory("rea-notice-selected-env-");
const selected = {
...(source === "XDG cache"
? { XDG_CACHE_HOME: join(root, "selected") }
: {}),
HOME: join(root, "selected-home"),
USERPROFILE: join(root, "selected-home"),
npm_config_registry: "http://127.0.0.1:9/selected/",
CI: "false",
NO_UPDATE_NOTIFIER: "",
SELECTED_SETTING: "original",
};
const cli = createCliShell({
selectedEnvironment: selected,
registrations: [],
});
const expected = snapshotEnvironment(selected, HOST_PLATFORM.platform);
selected.SELECTED_SETTING = "changed after CLI construction";
const argv = process.argv;
const exitCode = process.exitCode;
const tty = Object.getOwnPropertyDescriptor(process.stdout, "isTTY");
const child = new ChildProcess();
const spawn = vi.spyOn(childProcess, "spawn").mockReturnValue(child);
syncBuiltinESMExports();
vi.stubEnv("XDG_CACHE_HOME", join(root, "ambient"));
vi.stubEnv("HOME", join(root, "ambient-home"));
vi.stubEnv("USERPROFILE", join(root, "ambient-home"));
vi.stubEnv("npm_config_registry", "http://127.0.0.1:9/ambient/");
vi.stubEnv("AMBIENT_ONLY", "must not be inherited");
try {
process.argv = [process.execPath, resolve("scripts/rea.mjs")];
Object.defineProperty(process.stdout, "isTTY", {
configurable: true,
value: true,
});
await cli.serve(["no-such-command"], {
stdout: () => {},
exit: () => {},
});
expect(spawn).toHaveBeenCalledTimes(1);
expect(spawn.mock.calls[0]?.[2]).toMatchObject({
detached: true,
stdio: "ignore",
env: expected,
});
expect(spawn.mock.calls[0]?.[2]?.env).toEqual(expected);
const cache = join(
source === "XDG cache"
? selected.XDG_CACHE_HOME!
: join(selected.HOME, ".cache"),
"incur",
"updates",
`${encodeURIComponent(PRODUCT_IDENTITY.packageName)}.json`,
);
expect(JSON.parse(await readFile(cache, "utf8"))).toEqual({
checkedAt: expect.any(Number),
});
await expect(
readFile(
join(
root,
"ambient",
"incur",
"updates",
`${encodeURIComponent(PRODUCT_IDENTITY.packageName)}.json`,
),
),
).rejects.toMatchObject({ code: "ENOENT" });
} finally {
process.argv = argv;
process.exitCode = exitCode;
if (tty === undefined) Reflect.deleteProperty(process.stdout, "isTTY");
else Object.defineProperty(process.stdout, "isTTY", tty);
vi.restoreAllMocks();
syncBuiltinESMExports();
vi.unstubAllEnvs();
}
},
);