import { beforeEach, describe, expect, it, vi } from "vitest"; const envMock = vi.hoisted(() => ({ env: { NEXT_PUBLIC_IMAGER_URL: undefined as string | undefined, NEXT_PUBLIC_APP_URL: undefined as string | undefined, IMAGING_UPSTREAM_URL: undefined as string | undefined, }, })); vi.mock("@/env", () => envMock); import { getAvatarUrl, resolveImagerBase, resolveUpstreamBase } from "./imager"; describe("resolveImagerBase", () => { beforeEach(() => { envMock.env.NEXT_PUBLIC_IMAGER_URL = undefined; envMock.env.NEXT_PUBLIC_APP_URL = undefined; }); it("prefers a configured imager env var", () => { envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com/"; expect(resolveImagerBase()).toBe("https://img.example.com"); }); it("ignores the broken self-hosted proxy env route", () => { envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com/api/imaging/avatar"; envMock.env.NEXT_PUBLIC_APP_URL = "https://app.example.com/"; expect(resolveImagerBase()).toBe("https://app.example.com/imaging"); }); it("falls back to app URL imaging route", () => { envMock.env.NEXT_PUBLIC_APP_URL = "https://app.example.com/"; expect(resolveImagerBase()).toBe("https://app.example.com/imaging"); }); it("falls back to relative /imaging", () => { expect(resolveImagerBase()).toBe("/imaging"); }); it("uses configured setting when valid", () => { expect(resolveImagerBase("https://cdn.example.com/imager/")).toBe( "https://cdn.example.com/imager", ); }); it("rejects configured setting that is the broken proxy route", () => { expect( resolveImagerBase("https://cdn.example.com/api/imaging/avatar"), ).toBe("/imaging"); }); }); describe("resolveUpstreamBase", () => { beforeEach(() => { envMock.env.IMAGING_UPSTREAM_URL = undefined; }); it("uses env upstream when configured", () => { envMock.env.IMAGING_UPSTREAM_URL = "https://up.example.com/"; expect(resolveUpstreamBase()).toBe("https://up.example.com"); }); it("falls back to Habbo public imager", () => { expect(resolveUpstreamBase()).toBe( "https://www.habbo.com/habbo-imaging/avatarimage", ); }); }); describe("getAvatarUrl", () => { beforeEach(() => { envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com"; envMock.env.NEXT_PUBLIC_APP_URL = undefined; }); it("builds a query string with defaults", () => { const url = getAvatarUrl("hd-180-1"); expect(url.startsWith("https://img.example.com?")).toBe(true); expect(url).toContain("figure=hd-180-1"); expect(url).toContain("direction=2"); expect(url).toContain("head_direction=3"); expect(url).toContain("size=m"); expect(url).toContain("img_format=png"); }); it("honors custom options", () => { const url = getAvatarUrl("hd-180-1", { size: "l", direction: 1, headDirection: 2, headOnly: true, gesture: "sml", action: "sit", }); expect(url).toContain("size=l"); expect(url).toContain("direction=1"); expect(url).toContain("head_direction=2"); expect(url).toContain("headonly=1"); expect(url).toContain("gesture=sml"); expect(url).toContain("action=sit"); }); it("uses app URL imaging route when imager env absent", () => { envMock.env.NEXT_PUBLIC_IMAGER_URL = undefined; envMock.env.NEXT_PUBLIC_APP_URL = "https://app.example.com/"; const url = getAvatarUrl("hd-180-1"); expect(url.startsWith("https://app.example.com/imaging?")).toBe(true); }); it("escapes figure values in the query string", () => { const url = getAvatarUrl("hd-180-1&x=1"); expect(url).not.toContain("&x=1"); }); });