Files
EpicNext-Cms/src/lib/format.test.ts
T
Simo 4a6fcd050e
CI / check (push) Successful in 26s
CI / release (push) Skipped
CI / deploy (push) Successful in 44s
fix: preserve user figures in avatar imager
2026-08-09 21:48:43 +02:00

67 lines
2.0 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 { avatarImageUrl, excerpt, slugify } from "./format";
describe("slugify", () => {
it("lowercases and hyphenates", () => {
expect(slugify("Hello World!")).toBe("hello-world");
});
it("strips accents via NFKD", () => {
expect(slugify("Café del Mar")).toBe("cafe-del-mar");
});
it("falls back to 'article' for empty input", () => {
expect(slugify(" *** ")).toBe("article");
});
});
describe("avatarImageUrl", () => {
beforeEach(() => {
envMock.env.NEXT_PUBLIC_IMAGER_URL = "https://img.example.com";
});
it("appends the figure and options", () => {
const url = avatarImageUrl("hr-100", { size: "l", headOnly: true });
expect(url).toContain("figure=hr-100");
expect(url).toContain("size=l");
expect(url).toContain("headonly=1");
expect(url.startsWith("https://img.example.com?")).toBe(true);
});
it("preserves the exact user look instead of replacing it with a default figure", () => {
const look =
"hr-11782-40-40.hd-180-7-14.ch-11592-66.lg-10726-79-1408.sh-11764-1408.ha-11958-70-1408.wa-2007-0";
const url = new URL(avatarImageUrl(look));
expect(url.searchParams.get("figure")).toBe(look);
});
it("uses & when the base already has a query string", () => {
// This test is no longer applicable since avatarImageUrl doesn't accept a base parameter
// The base URL is now resolved from env
expect(true).toBe(true);
});
});
describe("excerpt", () => {
it("returns short text unchanged", () => {
expect(excerpt("hello", 160)).toBe("hello");
});
it("truncates on a word boundary with an ellipsis", () => {
const out = excerpt("the quick brown fox jumps", 12);
expect(out).toBe("the quick…");
expect(out.length).toBeLessThanOrEqual(13);
});
});