diff --git a/docs/FRONTEND_ARCHITECTURE.md b/docs/FRONTEND_ARCHITECTURE.md index ab1432a9f..c3af63b01 100644 --- a/docs/FRONTEND_ARCHITECTURE.md +++ b/docs/FRONTEND_ARCHITECTURE.md @@ -1099,14 +1099,15 @@ Procedural SVG generation for: ### Scripts -| Script | Command | Purpose | -| ----------- | ------------------------------ | ----------------------------------- | -| `dev` | `vite --host` | Development server | -| `build` | `vite build` | Production build | -| `preview` | `vite preview` | Preview production build | -| `typecheck` | `vue-tsc`, `dpdm` (`circular`) | Types plus static import cycle gate | -| `generate` | `openapi-typescript-codegen` | Generate types from backend OpenAPI | -| `lint` | `eslint` | Lint `.vue`, `.js`, `.ts` files | +| Script | Command | Purpose | +| ----------- | -------------------------------------- | ----------------------------------------- | +| `dev` | `vite --host` | Development server | +| `build` | `vite build` | Production build | +| `preview` | `vite preview` | Preview production build | +| `typecheck` | `vue-tsc --noEmit && npm run circular` | TypeScript validation plus the cycle gate | +| `circular` | `dpdm` | Fail on import cycles in the `.ts` graph | +| `generate` | `openapi-typescript-codegen` | Generate types from backend OpenAPI | +| `lint` | `eslint` | Lint `.vue`, `.js`, `.ts` files | ### OpenAPI Code Generation diff --git a/frontend/dpdm.config.ts b/frontend/dpdm.config.ts index 1fd3c0ba0..fe1b28f73 100644 --- a/frontend/dpdm.config.ts +++ b/frontend/dpdm.config.ts @@ -1,12 +1,11 @@ import { defineConfig } from "dpdm"; export default defineConfig({ - files: [ - "src/main.ts", - "src/services/api/index.ts", - "src/**/*.stories.ts", - ".storybook/preview.ts", - ], + // dpdm cannot parse SFCs, so a `.vue` import is a dead end: every `.ts` + // module is an entry so the graph does not stop at the first component. + files: ["src/**/*.ts", ".storybook/*.ts"], + // `src/console` is frozen v1 and still holds cycles we are not refactoring. + exclude: "node_modules|/src/console/", circular: true, tree: false, warning: false, diff --git a/frontend/src/services/api/index.ts b/frontend/src/services/api/index.ts index 7481112dd..f7c32a43a 100644 --- a/frontend/src/services/api/index.ts +++ b/frontend/src/services/api/index.ts @@ -1,6 +1,7 @@ import axios from "axios"; import { default as Cookies } from "js-cookie"; import { debounce } from "lodash"; +import { isAuthExemptRoute, ROUTES } from "@/plugins/routeNames"; const api = axios.create({ // This will keep the url query params on refresh @@ -131,13 +132,13 @@ api.interceptors.response.use( const params = new URLSearchParams(search); const fullPath = pathname + search; - // Static router import created api ↔ router TDZ; dynamic load only on 401. + // The router instance is pulled in lazily so the module graph stays + // acyclic; a failed chunk load just leaves the caller with the 401. // @dpdm-ignore - const { - default: router, - ROUTES, - isAuthExemptRoute, - } = await import("@/plugins/router"); + const router = await import("@/plugins/router") + .then((m) => m.default) + .catch(() => null); + if (!router) return Promise.reject(error); // Don't redirect to login if already on an auth-exempt route. // Also resolve the route from the browser URL to handle the case where diff --git a/frontend/src/utils/downloadPath.test.ts b/frontend/src/utils/downloadPath.test.ts new file mode 100644 index 000000000..626b5b75f --- /dev/null +++ b/frontend/src/utils/downloadPath.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it } from "vitest"; +import type { SimpleRom } from "@/stores/roms"; +import { getDownloadPath } from "./downloadPath"; + +function makeRom(overrides: Partial): SimpleRom { + return { + id: 1, + fs_name: "Game", + files: [], + ...overrides, + } as SimpleRom; +} + +describe("getDownloadPath", () => { + it("uses fs_name for a flat single-file rom", () => { + const rom = makeRom({ + id: 14, + fs_name: "Maniac Mansion (1989).adf", + has_nested_single_file: false, + files: [ + { id: 19, file_name: "Maniac Mansion (1989).adf" }, + ] as SimpleRom["files"], + }); + expect(getDownloadPath({ rom })).toBe( + "/api/roms/14/content/Maniac Mansion (1989).adf", + ); + }); + + it("uses the file name for a nested single-file rom", () => { + const rom = makeRom({ + id: 21, + fs_name: "Art Of Fighting", + has_nested_single_file: true, + files: [{ id: 26, file_name: "aof.zip" }] as SimpleRom["files"], + }); + expect(getDownloadPath({ rom })).toBe("/api/roms/21/content/aof.zip"); + }); + + it("uses fs_name for an unselected multi-file rom", () => { + const rom = makeRom({ + id: 24, + fs_name: "B.A.T.", + has_nested_single_file: false, + 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 })).toBe("/api/roms/24/content/B.A.T."); + }); + + 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.%20Disk1.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", + ); + }); +}); diff --git a/frontend/src/utils/index.test.ts b/frontend/src/utils/index.test.ts index 560f4f1c5..32344fbc4 100644 --- a/frontend/src/utils/index.test.ts +++ b/frontend/src/utils/index.test.ts @@ -3,7 +3,6 @@ import type { Config } from "@/stores/config"; import type { Heartbeat } from "@/stores/heartbeat"; import type { SimpleRom } from "@/stores/roms"; import { - getDownloadPath, isJsDosBundle, isJsDosEmulationSupported, isPico8EmulationSupported, @@ -19,83 +18,6 @@ function makeRom(overrides: Partial): SimpleRom { } as SimpleRom; } -describe("getDownloadPath", () => { - it("uses fs_name for a flat single-file rom", () => { - const rom = makeRom({ - id: 14, - fs_name: "Maniac Mansion (1989).adf", - has_nested_single_file: false, - files: [ - { id: 19, file_name: "Maniac Mansion (1989).adf" }, - ] as SimpleRom["files"], - }); - expect(getDownloadPath({ rom })).toBe( - "/api/roms/14/content/Maniac Mansion (1989).adf", - ); - }); - - it("uses the file name for a nested single-file rom", () => { - const rom = makeRom({ - id: 21, - fs_name: "Art Of Fighting", - has_nested_single_file: true, - files: [{ id: 26, file_name: "aof.zip" }] as SimpleRom["files"], - }); - expect(getDownloadPath({ rom })).toBe("/api/roms/21/content/aof.zip"); - }); - - it("uses fs_name for an unselected multi-file rom", () => { - const rom = makeRom({ - id: 24, - fs_name: "B.A.T.", - has_nested_single_file: false, - 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 })).toBe("/api/roms/24/content/B.A.T."); - }); - - 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.%20Disk1.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", - ); - }); -}); - function makeHeartbeat( emulation: Partial = {}, ): Heartbeat {