test(player): hold the session stop and the kept-point rule to the server
- The client's resume thresholds equal the server's (read from both sources). - The session stop's URL matches the route the server serves it on. - The player stops its server session by its item and its own id on an unload and on close, and keeps it when the page only goes into the back/forward cache.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// @vitest-environment jsdom
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { api } from "../api";
|
||||
|
||||
// The session stop is a keepalive beacon: nothing answers it on screen, so a URL that no longer
|
||||
// matches the server's route fails silently — every transcode the player leaves runs on until the
|
||||
// idle reaper. The route is read from the server's source, as the other contract tests do.
|
||||
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.
|
||||
const source = readFileSync(
|
||||
join(process.cwd(), "../backend/app/routes/plex/playback.py"),
|
||||
"utf8",
|
||||
);
|
||||
const prefix = /APIRouter\(prefix="([^"]+)"/.exec(source)?.[1];
|
||||
const path = /@router\.post\("(\/stream\/[^"]*\/stop)"\)/.exec(source)?.[1];
|
||||
// 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, "[^/]+");
|
||||
return new RegExp(`^${pattern}$`);
|
||||
}
|
||||
|
||||
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(fetch.mock.calls[0]![1]).toMatchObject({ method: "POST", keepalive: true });
|
||||
});
|
||||
Reference in New Issue
Block a user