Files
rea/tests/boundary/firmware/firmwareReportFiles.test.ts
T
2947a77507 fix(engines): keep format ceilings fixed and recover the EVMole deadline (#6088)
* fix(engines): keep format ceilings fixed and recover the EVMole deadline

Promote the owned EVMole worker wall clock to timeout_ms and name it in
recovery. Fixed CPU, byte, and address-space refusals in this theme say
the adjustable deadline does not raise them.

Refs #5910

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(engines): recover the allowance that refused the request

Model capacity recovery follows a structured limit instead of message text,
and the shared header ceiling no longer names Safetensors. Firmware signal
errors keep captured tool output, oversized reports do not suggest an
intermittent rerun, and NativeAOT hydration budget exhaustion explains the
fixed parser allowance on the partial report.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-10-22 03:06:59 +08:00

164 lines
5.8 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import {
inventoryFirmwareOutput,
FirmwareFileAccess,
} from "../../../src/firmware/FirmwareFiles.js";
import { readFirmwareCommandReport } from "../../../src/firmware/FirmwareCommand.js";
import { FIRMWARE_LIMITS } from "../../../src/firmware/FirmwareRelease.js";
import { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
it.each([
["invalid leading byte", [0xff]],
["incomplete sequence", [0xe2, 0x82]],
["overlong sequence", [0xc0, 0xaf]],
["encoded surrogate", [0xed, 0xa0, 0x80]],
])(
"rejects complete JSON containing %s without repairing producer bytes",
async (_name, bytes) => {
const root = await createTestTempDirectory("rea-firmware-report-");
const path = join(root, "report.json");
await writeFile(
path,
Buffer.concat([
Buffer.from('{"description":"'),
Buffer.from(bytes),
Buffer.from('"}'),
]),
);
await expect(
new FirmwareFileAccess().report(path, "inspect_firmware_regions"),
).rejects.toMatchObject({
_tag: "AnalysisOutputError",
reason: "Tool report is not valid complete JSON",
});
},
);
it.each([
["missing", "Report was not created", async () => {}],
["empty", "Report is empty", (path: string) => writeFile(path, "")],
[
"invalid_json",
"Tool report is not valid complete JSON",
(path: string) => writeFile(path, '[{"Analysis":'),
],
[
"oversized",
`Report exceeded ${FIRMWARE_LIMITS.reportBytes} bytes`,
(path: string) =>
writeFile(path, Buffer.alloc(FIRMWARE_LIMITS.reportBytes + 1, 0x20)),
],
[
"not_regular_file",
"Report is not a regular file",
(path: string) => mkdir(path),
],
] as const)(
"keeps report state %s after a successful exit distinct from an empty scan",
async (state, reason, prepare) => {
const root = await createTestTempDirectory("rea-firmware-report-");
const path = join(root, "report.json");
await prepare(path);
const failure: unknown = await readFirmwareCommandReport({
path,
engine: "binwalk",
operation: "inspect_firmware_regions",
execution: {
stdout: { text: "", bytes: 0, observedBytes: 0 },
stderr: { text: "", bytes: 0, observedBytes: 0 },
exit_code: 0,
signal: null,
command: "/opt/binwalk",
args: ["--quiet", "--log", path, "/work/firmware.bin"],
resource_limiter: "/usr/bin/prlimit",
},
files: new FirmwareFileAccess(),
}).catch((cause: unknown) => cause);
if (!(failure instanceof AnalysisError))
throw new Error("Expected a typed report failure");
const projected = projectAnalysisError(failure);
expect(projected).toMatchObject({
code: "unreadable_output",
retryable: false,
message: expect.stringContaining("not an empty or complete result"),
details: {
reason: `binwalk exited 0 without a usable report: ${reason}`,
diagnostics: {
report_state: state,
report_path: path,
command: "/opt/binwalk",
args: ["--quiet", "--log", path, "/work/firmware.bin"],
resource_limiter: "/usr/bin/prlimit",
exit_code: 0,
signal: null,
stdout_bytes: 0,
stderr_bytes: 0,
},
},
});
// Generic doctor advice is not presented as a repair for a missing report.
expect(projected.message).not.toContain("rea doctor");
// Recorded arguments name a removed private workspace; recovery must not
// present them as a replayable command.
expect(projected.remediation.action).toContain("diagnostics.report_state");
expect(projected.remediation.action).toContain("cannot be replayed as-is");
if (state === "oversized") {
expect(projected.remediation.action).toContain("no runtime override");
expect(projected.remediation.action).toContain(
"Repeating the unchanged request fails the same way",
);
expect(projected.remediation.action).not.toContain(
"Rerun the same REA request",
);
expect(projected.message).toContain("no runtime override");
expect(projected.message).not.toContain("Rerun the same REA request");
} else {
expect(projected.remediation.action).toContain(
"replacing every private path",
);
expect(projected.remediation.action).toContain(
"Rerun the same REA request",
);
expect(projected.remediation.action).not.toContain(
"Repeating the unchanged request fails the same way",
);
}
},
);
it("preserves valid Unicode and a literal replacement character in producer JSON", async () => {
const root = await createTestTempDirectory("rea-firmware-report-");
const path = join(root, "report.json");
const report = { description: "固件 🧩 \ufffd", path: "/输入/firmware.bin" };
await writeFile(path, JSON.stringify(report));
expect(
await new FirmwareFileAccess().report(path, "inspect_firmware_regions"),
).toEqual(report);
});
it("orders extracted firmware paths by code point independently of host locale", async () => {
const root = await createTestTempDirectory("rea-firmware-inventory-");
await mkdir(join(root, "Lib"));
for (const path of ["a.bin", "z.bin", "\u00e4.bin", "B.bin", "Lib/x.bin"])
await writeFile(join(root, path), "");
const entries = await inventoryFirmwareOutput(
root,
1024,
16,
"inspect_firmware_regions",
);
// Locale collation would interleave case and place "ä" by language rules.
expect(entries.map(({ relativePath }) => relativePath)).toEqual([
"B.bin",
"Lib/x.bin",
"a.bin",
"z.bin",
"\u00e4.bin",
]);
});