import { describe, expect, it, vi, afterEach } from "vitest"; import { fetchSchedule, fetchServerNow } from "./watchparty"; const mockFetch = (payload: unknown, ok = true, status = 200) => { const response = new Response(JSON.stringify(payload), { status, statusText: ok ? "OK" : "Error" }); return vi.spyOn(globalThis, "fetch").mockResolvedValue(response as Response); }; afterEach(() => { vi.restoreAllMocks(); }); describe("fetchSchedule", () => { it("normalizes schedule payload", async () => { const spy = mockFetch({ ep_num: "2", ep_title: "Title", season_name: "S1", start_time: "10:00", playback_length: "01:00:00", }); const res = await fetchSchedule(); expect(res.ep_num).toBe(2); expect(res.ep_title).toBe("Title"); expect(spy).toHaveBeenCalled(); }); }); describe("fetchServerNow", () => { it("converts seconds to ms", async () => { mockFetch({ now: 1700000000 }); // seconds const ms = await fetchServerNow(); expect(ms).toBe(1700000000 * 1000); }); });