test: prune redundant cases and promote configuration lifecycle coverage

This commit is contained in:
morluto
2026-10-09 02:58:03 +08:00
parent bef234d6ce
commit 5945915f91
18 changed files with 237 additions and 417 deletions
@@ -11,54 +11,61 @@ import { cliTest } from "../../support/cli/cliFixture.js";
const filter = "evidence_id,normalized_result.graph.nodes[0:1].node_id";
for (const command of ["analyze-javascript-application", "analyze"]) {
for (const format of ["json", "jsonl"]) {
for (const fullOutput of [false, true]) {
for (const filtered of [false, true]) {
cliTest(
`${command} preserves ${format}, full output ${String(fullOutput)}, filtering ${String(filtered)}`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-js-json-output-");
await writeFile(join(root, "main.js"), "fetch('');\n");
const direct = await analyzeJavaScriptApplication({
input_path: root,
});
if (!direct.ok) throw direct.error;
const output = await cli.run({
arguments: [
command,
root,
"--format",
format,
...(fullOutput ? ["--full-output"] : []),
...(filtered ? ["--filter-output", filter] : []),
],
});
expect(output.exitCode).toBe(0);
let data: unknown = output.json;
if (fullOutput) {
const envelope = z
.object({
ok: z.literal(true),
data: z.unknown(),
meta: z.object({
command: z.literal(command),
duration: z.string().regex(/^\d+ms$/u),
}),
})
.parse(output.json);
data = envelope.data;
}
expect(data).toEqual(
filtered
? Filter.apply(direct.value, Filter.parse(filter))
: direct.value,
);
},
);
for (const scenario of [
{
command: "analyze-javascript-application",
format: "json",
fullOutput: false,
filtered: true,
},
{
command: "analyze-javascript-application",
format: "jsonl",
fullOutput: true,
filtered: true,
},
{ command: "analyze", format: "json", fullOutput: true, filtered: false },
{ command: "analyze", format: "jsonl", fullOutput: false, filtered: false },
] as const) {
cliTest(
`${scenario.command} preserves representative ${scenario.format} output`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-js-json-output-");
await writeFile(join(root, "main.js"), "fetch('');\n");
const direct = await analyzeJavaScriptApplication({ input_path: root });
if (!direct.ok) throw direct.error;
const output = await cli.run({
arguments: [
scenario.command,
root,
"--format",
scenario.format,
...(scenario.fullOutput ? ["--full-output"] : []),
...(scenario.filtered ? ["--filter-output", filter] : []),
],
});
expect(output.exitCode).toBe(0);
let data: unknown = output.json;
if (scenario.fullOutput) {
const envelope = z
.object({
ok: z.literal(true),
data: z.unknown(),
meta: z.object({
command: z.literal(scenario.command),
duration: z.string().regex(/^\d+ms$/u),
}),
})
.parse(output.json);
data = envelope.data;
}
}
}
expect(data).toEqual(
scenario.filtered
? Filter.apply(direct.value, Filter.parse(filter))
: direct.value,
);
},
);
}
cliTest(
@@ -21,114 +21,115 @@ const unsupportedHostOutput = {
};
describe("compiled Windows reference-source import", () => {
for (const fullOutput of [false, true]) {
for (const logging of [false, true]) {
cliTest.runIf(process.platform === "win32")(
`readable root, ${fullOutput ? "full-output" : "JSON"}, logging ${String(logging)}`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-reference-host-");
const source = join(root, "source.js");
const bytes = "export const answer = 42;\n";
await writeFile(source, bytes);
const result = await cli.run({
arguments: [
"import-reference-source",
root,
"--json",
...(fullOutput ? ["--full-output"] : []),
],
cwd: root,
environment: {
USERPROFILE: root,
XDG_CONFIG_HOME: root,
XDG_CACHE_HOME: root,
...(logging ? { REA_LOG_LEVEL: "info" } : {}),
},
for (const scenario of [
{ fullOutput: false, logging: false },
{ fullOutput: true, logging: true },
]) {
cliTest.runIf(process.platform === "win32")(
`readable root, ${scenario.fullOutput ? "full-output" : "JSON"}, logging ${String(scenario.logging)}`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-reference-host-");
const source = join(root, "source.js");
const bytes = "export const answer = 42;\n";
await writeFile(source, bytes);
const result = await cli.run({
arguments: [
"import-reference-source",
root,
"--json",
...(scenario.fullOutput ? ["--full-output"] : []),
],
cwd: root,
environment: {
USERPROFILE: root,
XDG_CONFIG_HOME: root,
XDG_CACHE_HOME: root,
...(scenario.logging ? { REA_LOG_LEVEL: "info" } : {}),
},
});
expect(result.json).toEqual(
scenario.fullOutput
? expect.objectContaining({
ok: true,
data: unsupportedHostOutput,
})
: unsupportedHostOutput,
);
expect.soft(result.exitCode).toBe(1);
expect(await readFile(source, "utf8")).toBe(bytes);
if (scenario.logging) {
expect(JSON.parse(result.stderr.trim())).toMatchObject({
command: "import-reference-source",
status: "error",
level: 50,
msg: "CLI command failed",
});
expect(result.json).toEqual(
fullOutput
? expect.objectContaining({
ok: true,
data: unsupportedHostOutput,
})
: unsupportedHostOutput,
);
expect.soft(result.exitCode).toBe(1);
expect(await readFile(source, "utf8")).toBe(bytes);
if (logging) {
expect(JSON.parse(result.stderr.trim())).toMatchObject({
command: "import-reference-source",
status: "error",
level: 50,
msg: "CLI command failed",
});
} else expect(result.stderr).toBe("");
},
);
}
} else expect(result.stderr).toBe("");
},
);
}
});
describe("compiled reference-source import preflight failures", () => {
for (const rootKind of ["missing", "regular-file"] as const) {
for (const fullOutput of [false, true]) {
for (const logging of [false, true]) {
cliTest(
`${rootKind} root, ${fullOutput ? "full-output" : "JSON"}, logging ${String(logging)}`,
async ({ cli }) => {
const directory = await createTestTempDirectory(
"rea-reference-cli-status-",
);
const root = join(directory, "input");
if (rootKind === "regular-file")
await writeFile(root, "inert input\n");
const result = await cli.run({
arguments: [
"import-reference-source",
root,
"--json",
...(fullOutput ? ["--full-output"] : []),
],
cwd: directory,
environment: {
HOME: directory,
USERPROFILE: directory,
XDG_CONFIG_HOME: directory,
XDG_CACHE_HOME: directory,
...(logging ? { REA_LOG_LEVEL: "info" } : {}),
},
});
if (fullOutput) {
// Incur's legacy wrapper remains unchanged; operation status is
// conveyed by the exit code and command log, not this `ok` field.
expect(result.json).toMatchObject({
ok: true,
data: invalidRootOutput,
});
expect(result.json).toHaveProperty("data", invalidRootOutput);
} else {
expect(result.json).toEqual(invalidRootOutput);
}
expect.soft(result.exitCode).toBe(1);
if (logging) {
const records: unknown[] = result.stderr
.trim()
.split("\n")
.map((line) => JSON.parse(line) as unknown);
expect(records).toEqual([
expect.objectContaining({
command: "import-reference-source",
status: "error",
level: 50,
msg: "CLI command failed",
}),
]);
} else {
expect(result.stderr).toBe("");
}
},
for (const scenario of [
{ rootKind: "missing", fullOutput: false, logging: false },
{ rootKind: "missing", fullOutput: true, logging: true },
{ rootKind: "regular-file", fullOutput: false, logging: true },
{ rootKind: "regular-file", fullOutput: true, logging: false },
] as const) {
cliTest(
`${scenario.rootKind} root, ${scenario.fullOutput ? "full-output" : "JSON"}, logging ${String(scenario.logging)}`,
async ({ cli }) => {
const directory = await createTestTempDirectory(
"rea-reference-cli-status-",
);
}
}
const root = join(directory, "input");
if (scenario.rootKind === "regular-file")
await writeFile(root, "inert input\n");
const result = await cli.run({
arguments: [
"import-reference-source",
root,
"--json",
...(scenario.fullOutput ? ["--full-output"] : []),
],
cwd: directory,
environment: {
HOME: directory,
USERPROFILE: directory,
XDG_CONFIG_HOME: directory,
XDG_CACHE_HOME: directory,
...(scenario.logging ? { REA_LOG_LEVEL: "info" } : {}),
},
});
if (scenario.fullOutput) {
// Incur's legacy wrapper remains unchanged; operation status is
// conveyed by the exit code and command log, not this `ok` field.
expect(result.json).toMatchObject({
ok: true,
data: invalidRootOutput,
});
} else {
expect(result.json).toEqual(invalidRootOutput);
}
expect.soft(result.exitCode).toBe(1);
if (scenario.logging) {
const records: unknown[] = result.stderr
.trim()
.split("\n")
.map((line) => JSON.parse(line) as unknown);
expect(records).toEqual([
expect.objectContaining({
command: "import-reference-source",
status: "error",
level: 50,
msg: "CLI command failed",
}),
]);
} else {
expect(result.stderr).toBe("");
}
},
);
}
});
@@ -29,48 +29,35 @@ const observe = async (path: string) => {
};
describe("artifact comparison summary", () => {
it.each([
{ removed: 1, added: 1 },
{ removed: 0, added: 1 },
{ removed: 0, added: 0 },
])(
"counts unchanged paths with $removed removals and $added additions",
async ({ removed, added }) => {
const root = await createTestTempDirectory("rea-artifact-summary-");
const leftPath = join(root, "left");
const rightPath = join(root, "right");
await Promise.all([mkdir(leftPath), mkdir(rightPath)]);
await Promise.all(
[leftPath, rightPath].flatMap((path) =>
["same.txt", "duplicate.txt"].map((name) =>
writeFile(join(path, name), "unchanged content"),
),
it("counts unchanged paths alongside additions and removals", async () => {
const root = await createTestTempDirectory("rea-artifact-summary-");
const leftPath = join(root, "left");
const rightPath = join(root, "right");
await Promise.all([mkdir(leftPath), mkdir(rightPath)]);
await Promise.all(
[leftPath, rightPath].flatMap((path) =>
["same.txt", "duplicate.txt"].map((name) =>
writeFile(join(path, name), "unchanged content"),
),
);
await Promise.all([
...Array.from({ length: removed }, (_, index) =>
writeFile(join(leftPath, `removed-${index}.txt`), "old content"),
),
...Array.from({ length: added }, (_, index) =>
writeFile(join(rightPath, `added-${index}.txt`), "new content"),
),
]);
const [left, right] = await Promise.all([
observe(leftPath),
observe(rightPath),
]);
const comparison = compareArtifacts(left, right);
const changed = removed + added > 0 ? 1 : 0;
expect(comparison.summary).toEqual({
unchanged: 3 - changed,
added,
removed,
changed,
unknown: 0,
contradiction: 0,
});
expect(comparison.changes).toHaveLength(removed + added + changed);
},
);
),
);
await Promise.all([
writeFile(join(leftPath, "removed.txt"), "old content"),
writeFile(join(rightPath, "added.txt"), "new content"),
]);
const [left, right] = await Promise.all([
observe(leftPath),
observe(rightPath),
]);
const comparison = compareArtifacts(left, right);
expect(comparison.summary).toEqual({
unchanged: 2,
added: 1,
removed: 1,
changed: 1,
unknown: 0,
contradiction: 0,
});
expect(comparison.changes).toHaveLength(3);
});
});
@@ -4,10 +4,8 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { classifyArtifactContent } from "../../../src/artifacts/inventory/ArtifactGraphConstruction.js";
import { classifyRoot } from "../../../src/artifacts/inventory/classify.js";
import { ARTIFACT_CLASSIFICATION_PREFIX_BYTES } from "../../../src/artifacts/ArtifactHash.js";
import { scanCanonicalArtifactInventory } from "../../../src/artifacts/inventory/scanCanonical.js";
import { targetFormatSchema } from "../../../src/contracts/toolOutputSchemaPrimitives.js";
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -31,11 +29,6 @@ describe("MZ artifact classification", () => {
expect(classifyArtifactContent("bad.exe", Buffer.from("MZbroken"))).toEqual(
{ kind: "unknown", format: "unknown" },
);
expect(classifyArtifactContent("NATIVE.EXE", peFixture(128))).toMatchObject(
{
format: "pe",
},
);
const distantPe = peFixture(ARTIFACT_CLASSIFICATION_PREFIX_BYTES + 64);
expect(
classifyArtifactContent(
@@ -56,7 +49,6 @@ describe("MZ artifact classification", () => {
distantRelocations.length,
).format,
).toBe("unknown");
expect(targetFormatSchema.parse("dos-mz")).toBe("dos-mz");
});
it("keeps standalone and embedded DOS identities through inventory", async () => {
@@ -65,8 +57,6 @@ describe("MZ artifact classification", () => {
const pePath = join(directory, "NATIVE.EXE");
await writeFile(dosPath, dosFixture());
await writeFile(pePath, peFixture(128));
expect(await classifyRoot(dosPath, false)).toBe("dos-mz");
expect(await classifyRoot(pePath, false)).toBe("pe");
const standalone = artifactInventoryResultSchema.parse(
await scanCanonicalArtifactInventory(dosPath),
);
@@ -14,6 +14,10 @@ import {
configureClientConfiguration,
inspectClientConfiguration,
} from "../../../src/application/SetupClientConfiguration.js";
import {
runUninstall,
systemUninstallHost,
} from "../../../src/application/Uninstall.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -114,3 +118,38 @@ it("rejects malformed BOM-prefixed JSON without writing or backing it up", async
expect(await readFile(configPath)).toEqual(Buffer.from(original));
expect(await readdir(home)).toEqual(["mcp.json"]);
});
it("restores original BOM-prefixed JSONC bytes after update and removal", async () => {
const home = await createTestTempDirectory("rea-client-config-bom-cycle-");
const client = supportedClients(home, "linux", {}).find(
(candidate) => candidate.name === "cursor",
);
if (client?.format !== "json") throw new Error("missing Cursor JSON config");
const serversKey = clientConfigurationServersKey(client.format);
const prefix = `\uFEFF{\r\n // Keep prefix \uFEFF comment.\r\n "label": "left\uFEFFright",\r\n "${serversKey}": {\r\n // Keep this unrelated registration.\r\n "other": {\r\n "command": "other\uFEFFtool"\r\n },\r\n`;
const suffix = ` },\r\n // Keep suffix comment.\r\n "theme": "dark"\r\n}\r\n`;
const original = `${prefix}${suffix}`;
await mkdir(dirname(client.configPath), { recursive: true });
await writeFile(client.configPath, original);
const firstCommand = [join(home, "bin", "rea"), "mcp"];
const updatedCommand = [join(home, "updated", "bin", "rea"), "mcp"];
expect(
await configureClientConfiguration(client, {}, firstCommand),
).toMatchObject({ status: "configured" });
expect(
await configureClientConfiguration(client, {}, updatedCommand),
).toMatchObject({ status: "configured" });
const updated = await readFile(client.configPath, "utf8");
expect(parseClientConfiguration(updated, client.format).servers).toEqual({
other: { command: "other\uFEFFtool" },
rea: clientRegistrationEntry(client.format, updatedCommand, {}),
});
const result = await runUninstall(false, systemUninstallHost(home));
expect(result.status).toBe("complete");
expect(result.items).toContainEqual(
expect.objectContaining({ name: "cursor", status: "removed" }),
);
expect(await readFile(client.configPath, "utf8")).toBe(original);
});
@@ -39,8 +39,6 @@ it("publishes valid SDK schemas and records the named module trace without a bin
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
const args = { manifest_path: "/analysis/manifest.json", script_index: 0 };
expect(ajv.validate(inputSchema, args)).toBe(true);
const response = await client.callTool({
@@ -45,8 +45,6 @@ it("publishes valid SDK schemas and records the named module trace without a bin
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
expect(ajv.validate(inputSchema, args)).toBe(true);
const response = await client.callTool({
name: "trace_web_source_location",
@@ -37,7 +37,6 @@ describe("development test selection", () => {
]),
);
expect(plan.needsBuild).toBe(true);
expect(plan.vitestArguments).not.toContain("--passWithNoTests");
expect(plan.vitestArguments).not.toContain("--changed");
});