diff --git a/frontend/src/v2/components/Gallery/FirmwareTab.test.ts b/frontend/src/v2/components/Gallery/FirmwareTab.test.ts new file mode 100644 index 000000000..a095f6463 --- /dev/null +++ b/frontend/src/v2/components/Gallery/FirmwareTab.test.ts @@ -0,0 +1,110 @@ +import { shallowMount } from "@vue/test-utils"; +import { describe, expect, it, vi } from "vitest"; +import type { FirmwareSchema } from "@/__generated__"; +import type { Platform } from "@/stores/platforms"; +import HashChip from "@/v2/components/shared/HashChip.vue"; +import FirmwareTab from "./FirmwareTab.vue"; + +vi.mock("vue-i18n", () => ({ + useI18n: () => ({ t: (key: string, fallback?: string) => fallback ?? key }), +})); + +vi.mock("@/services/api/firmware", () => ({ + default: { + getFirmware: vi.fn(), + uploadFirmware: vi.fn(), + deleteFirmware: vi.fn(), + }, +})); + +vi.mock("@/stores/platforms", () => ({ + default: () => ({ update: vi.fn() }), +})); + +vi.mock("@/v2/stores/galleryRoms", () => ({ + default: () => ({ currentPlatform: null, setCurrentPlatform: vi.fn() }), +})); + +vi.mock("@/v2/composables/useCan", () => ({ + useCan: () => ({ value: true }), +})); + +vi.mock("@/v2/composables/useSnackbar", () => ({ + useSnackbar: () => ({ error: vi.fn(), success: vi.fn(), warning: vi.fn() }), +})); + +const CRC = "aabbccdd"; +const MD5 = "0123456789abcdef0123456789abcdef"; +const SHA1 = "0123456789abcdef0123456789abcdef01234567"; + +function firmware(overrides: Partial = {}): FirmwareSchema { + return { + id: 1, + file_name: "disksys.rom", + file_name_no_tags: "disksys", + file_name_no_ext: "disksys", + file_extension: "rom", + file_path: "fds", + file_size_bytes: 8192, + full_path: "fds/disksys.rom", + is_verified: true, + crc_hash: CRC, + md5_hash: MD5, + sha1_hash: SHA1, + missing_from_fs: false, + platform_id: 1, + created_at: "", + updated_at: "", + ...overrides, + } as FirmwareSchema; +} + +function platform(firmwareList: FirmwareSchema[]): Platform { + return { + id: 1, + slug: "fds", + fs_slug: "fds", + name: "Family Computer Disk System", + display_name: "Family Computer Disk System", + rom_count: 0, + firmware_count: firmwareList.length, + firmware: firmwareList, + } as Platform; +} + +function mountTab(firmwareList: FirmwareSchema[]) { + return shallowMount(FirmwareTab, { + props: { platform: platform(firmwareList) }, + global: { + // The whole tab is wrapped in a dropzone whose default slot holds + // the list, and a stubbed component does not render its slots. + stubs: { RDropzone: { template: "
" } }, + }, + }); +} + +function chipEntries(wrapper: ReturnType) { + return wrapper + .findAllComponents(HashChip) + .map((c) => [c.props("label"), c.props("value")]); +} + +describe("FirmwareTab", () => { + // #4082: the MD5 used to render as a plain chip with no copy affordance + // and `user-select: none`, and CRC / SHA-1 were never shown at all. + it("renders a copyable chip for every stored hash", () => { + const wrapper = mountTab([firmware()]); + + expect(chipEntries(wrapper)).toEqual([ + ["SHA-1", SHA1], + ["MD5", MD5], + ["CRC", CRC], + ]); + }); + + it("skips hashes the firmware record does not carry", () => { + const wrapper = mountTab([firmware({ crc_hash: "", sha1_hash: "" })]); + + expect(chipEntries(wrapper)).toEqual([["MD5", MD5]]); + }); +}); diff --git a/frontend/src/v2/components/Gallery/FirmwareTab.vue b/frontend/src/v2/components/Gallery/FirmwareTab.vue index 382609f5d..2cc26bd1e 100644 --- a/frontend/src/v2/components/Gallery/FirmwareTab.vue +++ b/frontend/src/v2/components/Gallery/FirmwareTab.vue @@ -32,6 +32,7 @@ import type { Platform } from "@/stores/platforms"; import storePlatforms from "@/stores/platforms"; import { formatBytes } from "@/utils"; import DeleteFirmwareDialog from "@/v2/components/Gallery/DeleteFirmwareDialog.vue"; +import HashChip from "@/v2/components/shared/HashChip.vue"; import { useCan } from "@/v2/composables/useCan"; import { useSnackbar } from "@/v2/composables/useSnackbar"; import storeGalleryRoms from "@/v2/stores/galleryRoms"; @@ -385,15 +386,24 @@ async function performDelete( {{ formatBytes(f.file_size_bytes) }} - - {{ f.md5_hash }} - + + + ({ + useI18n: () => ({ t: (key: string) => key }), +})); + +const CRC = "aabbccdd"; +const MD5 = "0123456789abcdef0123456789abcdef"; +const SHA1 = "0123456789abcdef0123456789abcdef01234567"; +const CHD_SHA1 = "89abcdef0123456789abcdef0123456789abcdef"; +const RA = "fedcba9876543210fedcba9876543210"; + +function rom(overrides: Partial = {}): DetailedRom { + return { + id: 1, + fs_name: "game.chd", + fs_size_bytes: 1024, + crc_hash: CRC, + md5_hash: MD5, + sha1_hash: SHA1, + ra_hash: RA, + has_simple_single_file: true, + files: [{ chd_sha1_hash: "" }], + ...overrides, + } as DetailedRom; +} + +function hashLabels(r: DetailedRom) { + const wrapper = shallowMount(MetadataTab, { props: { rom: r } }); + return wrapper + .findAllComponents({ name: "HashChip" }) + .map((c) => c.props("label")); +} + +describe("MetadataTab hash rows", () => { + // The files list and the firmware list both read SHA-1, CHD SHA-1, MD5, + // CRC, RA. This tab used to lead with CRC, so the two tabs disagreed. + it("orders hashes the same way every other surface does", () => { + expect(hashLabels(rom())).toEqual(["SHA-1", "MD5", "CRC", "RA"]); + }); + + // Not reachable in the browser: the mock library holds no CHD. + it("slots CHD SHA-1 directly after SHA-1 when the ROM is a CHD", () => { + const chd = rom({ + files: [{ chd_sha1_hash: CHD_SHA1 }], + } as Partial); + + expect(hashLabels(chd)).toEqual(["SHA-1", "CHD SHA-1", "MD5", "CRC", "RA"]); + }); +}); diff --git a/frontend/src/v2/components/GameDetails/MetadataTab.vue b/frontend/src/v2/components/GameDetails/MetadataTab.vue index 4fab0dade..b59bd2b7a 100644 --- a/frontend/src/v2/components/GameDetails/MetadataTab.vue +++ b/frontend/src/v2/components/GameDetails/MetadataTab.vue @@ -1,7 +1,8 @@