Files
reverse-engineering-anything/tests/acceptance/androidSparseImageCli.test.ts
T
morluto a5bf175e06 fix(artifacts)!: report reader admission limits as resource_constraint (#15588)
* fix(artifacts)!: report reader admission limits as resource_constraint

An artifact read refused by a reader limit (artifact_limits.*) or a
release-fixed allowance was code partial, which the error contract
reserves for a result cut short by a delivery or decode limit. The
read was refused at admission, so it is now resource_constraint, like
every other admission limit. The retry class is unchanged: change_first
when the limit can be raised, never when it cannot. A limit without
admission facts, such as a parser budget, stays partial.

The reader's own recovery, which names the parameter, its supported
range and the next step, and the reader_limit fact are unchanged.

Refs #15323

BREAKING CHANGE: artifact refusals with details.reason "limit" and a
details.reader_limit or fixed allowance report code and category
resource_constraint instead of partial. Match resource_constraint, or
read details.reason and details.reader_limit, which are unchanged.

* fix(errors): catalogue each artifact admission limit with the retry it returns

The catalogue listed one row for reader limits and fixed allowances with
change_first, though a fixed allowance and a reader limit already at its
maximum return never, and the release-fixed row still said partial.
List the raisable reader limit, the reader limit at its maximum and the
release-fixed allowance as separate rows, and test each row against the
error it projects.
2026-12-01 17:10:56 -05:00

160 lines
5.5 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { cliTest } from "../support/cli/cliFixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js";
import { artifactExtractionResultSchema } from "../../src/domain/artifactGraph.js";
import {
MIXED_ANDROID_SPARSE_EXPANDED,
buildAndroidSparseImage,
mixedAndroidSparseImage,
} from "../../src/domain/firmware/androidSparseImage.fixture.js";
const sparseCoverageCases = [
{
label: "without trailing bytes",
trailing: Buffer.alloc(0),
coverage: "complete",
},
{
label: "with trailing bytes",
trailing: mixedAndroidSparseImage(),
coverage: "unknown",
},
] as const;
cliTest.for(sparseCoverageCases)(
"inspects an Android sparse image $label through the compiled CLI",
async ({ trailing, coverage }, { cli }) => {
const root = await createTestTempDirectory("rea-android-sparse-cli-");
const source = join(root, "system.img");
const bytes = Buffer.concat([mixedAndroidSparseImage(), trailing]);
await writeFile(source, bytes);
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
const inspection = artifactInspectionResultSchema.parse(
parseEvidence(inspected.json).normalized_result,
);
const graph = inspection.substeps[0]!.evidence.normalized_result;
expect(graph.manifest.root_format).toBe("android-sparse-image");
expect(graph.coverage.status).toBe(coverage);
expect(
graph.occurrences.find((row) => row.logical_path === ".")
?.format_metadata,
).toMatchObject({
kind: "android-sparse-image",
block_size: 4,
expanded_size: 16,
trailing_bytes: trailing.length,
checksums_verified: false,
chunk_counts: { raw: 1, fill: 1, dont_care: 1, crc32: 1 },
});
expect((await readFile(source)).equals(bytes)).toBe(true);
},
);
cliTest.for(sparseCoverageCases)(
"extracts an Android sparse image $label through the compiled CLI",
async ({ trailing, coverage }, { cli }) => {
const root = await createTestTempDirectory("rea-android-sparse-cli-");
const source = join(root, "system.img");
const bytes = Buffer.concat([mixedAndroidSparseImage(), trailing]);
await writeFile(source, bytes);
const extracted = await cli.run({
arguments: ["extract-artifact", source, "--json"],
environment: { TMPDIR: root, TMP: root, TEMP: root },
});
expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
const result = artifactExtractionResultSchema.parse(
parseEvidence(extracted.json).normalized_result,
);
expect(result.containment_verified).toBe(true);
expect(result.coverage.status).toBe(coverage);
expect(
(await readFile(join(result.output_root, "image.raw"))).equals(
MIXED_ANDROID_SPARSE_EXPANDED,
),
).toBe(true);
expect((await readFile(source)).equals(bytes)).toBe(true);
},
);
cliTest(
"refuses a sparse image whose chunks do not add up to total_blks",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-android-sparse-cli-bad-");
const source = join(root, "system.img");
await writeFile(
source,
buildAndroidSparseImage({
blockSize: 4,
totalBlocks: 3,
chunks: [{ type: "dont-care", blocks: 2 }],
}),
);
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode).toBe(1);
expect(`${inspected.stdout}\n${inspected.stderr}`).toContain("total_blks");
},
);
cliTest(
"refuses a chunk table above the selected budget and admits it on retry through the compiled CLI",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-android-sparse-cli-");
const source = join(root, "system.img");
const bytes = mixedAndroidSparseImage();
await writeFile(source, bytes);
const command = ["inspect-artifact", source, "--json"];
const refused = await cli.run({
arguments: [
...command,
"--artifact-limits",
'{"android_sparse_image":{"chunk_count":3}}',
],
});
expect(refused.exitCode).toBe(1);
expect(refused.json).toMatchObject({
code: "resource_constraint",
retryable: false,
details: {
reader_limit: {
parameter: "artifact_limits.android_sparse_image.chunk_count",
effective_value: 3,
required_value: 4,
can_increase: true,
update_mode: "parameter",
},
},
});
const recovered = await cli.run({
arguments: [
...command,
"--artifact-limits",
'{"android_sparse_image":{"chunk_count":4}}',
],
});
expect(recovered.exitCode, recovered.stdout + recovered.stderr).toBe(0);
const evidence = parseEvidence(recovered.json);
const inventory = artifactInspectionResultSchema.parse(
evidence.normalized_result,
).substeps[0]!.evidence.normalized_result;
expect(inventory.effective_limits).toMatchObject({
android_sparse_image: { chunk_count: 4 },
});
expect(
inventory.occurrences.find((row) => row.logical_path === ".")
?.format_metadata,
).toMatchObject({ total_chunks: 4 });
expect((await readFile(source)).equals(bytes)).toBe(true);
},
);