mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* refactor(cli): render command help with REA's own help owner Add src/cli/help.ts and move the three incur Help.formatCommand callers (rea mcp add, rea mcp doctor, rea mcp --http) onto it. Option spelling and the unwrapped field type move to src/cli/optionFields.ts, which completion now reads instead of its own copies. A counted incur-import guard records the names still imported from incur so the list can only shrink. Refs #11654 * refactor(cli): share option spelling across CLI owners and count every incur import The argv parser, strict positionals, contract command projection and the command registry now read the flag spelling, declared field type and count marker from src/cli/optionFields.ts instead of their own copies. The incur-import guard also counts namespace, default, star, side-effect and dynamic imports, so `import * as Incur from "incur"` cannot bring a removed owner back. * refactor(cli): format and filter CLI output without incur (#11684) * refactor(cli): format and filter CLI output without incur formatCliOutput encodes TOON (@toon-format/toon), JSON, YAML and JSON Lines itself instead of calling incur's Formatter; YAML still loads on first use. src/cli/outputFilter.ts owns the --filter-output grammar and returns a Result: an unclosed slice is a parse failure, so output validation reads the parser instead of a second bracket scanner, and the parsed paths travel with the output arguments to the result writer and JSON Lines. @toon-format/toon moves from devDependencies to dependencies because REA now imports it at runtime. Refs #11654 * test(cli): pin output encodings and filter grammar with literal cases Literal expected outputs for empty values, TOON scalars, objects and rows, JSON-text re-indentation, nested BigInt values, JSON Lines rows and YAML, and literal parsed paths and unclosed-slice refusals for --filter-output, so a change to either owner fails without comparing against itself.
208 lines
5.8 KiB
TypeScript
208 lines
5.8 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import {
|
|
encodeMarkdownTableCell,
|
|
formatCliOutput,
|
|
prepareDelimitedOutput,
|
|
} from "../../../src/cli/cliOutputFormatter.js";
|
|
|
|
it.each([
|
|
["", ""],
|
|
["plain", "plain"],
|
|
["a\u00a0b", "a\u00a0b"],
|
|
[
|
|
" \t\u00a0\u202f\u2003\u2028\u2029\ufeff",
|
|
" 	   

",
|
|
],
|
|
["\u00a0🙂\ufeff", " 🙂"],
|
|
[" \t name\u00a0", " 	&\\#160;name "],
|
|
["\u00a0\u0000\u202f", " \\\\u0000 "],
|
|
["\u00a0\r\n\u202f", " <br> "],
|
|
["\v \u00a0x\u202f \f", "\v \u00a0x\u202f \f"],
|
|
[" \v\u00a0x\u202f\f ", " \v\u00a0x\u202f\f "],
|
|
])(
|
|
"encodes only representable Markdown table edge spacing in %j",
|
|
(value, expected) => {
|
|
expect(encodeMarkdownTableCell(value)).toBe(expected);
|
|
},
|
|
);
|
|
|
|
it.each<[string, unknown, "toon" | "json" | "yaml" | "jsonl", string]>([
|
|
["no value as TOON", null, "toon", ""],
|
|
["no value as JSON", undefined, "json", ""],
|
|
["no value as YAML", null, "yaml", ""],
|
|
["a scalar as TOON", 12, "toon", "12"],
|
|
["text as TOON", "text", "toon", "text"],
|
|
[
|
|
"an object as TOON",
|
|
{ a: 1, b: [1, 2], c: { d: "x" } },
|
|
"toon",
|
|
"a: 1\nb[2]: 1,2\nc:\n d: x",
|
|
],
|
|
[
|
|
"rows as TOON",
|
|
[
|
|
{ id: 1, name: "a" },
|
|
{ id: 2, name: "b" },
|
|
],
|
|
"toon",
|
|
"[2]{id,name}:\n 1,a\n 2,b",
|
|
],
|
|
["JSON object text as JSON", ' {"a":1} ', "json", '{\n "a": 1\n}'],
|
|
["JSON array text as JSON", "[1,2]", "json", "[\n 1,\n 2\n]"],
|
|
["malformed JSON text as JSON", "{not json", "json", '"{not json"'],
|
|
["plain text as JSON", "plain", "json", '"plain"'],
|
|
[
|
|
"nested BigInt values as JSON",
|
|
{ big: 9007199254740993n, nested: [1n, { x: 2n }] },
|
|
"json",
|
|
'{\n "big": "9007199254740993",\n "nested": [\n "1",\n {\n "x": "2"\n }\n ]\n}',
|
|
],
|
|
[
|
|
"a BigInt value as JSON Lines",
|
|
{ big: 9007199254740993n },
|
|
"jsonl",
|
|
'{"big":"9007199254740993"}',
|
|
],
|
|
["rows as JSON Lines", [{ a: 1 }, { a: 2 }], "jsonl", '{"a":1}\n{"a":2}'],
|
|
[
|
|
"an object as YAML",
|
|
{ a: 1, b: [1, 2] },
|
|
"yaml",
|
|
"a: 1\nb:\n - 1\n - 2\n",
|
|
],
|
|
])("formats %s", (_name, value, format, expected) => {
|
|
expect(formatCliOutput(value, format)).toBe(expected);
|
|
});
|
|
|
|
const parseDelimited = (source: string, delimiter: string): string[][] => {
|
|
const records: string[][] = [];
|
|
let record: string[] = [];
|
|
let cell = "";
|
|
let quoted = false;
|
|
for (let index = 0; index < source.length; index += 1) {
|
|
const character = source[index]!;
|
|
if (quoted) {
|
|
if (character === '"') {
|
|
if (source[index + 1] === '"') {
|
|
cell += '"';
|
|
index += 1;
|
|
} else quoted = false;
|
|
} else cell += character;
|
|
continue;
|
|
}
|
|
if (character === '"' && cell.length === 0) {
|
|
quoted = true;
|
|
continue;
|
|
}
|
|
if (character === delimiter) {
|
|
record.push(cell);
|
|
cell = "";
|
|
continue;
|
|
}
|
|
if (character === "\r" || character === "\n") {
|
|
if (character === "\r" && source[index + 1] === "\n") index += 1;
|
|
record.push(cell);
|
|
records.push(record);
|
|
record = [];
|
|
cell = "";
|
|
continue;
|
|
}
|
|
cell += character;
|
|
}
|
|
if (cell.length > 0 || record.length > 0) {
|
|
record.push(cell);
|
|
records.push(record);
|
|
}
|
|
return records;
|
|
};
|
|
|
|
it.each([
|
|
{ format: "csv" as const, delimiter: "," },
|
|
{ format: "tsv" as const, delimiter: "\t" },
|
|
])(
|
|
"round-trips escaped $format fields and Unicode values",
|
|
({ format, delimiter }) => {
|
|
const rows = [
|
|
{
|
|
name: 'A, "quoted" name',
|
|
note: "first line\r\nsecond\tline — café 雪",
|
|
count: 0,
|
|
ignored: { nested: true },
|
|
},
|
|
{ name: "", note: "", count: false },
|
|
{ name: "欠", count: null },
|
|
];
|
|
const output = formatCliOutput(rows, format, {
|
|
fields: ["name", "note", "count"],
|
|
metadata: {
|
|
version: 1,
|
|
coverage: { status: "page", examined: 3, total: 8, next_offset: 3 },
|
|
view: { offset: 0, limit: 3 },
|
|
},
|
|
});
|
|
const lineEnding = format === "csv" ? "\r\n" : "\n";
|
|
const metadataEnd = output.indexOf(lineEnding);
|
|
const metadataLine = output.slice(0, metadataEnd);
|
|
const records = output
|
|
.slice(metadataEnd + lineEnding.length)
|
|
.replace(/\r?\n$/u, "");
|
|
expect(
|
|
JSON.parse(metadataLine!.slice("# rea-delimited-v1 ".length)),
|
|
).toEqual({
|
|
version: 1,
|
|
coverage: { status: "page", examined: 3, total: 8, next_offset: 3 },
|
|
view: { offset: 0, limit: 3 },
|
|
});
|
|
expect(parseDelimited(records, delimiter)).toEqual([
|
|
["name", "note", "count"],
|
|
['A, "quoted" name', "first line\r\nsecond\tline — café 雪", "0"],
|
|
["", "", "false"],
|
|
["欠", "", ""],
|
|
]);
|
|
},
|
|
);
|
|
|
|
it("does not flatten nested selected field values", () => {
|
|
expect(
|
|
prepareDelimitedOutput([{ details: { name: "nested" } }], "csv", {
|
|
fields: ["details"],
|
|
metadata: {},
|
|
}),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
});
|
|
|
|
it.each(["=1+1", " =1+1", "+1", "-1", "@SUM(A1:A2)"])(
|
|
"refuses formula-leading string cells without changing the source value %j",
|
|
(value) => {
|
|
const row = { target: value };
|
|
const result = prepareDelimitedOutput([row], "csv", {
|
|
fields: ["target"],
|
|
metadata: {},
|
|
});
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisInputError",
|
|
userMessage: expect.stringContaining("formula-leading"),
|
|
},
|
|
});
|
|
expect(row.target).toBe(value);
|
|
},
|
|
);
|
|
|
|
it("refuses formula-leading column headers", () => {
|
|
expect(
|
|
prepareDelimitedOutput([{ value: "safe" }], "csv", {
|
|
fields: ["=header"],
|
|
metadata: {},
|
|
}),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisInputError" },
|
|
});
|
|
});
|