// @vitest-environment jsdom // // Every Plex read that returns cards carries the reader's language, so the server can serve the // Hungarian titles. The grid and the detail pages always did; these reads did not. import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { plexApi } from "./plex"; const fetchMock = vi.fn(); beforeEach(() => { fetchMock.mockReset().mockResolvedValue({ ok: true, status: 200, headers: new Headers({ "content-type": "application/json" }), json: async () => ({}), text: async () => "{}", }); vi.stubGlobal("fetch", fetchMock); }); afterEach(() => vi.unstubAllGlobals()); const lastUrl = (): string => { const calls = fetchMock.mock.calls; return String(calls[calls.length - 1]?.[0]); }; describe("Plex card reads in the reader's language", () => { it.each([ ["a playlist", () => plexApi.plexPlaylist(5, "hu"), () => plexApi.plexPlaylist(5, "en")], ["a person", () => plexApi.plexPerson("Anna", "hu"), () => plexApi.plexPerson("Anna", "en")], [ "a collection", () => plexApi.plexCollection("c1", { key: "title", dir: "asc" }, "hu"), () => plexApi.plexCollection("c1", { key: "title", dir: "asc" }, "en"), ], ])("%s asks for Hungarian, and leaves English implied", async (_what, hu, en) => { await hu(); expect(lastUrl()).toContain("lang=hu"); await en(); expect(lastUrl()).not.toContain("lang="); }); });