import { describe, expect, it, vi } from "vitest"; import { RECENT_FURNI_RESYNC_URL, requestRecentFurniResync, } from "./recent-furni-resync"; describe("requestRecentFurniResync", () => { it("POSTs to the resync endpoint and maps the payload", async () => { const fetcher = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ examined: 10, resynced: 4, failed: 1, rconOk: true, errors: [{ classname: "sofa", message: "boom" }], }), }); const result = await requestRecentFurniResync(fetcher); expect(fetcher).toHaveBeenCalledWith(RECENT_FURNI_RESYNC_URL, { method: "POST", }); expect(result).toEqual({ examined: 10, resynced: 4, failed: 1, rconOk: true, errors: [{ classname: "sofa", message: "boom" }], }); }); it("throws with the server error message on failure", async () => { const fetcher = vi.fn().mockResolvedValue({ ok: false, json: async () => ({ error: "no permission" }), }); await expect(requestRecentFurniResync(fetcher)).rejects.toThrow( "no permission", ); }); it("falls back to a generic error message", async () => { const fetcher = vi.fn().mockResolvedValue({ ok: false, json: async () => ({}), }); await expect(requestRecentFurniResync(fetcher)).rejects.toThrow( "Furni resync failed", ); }); it("filters malformed error entries and coerces counts", async () => { const fetcher = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ examined: "10", resynced: undefined, failed: null, rconOk: "yes", errors: [{ classname: "a", message: "b" }, { nope: 1 }, "x"], }), }); const result = await requestRecentFurniResync(fetcher); expect(result.examined).toBe(10); expect(result.resynced).toBe(0); expect(result.failed).toBe(0); expect(result.rconOk).toBe(false); expect(result.errors).toEqual([{ classname: "a", message: "b" }]); }); });