| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { avatarHue, avatarInitial, avatarPath, avatarSize, initialsSvg } from "../../src/http/avatars.js"; |
| 3 | |
| 4 | describe("avatar URLs and initials", () => { |
| 5 | it("snaps requested sizes to the stored ones", () => { |
| 6 | expect([1, 20, 64, 65, 128, 129, 256, 4000].map(avatarSize)).toEqual([64, 64, 64, 128, 128, 256, 256, 256]); |
| 7 | }); |
| 8 | |
| 9 | it("builds image paths with an optional size and version", () => { |
| 10 | expect(avatarPath("dev")).toBe("/api/avatars/dev"); |
| 11 | expect(avatarPath("dev", 128)).toBe("/api/avatars/dev"); |
| 12 | expect(avatarPath("dev", 64, 1791892043)).toBe("/api/avatars/dev?s=64&v=1791892043"); |
| 13 | expect(avatarPath("a.b", 256, null)).toBe("/api/avatars/a.b?s=256"); |
| 14 | }); |
| 15 | |
| 16 | it("draws initials like the old .av span: same hue hash, first letter or digit", () => { |
| 17 | // The hash the web UI used before pictures (h * 31 + code, mod 360). |
| 18 | const old = (s: string) => [...s].reduce((h, ch) => (h * 31 + ch.charCodeAt(0)) >>> 0, 0) % 360; |
| 19 | for (const login of ["dev", "acme", "Claude-bot", "x"]) expect(avatarHue(login)).toBe(old(login)); |
| 20 | expect(avatarInitial("dev")).toBe("D"); |
| 21 | expect(avatarInitial("-9lives")).toBe("9"); |
| 22 | expect(avatarInitial("---")).toBe("?"); |
| 23 | const svg = initialsSvg("dev", 64); |
| 24 | expect(svg).toContain(`fill="hsl(${avatarHue("dev")},55%,55%)"`); |
| 25 | expect(svg).toContain('width="64" height="64"'); |
| 26 | expect(svg).toMatch(/>D<\/text><\/svg>$/); |
| 27 | // Logins are restricted, but the SVG never carries more than one [A-Za-z0-9?] character of them. |
| 28 | expect(initialsSvg("<script>", 128)).toMatch(/>S<\/text>/); |
| 29 | }); |
| 30 | }); |