diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.ts index e23834f63..9ab0a33a9 100644 --- a/src/application/BinaryTargetResolver.ts +++ b/src/application/BinaryTargetResolver.ts @@ -157,9 +157,16 @@ export const parseBinaryTarget = async ( availableArchitectures: ["x86"], }); } + // Explicit database selection owns routing; a suffix is only a fallback. + const artifactFormat = + targetKind === "database" + ? undefined + : await detectArtifactFormat(path, handle, metadata, signal); if ( targetKind === "database" || - (targetKind === undefined && path.toLowerCase().endsWith(".hop")) + (targetKind === undefined && + artifactFormat === undefined && + path.toLowerCase().endsWith(".hop")) ) return ok({ path, @@ -168,12 +175,6 @@ export const parseBinaryTarget = async ( kind: "database", format: "analysis-database", }); - const artifactFormat = await detectArtifactFormat( - path, - handle, - metadata, - signal, - ); if (artifactFormat !== undefined) { const identity = { path, diff --git a/tests/acceptance/inesContainer.test.ts b/tests/acceptance/inesContainer.test.ts index 25c85b3d3..0077d61d5 100644 --- a/tests/acceptance/inesContainer.test.ts +++ b/tests/acceptance/inesContainer.test.ts @@ -45,7 +45,7 @@ cliTest( "classic iNES has the same complete byte inventory through compiled CLI and stdio MCP", async ({ cli }) => { const root = await createTestTempDirectory("rea-ines-public-"); - const path = join(root, "卡带.data"); + const path = join(root, "卡带.hop"); const bytes = inesFixture({ trainer: true, trailing: Buffer.from([0, 255, 3, 0]), diff --git a/tests/boundary/filesystem/binaryTarget.test.ts b/tests/boundary/filesystem/binaryTarget.test.ts index 0aae910e8..94fa18aae 100644 --- a/tests/boundary/filesystem/binaryTarget.test.ts +++ b/tests/boundary/filesystem/binaryTarget.test.ts @@ -245,16 +245,19 @@ describe("binary target I/O: package classification", () => { }); }); - it("classifies a binary plist by its magic under any extension", async () => { - const directory = await createTestTempDirectory("rea-bplist-target-"); - const path = join(directory, "Model.archive"); - await writeFile(path, buildBinary({ $archiver: "NSKeyedArchiver" })); - const result = await parseBinaryTarget(path); - expect(result.ok && result.value).toMatchObject({ - kind: "artifact", - format: "plist", - }); - }); + it.each(["archive", "hop"])( + "classifies binary plist magic under a .%s extension", + async (suffix) => { + const directory = await createTestTempDirectory("rea-bplist-target-"); + const path = join(directory, `Model.${suffix}`); + await writeFile(path, buildBinary({ $archiver: "NSKeyedArchiver" })); + const result = await parseBinaryTarget(path); + expect(result.ok && result.value).toMatchObject({ + kind: "artifact", + format: "plist", + }); + }, + ); it("does not trust a ZIP-family extension without ZIP magic", async () => { const directory = await createTestTempDirectory("rea-fake-archive-"); @@ -274,6 +277,10 @@ describe("binary target I/O: filesystem and app bundle target resolution", () => await writeFile(join(directory, "sample.hop"), "database"); await writeFile(join(directory, "text"), "hello"); const database = await parseBinaryTarget("sample.hop", { cwd: directory }); + expect(database).toMatchObject({ + ok: true, + value: { kind: "database", format: "analysis-database" }, + }); expect(database.ok && database.value.sha256).toBe( "3549b0028b75d981cdda2e573e9cb49dedc200185876df299f912b79f69dabd8", ); diff --git a/tests/boundary/filesystem/inesArtifact.test.ts b/tests/boundary/filesystem/inesArtifact.test.ts index 8ac63b3f1..9720ad8f7 100644 --- a/tests/boundary/filesystem/inesArtifact.test.ts +++ b/tests/boundary/filesystem/inesArtifact.test.ts @@ -28,15 +28,31 @@ import { const sha256 = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex"); -it("routes iNES bytes as an archive independently of the file suffix", async () => { - const root = await createTestTempDirectory("rea-ines-target-"); - const path = join(root, "cartridge.data"); - await writeFile(path, inesFixture()); - const target = await parseBinaryTarget(path); - if (!target.ok) throw target.error; - expect(target.value).toMatchObject({ kind: "archive", format: "ines" }); -}); +it.each(["data", "hop", "HOP"])( + "routes iNES bytes as an archive with a .%s suffix", + async (suffix) => { + const root = await createTestTempDirectory("rea-ines-target-"); + const path = join(root, `cartridge.${suffix}`); + await writeFile(path, inesFixture()); + const target = await parseBinaryTarget(path); + if (!target.ok) throw target.error; + expect(target.value).toMatchObject({ kind: "archive", format: "ines" }); + }, +); +it("honors explicit database selection before parsing NES-signature bytes", async () => { + const root = await createTestTempDirectory("rea-ines-database-"); + for (const bytes of [inesFixture(), Buffer.from("NES\x1a")]) { + const path = join(root, "selected.hop"); + await writeFile(path, bytes); + expect( + await parseBinaryTarget(path, { targetKind: "database" }), + ).toMatchObject({ + ok: true, + value: { kind: "database", format: "analysis-database" }, + }); + } +}); it.each([true, false])( "inventories and reconstructs all bytes with trainer=%s", async (trainer) => {