mirror of
https://github.com/morluto/rea.git
synced 2026-11-17 04:04:25 +00:00
Run ordinary CLI commands through REA's registry, argv parser and result owner. Remove Incur, the second admission parse, SDK error middleware, output capture and redundant command traversal. Preserve explicit redaction, publication receipts, incremental output, terminal notice eligibility and drain ownership. Migrate fixtures to real registrations and retain the derived-source notices. Validation includes unchanged public parity goldens, public and lifecycle regressions, repaired fixture consumers, real TTY base comparisons, exports beyond the engine string limit, four schema catalogs and final static/docs checks. Native provider and optional validator coverage limits are in the PR. Fixes #11654. Fixes #10583. Refs #11682, #11476 and #10511. BREAKING CHANGE: Markdown argument diagnostics use REA's literal escaping, table widths and token counts. Flag-shaped unknown MCP subcommands honor the selected format. Completion --schema/--version routes use the canonical REA-owned hook rather than the retired SDK generator. Refresh affected literal snapshots and regenerate completion hooks with rea completions <shell>. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
243 lines
9.9 KiB
TypeScript
243 lines
9.9 KiB
TypeScript
import { join, resolve } from "node:path";
|
|
import { pathToFileURL } from "node:url";
|
|
import { cliCommands } from "../../../src/cli/commandRegistry.js";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { createCli } from "../../../src/cli.js";
|
|
import {
|
|
CLI_COMMAND_REGISTRATIONS,
|
|
CLI_REGISTRATION_ORDER,
|
|
type CliCommandRegistrationId,
|
|
} from "../../../src/cli/cliCommandRegistrations.js";
|
|
import { selectCliRegistrations } from "../../../src/cli/cliLoader.js";
|
|
import {
|
|
cliCompletionShell,
|
|
parseCliGlobalArguments,
|
|
} from "../../../src/cliBuiltinArguments.js";
|
|
|
|
/** Select registrars as the executable does: completion has no global arguments. */
|
|
const selectFor = (
|
|
arguments_: readonly string[],
|
|
environment: NodeJS.ProcessEnv,
|
|
recorded: Readonly<Record<string, string>> | undefined,
|
|
) =>
|
|
selectCliRegistrations(
|
|
cliCompletionShell(environment) === undefined
|
|
? parseCliGlobalArguments(arguments_)
|
|
: undefined,
|
|
recorded,
|
|
);
|
|
import { createCliShell } from "../../../src/cli/cliShell.js";
|
|
import type { CliInstance } from "../../../src/cli/types.js";
|
|
|
|
import { createDirectAnalysis } from "../../../src/composition/directAnalysis.js";
|
|
import { PRODUCT_IDENTITY } from "../../../src/identity.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { processTest } from "../../support/process/processFixture.js";
|
|
import { serveInProcessCli } from "../../support/cli/serveInProcessCli.js";
|
|
|
|
const ENVIRONMENT = { PATH: process.env.PATH };
|
|
|
|
const topLevelNames = (cli: CliInstance): readonly string[] =>
|
|
cliCommands(cli).map(({ name }) => name);
|
|
|
|
const createRegistrarCli = async (
|
|
id: CliCommandRegistrationId,
|
|
): Promise<CliInstance> => {
|
|
const registration = CLI_COMMAND_REGISTRATIONS[id];
|
|
return createCliShell({
|
|
selectedEnvironment: ENVIRONMENT,
|
|
registrations: [{ registration, module: await registration.load() }],
|
|
createAnalysis: createDirectAnalysis,
|
|
});
|
|
};
|
|
|
|
const help = async (cli: CliInstance, path: string) =>
|
|
serveInProcessCli(cli, [...path.split(" "), "--help"]);
|
|
|
|
const recordedRegistrations = async (): Promise<Record<string, string>> => {
|
|
const module = (await import(
|
|
pathToFileURL(resolve("dist/recordedCliCommandRegistrations.js")).href
|
|
)) as { RECORDED_CLI_COMMAND_REGISTRATIONS: Record<string, string> };
|
|
return module.RECORDED_CLI_COMMAND_REGISTRATIONS;
|
|
};
|
|
|
|
describe("lazy CLI registration", () => {
|
|
it("records every top-level command under the one registrar that defines it", async () => {
|
|
const recorded = await recordedRegistrations();
|
|
const complete = createCli(ENVIRONMENT);
|
|
expect(Object.keys(recorded).sort()).toEqual(
|
|
[...topLevelNames(complete)].sort(),
|
|
);
|
|
for (const id of CLI_REGISTRATION_ORDER) {
|
|
const alone = await createRegistrarCli(id);
|
|
expect(
|
|
[...topLevelNames(alone)].sort(),
|
|
`${id} registers what the recording assigns it`,
|
|
).toEqual(
|
|
Object.entries(recorded)
|
|
.filter(([, owner]) => owner === id)
|
|
.map(([name]) => name)
|
|
.sort(),
|
|
);
|
|
}
|
|
});
|
|
|
|
it("gives each command the same definition alone as in the complete CLI", async () => {
|
|
const complete = createCli(ENVIRONMENT);
|
|
const completePaths = new Set(topLevelNames(complete));
|
|
const covered = new Set<string>();
|
|
for (const id of CLI_REGISTRATION_ORDER) {
|
|
const alone = await createRegistrarCli(id);
|
|
for (const path of topLevelNames(alone)) {
|
|
expect(completePaths.has(path), path).toBe(true);
|
|
covered.add(path);
|
|
expect(await help(alone, path), path).toEqual(
|
|
await help(complete, path),
|
|
);
|
|
}
|
|
}
|
|
expect(covered).toEqual(completePaths);
|
|
});
|
|
|
|
it("loads one registrar only for a recorded command that runs", () => {
|
|
const recorded = { "inspect-go-binary": "go", setup: "setup" };
|
|
const select = (
|
|
arguments_: readonly string[],
|
|
environment: NodeJS.ProcessEnv = {},
|
|
record: Readonly<Record<string, string>> | null = recorded,
|
|
) => selectFor(arguments_, environment, record ?? undefined);
|
|
|
|
expect(select(["inspect-go-binary", "a.out"])).toEqual(["go"]);
|
|
expect(select(["inspect-go-binary", "a.out", "--json"])).toEqual(["go"]);
|
|
expect(select(["--format", "json", "inspect-go-binary", "a.out"])).toEqual([
|
|
"go",
|
|
]);
|
|
expect(select(["--timings", "setup", "--dry-run"])).toEqual(["setup"]);
|
|
// A standalone version query needs no registrar, recorded or not.
|
|
expect(select(["--version"])).toEqual([]);
|
|
expect(select(["--version"], {}, null)).toEqual([]);
|
|
// Combined with anything else, or completing, it keeps the complete CLI.
|
|
expect(select(["--version", "--help"])).toBeUndefined();
|
|
expect(select(["--version", "--timings"])).toBeUndefined();
|
|
expect(select(["--version"], { COMPLETE: "bash" })).toBeUndefined();
|
|
// Help, schemas, LLM manifests and builtins describe the whole catalog.
|
|
for (const flag of ["--help", "-h", "--schema", "--llms", "--llms-full"])
|
|
expect(select(["inspect-go-binary", flag]), flag).toBeUndefined();
|
|
// A retired mode is refused without constructing command analysis.
|
|
expect(select(["inspect-go-binary", "--mcp"])).toEqual([]);
|
|
expect(select(["unknown", "--mcp"], {}, null)).toEqual([]);
|
|
expect(select(["--mcp"])).toBeUndefined();
|
|
expect(select([])).toBeUndefined();
|
|
expect(select(["skills", "add"])).toBeUndefined();
|
|
// An unknown name needs every command for "did you mean".
|
|
expect(select(["inspect-go-binar"])).toBeUndefined();
|
|
expect(select(["constructor"])).toBeUndefined();
|
|
// Shell completion and a missing or stale recording use the complete CLI.
|
|
expect(select(["inspect-go-binary"], { COMPLETE: "bash" })).toBeUndefined();
|
|
expect(select(["inspect-go-binary"], {}, null)).toBeUndefined();
|
|
expect(
|
|
select(["inspect-go-binary"], {}, { "inspect-go-binary": "removed" }),
|
|
).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("lazy CLI execution", () => {
|
|
processTest(
|
|
"runs a recorded command without loading the complete CLI",
|
|
async ({ processes }) => {
|
|
const marker = "REA_TEST_CLI_ENTRY_RESOLVED";
|
|
// Report a resolution of the complete CLI entry without changing it.
|
|
const hook = `data:text/javascript,${encodeURIComponent(`
|
|
import { registerHooks } from "node:module";
|
|
registerHooks({ resolve(specifier, context, next) {
|
|
const resolved = next(specifier, context);
|
|
if (resolved.url.endsWith("/dist/cli-runtime/cli.js")) process.stderr.write(${JSON.stringify(`${marker}\n`)});
|
|
return resolved;
|
|
} });`)}`;
|
|
const home = await createTestTempDirectory("rea-cli-lazy-home-");
|
|
const run = (arguments_: readonly string[]) =>
|
|
processes.run(
|
|
process.execPath,
|
|
[`--import=${hook}`, join("scripts", "rea.mjs"), ...arguments_],
|
|
{ env: { ...ENVIRONMENT, HOME: home } },
|
|
);
|
|
const lazy = await run(["list-cutter-sessions", "--json"]);
|
|
expect(lazy.exitCode, lazy.stderr).toBe(0);
|
|
expect(JSON.parse(lazy.stdout)).toMatchObject({ sessions: [] });
|
|
expect(lazy.stderr).not.toContain(marker);
|
|
const version = await run(["--version"]);
|
|
expect(version.exitCode, version.stderr).toBe(0);
|
|
expect(version.stdout).toBe(`${PRODUCT_IDENTITY.packageVersion}\n`);
|
|
expect(version.stderr).not.toContain(marker);
|
|
const retired = await run(["doctor", "--mcp", "--json", "--full-output"]);
|
|
expect(retired.exitCode, retired.stderr).toBe(1);
|
|
expect(JSON.parse(retired.stdout)).toMatchObject({
|
|
ok: false,
|
|
error: { code: "MCP_MODE_REMOVED" },
|
|
meta: { command: "doctor" },
|
|
});
|
|
expect(retired.stderr).not.toContain(marker);
|
|
const complete = await run(["list-cutter-sessions", "--help"]);
|
|
expect(complete.exitCode, complete.stderr).toBe(0);
|
|
expect(complete.stderr).toContain(marker);
|
|
},
|
|
);
|
|
|
|
processTest(
|
|
"rejects each registrar's command the same way in a fresh lazy process",
|
|
async ({ processes }) => {
|
|
// In-process comparisons share every module the complete CLI imported.
|
|
// A fresh process loads only the selected registrar, so it exposes one
|
|
// that relies on another module's import-time setup.
|
|
const recorded = await recordedRegistrations();
|
|
const firstNames = new Map<string, string>();
|
|
for (const [name, id] of Object.entries(recorded))
|
|
if (!firstNames.has(id)) firstNames.set(id, name);
|
|
expect([...firstNames.keys()].sort()).toEqual(
|
|
[...CLI_REGISTRATION_ORDER].sort(),
|
|
);
|
|
const home = await createTestTempDirectory("rea-cli-lazy-fresh-");
|
|
const complete = createCli(ENVIRONMENT);
|
|
// An unknown option is rejected before any command handler runs.
|
|
const invocation = (name: string) => [
|
|
name,
|
|
"--rea-test-unknown-option",
|
|
"--json",
|
|
];
|
|
const names = [...firstNames.values()];
|
|
// The one-shot host writes compact JSON; compare the documents.
|
|
const document = (stdout: string): unknown => {
|
|
try {
|
|
return JSON.parse(stdout);
|
|
} catch {
|
|
return stdout;
|
|
}
|
|
};
|
|
const lazy = new Map<string, { document: unknown; exitCode: number }>();
|
|
const LAUNCHES = 4;
|
|
for (let start = 0; start < names.length; start += LAUNCHES)
|
|
await Promise.all(
|
|
names.slice(start, start + LAUNCHES).map(async (name) => {
|
|
const result = await processes.run(
|
|
process.execPath,
|
|
[join("scripts", "rea.mjs"), ...invocation(name)],
|
|
{ env: { ...ENVIRONMENT, HOME: home } },
|
|
);
|
|
lazy.set(name, {
|
|
document: document(result.stdout),
|
|
exitCode: result.exitCode ?? -1,
|
|
});
|
|
}),
|
|
);
|
|
for (const name of names) {
|
|
const expected = await serveInProcessCli(complete, invocation(name));
|
|
expect(lazy.get(name), name).toEqual({
|
|
document: document(expected.stdout),
|
|
exitCode: expected.exitCode,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
});
|