import { afterEach, describe, expect, it, vi } from "vitest"; import { ENV } from "./_core/env"; import { getRemoteMediaSession } from "./mediaService"; const originalMediaServiceUrl = ENV.mediaServiceUrl; const originalFetch = global.fetch; afterEach(() => { ENV.mediaServiceUrl = originalMediaServiceUrl; global.fetch = originalFetch; vi.restoreAllMocks(); }); describe("getRemoteMediaSession", () => { it("falls back to /media-prefixed routes when the root route returns 404", async () => { ENV.mediaServiceUrl = "http://127.0.0.1:8081"; const fetchMock = vi.fn() .mockResolvedValueOnce({ ok: false, status: 404, text: vi.fn().mockResolvedValue("404 page not found\n"), statusText: "Not Found", }) .mockResolvedValueOnce({ ok: true, json: vi.fn().mockResolvedValue({ session: { id: "session-1", userId: "1", title: "demo", archiveStatus: "idle", playback: { ready: false, }, }, }), }); global.fetch = fetchMock as typeof fetch; const session = await getRemoteMediaSession("session-1"); expect(session.id).toBe("session-1"); expect(fetchMock).toHaveBeenNthCalledWith( 1, "http://127.0.0.1:8081/sessions/session-1", expect.objectContaining({ signal: expect.any(AbortSignal) }), ); expect(fetchMock).toHaveBeenNthCalledWith( 2, "http://127.0.0.1:8081/media/sessions/session-1", expect.objectContaining({ signal: expect.any(AbortSignal) }), ); }); it("uses the configured /media base URL directly when already present", async () => { ENV.mediaServiceUrl = "http://media:8081/media"; const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({ session: { id: "session-2", userId: "2", title: "demo", archiveStatus: "processing", playback: { ready: false, }, }, }), }); global.fetch = fetchMock as typeof fetch; const session = await getRemoteMediaSession("session-2"); expect(session.id).toBe("session-2"); expect(fetchMock).toHaveBeenCalledTimes(1); expect(fetchMock).toHaveBeenCalledWith( "http://media:8081/media/sessions/session-2", expect.objectContaining({ signal: expect.any(AbortSignal) }), ); }); });