Files
Epicnabbo-Catalogus-Updated…/src/lib/imager.test.ts
T
openhands 0aef27efc4 fix: omit default params in avatar URL for cleaner URLs
- getAvatarUrl now omits direction=2, head_direction=3, size=m when they match defaults
- URL now matches: figure=xxx&effect=14&img_format=apng
2026-08-09 20:03:26 +02:00

95 lines
2.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const envMock = vi.hoisted(() => ({
env: {
NEXT_PUBLIC_IMAGER_URL: "https://img.example.com" as string | undefined,
NEXT_PUBLIC_APP_URL: undefined as string | undefined,
IMAGING_UPSTREAM_URL: "https://up.example.com" as string | undefined,
},
}));
vi.mock("@/env", () => envMock);
import { getAvatarUrl, resolveImagerBase } from "./imager";
describe("resolveImagerBase", () => {
beforeEach(() => {
envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com";
});
it("returns configured imager env var", () => {
expect(resolveImagerBase()).toBe("https://img.example.com");
});
it("strips trailing slashes", () => {
envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com/";
expect(resolveImagerBase()).toBe("https://img.example.com");
});
it("falls back to epicnabbo.nl when env var is not set", () => {
envMock.env.NEXT_PUBLIC_IMAGER_URL = undefined;
expect(resolveImagerBase()).toBe(
"https://epicnabbo.nl/imaging/avatarimage",
);
});
});
describe("getAvatarUrl", () => {
beforeEach(() => {
envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com";
});
it("builds a query string with defaults (omits default params)", () => {
const url = getAvatarUrl("hd-180-1");
expect(url.startsWith("https://img.example.com?")).toBe(true);
expect(url).toContain("figure=hd-180-1");
expect(url).not.toContain("direction=2");
expect(url).not.toContain("head_direction=3");
expect(url).not.toContain("size=m");
expect(url).toContain("img_format=apng");
expect(url).toContain("effect=14");
});
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("escapes figure values in the query string", () => {
const url = getAvatarUrl("hd-180-1&x=1");
expect(url).not.toContain("&x=1");
});
it("allows custom img_format", () => {
const url = getAvatarUrl("hd-180-1", { img_format: "png" });
expect(url).toContain("img_format=png");
});
it("allows custom effect", () => {
const url = getAvatarUrl("hd-180-1", { effect: 0 });
expect(url).toContain("effect=0");
});
it("uses epicnabbo.nl when no env var is set", () => {
envMock.env.NEXT_PUBLIC_IMAGER_URL = undefined;
const url = getAvatarUrl("hd-180-1");
expect(url.startsWith("https://epicnabbo.nl/imaging/avatarimage?")).toBe(
true,
);
expect(url).toContain("img_format=apng");
expect(url).toContain("effect=14");
});
});