Files
reverse-engineering-anything/tests/acceptance/godotPck.test.ts
T
morluto b138f52e12 refactor: prune internal seams and preserve configuration ownership (#10430)
* refactor: prune internal seams and preserve configuration ownership

* fix(setup): keep YAML aliases and inherited registration settings on refresh

- Reuse an unchanged YAML collection only while every alias in it still
  resolves after the edit; otherwise rebuild it so the alias becomes its
  value. A sibling anchor replaced or moved in the same refresh no longer
  leaves an unresolved alias in Hermes or Goose output.
- Keep a connection selector whose existing value already chooses stdio
  (for example an inherited Hermes transport: stdio) instead of superseding
  it; remote selectors are still replaced. Explicit stdio selectors are
  therefore no longer rewritten (Kimi expectation updated).
- When the whole Hermes registration is inherited through a merge,
  override it with that merge and write only the changed fields.

* fix(setup): edit registration fields whose YAML key is an alias

A key written as an alias to a string scalar (for example *field with
field: &field env) was treated as a source-only key, so any change to
its value was refused in Hermes and Goose. Resolve such keys to their
string, keep the aliased spelling while its anchor survives the edit,
and write the plain key when the same edit rebuilds the anchor.

* test(process): bound cleanup-boundary waits and always release gates

Both cancellation tests awaited a bare started promise and released
their gates only on success, so a missed boundary waited for the outer
deadline and could leave cleanup parked. Use awaitAcknowledgement and
release and join the cleanup in finally.

* fix(setup): repair only aliases an edit leaves unresolved

Replace the pre-edit alias survival guess, which expanded aliases to
sibling anchors that survive unchanged, with an exact repair: record
what each alias in the replaced value resolves to, write the edit, then
replace only aliases left without a preceding anchor by that value.

- Keep an aliased key's source position, so its anchor stays ahead of
  later aliases.
- Treat an aliased key as its string only when no other key in the map
  resolves to the same string; an ambiguous one stays guarded.
- Override an inherited Hermes registration only when the requested
  entry keeps every inherited field, so an explicit replacement can drop
  remote selectors.

* fix(setup): repair aliases whose binding an edit changes, keeping comments

An alias whose own anchor was removed could resolve to an earlier anchor
with the same name and silently change value. Record the node each alias
resolved to and repair any alias that no longer resolves to that node.
Carry the alias's comments and spacing onto the repaired value.

* test(setup): split the Hermes anchor and merge refresh cases

* fix(setup): refuse removing an overridden key its merge would restore

The merge-retention check skipped keys the mapping also overrides, so
dropping such a key (for example a remote url during an explicit stdio
replacement) let the retained merge restore it. Check removal before
skipping overridden keys, so the edit is refused with the existing
message instead of producing a registration that cannot be applied.
2026-11-14 04:08:37 -05:00

427 lines
14 KiB
TypeScript

import { createHash } from "node:crypto";
import { readFile, readdir, unlink, 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 {
buildAliasedGodotPck,
buildGodotPck,
buildGodotPck1,
pckU64,
} from "../fixtures/godotPck.js";
import { elf } from "../../src/domain/binaryTarget.fixture.js";
import { readEvidenceBundle } from "../../src/application/EvidenceBundleFiles.js";
import { analysisViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js";
import {
artifactExtractionResultSchema,
artifactInventoryResultSchema,
} from "../../src/domain/artifactGraph.js";
const members = [
{ path: "res://scripts/main.gd", data: "extends Node\n" },
{ path: "res://data/玩家.txt", data: "玩家数据\n" },
];
const graphFrom = (reply: unknown) => {
const evidence = parseEvidence(reply);
const inspection = artifactInspectionResultSchema.parse(
evidence.normalized_result,
);
return artifactInventoryResultSchema.parse(
inspection.substeps[0]?.evidence.normalized_result,
);
};
cliTest.for([1, 2, 3, 4] as const)(
"inspects and safely extracts format %s PCK through the compiled CLI",
async (version, { cli }) => {
const root = await createTestTempDirectory("rea-pck-cli-");
const source = join(root, "game.pck");
await writeFile(
source,
version === 1 ? buildGodotPck1(members) : buildGodotPck(version, members),
);
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
const graph = graphFrom(inspected.json);
expect(graph.coverage).toEqual({
scope: "container-entries",
status: "complete",
});
const rootRow = graph.occurrences.find((row) => row.logical_path === ".");
expect(rootRow?.format_metadata).toMatchObject({
kind: "godot-pck",
format_version: version,
declared_file_count: 2,
});
for (const [index, member] of members.entries()) {
const row = graph.occurrences.find(
(row) => row.logical_path === member.path.slice(6),
);
expect(row?.declared_integrity).toEqual({
algorithm: "md5",
digest: createHash("md5").update(member.data).digest("hex"),
});
expect(row?.format_metadata).toMatchObject({
index,
source_path: member.path,
flags: 0,
});
expect(row?.source_path).toBe(member.path);
expect(row?.hash_status).toBe("verified");
}
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);
for (const member of members)
expect(
await readFile(join(result.output_root, member.path.slice(6)), "utf8"),
).toBe(member.data);
},
);
cliTest.for([1, 4] as const)(
"reads an embedded format %s pack while preserving the native carrier's content classification",
async (version, { cli }) => {
const root = await createTestTempDirectory("rea-pck-embedded-cli-");
const source = join(root, "game.x86_64");
const carrier = elf(2, 1, 62);
const pack =
version === 1
? buildGodotPck1(members, { carrierBytes: carrier.length })
: buildGodotPck(4, members);
const bytes = Buffer.concat([
carrier,
pack,
pckU64(pack.length),
Buffer.from("GDPC"),
]);
await writeFile(source, bytes);
const reply = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(reply.exitCode, reply.stdout + reply.stderr).toBe(0);
const graph = graphFrom(reply.json);
expect(graph.manifest.root_sha256).toBe(
createHash("sha256").update(bytes).digest("hex"),
);
expect(
graph.nodes.find(
(row) => row.artifact_id === graph.manifest.root_artifact_id,
)?.format,
).toBe("elf");
expect(
graph.occurrences.find((row) => row.logical_path === ".")
?.format_metadata,
).toMatchObject({
embedded: true,
pack_start: carrier.length,
pack_end: carrier.length + pack.length,
});
},
);
cliTest(
"keeps native inventory when an incidental GDPC trailer does not point at a pack header",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pck-marker-candidate-");
const carrier = elf(2, 1, 62);
const candidates = [
Buffer.concat([carrier, Buffer.from("12345678ABCD", "ascii")]),
Buffer.concat([carrier, Buffer.from("12345678GDPC", "ascii")]),
Buffer.concat([
carrier,
Buffer.from("notpack!", "ascii"),
pckU64(8),
Buffer.from("GDPC", "ascii"),
]),
];
for (const [index, bytes] of candidates.entries()) {
const source = join(root, `candidate-${String(index)}.x86_64`);
await writeFile(source, bytes);
const reply = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(reply.exitCode, reply.stdout + reply.stderr).toBe(0);
const graph = graphFrom(reply.json);
expect(graph.manifest.root_format).toBe("elf");
expect(
graph.nodes.find(
(row) => row.artifact_id === graph.manifest.root_artifact_id,
)?.format,
).toBe("elf");
expect(graph.occurrences).toHaveLength(1);
}
},
);
cliTest(
"refuses an unsupported version after an embedded trailer points at a PCK header",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pck-confirmed-malformed-");
const source = join(root, "unsupported.x86_64");
const pack = buildGodotPck(4, members);
pack.writeUInt32LE(5, 4);
await writeFile(
source,
Buffer.concat([
elf(2, 1, 62),
pack,
pckU64(pack.length),
Buffer.from("GDPC", "ascii"),
]),
);
const reply = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(reply.exitCode).toBe(1);
expect(reply.json).toMatchObject({
code: "artifact_operation_failed",
details: {
reason: "unavailable",
detail: expect.stringContaining("Godot PCK format 5 is unsupported"),
},
});
},
);
cliTest(
"applies strict and record-and-continue MD5 integrity to inspection and extraction",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pck-integrity-cli-");
const source = join(root, "contradiction.pck");
await writeFile(
source,
buildGodotPck(4, [
{ path: "res://main.gd", data: "real", md5: Buffer.alloc(16, 1) },
]),
);
const rejected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(rejected.exitCode).toBe(1);
expect(rejected.json).toMatchObject({
code: "artifact_integrity_mismatch",
details: {
algorithm: "md5",
logical_path: "main.gd",
declared_md5: "01".repeat(16),
calculated_md5: createHash("md5").update("real").digest("hex"),
},
});
const inspected = await cli.run({
arguments: [
"inspect-artifact",
source,
"--integrity-policy",
"record-and-continue",
"--json",
],
});
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
const graph = graphFrom(inspected.json);
expect(graph.integrity_contradictions).toEqual([
expect.objectContaining({
algorithm: "md5",
logical_path: "main.gd",
declared_md5: "01".repeat(16),
observed_md5: createHash("md5").update("real").digest("hex"),
trust: "observed-untrusted",
}),
]);
expect(
graph.occurrences.find((row) => row.logical_path === "main.gd")
?.hash_status,
).toBe("mismatched");
const extracted = await cli.run({
arguments: [
"extract-artifact",
source,
"--integrity-policy",
"record-and-continue",
"--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.integrity_contradictions).toHaveLength(1);
expect(await readFile(join(result.output_root, "main.gd"), "utf8")).toBe(
"real",
);
},
);
cliTest(
"retains encrypted-directory declarations without claiming an empty package",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pck-encrypted-cli-");
const source = join(root, "encrypted.pck");
const bytes = buildGodotPck(4, members, { packFlags: 3 });
const directory = Number(bytes.readBigUInt64LE(32));
bytes.fill(0xff, directory + 4);
await writeFile(source, bytes);
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
const graph = graphFrom(inspected.json);
expect(graph.coverage).toEqual({
scope: "container-entries",
status: "unknown",
});
expect(graph.occurrences).toHaveLength(1);
expect(graph.occurrences[0]).toMatchObject({
encrypted: true,
format_metadata: {
directory_status: "encrypted",
declared_file_count: 2,
},
});
const extracted = await cli.run({
arguments: ["extract-artifact", source, "--json"],
environment: { TMPDIR: root, TMP: root, TEMP: root },
});
expect(extracted.exitCode).toBe(1);
},
);
cliTest(
"exports compact CLI delivery and queries its retained complete Evidence after unlink",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pck-export-");
const source = join(root, "game.pck");
const bundle = join(root, "evidence.json");
const refused = await cli.run({
arguments: ["inspect-artifact", source, "--detail", "summary", "--json"],
});
expect(refused.exitCode).toBe(1);
expect(refused.json).toMatchObject({
code: "invalid_request",
details: { issues: [{ path: ["evidence_output"] }] },
});
await writeFile(source, buildGodotPck(4, members));
const args = [
"inspect-artifact",
source,
"--detail",
"summary",
"--evidence_output",
bundle,
"--json",
];
const reply = await cli.run({ arguments: args });
expect(reply.exitCode, reply.stdout + reply.stderr).toBe(0);
expect(reply.json).toMatchObject({
evidence_bundle: { path: bundle, bytes: expect.any(Number) },
summary: {
normalized_result: {
kind: "summary",
summary: { root_format_metadata: { format_version: 4 } },
},
},
});
const saved = await readEvidenceBundle(bundle);
if (!saved.ok) throw saved.error;
const evidence = saved.value.records.find(
(record) => record.operation === "inspect_artifact",
);
if (evidence === undefined) throw new Error("Missing complete inspection");
expect(graphFrom(evidence).occurrences).toHaveLength(3);
const inspection = artifactInspectionResultSchema.parse(
evidence.normalized_result,
);
expect(saved.value.records).toContainEqual(
inspection.substeps[0]?.evidence,
);
const before = await readFile(bundle);
expect((await cli.run({ arguments: args })).exitCode).toBe(1);
expect(Buffer.compare(await readFile(bundle), before)).toBe(0);
await unlink(source);
const query = join(root, "query.json");
await writeFile(
query,
JSON.stringify({
source: { kind: "inline", evidence },
view: {
kind: "inventory",
collection: "occurrences",
logical_paths: ["scripts/main.gd"],
fields: ["logical_path"],
},
}),
);
const selected = await cli.run({
arguments: ["inspect-analysis-view", query, "--json"],
});
expect(selected.exitCode, selected.stdout + selected.stderr).toBe(0);
const view = analysisViewResultSchema.parse(
parseEvidence(selected.json).normalized_result,
);
if (view.kind !== "inventory") throw new Error("Expected inventory view");
expect(view.parent_evidence_id).toBe(evidence.evidence_id);
expect(view.items).toEqual([
expect.objectContaining({
logical_path: "scripts/main.gd",
occurrence_id: expect.any(String),
artifact_id: expect.any(String),
parent_occurrence_id: expect.any(String),
}),
]);
},
);
cliTest(
"rejects repeated-range expansion without creating extraction output",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pck-expansion-");
const source = join(root, "aliased.pck");
await writeFile(source, buildAliasedGodotPck(4, 1100));
for (const operation of ["inspect-artifact", "extract-artifact"]) {
for (const policy of ["fail", "record-and-continue"]) {
const reply = await cli.run({
arguments: [
operation,
source,
"--integrity-policy",
policy,
"--json",
],
environment: { TMPDIR: root, TMP: root, TEMP: root },
});
expect(reply.exitCode, reply.stdout + reply.stderr).toBe(1);
expect(reply.json).toMatchObject({
code: "truncated",
retryable: true,
details: {
reason: "limit",
truncated: true,
reader_limit: {
parameter: "artifact_limits.expanded_bytes",
effective_value: 268_435_456,
required_value: 268_697_600,
unit: "bytes",
can_increase: true,
},
},
});
expect(await readdir(root)).toEqual(["aliased.pck"]);
}
}
},
);