Files
fristydev/lib/game/lanyard.test.ts
T
fristy ed149ded52
CI / Code Health (pull_request) Successful in 24s
CI / Lint, Test & Build (pull_request) Successful in 59s
fix: prettier formatting and downgrade upload-artifact to v3 for Gitea
2026-09-12 00:47:38 +03:00

110 lines
3.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import {
activityImageUrl,
discordAvatarUrl,
formatSpotifyList,
getLanyard,
spotifyTrackUrl,
type LanyardActivity,
} from "@/lib/game/lanyard";
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
function activity(overrides: Partial<LanyardActivity> = {}): LanyardActivity {
return { id: "1", name: "Game", type: 0, ...overrides };
}
describe("activityImageUrl", () => {
it("resolves external media keys", () => {
expect(activityImageUrl(activity({ assets: { large_image: "mp:abc" } }), "large")).toBe(
"https://media.discordapp.net/external/abc",
);
});
it("resolves Spotify keys", () => {
expect(
activityImageUrl(activity({ assets: { large_image: "spotify:img" } }), "large"),
).toBe("https://i.scdn.co/image/img");
});
it("resolves app assets with application id", () => {
expect(
activityImageUrl(
activity({ application_id: "9", assets: { large_image: "art" } }),
"large",
),
).toBe("https://cdn.discordapp.com/app-assets/9/art.png");
});
it("returns null without a key", () => {
expect(activityImageUrl(activity(), "large")).toBeNull();
});
it("picks small vs large art", () => {
const withBoth = activity({
assets: { large_image: "mp:big", small_image: "mp:small" },
});
expect(activityImageUrl(withBoth, "small")).toContain("small");
expect(activityImageUrl(withBoth, "large")).toContain("big");
});
});
describe("formatSpotifyList", () => {
it("normalizes separators", () => {
expect(formatSpotifyList("a;b, ,c")).toBe("a,b,c");
expect(formatSpotifyList("a b")).toBe("a b");
});
it("returns empty for nullish input", () => {
expect(formatSpotifyList(null)).toBe("");
expect(formatSpotifyList(undefined)).toBe("");
});
});
describe("spotifyTrackUrl", () => {
it("builds the track URL", () => {
expect(spotifyTrackUrl("xyz")).toBe("https://open.spotify.com/track/xyz");
});
});
describe("discordAvatarUrl", () => {
it("falls back to the default avatar", () => {
expect(discordAvatarUrl("1", null)).toContain("embed/avatars/0.png");
});
it("picks gif for animated hashes", () => {
expect(discordAvatarUrl("1", "a_hash")).toContain(".gif");
expect(discordAvatarUrl("1", "hash")).toContain(".png");
});
});
describe("getLanyard", () => {
it("returns data on success and null otherwise", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => ({ ok: true, json: async () => ({ success: true, data: {} }) })),
);
await expect(getLanyard()).resolves.toEqual({});
vi.stubGlobal(
"fetch",
vi.fn(async () => ({ ok: true, json: async () => ({ success: false }) })),
);
await expect(getLanyard()).resolves.toBeNull();
vi.stubGlobal(
"fetch",
vi.fn(async () => ({ ok: false, status: 500 }) as Response),
);
await expect(getLanyard()).resolves.toBeNull();
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new Error("down");
}),
);
await expect(getLanyard()).resolves.toBeNull();
});
});