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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GCi2WPmUMtYNKvP1GbzgrS
This commit is contained in:
Sam Dornan
2026-10-04 17:45:54 +00:00
committed by Claude
parent 7d8484477a
commit ef9a601524
10 changed files with 45 additions and 27 deletions

View File

@@ -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.

View File

@@ -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";
});

View File

@@ -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)]);

View File

@@ -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", () => {

View File

@@ -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,

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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<string, string> }>({ 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;
}