Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

13.6 KB
Raw Blame
import { createRequire } from "node:module"
import { readFile } from "node:fs/promises"
import { resolve } from "node:path"
import { describe, expect, it } from "vitest"
import {
  androidAdaptiveForegroundSizes,
  androidSplashArtworkSizes,
  androidSplashSizes,
  androidSplashSystemIconSize,
  appleSplashSizes,
  assertSvgContract,
  canonicalizeIcns,
  desktopRasterAssets,
  fullBleedSvg,
  iosRasterAssets,
  mobileSplashVisibleSize,
  preservedAssets,
  trayRasterAssets,
} from "../generate-logo-assets.mjs"

const repoRoot = resolve(import.meta.dirname, "../..")
const requireFromRoot = createRequire(resolve(repoRoot, "package.json"))
const sharp = requireFromRoot("sharp")

const androidLegacyIconSizes = {
  mdpi: 48,
  hdpi: 49,
  xhdpi: 96,
  xxhdpi: 144,
  xxxhdpi: 192,
} as const

const desktopPngSizes = {
  "32x32.png": 32,
  "64x64.png": 64,
  "128x128.png": 128,
  "[email protected]": 256,
  "icon.png": 512,
  "Square30x30Logo.png": 30,
  "Square44x44Logo.png": 44,
  "Square71x71Logo.png": 71,
  "Square89x89Logo.png": 89,
  "Square107x107Logo.png": 107,
  "Square142x142Logo.png": 142,
  "Square150x150Logo.png": 150,
  "Square284x284Logo.png": 284,
  "Square310x310Logo.png": 310,
  "StoreLogo.png": 50,
} as const

const iosPngSizes = {
  "[email protected]": 20,
  "[email protected]": 40,
  "[email protected]": 40,
  "[email protected]": 60,
  "[email protected]": 29,
  "[email protected]": 58,
  "[email protected]": 58,
  "[email protected]": 87,
  "[email protected]": 40,
  "[email protected]": 80,
  "[email protected]": 80,
  "[email protected]": 120,
  "[email protected]": 120,
  "[email protected]": 180,
  "[email protected]": 76,
  "[email protected]": 152,
  "[email protected]": 167,
  "[email protected]": 1024,
} as const

async function alphaBounds(path: string) {
  const { data, info } = await sharp(path).ensureAlpha().raw().toBuffer({ resolveWithObject: true })
  let left = info.width
  let top = info.height
  let right = 0
  let bottom = 0
  let opaque = true
  for (let y = 0; y < info.height; y += 1) {
    for (let x = 0; x < info.width; x += 1) {
      const alpha = data[(y * info.width + x) * info.channels + 3]
      if (alpha !== 255) opaque = false
      if (alpha === 0) continue
      left = Math.min(left, x)
      top = Math.min(top, y)
      right = Math.max(right, x + 1)
      bottom = Math.max(bottom, y + 1)
    }
  }
  return { width: info.width, height: info.height, bounds: [left, top, right, bottom], opaque }
}

async function visibleColors(path: string) {
  const { data, info } = await sharp(path).ensureAlpha().raw().toBuffer({ resolveWithObject: true })
  const colors = new Set<string>()
  for (let offset = 0; offset < data.length; offset += info.channels) {
    if (data[offset + 3] === 0) continue
    colors.add(`${data[offset]},${data[offset + 1]},${data[offset + 2]}`)
  }
  return colors
}

