2026-07-22 20:38:26 +08:00
import { mkdir , writeFile } from "node:fs/promises" ;
2026-07-14 06:28:10 +08:00
import { join } from "node:path" ;
2026-07-13 19:09:27 +00:00
2026-07-14 06:28:10 +08:00
import { createPackageWithOptions } from "@electron/asar" ;
2026-07-13 19:09:27 +00:00
import { describe , expect , it } from "vitest" ;
2026-08-10 14:57:44 +08:00
import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js" ;
2026-07-22 20:38:26 +08:00
import {
renderCliOutputArgumentError ,
sanitizeCliOutput ,
validateCliOutputArguments ,
2026-07-29 22:00:38 +00:00
} from "../../../src/cliOutput.js" ;
2026-07-13 19:09:27 +00:00
2026-07-14 05:03:08 +00:00
const CLI_INTEGRATION_TIMEOUT_MS = 60 _000 ;
2026-07-13 19:09:27 +00:00
2026-07-29 22:00:38 +00:00
describe ( "CLI output argument and sanitization boundary" , () => {
2026-07-22 20:38:26 +08:00
it ( "rejects token windows that would corrupt structured output" , () => {
for ( const format of [ "json" , "jsonl" , "yaml" ] as const ) {
const validation = validateCliOutputArguments ([
"providers" ,
"--token-limit" ,
"5" ,
"--format" ,
format ,
]);
expect ( validation ). toMatchObject ({
ok : false ,
format ,
code : "UNSUPPORTED_OUTPUT_COMBINATION" ,
});
if ( ! validation . ok ) {
const rendered = renderCliOutputArgumentError ( validation );
expect ( rendered ). not . toContain ( "[truncated:" );
if ( format === "json" || format === "jsonl" )
expect ( JSON . parse ( rendered )). toMatchObject ({
ok : false ,
error : { code : "UNSUPPORTED_OUTPUT_COMBINATION" },
});
else
expect ( rendered ). toMatch (
/^ok: false\nerror:\n code: UNSUPPORTED_OUTPUT_COMBINATION\n/u,
);
}
}
expect (
validateCliOutputArguments ([
"providers" ,
"--token-limit" ,
"5" ,
"--format" ,
"toon" ,
]),
). toEqual ({ ok : true });
expect (
validateCliOutputArguments ([ "providers" , "--token-count" , "--json" ]),
). toEqual ({ ok : true });
});
2026-08-10 14:57:44 +08:00
workspaceCliTest (
2026-07-29 22:00:38 +00:00
"fails before emitting a truncated JSON document" ,
async ({ cli }) => {
const result = await cli . run ({
arguments : [ "providers" , "--token-limit" , "5" , "--json" ],
});
expect ( result . exitCode ). toBe ( 1 );
expect ( result . json ). toMatchObject ({
ok : false ,
error : { code : "UNSUPPORTED_OUTPUT_COMBINATION" },
});
},
CLI_INTEGRATION_TIMEOUT_MS ,
);
2026-07-22 20:38:26 +08:00
2026-07-13 19:09:27 +00:00
it ( "preserves normal output and sanitizes text and JSON validation errors" , () => {
expect ( sanitizeCliOutput ( "result: ok\n" )). toBe ( "result: ok\n" );
expect ( sanitizeCliOutput ( "result: VALIDATION_ERROR\n" )). toBe (
"result: VALIDATION_ERROR\n" ,
);
const raw =
'code: VALIDATION_ERROR\nmessage: "raw Zod details"\nfieldErrors: SECRET\n' ;
expect ( sanitizeCliOutput ( raw )). toBe (
'code: VALIDATION_ERROR\nmessage: "REA could not read the command arguments. Run `rea --help`, correct the arguments, then try again."\n' ,
);
expect (
JSON . parse (
sanitizeCliOutput (
JSON . stringify ({
code : "VALIDATION_ERROR" ,
message : "raw Zod details" ,
fieldErrors : [{ code : "invalid_type" }],
}),
),
),
). toEqual ({
code : "VALIDATION_ERROR" ,
message :
"REA could not read the command arguments. Run `rea --help`, correct the arguments, then try again." ,
});
expect (
JSON . parse (
sanitizeCliOutput (
JSON . stringify ({
ok : false ,
error : {
code : "VALIDATION_ERROR" ,
message : "raw Zod details" ,
fieldErrors : [{ code : "invalid_type" }],
},
meta : { command : "analyze" },
}),
),
),
). toEqual ({
ok : false ,
error : {
code : "VALIDATION_ERROR" ,
message :
"REA could not read the command arguments. Run `rea --help`, correct the arguments, then try again." ,
},
meta : { command : "analyze" },
});
});
2026-07-29 22:00:38 +00:00
});
2026-07-13 19:09:27 +00:00
2026-07-29 22:00:38 +00:00
describe ( "compiled CLI output boundary" , () => {
2026-08-10 14:57:44 +08:00
workspaceCliTest (
2026-07-14 05:03:08 +00:00
"sanitizes a real missing-argument dispatcher failure" ,
2026-07-29 22:00:38 +00:00
async ({ cli }) => {
const ordinary = await cli . run ({ arguments : [ "analyze" ] });
expect ( ordinary ). toMatchObject ({
exitCode : 1 ,
2026-07-14 05:03:08 +00:00
stdout :
'code: VALIDATION_ERROR\nmessage: "REA could not read the command arguments. Run `rea --help`, correct the arguments, then try again."\n' ,
});
2026-07-29 22:00:38 +00:00
const json = await cli . run ({
2026-09-29 07:08:46 +08:00
arguments : [ "--json" , "analyze" ],
2026-07-29 22:00:38 +00:00
});
expect ( json ). toMatchObject ({
exitCode : 1 ,
2026-07-14 05:03:08 +00:00
stdout : expect.not.stringContaining ( "fieldErrors" ),
});
2026-07-29 22:00:38 +00:00
const yaml = await cli . run ({
arguments : [ "--full-output" , "analyze" ],
});
expect ( yaml ). toMatchObject ({
exitCode : 1 ,
2026-07-14 05:03:08 +00:00
stdout :
'ok: false\nerror:\n code: VALIDATION_ERROR\n message: "REA could not read the command arguments. Run `rea --help`, correct the arguments, then try again."\n' ,
});
},
CLI_INTEGRATION_TIMEOUT_MS ,
);
2026-07-14 06:28:10 +08:00
2026-08-10 14:57:44 +08:00
workspaceCliTest (
2026-09-29 07:08:46 +08:00
"preserves artifact diagnostics in JSON output" ,
2026-07-29 22:00:38 +00:00
async ({ cli , workspace }) => {
const source = workspace . path ( "source" );
2026-07-14 05:03:08 +00:00
await mkdir ( source );
await writeFile ( join ( source , "main.js" ), "console.log('ok');\n" );
2026-07-29 22:00:38 +00:00
const archive = workspace . path ( "fixture.asar" );
2026-07-14 05:03:08 +00:00
await createPackageWithOptions ( source , archive , { unpack : "*.js" });
await writeFile ( join ( ` ${ archive } .unpacked` , "main.js" ), "changed();\n" );
2026-07-14 06:28:10 +08:00
2026-09-29 07:08:46 +08:00
const result = await cli . run ({
2026-09-29 09:22:57 +08:00
arguments : [ "--json" , "inspect-artifact" , archive ],
2026-09-29 07:08:46 +08:00
});
expect ( result . exitCode ). toBe ( 1 );
const output = JSON . stringify ( result . json );
expect ( output ). toContain ( '"logical_path":"main.js"' );
expect ( output ). toMatch ( /"declared_sha256":"[a-f0-9]{64}"/u);
expect ( output ). toMatch ( /"calculated_sha256":"[a-f0-9]{64}"/u);
expect ( output ). toContain ( '"unpacked":true' );
2026-07-14 05:03:08 +00:00
},
CLI_INTEGRATION_TIMEOUT_MS ,
);
2026-07-13 19:09:27 +00:00
});