Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions frontend/src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from "vitest";
import type { SimpleRom } from "@/stores/roms";
import { getDownloadPath } from "./index";

function makeRom(overrides: Partial<SimpleRom>): SimpleRom {
return {
id: 1,
fs_name: "Game",
files: [],
...overrides,
} as SimpleRom;
}

describe("getDownloadPath", () => {
it("uses fs_name when no file is selected", () => {
const rom = makeRom({ id: 14, fs_name: "Maniac Mansion (1989).adf" });
expect(getDownloadPath({ rom })).toBe(
"/api/roms/14/content/Maniac Mansion (1989).adf",
);
});

it("uses the selected file name (with extension) for a single file", () => {
// Multi-file rom: fs_name is the folder name with no extension. The URL
// path segment must carry the selected file's real name so the emulator
// receives a file with the correct extension.
const rom = makeRom({
id: 24,
fs_name: "B.A.T.",
files: [
{ id: 29, file_name: "B.A.T. Disk1.adf" },
{ id: 30, file_name: "B.A.T. Disk2.adf" },
] as SimpleRom["files"],
});
expect(getDownloadPath({ rom, fileIDs: [29] })).toBe(
"/api/roms/24/content/B.A.T. Disk1.adf?file_ids=29",
);
});

it("falls back to fs_name when multiple files are selected (zip)", () => {
const rom = makeRom({
id: 24,
fs_name: "B.A.T.",
files: [
{ id: 29, file_name: "B.A.T. Disk1.adf" },
{ id: 30, file_name: "B.A.T. Disk2.adf" },
] as SimpleRom["files"],
});
expect(getDownloadPath({ rom, fileIDs: [29, 30] })).toBe(
"/api/roms/24/content/B.A.T.?file_ids=29%2C30",
);
});

it("falls back to fs_name when the selected file id is unknown", () => {
const rom = makeRom({ id: 24, fs_name: "B.A.T." });
expect(getDownloadPath({ rom, fileIDs: [999] })).toBe(
"/api/roms/24/content/B.A.T.?file_ids=999",
);
});
});
12 changes: 11 additions & 1 deletion frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,17 @@ export function getDownloadPath({
queryParams.append("file_ids", fileIDs.join(","));
}
const queryString = queryParams.toString();
return `/api/roms/${rom.id}/content/${rom.fs_name}${
// When a single file is selected, use its real name (extension included) as
// the path segment. EmulatorJS derives the on-disk filename, and therefore
// the file format handed to the core, from the URL. Multi-file roms have no
// root extension (`fs_name` is the folder name), so falling back to it would
// give the core an extension-less file and fail with "Unsupported file format".
const selectedFile =
fileIDs.length === 1
? rom.files?.find((f) => f.id === fileIDs[0])
: undefined;
const contentName = selectedFile?.file_name ?? rom.fs_name;
Comment thread
gantoine marked this conversation as resolved.
Outdated
return `/api/roms/${rom.id}/content/${contentName}${
queryString ? `?${queryString}` : ""
}`;
}
Expand Down
Loading