providerLogoAssets.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. interface ProviderLogoSourceManifest {
  4. logos: Array<{ file: string }>;
  5. }
  6. const assetDirectory = new URL("../src/assets/provider-icons/", import.meta.url);
  7. const manifest = JSON.parse(
  8. readFileSync(new URL("model-provider-logo-sources.json", assetDirectory), "utf8"),
  9. ) as ProviderLogoSourceManifest;
  10. function readPngSize(bytes: Buffer): { width: number; height: number } {
  11. expect(bytes.subarray(1, 4).toString("ascii")).toBe("PNG");
  12. return {
  13. width: bytes.readUInt32BE(16),
  14. height: bytes.readUInt32BE(20),
  15. };
  16. }
  17. function readSvgSize(source: string): { width: number; height: number } {
  18. const viewBox = source.match(/viewBox=["']\s*[-\d.]+\s+[-\d.]+\s+([\d.]+)\s+([\d.]+)\s*["']/u);
  19. if (viewBox) return { width: Number(viewBox[1]), height: Number(viewBox[2]) };
  20. const width = source.match(/\bwidth=["']([\d.]+)/u)?.[1];
  21. const height = source.match(/\bheight=["']([\d.]+)/u)?.[1];
  22. if (!width || !height) throw new Error("SVG 缺少可机械验证的固有尺寸");
  23. return { width: Number(width), height: Number(height) };
  24. }
  25. describe("Provider built-in logo assets", () => {
  26. it("Z.AI PNG 原样复用无额外留白的应用图标,不再误用同名旧 SVG", () => {
  27. const source = readFileSync(new URL("../../desktop/build/icons/128x128.png", import.meta.url));
  28. const packaged = readFileSync(new URL("model-provider-zai-app.png", assetDirectory));
  29. expect(packaged.equals(source)).toBe(true);
  30. expect(readPngSize(packaged)).toEqual({ width: 128, height: 128 });
  31. });
  32. it("来源清单中的每个内置 Logo 素材本身都是方形", () => {
  33. for (const { file } of manifest.logos) {
  34. const assetUrl = new URL(file, assetDirectory);
  35. const size = file.endsWith(".png")
  36. ? readPngSize(readFileSync(assetUrl))
  37. : readSvgSize(readFileSync(assetUrl, "utf8"));
  38. expect(size, file).toEqual({ width: size.width, height: size.width });
  39. }
  40. });
  41. });