64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { getAvatarUrl } from "./imager";
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
describe("getAvatarUrl", () => {
|
|
beforeEach(() => {
|
|
vi.stubEnv("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("/api/imaging/avatar?")).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 the local runtime proxy when no env var is set", () => {
|
|
vi.stubEnv("NEXT_PUBLIC_IMAGER_URL", "");
|
|
const url = getAvatarUrl("hd-180-1");
|
|
expect(url.startsWith("/api/imaging/avatar?")).toBe(true);
|
|
expect(url).toContain("img_format=apng");
|
|
expect(url).toContain("effect=14");
|
|
});
|
|
});
|