test: add unit tests for pure logic modules

Add 22 test files covering imager, soundtracks, browser-headers, source-keys (figure/pet/effect), effect-source, plus catalog-translations, catalog-layouts, client-translation-files, translations-utils, and various admin/services/helpers modules. Total test count increases by 120+.
This commit is contained in:
openhands committed 2026-08-07 18:53:59 +02:00
1 parent 11e8b06bf8
commit 82e8448f26
25 files changed
+1400 -304

No files matched your search

+96 -30
View File
@@ -1,50 +1,116 @@
import { describe, expect, it } from "vitest";
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", () => {
it("defaults to /imaging when no public URL env is set", () => {
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");
expect(resolveImagerBase("")).toBe("/imaging");
expect(resolveImagerBase(null)).toBe("/imaging");
});
it("rejects the broken self-hosted proxy", () => {
expect(resolveImagerBase("/api/imaging/avatar")).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://www.epicnabbo.nl/api/imaging/avatar"),
resolveImagerBase("https://cdn.example.com/api/imaging/avatar"),
).toBe("/imaging");
});
it("accepts /imaging as a valid configured URL", () => {
expect(resolveImagerBase("/imaging")).toBe("/imaging");
});
it("keeps a custom Habbo-compatible base", () => {
expect(
resolveImagerBase("https://www.habbo.com/habbo-imaging/avatarimage/"),
).toBe("https://www.habbo.com/habbo-imaging/avatarimage");
});
});
describe("resolveUpstreamBase", () => {
it("defaults to Habbo's public imager", () => {
const url = resolveUpstreamBase();
expect(url).toBe("https://www.habbo.com/habbo-imaging/avatarimage");
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", () => {
it("builds an imager URL", () => {
const url = getAvatarUrl("hr-115-42.hd-180-1", {
size: "l",
headOnly: true,
direction: 2,
});
expect(url.startsWith("/imaging?")).toBe(true);
expect(url).toContain("figure=hr-115-42.hd-180-1");
expect(url).toContain("size=l");
expect(url).toContain("headonly=1");
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");
});
});