async function expressionBounds(path: string) {
  const { data, info } = await sharp(path).ensureAlpha().raw().toBuffer({ resolveWithObject: true })
  let left = info.width
  let top = info.height
  let right = 0
  let bottom = 0
  let hasBlack = false
  let hasWhite = false
  for (let y = 0; y < info.height; y += 1) {
    for (let x = 0; x < info.width; x += 1) {
      const offset = (y * info.width + x) * info.channels
      if (data[offset + 3] === 0) continue
      const isBlack = data[offset] === 0 && data[offset + 1] === 0 && data[offset + 2] === 0
      const isWhite = data[offset] === 255 && data[offset + 1] === 255 && data[offset + 2] === 255
      if (!isBlack && !isWhite) continue
      hasBlack ||= isBlack
      hasWhite ||= isWhite
      left = Math.min(left, x)
      top = Math.min(top, y)
      right = Math.max(right, x + 1)
      bottom = Math.max(bottom, y + 1)
    }
  }
  if (!hasBlack || !hasWhite) throw new Error(`${path} is missing visible expression colors`)
  return [left, top, right, bottom]
}

describe("logo asset generator", () => {
  it("locks the canonical structured transparent SVG", async () => {
    const canonical = await readFile(resolve(repoRoot, "assets/alook.svg"), "utf8")
    const publicCopy = await readFile(resolve(repoRoot, "src/web/public/alook.svg"), "utf8")

    expect(() => assertSvgContract(canonical)).not.toThrow()
    expect(publicCopy).toBe(canonical)
    expect(fullBleedSvg(canonical)).toContain("<g>")
    expect(fullBleedSvg(canonical)).not.toContain('<g clip-path="url(#alook-logo-clip)">')
  })

  it("covers every approved platform family and protects the exceptions", () => {
    expect(desktopRasterAssets).toHaveLength(17)
    expect(iosRasterAssets).toHaveLength(18)
    expect(Object.keys(desktopPngSizes)).toEqual(desktopRasterAssets.filter(file => file.endsWith(".png")))
    expect(Object.keys(iosPngSizes)).toEqual(iosRasterAssets)
    expect(preservedAssets).toEqual([
      "src/web/src/app/favicon.ico",
      "assets/readme/banner.png",
    ])
    expect(trayRasterAssets).toEqual([
      ["assets/alook-tray-macos-template.svg", "src/desktop/src-tauri/icons/tray-macos-template.png"],
      ["assets/alook.svg", "src/desktop/src-tauri/icons/tray-windows-linux-color.png"],
    ])
  })

  it("canonicalizes nondeterministic ICNS chunk order", () => {
    const chunk = (type: string, payload: string) => {
      const data = Buffer.from(payload)
      const output = Buffer.alloc(8 + data.length)
      output.write(type, 0, "ascii")
      output.writeUInt32BE(output.length, 4)
      data.copy(output, 8)
      return output
    }
    const header = Buffer.alloc(8)
    header.write("icns", 0, "ascii")
    const unordered = Buffer.concat([header, chunk("zzzz", "second"), chunk("aaaa", "first")])
    unordered.writeUInt32BE(unordered.length, 4)

    const canonical = canonicalizeIcns(unordered)
    expect(canonical.subarray(8, 12).toString("ascii")).toBe("aaaa")
    expect(canonicalizeIcns(canonical)).toEqual(canonical)
  })

  it("keeps platform tray assets generated from their SVG sources", async () => {
    for (const [source, destination] of trayRasterAssets) {
      const svg = await readFile(resolve(repoRoot, source))
      const expected = await sharp(svg).resize(36, 36).png().toBuffer()
      expect(await readFile(resolve(repoRoot, destination))).toEqual(expected)
    }

    const template = resolve(repoRoot, "src/desktop/src-tauri/icons/tray-macos-template.png")
    expect(await alphaBounds(template)).toEqual({
      width: 36,
      height: 36,
      bounds: [2, 2, 35, 33],
      opaque: false,
    })
    expect([...await visibleColors(template)]).toEqual(["0,0,0"])

    const color = resolve(repoRoot, "src/desktop/src-tauri/icons/tray-windows-linux-color.png")
    expect(await alphaBounds(color)).toMatchObject({
      width: 36,
      height: 36,
      opaque: false,
    })
    expect((await visibleColors(color)).size).toBeGreaterThan(5)
  })

  it("locks compile-time tray selection for macOS and Windows/Linux", async () => {
    const source = await readFile(resolve(repoRoot, "src/desktop/src-tauri/src/commands.rs"), "utf8")

    expect(source).toMatch(/#\[cfg\(target_os = "macos"\)\]\nconst TRAY_ICON_BYTES: &\[u8\] = include_bytes!\("\.\.\/icons\/tray-macos-template\.png"\);/)
    expect(source).toMatch(/#\[cfg\(target_os = "macos"\)\]\nconst TRAY_ICON_IS_TEMPLATE: bool = true;/)
    expect(source).toMatch(/#\[cfg\(any\(target_os = "windows", target_os = "linux"\)\)\]\nconst TRAY_ICON_BYTES: &\[u8\] = include_bytes!\("\.\.\/icons\/tray-windows-linux-color\.png"\);/)
    expect(source).toMatch(/#\[cfg\(any\(target_os = "windows", target_os = "linux"\)\)\]\nconst TRAY_ICON_IS_TEMPLATE: bool = false;/)
    expect(source).not.toMatch(/tray-(?:default|online|offline)/)
  })

  it("preserves web and platform canvas contracts", async () => {
    expect(await alphaBounds(resolve(repoRoot, "src/web/public/icon-192.png"))).toEqual({
      width: 192,
      height: 192,
      bounds: [0, 0, 192, 192],
      opaque: false,
    })
    expect(await alphaBounds(resolve(repoRoot, "src/web/public/apple-touch-icon.png"))).toEqual({
      width: 180,
      height: 180,
      bounds: [0, 0, 180, 180],
      opaque: true,
    })
    expect(await alphaBounds(resolve(repoRoot, "src/desktop/src-tauri/gen/android/app/src/main/res/mipmap-mdpi/ic_launcher.png"))).toMatchObject({
      width: 48,
      height: 48,
      bounds: [4, 4, 44, 44],
    })
    expect(await alphaBounds(resolve(repoRoot, "src/desktop/src-tauri/gen/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png"))).toMatchObject({
      width: 48,
      height: 48,
      bounds: [2, 2, 46, 46],
    })
  })

  it("aligns native mobile splash artwork with the mobile unknown/neutral frame", async () => {
    expect(mobileSplashVisibleSize).toBe(106)

    for (const [density, canvas] of Object.entries(androidSplashSizes)) {
      const artwork = androidSplashArtworkSizes[density as keyof typeof androidSplashArtworkSizes]
      const offset = Math.floor((canvas - artwork) / 2)
      const path = resolve(repoRoot, `src/desktop/src-tauri/gen/android/app/src/main/res/drawable-${density}/splash_icon.png`)
      const systemVisibleSize = artwork * androidSplashSystemIconSize / canvas
      expect(Math.abs(systemVisibleSize - mobileSplashVisibleSize)).toBeLessThanOrEqual(1)
      expect(await alphaBounds(path)).toEqual({
        width: canvas,
        height: canvas,
        bounds: [offset, offset, offset + artwork, offset + artwork],
        opaque: false,
      })
    }

    for (const [file, size] of Object.entries(appleSplashSizes)) {
      const path = resolve(repoRoot, "src/desktop/src-tauri/gen/apple/Assets.xcassets/SplashIcon.imageset", file)
      expect(await alphaBounds(path)).toEqual({
        width: size,
        height: size,
        bounds: [0, 0, size, size],
        opaque: false,
      })
    }

    const storyboard = await readFile(resolve(repoRoot, "src/desktop/src-tauri/gen/apple/LaunchScreen.storyboard"), "utf8")
    expect(storyboard).toContain('<constraint firstAttribute="width" constant="106" id="w-constraint"/>')
    expect(storyboard).toContain('<constraint firstAttribute="height" constant="106" id="h-constraint"/>')
    expect(storyboard).toContain('<image name="SplashIcon" width="106" height="106"/>')

    const serverRail = await readFile(resolve(repoRoot, "src/web/src/components/community/shell/server-rail.tsx"), "utf8")
    expect(serverRail).toContain('<AnimatedAlookLogo className="size-10"')
  })

  it("fills Android adaptive masks while keeping the expression inside the safe zone at every density", async () => {
    const adaptiveXml = await readFile(resolve(repoRoot, "src/desktop/src-tauri/gen/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml"), "utf8")
    expect(adaptiveXml).toContain('<foreground android:drawable="@mipmap/ic_launcher_foreground"/>')
    expect(adaptiveXml).toContain('<background android:drawable="@color/ic_launcher_background"/>')

    for (const [density, { canvas, artwork }] of Object.entries(androidAdaptiveForegroundSizes)) {
      const offset = Math.floor((canvas - artwork) / 2)
      const safeSize = canvas * 66 / 108
      const safeOffset = (canvas - safeSize) / 2
      const base = resolve(repoRoot, `src/desktop/src-tauri/gen/android/app/src/main/res/mipmap-${density}`)
      const foreground = resolve(base, "ic_launcher_foreground.png")
      expect(artwork).toBe(canvas * 84 / 108)
      expect(await alphaBounds(foreground)).toEqual({
        width: canvas,
        height: canvas,
        bounds: [offset, offset, offset + artwork, offset + artwork],
        opaque: false,
      })
      const [left, top, right, bottom] = await expressionBounds(foreground)
      expect(left).toBeGreaterThanOrEqual(safeOffset)
      expect(top).toBeGreaterThanOrEqual(safeOffset)
      expect(right).toBeLessThanOrEqual(safeOffset + safeSize)
      expect(bottom).toBeLessThanOrEqual(safeOffset + safeSize)
      for (const file of ["ic_launcher.png", "ic_launcher_round.png"]) {
        expect(await alphaBounds(resolve(base, file))).toMatchObject({
          width: androidLegacyIconSizes[density as keyof typeof androidLegacyIconSizes],
          height: androidLegacyIconSizes[density as keyof typeof androidLegacyIconSizes],
          opaque: false,
        })
      }
    }
  })

  it("locks Tauri Apple, macOS, Windows, and Linux icon container contracts", async () => {
    for (const [file, size] of Object.entries(iosPngSizes)) {
      const generated = resolve(repoRoot, "src/desktop/src-tauri/gen/apple/Assets.xcassets/AppIcon.appiconset", file)
      const packaged = resolve(repoRoot, "src/desktop/src-tauri/icons/ios", file)
      expect(await readFile(packaged)).toEqual(await readFile(generated))
      expect(await alphaBounds(generated)).toEqual({
        width: size,
        height: size,
        bounds: [0, 0, size, size],
        opaque: true,
      })
    }

    for (const [file, size] of Object.entries(desktopPngSizes)) {
      expect(await alphaBounds(resolve(repoRoot, "src/desktop/src-tauri/icons", file))).toMatchObject({
        width: size,
        height: size,
        bounds: [0, 0, size, size],
        opaque: false,
      })
    }

    const ico = await readFile(resolve(repoRoot, "src/desktop/src-tauri/icons/icon.ico"))
    const icoSizes = Array.from({ length: ico.readUInt16LE(4) }, (_, index) => {
      const offset = 6 + index * 16
      return [ico[offset] || 256, ico[offset + 1] || 256]
    })
    expect(ico.subarray(0, 4)).toEqual(Buffer.from([0, 0, 1, 0]))
    expect(icoSizes).toEqual([[32, 32], [16, 16], [24, 24], [48, 48], [64, 64], [256, 256]])

    const icns = await readFile(resolve(repoRoot, "src/desktop/src-tauri/icons/icon.icns"))
    expect(icns.subarray(0, 4).toString("ascii")).toBe("icns")
    expect(icns.readUInt32BE(4)).toBe(icns.length)
    expect(canonicalizeIcns(icns)).toEqual(icns)
  })
})