Files
reverse-engineering-anything/src/artifacts/ArtifactOperationFailure.test.ts
T
morluto 7134053d27 feat(artifacts): name integrity checks for DMG, directory and release-exposure readers (#11056)
* feat(artifacts): name integrity checks for DMG, directory and release-exposure readers

Directory members and release-exposure members whose file identity or
bytes changed since inventory report member-identity; a directory entry
without captured state reports entry-metadata and a changed directory root
container-identity; an hdiutil checksum failure reports image-checksum.
AnalysisArtifactChangedError carries the check from the reader failure and
projects it as details.integrity_check.

* fix(artifacts): name missing directory entry state and keep integrity checks through error copies

* feat(artifacts): name integrity checks for ZIP, PyInstaller and changed release files (#11081)

feat(artifacts): name integrity checks for ZIP, PyInstaller and changed release files, and keep them through reader projections

* feat(artifacts): name integrity checks for the remaining readers and their projections (#11098)
2026-11-15 20:35:28 +08:00

252 lines
7.9 KiB
TypeScript

import { expect, it } from "vitest";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
import { translateArtifactFailure } from "./ArtifactOperationFailure.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { artifactReaderLimitFailure } from "./ArtifactReaderLimits.js";
import { constants as bufferConstants } from "node:buffer";
import {
imageFileSizeRemediation,
readerIntegrityFailure,
} from "./StableArtifactFailure.js";
import { artifactIntegrityDetails } from "../domain/artifactIntegrity.js";
import { ArtifactFileCleanupFailure } from "./ArtifactFileOperation.js";
import { AnalysisCleanupLease } from "./AnalysisCleanupLease.js";
import { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js";
// The provider ceiling test uses a sparse file and is skipped on Windows.
it.skipIf(process.platform !== "win32")(
"names the fixed image ceiling when the selected budget is already maximal",
() => {
expect(imageFileSizeRemediation(1024, 1024)).toContain(
"no runtime override",
);
},
);
it.each([
{ effective: 1, required: bufferConstants.MAX_STRING_LENGTH + 1 },
{ effective: bufferConstants.MAX_STRING_LENGTH, required: undefined },
])(
"does not recommend a larger budget when no supported increase can repair it: %o",
({ effective, required }) => {
const failure = artifactReaderLimitFailure(
"artifact_limits.godot.directory_bytes",
effective,
"bytes",
required,
);
const projected = projectAnalysisError(
translateArtifactFailure("inspect_artifact", failure),
);
expect(projected).toMatchObject({
retryable: false,
details: {
reader_limit: {
can_increase: false,
maximum: bufferConstants.MAX_STRING_LENGTH,
},
},
});
expect(projected.remediation.action).toContain("No supported increase");
},
);
it("preserves adjustable reader admission facts and gives cleanup priority", () => {
const failure = artifactReaderLimitFailure(
"artifact_limits.node_sea.argument_count",
1,
"count",
2,
);
const adjustable = projectAnalysisError(
translateArtifactFailure("inspect_artifact", failure),
);
expect(adjustable).toMatchObject({
code: "truncated",
retryable: true,
details: {
reader_limit: {
parameter: "artifact_limits.node_sea.argument_count",
effective_value: 1,
required_value: 2,
unit: "count",
applies_at: "next_operation",
update_mode: "parameter",
pending_restart: false,
},
},
});
expect(adjustable.remediation.action).toContain(
"retry the same complete artifact",
);
const wrapped = ArtifactReaderFailure.withCleanup(failure, {
reason: "Reader descriptor release failed",
resources: ["owned input descriptor"],
});
const projected = projectAnalysisError(
translateArtifactFailure("inspect_artifact", wrapped),
);
expect(projected.retryable).toBe(false);
expect(projected.details).toMatchObject({
reader_limit: adjustable.details?.reader_limit,
cleanup: "incomplete",
resources: ["owned input descriptor"],
});
});
it.each(["capacity", "file-size", "memory"] as const)(
"retains the producing %s limit through public error projection",
(boundary) => {
const failure = new ArtifactReaderFailure(
"limit",
"The specific reader allowance was exceeded",
{ boundary },
);
const result = projectAnalysisError(
translateArtifactFailure("inventory_artifact", failure),
);
expect(result).toMatchObject({
code: "truncated",
retryable: false,
message: failure.message,
details: { boundary, detail: failure.message },
});
const wrapped = ArtifactReaderFailure.withCleanup(failure, {
reason: "close failed",
resources: ["handle"],
});
expect(
projectAnalysisError(
translateArtifactFailure("inventory_artifact", wrapped),
),
).toMatchObject({
code: "cleanup_incomplete",
details: { boundary, cleanup: "incomplete", resources: ["handle"] },
});
},
);
it("keeps producer-owned recovery through cleanup wrapping and public projection", () => {
const userMessage = "Caller-selected reader recovery";
const failure = new ArtifactReaderFailure(
"unavailable",
"Exact command diagnostic",
{
userMessage,
},
);
const wrapped = ArtifactReaderFailure.withCleanup(failure, {
reason: "Owned staging cleanup needs retry",
resources: ["owned staging"],
});
const projected = projectAnalysisError(
translateArtifactFailure("inspect_artifact", wrapped),
);
expect(projected.message).toBe(userMessage);
expect(projected.remediation.action).toBe(userMessage);
expect(projected.details?.detail).toBe("Exact command diagnostic");
expect(projected.details).toMatchObject({
cleanup: "incomplete",
cleanup_reason: "Owned staging cleanup needs retry",
resources: ["owned staging"],
});
});
it("names the failed integrity check through cleanup wrapping and public projection", () => {
const failure = new ArtifactReaderFailure(
"integrity",
"ASAR container changed before read: main.js",
{ integrityCheck: "container-identity" },
);
expect(
projectAnalysisError(
translateArtifactFailure("inventory_artifact", failure),
),
).toMatchObject({
details: { reason: "integrity", integrity_check: "container-identity" },
});
const wrapped = ArtifactReaderFailure.withCleanup(failure, {
reason: "close failed",
resources: ["handle"],
});
expect(
projectAnalysisError(
translateArtifactFailure("inventory_artifact", wrapped),
),
).toMatchObject({
code: "cleanup_incomplete",
details: {
integrity_check: "container-identity",
cleanup: "incomplete",
resources: ["handle"],
},
});
const unpacked = new ArtifactReaderFailure(
"integrity",
"ASAR unpacked entry changed before read: main.js",
{ integrityCheck: "unpacked-entry-identity" },
artifactIntegrityDetails("main.js", null, null, true),
);
expect(
projectAnalysisError(
translateArtifactFailure("extract_artifact", unpacked),
),
).toMatchObject({
details: {
logical_path: "main.js",
unpacked: true,
integrity_check: "unpacked-entry-identity",
},
});
});
it("keeps the named integrity check when a descriptor close failure rebuilds the reader failure", () => {
const failure = new ArtifactFileCleanupFailure(
new RegularFileCleanupFailure({
path: "/artifact.asar",
owner: { close: async () => undefined } as never,
outcome: {
kind: "failed",
cause: new ArtifactReaderFailure(
"integrity",
"ASAR read coverage was short: main.js",
{ integrityCheck: "read-coverage" },
),
},
cleanupCause: new Error("close failed"),
}),
new AnalysisCleanupLease(async () => undefined),
);
expect(failure.integrityCheck).toBe("read-coverage");
expect(
projectAnalysisError(translateArtifactFailure("extract_artifact", failure)),
).toMatchObject({
code: "cleanup_incomplete",
details: { integrity_check: "read-coverage", cleanup: "incomplete" },
});
});
it("keeps a reader's named check when a provider supplies its own cause and options", () => {
const failure = new ArtifactReaderFailure("integrity", "region changed", {
integrityCheck: "reconstruction",
});
const wrapper = new Error("cleanup wrapper", { cause: failure });
const capturedOutput = { stdout: "out", stderr: "", truncated: false };
const changed = readerIntegrityFailure("inspect_gpu", "/target", failure, {
cause: wrapper,
capturedOutput,
});
expect(changed).toMatchObject({
cause: wrapper,
capturedOutput,
reason: "region changed",
integrityCheck: "reconstruction",
});
expect(projectAnalysisError(changed)).toMatchObject({
code: "artifact_changed",
details: { integrity_check: "reconstruction" },
});
});