← Files ProdVerityARCHIVED FILE

source/scripts/prodverity-vision-audit-lib.mjs

3.6 KB · Nov 15, 2026 · 06:42 UTC

↓ Download file

See the change to this file →

import { createHash } from 'node:crypto'
import pixelmatch from 'pixelmatch'
import { PNG } from 'pngjs'

export const VIEWPORTS = Object.freeze([
  { id: 'mobile', width: 390, height: 844 },
  { id: 'tablet', width: 768, height: 1024 },
  { id: 'desktop', width: 1440, height: 900 },
])

export const DEFAULT_ROUTES = Object.freeze([
  { id: 'home', path: '/' },
  { id: 'catalog', path: '/?page=catalog' },
  { id: 'credentials', path: '/?page=credentials' },
  { id: 'prodverity', path: '/prodverity/' },
])

export function parseVisionRoutes(rawValue) {
  if (!rawValue) return DEFAULT_ROUTES.map(route => ({ ...route }))
  let parsed
  try { parsed = JSON.parse(rawValue) } catch { throw new Error('PRODVERITY_VISION_ROUTES must be valid JSON.') }
  if (!Array.isArray(parsed) || parsed.length === 0 || parsed.length > 10) {
    throw new Error('PRODVERITY_VISION_ROUTES must contain between 1 and 10 routes.')
  }
  const seen = new Set()
  return parsed.map((route, index) => {
    const id = String(route?.id || '').trim().toLowerCase()
    const routePath = String(route?.path || '').trim()
    if (!/^[a-z0-9][a-z0-9-]{0,39}$/.test(id)) throw new Error(`Invalid route id at index ${index}.`)
    if (!routePath.startsWith('/') || routePath.startsWith('//')) throw new Error(`Route ${id} must be a same-origin absolute path.`)
    const resolved = new URL(routePath, 'https://audit.invalid')
    if (resolved.origin !== 'https://audit.invalid') throw new Error(`Route ${id} leaves the audited origin.`)
    if (seen.has(id)) throw new Error(`Duplicate route id: ${id}`)
    seen.add(id)
    return { id, path: resolved.pathname + resolved.search }
  })
}

export function boundedRatio(value, fallback, min = 0, max = 0.5) {
  const parsed = Number.parseFloat(String(value ?? ''))
  return Number.isFinite(parsed) ? Math.min(max, Math.max(min, parsed)) : fallback
}

export function sha256(buffer) {
  return createHash('sha256').update(buffer).digest('hex')
}

export function comparePngBuffers(currentBuffer, baselineBuffer, threshold = 0.1) {
  const current = PNG.sync.read(currentBuffer)
  const baseline = PNG.sync.read(baselineBuffer)
  if (current.width !== baseline.width || current.height !== baseline.height) {
    return {
      comparable: false,
      reason: 'dimension_mismatch',
      current_dimensions: { width: current.width, height: current.height },
      baseline_dimensions: { width: baseline.width, height: baseline.height },
      changed_pixels: null,
      difference_ratio: 1,
      diff_png: null,
    }
  }
  const diff = new PNG({ width: current.width, height: current.height })
  const changedPixels = pixelmatch(current.data, baseline.data, diff.data, current.width, current.height, {
    threshold,
    includeAA: false,
  })
  const totalPixels = current.width * current.height
  return {
    comparable: true,
    reason: null,
    current_dimensions: { width: current.width, height: current.height },
    baseline_dimensions: { width: baseline.width, height: baseline.height },
    changed_pixels: changedPixels,
    difference_ratio: totalPixels === 0 ? 0 : changedPixels / totalPixels,
    diff_png: PNG.sync.write(diff),
  }
}

export function makeReferenceManifest({ revision, screenshots, observedAt }) {
  return {
    schema_version: '1',
    evidence_source: 'BROWSER_RENDERED_PLAYWRIGHT',
    created_at: observedAt,
    revision,
    immutable_reference_key: 'prodverity-vision-reference-v1',
    screenshots: screenshots.map(item => ({
      route_id: item.route_id,
      viewport_id: item.viewport_id,
      filename: item.filename,
      sha256: item.sha256,
      width: item.width,
      height: item.height,
    })),
  }
}

SHA-256: beaaed8e8a7d42a98a0134ca04688654f74fd599ea63568d5d48fe59cf56eb1b