From ef9a6015246e4876d80a099d7aa5b6cfdfee5add Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Sun, 4 Oct 2026 17:45:54 +0000 Subject: [PATCH] test: build users, collections and audit events with their fixtures Tests cast object literals to User, Collection and AuditEventSchema, which left every other field undefined; they now use userFixture, collectionFixture and makeAuditEvent. A modelCastRule bans casting an object literal to those types, or to Save/State, in tests and v2 stories. Reading a value back as one of them stays allowed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GCi2WPmUMtYNKvP1GbzgrS --- frontend/eslint.config.js | 24 +++++++++++++++++-- frontend/src/plugins/router.test.ts | 6 ++--- .../Dialogs/ManageCollectionsDialog.test.ts | 10 ++++---- .../GameDetails/SaveDataTab.test.ts | 4 ++-- .../v2/composables/useAuditLog/index.test.ts | 4 ++-- .../useNotificationInbox/index.test.ts | 4 ++-- .../src/v2/views/CollectionsIndex.test.ts | 3 ++- .../src/v2/views/Gallery/Collection.test.ts | 3 ++- frontend/src/v2/views/Home.test.ts | 10 +++----- frontend/src/v2/views/Settings/Logs.test.ts | 4 ++-- 10 files changed, 45 insertions(+), 27 deletions(-) diff --git a/frontend/eslint.config.js b/frontend/eslint.config.js index ef9c6d815..98fe8c40d 100644 --- a/frontend/eslint.config.js +++ b/frontend/eslint.config.js @@ -43,6 +43,14 @@ const platformCastRule = { "Build platform fixtures with platformFixture from @/utils/platform.fixtures instead of a cast.", }; +// Only object literals: reading a value back as one of these types is fine. +const modelCastRule = { + selector: + ":matches(TSAsExpression[expression.type='ObjectExpression'], TSAsExpression[expression.expression.type='ObjectExpression'])[typeAnnotation.typeName.name=/^(User|UserSchema|Collection|CollectionSchema|SaveSchema|StateSchema|AuditEventSchema)$/]", + message: + "Build users, collections, saves, states and audit events with their fixtures (userFixture, collectionFixture, saveFixture, stateFixture, makeAuditEvent) instead of casting an object literal.", +}; + export default tseslint.config( eslint.configs.recommended, ...tseslint.configs.recommended, @@ -342,12 +350,24 @@ export default tseslint.config( "src/layouts/**", ], rules: { - "no-restricted-syntax": ["error", romCastRule, platformCastRule], + "no-restricted-syntax": [ + "error", + romCastRule, + platformCastRule, + modelCastRule, + ], }, }, { files: ["src/v2/**/*.stories.ts"], - rules: { "no-restricted-syntax": ["error", romCastRule, platformCastRule] }, + rules: { + "no-restricted-syntax": [ + "error", + romCastRule, + platformCastRule, + modelCastRule, + ], + }, }, // Keep last: Prettier owns formatting, so this switches off every // stylistic rule the two tools would otherwise fight over. diff --git a/frontend/src/plugins/router.test.ts b/frontend/src/plugins/router.test.ts index 8f1506fb2..ba356111b 100644 --- a/frontend/src/plugins/router.test.ts +++ b/frontend/src/plugins/router.test.ts @@ -7,8 +7,8 @@ import i18n, { localesReady } from "@/locales"; import router, { applyRouteTitle, ROUTES } from "@/plugins/router"; import storeAuth from "@/stores/auth"; import storeRoms from "@/stores/roms"; -import type { User } from "@/stores/users"; import { detailedRomFixture } from "@/utils/rom.fixtures"; +import { userFixture } from "@/utils/user.fixtures"; const { getRom, stubView } = vi.hoisted(() => ({ getRom: vi.fn(), @@ -97,7 +97,7 @@ describe("the rom route", () => { // matching the route is not proof the store's copy is current. it("re-reads a rom the store already holds", async () => { const roms = storeRoms(); - storeAuth().setCurrentUser({ id: 1 } as User); + storeAuth().setCurrentUser(userFixture({ id: 1 })); roms.setCurrentRom( detailedRomFixture({ id: 9, name: "before the session" }), ); @@ -146,7 +146,7 @@ describe("the inactive UI's views", () => { router.getRoutes().find((r) => r.name === "deferred-views")?.components; beforeEach(() => { - storeAuth().setCurrentUser({ id: 1 } as User); + storeAuth().setCurrentUser(userFixture({ id: 1 })); uiVersion.value = "v2"; }); diff --git a/frontend/src/v2/components/Dialogs/ManageCollectionsDialog.test.ts b/frontend/src/v2/components/Dialogs/ManageCollectionsDialog.test.ts index 1fdd8f41a..5c61daad3 100644 --- a/frontend/src/v2/components/Dialogs/ManageCollectionsDialog.test.ts +++ b/frontend/src/v2/components/Dialogs/ManageCollectionsDialog.test.ts @@ -5,8 +5,8 @@ import { ref } from "vue"; import storeAuth from "@/stores/auth"; import storeCollections, { type Collection } from "@/stores/collections"; import storeRoms, { type SimpleRom } from "@/stores/roms"; -import type { User } from "@/stores/users"; import type { Events } from "@/types/emitter"; +import { collectionFixture } from "@/utils/collection.fixtures"; import { romFixture } from "@/utils/rom.fixtures"; import { userFixture } from "@/utils/user.fixtures"; import storeGalleryRoms from "@/v2/stores/galleryRoms"; @@ -59,13 +59,13 @@ function rom(id: number): SimpleRom { } function collection(romIds: number[]): Collection { - return { + return collectionFixture({ id: 12, name: "Shooters", user_id: USER_ID, rom_ids: romIds, rom_count: romIds.length, - } as Collection; + }); } /** Mount the dialog, open it over `roms`, and click the one collection row. */ @@ -118,7 +118,7 @@ function mountDialog( describe("ManageCollectionsDialog gallery reconcile", () => { beforeEach(() => { - storeAuth().setCurrentUser({ id: USER_ID } as User); + storeAuth().setCurrentUser(userFixture({ id: USER_ID })); }); it("drops the roms from the gallery when removing them from the collection on screen", async () => { @@ -148,7 +148,7 @@ describe("ManageCollectionsDialog gallery reconcile", () => { collections.setCollections([collection([1, 2])]); removeRomsFromCollection.mockResolvedValue({ data: collection([]) }); const gallery = storeGalleryRoms(); - gallery.setCurrentCollection({ ...collection([]), id: 99 } as Collection); + gallery.setCurrentCollection({ ...collection([]), id: 99 }); const galleryRemove = vi.spyOn(gallery, "remove"); await toggleRow([rom(1), rom(2)]); diff --git a/frontend/src/v2/components/GameDetails/SaveDataTab.test.ts b/frontend/src/v2/components/GameDetails/SaveDataTab.test.ts index b6c9ce8ca..5b3c022a1 100644 --- a/frontend/src/v2/components/GameDetails/SaveDataTab.test.ts +++ b/frontend/src/v2/components/GameDetails/SaveDataTab.test.ts @@ -2,8 +2,8 @@ import { mount } from "@vue/test-utils"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { UserStateSchema } from "@/__generated__"; import storeAuth from "@/stores/auth"; -import type { User } from "@/stores/users"; import { detailedRomFixture } from "@/utils/rom.fixtures"; +import { userFixture } from "@/utils/user.fixtures"; import SaveDataTab from "./SaveDataTab.vue"; vi.mock("vue-i18n"); @@ -60,7 +60,7 @@ function mountTab(states: UserStateSchema[]) { describe("SaveDataTab upload cores", () => { beforeEach(() => { - storeAuth().setCurrentUser({ id: 1 } as User); + storeAuth().setCurrentUser(userFixture({ id: 1 })); }); it("offers an emulator configured in another case once", () => { diff --git a/frontend/src/v2/composables/useAuditLog/index.test.ts b/frontend/src/v2/composables/useAuditLog/index.test.ts index f2ba2c389..8890f1877 100644 --- a/frontend/src/v2/composables/useAuditLog/index.test.ts +++ b/frontend/src/v2/composables/useAuditLog/index.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; -import type { AuditEventSchema } from "@/__generated__"; import auditApi from "@/services/api/audit"; import { useAuditLog } from "@/v2/composables/useAuditLog"; +import { makeAuditEvent } from "@/v2/utils/auditEvents.fixtures"; vi.mock("@/services/api/audit", () => ({ default: { getAuditEvents: vi.fn() }, @@ -12,7 +12,7 @@ const getAuditEvents = vi.mocked(auditApi.getAuditEvents); function page(ids: number[], total: number, maxId: number | null = null) { return { data: { - items: ids.map((id) => ({ id }) as AuditEventSchema), + items: ids.map((id) => makeAuditEvent({ id })), total, limit: 2, offset: 0, diff --git a/frontend/src/v2/composables/useNotificationInbox/index.test.ts b/frontend/src/v2/composables/useNotificationInbox/index.test.ts index f4fd3de5d..f39a316ea 100644 --- a/frontend/src/v2/composables/useNotificationInbox/index.test.ts +++ b/frontend/src/v2/composables/useNotificationInbox/index.test.ts @@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { effectScope, type EffectScope } from "vue"; import type { NotificationSchema } from "@/__generated__"; import storeAuth from "@/stores/auth"; -import type { User } from "@/stores/users"; +import { userFixture } from "@/utils/user.fixtures"; import { installNotificationInbox } from "@/v2/composables/useNotificationInbox"; import storeNotificationInbox from "@/v2/stores/notificationInbox"; import { makeNotification } from "@/v2/utils/notifications.fixtures"; @@ -40,7 +40,7 @@ function notification(id: number): NotificationSchema { } function signIn(id: number) { - storeAuth().setCurrentUser({ id } as User); + storeAuth().setCurrentUser(userFixture({ id })); } function push(event: string, payload: unknown) { diff --git a/frontend/src/v2/views/CollectionsIndex.test.ts b/frontend/src/v2/views/CollectionsIndex.test.ts index c5b7ba30d..9ae0cd059 100644 --- a/frontend/src/v2/views/CollectionsIndex.test.ts +++ b/frontend/src/v2/views/CollectionsIndex.test.ts @@ -7,6 +7,7 @@ import storeCollections, { type SmartCollection, type VirtualCollection, } from "@/stores/collections"; +import { collectionFixture } from "@/utils/collection.fixtures"; import CollectionsIndex from "./CollectionsIndex.vue"; vi.mock("vue-i18n"); @@ -127,7 +128,7 @@ vi.mock("@/composables/useUISettings", () => ({ })); function collection(id: number, name: string): Collection { - return { id, name, rom_count: 3, is_public: true } as Collection; + return collectionFixture({ id, name, rom_count: 3, is_public: true }); } function smart(id: number, name: string): SmartCollection { diff --git a/frontend/src/v2/views/Gallery/Collection.test.ts b/frontend/src/v2/views/Gallery/Collection.test.ts index 9bab1e639..a13390635 100644 --- a/frontend/src/v2/views/Gallery/Collection.test.ts +++ b/frontend/src/v2/views/Gallery/Collection.test.ts @@ -10,6 +10,7 @@ import storeCollections, { type VirtualCollection, } from "@/stores/collections"; import type { SimpleRom } from "@/stores/roms"; +import { collectionFixture } from "@/utils/collection.fixtures"; import { romFixture } from "@/utils/rom.fixtures"; import { userFixture } from "@/utils/user.fixtures"; import storeGalleryRoms from "@/v2/stores/galleryRoms"; @@ -130,7 +131,7 @@ vi.mock("@/v2/composables/useWebpSupport", () => ({ })); function collection(id: number): Collection { - return { id, name: `Collection ${id}`, rom_count: 9000 } as Collection; + return collectionFixture({ id, name: `Collection ${id}`, rom_count: 9000 }); } function virtualCollection(romCount: number): VirtualCollection { diff --git a/frontend/src/v2/views/Home.test.ts b/frontend/src/v2/views/Home.test.ts index 05b2fd727..6bd6809dc 100644 --- a/frontend/src/v2/views/Home.test.ts +++ b/frontend/src/v2/views/Home.test.ts @@ -11,8 +11,9 @@ import storeCollections, { type Collection } from "@/stores/collections"; import storePlatforms, { type Platform } from "@/stores/platforms"; import storeRoms, { type SimpleRom } from "@/stores/roms"; import { useStreamingStore, type JoinableSession } from "@/stores/streaming"; -import { romFixture } from "@/utils/rom.fixtures"; +import { collectionFixture } from "@/utils/collection.fixtures"; import { platformFixture } from "@/utils/platform.fixtures"; +import { romFixture } from "@/utils/rom.fixtures"; import { SKELETON_DELAY_MS } from "@/v2/composables/useLoadingPhase"; import Home from "./Home.vue"; @@ -119,12 +120,7 @@ function platform(id: number): Platform { } function collection(id: number): Collection { - return { - id, - name: `Collection ${id}`, - rom_count: 3, - rom_ids: [], - } as unknown as Collection; + return collectionFixture({ id, name: `Collection ${id}`, rom_count: 3 }); } function rom(id: number): SimpleRom { diff --git a/frontend/src/v2/views/Settings/Logs.test.ts b/frontend/src/v2/views/Settings/Logs.test.ts index b468d41a9..3760edc93 100644 --- a/frontend/src/v2/views/Settings/Logs.test.ts +++ b/frontend/src/v2/views/Settings/Logs.test.ts @@ -5,7 +5,7 @@ import { defineComponent, reactive } from "vue"; import storeAuth from "@/stores/auth"; import storeHeartbeat from "@/stores/heartbeat"; import storePermissions from "@/stores/permissions"; -import type { User } from "@/stores/users"; +import { userFixture } from "@/utils/user.fixtures"; import Logs from "./Logs.vue"; const route = reactive<{ query: Record }>({ query: {} }); @@ -50,7 +50,7 @@ function signIn({ const permissions = storePermissions(); permissions.isAdmin = admin; permissions.hydrated = true; - storeAuth().setCurrentUser({ oauth_scopes: ["logs.read"] } as User); + storeAuth().setCurrentUser(userFixture({ oauth_scopes: ["logs.read"] })); storeHeartbeat().value.FRONTEND.DISABLE_LOGS_VIEWER = !viewer; }