import { describe, expect, it } from "vitest"; import sharp from "sharp"; import { appIconPfad, validateAppIcon, iconQuelle, iconVersion, iconUrl, parseIconAnfrage, renderAppIcon, type IconBranding, } from "./app-icon"; const basis: IconBranding = { id: "00000000-0000-0000-0000-00000000f001", name: "Widmann", logo_pfad: null, app_icon_pfad: null, farbe_primaer: "#0071ed", }; describe("appIconPfad", () => { it("baut den Storage-Pfad", () => { expect(appIconPfad("f", "png", 5)).toBe("f/app-icon-5.png"); }); }); describe("validateAppIcon", () => { it("lehnt unbekannte Typen und zu grosse Dateien ab", () => { expect(validateAppIcon({ type: "image/gif", size: 10 }).valid).toBe(false); expect(validateAppIcon({ type: "image/png", size: 1024 * 1024 + 1 }).valid).toBe(false); }); it("akzeptiert SVG", () => { expect(validateAppIcon({ type: "image/svg+xml", size: 10 })).toEqual({ valid: true, endung: "svg" }); }); it("nennt App-Icon in der Fehlermeldung", () => { const r = validateAppIcon({ type: "image/png", size: 1024 * 1024 + 1 }); expect(!r.valid && r.error).toContain("App-Icon"); }); }); describe("iconQuelle", () => { it("Fallback-Kette", () => { expect(iconQuelle({ ...basis, app_icon_pfad: "a.png", logo_pfad: "l.png" })).toEqual({ art: "app-icon", pfad: "a.png" }); expect(iconQuelle({ ...basis, logo_pfad: "l.png" })).toEqual({ art: "logo", pfad: "l.png" }); expect(iconQuelle({ ...basis, name: " widmann" })).toEqual({ art: "buchstabe", buchstabe: "W" }); expect(iconQuelle(null)).toEqual({ art: "buchstabe", buchstabe: "A" }); }); }); describe("iconVersion / iconUrl", () => { it("null ist standard", () => { expect(iconVersion(null)).toBe("standard"); }); it("aendert sich mit den Feldern, bleibt sonst gleich", () => { const v = iconVersion(basis); expect(iconVersion({ ...basis })).toBe(v); expect(iconVersion({ ...basis, app_icon_pfad: "x" })).not.toBe(v); expect(iconVersion({ ...basis, logo_pfad: "x" })).not.toBe(v); expect(iconVersion({ ...basis, farbe_primaer: "#000000" })).not.toBe(v); expect(iconVersion({ ...basis, name: "Andere" })).not.toBe(v); }); it("URL unterscheidet Firmen mit gleichen Daten", () => { const b = { ...basis, id: "00000000-0000-0000-0000-00000000f002" }; expect(iconUrl(basis, "192")).not.toBe(iconUrl(b, "192")); expect(iconUrl(null, "512-maskable")).toBe("/app-icon/standard/standard/512-maskable.png"); expect(iconUrl(basis, "192")).toBe(`/app-icon/${basis.id}/${iconVersion(basis)}/192.png`); }); }); describe("parseIconAnfrage", () => { it("parst gueltige Anfragen", () => { expect(parseIconAnfrage("standard", "192.png")).toEqual({ firmaId: null, groesse: "192" }); expect(parseIconAnfrage(basis.id, "512-maskable.png")).toEqual({ firmaId: basis.id, groesse: "512-maskable" }); }); it("lehnt Ungueltiges ab", () => { expect(parseIconAnfrage("keine-uuid", "192.png")).toBeNull(); expect(parseIconAnfrage("standard", "999.png")).toBeNull(); expect(parseIconAnfrage("standard", "192.svg")).toBeNull(); expect(parseIconAnfrage("../x", "192.png")).toBeNull(); }); }); describe("renderAppIcon", () => { const png = () => sharp({ create: { width: 40, height: 20, channels: 4, background: "#ff0000" } }).png().toBuffer(); const svg = Buffer.from( '' ); async function pruefe(buf: Buffer, n: number) { expect([...buf.subarray(0, 4)]).toEqual([0x89, 0x50, 0x4e, 0x47]); const m = await sharp(buf).metadata(); expect(m.width).toBe(n); expect(m.height).toBe(n); } it.each([ ["192", 192], ["180", 180], ["512", 512], ["512-maskable", 512], ] as const)("rendert alle Arten in Groesse %s", async (groesse, n) => { const farbe = "#0071ed"; await pruefe(await renderAppIcon({ quelle: await png(), art: "app-icon", farbe, groesse }), n); await pruefe(await renderAppIcon({ quelle: await png(), art: "logo", farbe, groesse }), n); await pruefe(await renderAppIcon({ quelle: svg, art: "app-icon", farbe, groesse }), n); await pruefe(await renderAppIcon({ quelle: svg, art: "logo", farbe, groesse }), n); await pruefe(await renderAppIcon({ quelle: null, art: "buchstabe", buchstabe: "W", farbe, groesse }), n); }); it("maskable: Rand hat Hintergrundfarbe", async () => { const buf = await renderAppIcon({ quelle: await png(), art: "logo", farbe: "#0071ed", groesse: "512-maskable" }); const data = await sharp(buf).raw().toBuffer(); expect([data[0], data[1], data[2]]).toEqual([0x00, 0x71, 0xed]); }); });