mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* refactor(cli): register every command in an REA-owned command table * fix(cli): flag grouped fields and named selectors in each command's input binding
115 lines
4.0 KiB
TypeScript
115 lines
4.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { createCli } from "../../../src/cli.js";
|
|
import { cliCommands } from "../../../src/cli/commandRegistry.js";
|
|
import { visitRegisteredCommands } from "../../../src/cli/visitRegisteredCommands.js";
|
|
|
|
const cli = createCli({});
|
|
const entries = cliCommands(cli);
|
|
const entry = (name: string) => {
|
|
const found = entries.find((candidate) => candidate.name === name);
|
|
if (found === undefined) throw new Error(`${name} is not registered`);
|
|
return found;
|
|
};
|
|
|
|
describe("REA's CLI command table", () => {
|
|
it("holds every dispatched command, in registration order, with the served definition", () => {
|
|
const dispatched: { path: string; command: object }[] = [];
|
|
visitRegisteredCommands(cli, (command, path) =>
|
|
dispatched.push({ path, command }),
|
|
);
|
|
expect(entries.map(({ name }) => name)).toEqual(
|
|
dispatched.map(({ path }) => path),
|
|
);
|
|
// Shell owners rewrite inputs and handlers in place; the table sees them.
|
|
for (const [index, { command }] of dispatched.entries())
|
|
expect(entries[index]!.definition).toBe(command);
|
|
});
|
|
|
|
it("names the operations a direct-analysis command runs and the flag of each field", () => {
|
|
expect(entry("function").operations).toEqual([
|
|
{ name: "analyze_function", flags: expect.any(Object) },
|
|
{
|
|
name: "open_binary",
|
|
flags: {
|
|
format: "--target-format",
|
|
architecture: "--architecture",
|
|
entry_point: "--entry-point",
|
|
provider_id: "--provider",
|
|
snapshot_path: "--snapshot",
|
|
},
|
|
},
|
|
]);
|
|
// `--kind procedures` chooses another operation.
|
|
expect(entry("search").operations.map(({ name }) => name)).toEqual([
|
|
"search_strings",
|
|
"open_binary",
|
|
"search_procedures",
|
|
]);
|
|
expect(entry("decompile").operations.map(({ name }) => name)).toEqual([
|
|
"procedure_pseudo_code",
|
|
"open_binary",
|
|
]);
|
|
// The overview route opens its target the same way.
|
|
expect(
|
|
entry("analyze").operations.find(({ name }) => name === "open_binary")
|
|
?.flags,
|
|
).toMatchObject({ snapshot_path: "--snapshot" });
|
|
});
|
|
|
|
it("records each route of a routed command and each contract command's own flags", () => {
|
|
expect(entry("analyze").operations.map(({ name }) => name)).toEqual([
|
|
"analyze_javascript_application",
|
|
"binary_overview",
|
|
"open_binary",
|
|
]);
|
|
expect(entry("analyze").source.kind).toBe("routed");
|
|
expect(entry("inspect-byte-entropy").operations).toEqual([
|
|
{
|
|
name: "inspect_byte_entropy",
|
|
flags: {
|
|
block_size: "--block-size",
|
|
offset: "--offset",
|
|
length: "--length",
|
|
},
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("flags every contract field an option sets: grouped fields and named selectors", () => {
|
|
const flags = (command: string, operation: string) =>
|
|
entry(command).operations.find(({ name }) => name === operation)?.flags;
|
|
// One JSON option supplies each field of its group; the group's CLI key
|
|
// is not a contract field.
|
|
const jni = flags(
|
|
"inspect-android-jni-bindings",
|
|
"inspect_android_jni_bindings",
|
|
);
|
|
expect(jni).toMatchObject({
|
|
class_name: "--query",
|
|
method_names: "--query",
|
|
descriptors: "--query",
|
|
abis: "--query",
|
|
library_paths: "--query",
|
|
});
|
|
expect(jni).not.toHaveProperty("query");
|
|
// A positional selector is also set by its named option.
|
|
expect(flags("function", "analyze_function")).toMatchObject({
|
|
procedure: "--procedure",
|
|
});
|
|
expect(flags("decompile", "procedure_pseudo_code")).toMatchObject({
|
|
procedure: "--procedure",
|
|
});
|
|
expect(flags("search", "search_strings")).toMatchObject({
|
|
pattern: "--pattern",
|
|
});
|
|
});
|
|
|
|
it("lists host commands without operations", () => {
|
|
for (const name of ["setup", "doctor", "guidance", "capture-process"]) {
|
|
expect(entry(name).source).toEqual({ kind: "host" });
|
|
expect(entry(name).operations).toEqual([]);
|
|
}
|
|
});
|
|
});
|