Files
romm/frontend/src/utils/playerFirmware.test.ts
Georges-Antoine Assi cf47d4572f fix(player): point RetroArch's system directory at PUAE Kickstarts (#5444)
* fix(player): point RetroArch's system directory at PUAE Kickstarts

EmulatorJS's retroarch.cfg has no system_directory, so RetroArch falls
back to the content directory. WHDLoad games packed in a subfolder then
miss the Kickstarts RomM writes to the system directory, and PUAE boots
AROS instead.

When EJS_externalFiles targets the system directory (only PUAE today),
append system_directory to retroarch.cfg as soon as gameManager is
assigned, before the core boots. Other cores keep the content directory
fallback, since EJS_biosUrl and keepArcadeBiosWhole put their BIOS there.

Fixes #5247

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(player): reword the system directory comment per Greptile review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:16:29 -05:00

273 lines
8.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
firmwareExternalFiles,
keepArcadeBiosWhole,
pointSystemDirectoryAtFirmware,
resolveInitialFirmware,
} from "./playerFirmware";
// Only these three fields are read; minimal stubs stand in for FirmwareSchema.
const fw = (id: number, file_name: string, missing_from_fs = false) => ({
id,
file_name,
missing_from_fs,
});
describe("resolveInitialFirmware", () => {
it("prefers the id the user last picked for the platform", () => {
const options = [fw(1, "a.bin"), fw(2, "b.bin")];
expect(
resolveInitialFirmware({
options,
storedBiosId: "2",
configBiosFile: "a.bin",
})?.id,
).toBe(2);
});
it("falls back to the core config's bios_file", () => {
const options = [fw(1, "a.bin"), fw(2, "b.bin")];
expect(
resolveInitialFirmware({
options,
storedBiosId: null,
configBiosFile: "b.bin",
})?.id,
).toBe(2);
});
it("auto-selects when the platform has exactly one usable entry", () => {
expect(
resolveInitialFirmware({
options: [fw(7, "only.bin")],
storedBiosId: null,
configBiosFile: undefined,
})?.id,
).toBe(7);
});
// Issue #4075: a single, stale entry used to be auto-selected, and the game
// then failed to boot with nothing pointing at the BIOS.
it("never auto-selects the sole entry when its file is missing", () => {
expect(
resolveInitialFirmware({
options: [fw(7, "gone.bin", true)],
storedBiosId: null,
configBiosFile: undefined,
}),
).toBeNull();
});
it("ignores a stored id that now points at missing firmware", () => {
const options = [fw(1, "gone.bin", true), fw(2, "present.bin")];
// The stored pick is discarded, leaving one usable entry to fall back on.
expect(
resolveInitialFirmware({
options,
storedBiosId: "1",
configBiosFile: undefined,
})?.id,
).toBe(2);
});
it("ignores a config bios_file that now points at missing firmware", () => {
const options = [fw(1, "gone.bin", true), fw(2, "present.bin")];
expect(
resolveInitialFirmware({
options,
storedBiosId: null,
configBiosFile: "gone.bin",
})?.id,
).toBe(2);
});
it("selects nothing when the platform has no firmware at all", () => {
expect(
resolveInitialFirmware({
options: [],
storedBiosId: null,
configBiosFile: undefined,
}),
).toBeNull();
});
it("selects nothing when several usable entries make the choice ambiguous", () => {
expect(
resolveInitialFirmware({
options: [fw(1, "a.bin"), fw(2, "b.bin")],
storedBiosId: null,
configBiosFile: undefined,
}),
).toBeNull();
});
});
const firmware = [
fw(1, "kick34005.A500"),
fw(2, "kick40068.A1200"),
fw(3, "gone.rom", true),
fw(4, "bios.zip"),
];
describe("firmwareExternalFiles", () => {
it("places every available Kickstart in the PUAE system directory", () => {
expect(firmwareExternalFiles("puae", firmware)).toEqual({
"/home/web_user/retroarch/userdata/system/kick34005.A500":
"/api/firmware/1/content/kick34005.A500",
"/home/web_user/retroarch/userdata/system/kick40068.A1200":
"/api/firmware/2/content/kick40068.A1200",
});
});
it("does not change firmware loading for another core", () => {
expect(firmwareExternalFiles("pcsx_rearmed", firmware)).toEqual({});
});
it("ignores unsafe filenames", () => {
expect(
firmwareExternalFiles("puae", [
fw(1, "../other.rom"),
fw(2, "folder\\file.rom"),
fw(3, ".."),
fw(4, "."),
]),
).toEqual({});
});
it("encodes the name in the URL but keeps it verbatim in the path", () => {
expect(firmwareExternalFiles("puae", [fw(5, "kick #1?.rom")])).toEqual({
"/home/web_user/retroarch/userdata/system/kick #1?.rom":
"/api/firmware/5/content/kick%20%231%3F.rom",
});
});
});
// EmulatorJS 4.2.3 is the build with downloadGameFile; nightly has none.
const emulator = (
system: string,
biosUrl: string,
{
nightly = false,
externalFiles,
}: {
nightly?: boolean;
externalFiles?: Record<string, string>;
} = {},
) => ({
config: { biosUrl, externalFiles },
getCore: () =>
["fbneo", "mame2003_plus"].includes(system) ? "arcade" : system,
downloadGameFile: nightly ? undefined : () => undefined,
});
describe("keepArcadeBiosWhole", () => {
it("writes an arcade BIOS archive whole beside the game", () => {
const instance = emulator("fbneo", "/api/firmware/9/content/neogeo.zip", {
externalFiles: { "/other": "/api/firmware/1/content/other" },
});
keepArcadeBiosWhole(instance);
expect(instance.config).toEqual({
biosUrl: "",
externalFiles: {
"/other": "/api/firmware/1/content/other",
"/neogeo.zip": "/api/firmware/9/content/neogeo.zip",
},
});
});
it("writes the CD-i BIOS archive whole for same_cdi", () => {
const instance = emulator(
"same_cdi",
"/api/firmware/4/content/cdimono1.zip",
);
keepArcadeBiosWhole(instance);
expect(instance.config).toEqual({
biosUrl: "",
externalFiles: {
"/cdimono1.zip": "/api/firmware/4/content/cdimono1.zip",
},
});
});
it("leaves the BIOS to EmulatorJS on a build that keeps it whole", () => {
const instance = emulator("fbneo", "/api/firmware/9/content/neogeo.zip", {
nightly: true,
});
keepArcadeBiosWhole(instance);
expect(instance.config.biosUrl).toBe("/api/firmware/9/content/neogeo.zip");
expect(instance.config.externalFiles).toBeUndefined();
});
it("leaves other cores and non-archive BIOS files alone", () => {
const psx = emulator("psx", "/api/firmware/2/content/scph1001.zip");
const raw = emulator("fbneo", "/api/firmware/3/content/bios.bin");
keepArcadeBiosWhole(psx);
keepArcadeBiosWhole(raw);
expect(psx.config.biosUrl).toBe("/api/firmware/2/content/scph1001.zip");
expect(raw.config.biosUrl).toBe("/api/firmware/3/content/bios.bin");
});
it("does nothing without a BIOS", () => {
const instance = emulator("fbneo", "");
keepArcadeBiosWhole(instance);
expect(instance.config.externalFiles).toBeUndefined();
});
});
const RETROARCH_CFG = "/home/web_user/.config/retroarch/retroarch.cfg";
// A GameManager whose constructor already wrote retroarch.cfg, as EmulatorJS's does.
const gameManager = (cfg = "audio_latency = 64\n") => {
const files: Record<string, string> = { [RETROARCH_CFG]: cfg };
return {
files,
FS: {
readFile: (path: string) => files[path] ?? "",
writeFile: (path: string, data: string) => {
files[path] = data;
},
},
};
};
const booting = (externalFiles?: Record<string, string>, cfg?: string) => {
const instance: {
config: { externalFiles?: Record<string, string> | undefined };
gameManager?: ReturnType<typeof gameManager> | undefined;
} = { config: { externalFiles } };
pointSystemDirectoryAtFirmware(instance);
const manager = gameManager(cfg);
instance.gameManager = manager;
return { instance, manager };
};
describe("pointSystemDirectoryAtFirmware", () => {
it("points RetroArch at the Kickstarts written for PUAE", () => {
const { instance, manager } = booting(
firmwareExternalFiles("puae", firmware),
);
expect(instance.gameManager).toBe(manager);
expect(manager.files[RETROARCH_CFG]).toBe(
'audio_latency = 64\nsystem_directory = "/home/web_user/retroarch/userdata/system"\n',
);
});
it("keeps a system_directory the config already sets", () => {
const { manager } = booting(
firmwareExternalFiles("puae", firmware),
'system_directory = "/bios"\n',
);
expect(manager.files[RETROARCH_CFG]).toBe('system_directory = "/bios"\n');
});
// Games may bundle their own Kickstarts, and other cores find their BIOS in
// the content directory RetroArch falls back to.
it.each([
["no firmware", undefined],
["a BIOS beside the game", { "/neogeo.zip": "/api/firmware/9" }],
])("keeps the content directory with %s", (_, externalFiles) => {
const { manager } = booting(externalFiles);
expect(manager.files[RETROARCH_CFG]).toBe("audio_latency = 64\n");
});
});