test(player): pin the stop URL's ids, the beacon's settle, the kept point's edges
The stop-URL contract passed with the two ids swapped and read the backend from the working directory; nothing called a real beacon's onSettled or its library pass-through; the kept-point rule was held by its constants only.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// @vitest-environment jsdom
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { resolve } from "node:path";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { api } from "../api";
|
||||
|
||||
@@ -10,10 +10,9 @@ import { api } from "../api";
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
function serverRoute(): RegExp {
|
||||
// From the frontend root (vitest's cwd): this file runs under jsdom — `api` boots i18n, which reads
|
||||
// localStorage — and there `import.meta.url` is not a file URL `readFileSync` takes.
|
||||
// Anchored to this file, not the working directory, so the test holds wherever vitest starts.
|
||||
const source = readFileSync(
|
||||
join(process.cwd(), "../backend/app/routes/plex/playback.py"),
|
||||
resolve(__dirname, "../../../../backend/app/routes/plex/playback.py"),
|
||||
"utf8",
|
||||
);
|
||||
const prefix = /APIRouter\(prefix="([^"]+)"/.exec(source)?.[1];
|
||||
@@ -21,7 +20,9 @@ function serverRoute(): RegExp {
|
||||
// If either is missing the route was renamed or moved — re-point this test, the rule stands.
|
||||
expect(prefix, "router prefix").toBeDefined();
|
||||
expect(path, "the session stop route").toBeDefined();
|
||||
const pattern = (prefix! + path!).replace(/\{[^}]+\}/g, "[^/]+");
|
||||
// Each path parameter becomes a group of its own NAME: two ids that swapped places still match a
|
||||
// pattern of anonymous wildcards, and the server then finds no session to stop.
|
||||
const pattern = (prefix! + path!).replace(/\{(\w+)\}/g, "(?<$1>[^/]+)");
|
||||
return new RegExp(`^${pattern}$`);
|
||||
}
|
||||
|
||||
@@ -29,8 +30,6 @@ it("sends the session stop to the route the server serves it on", () => {
|
||||
const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(null));
|
||||
api.plexStopSession("12345", "a1b2c3");
|
||||
const url = String(fetch.mock.calls[0]![0]);
|
||||
expect(url).toMatch(serverRoute());
|
||||
expect(url).toContain("/12345/");
|
||||
expect(url).toContain("/a1b2c3/");
|
||||
expect(url.match(serverRoute())?.groups).toEqual({ rating_key: "12345", session_id: "a1b2c3" });
|
||||
expect(fetch.mock.calls[0]![1]).toMatchObject({ method: "POST", keepalive: true });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user