mirror of
https://github.com/rommapp/romm.git
synced 2026-12-05 05:07:31 +01:00
fix(frontend): widen the dpdm gate and harden the 401 redirect
code-review pass. dpdm cannot parse Vue SFCs, so the four entry points only reached 133 of the frontend's modules: every `.vue` import was an unresolved dead end and traversal stopped there. Make every `.ts` module an entry instead, which brings the analysed graph to 771 modules. Frozen v1 `src/console` is carved out because it still holds cycles we are not refactoring. Take `ROUTES` and `isAuthExemptRoute` from the leaf module statically, and keep only the router instance lazy. Guard the dynamic import so a failed chunk load still rejects with the original AxiosError. Move the `getDownloadPath` tests next to the module that now owns them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
89
frontend/src/utils/downloadPath.test.ts
Normal file
89
frontend/src/utils/downloadPath.test.ts
Normal file
@@ -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>): 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",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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>): 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["EMULATION"]> = {},
|
||||
): Heartbeat {
|
||||
|
||||
Reference in New Issue
Block a user