← Files ProdVerityARCHIVED FILE

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

2.58 KB · Nov 29, 2026 · 00:48 UTC

↓ Download file

See the change to this file →

import test from 'node:test'
import assert from 'node:assert/strict'
import { PNG } from 'pngjs'
import {
  DEFAULT_ROUTES,
  VIEWPORTS,
  boundedRatio,
  comparePngBuffers,
  makeReferenceManifest,
  parseVisionRoutes,
} from './prodverity-vision-audit-lib.mjs'

function image(width, height, rgba) {
  const png = new PNG({ width, height })
  for (let offset = 0; offset < png.data.length; offset += 4) {
    png.data.set(rgba, offset)
  }
  return PNG.sync.write(png)
}

test('keeps the required mobile, tablet and desktop widths', () => {
  assert.deepEqual(VIEWPORTS.map(item => item.width), [390, 768, 1440])
})

test('uses bounded public routes and rejects cross-origin or duplicate inputs', () => {
  assert.equal(parseVisionRoutes().length, DEFAULT_ROUTES.length)
  assert.deepEqual(parseVisionRoutes('[{"id":"docs","path":"/docs?q=1#ignored"}]'), [{ id: 'docs', path: '/docs?q=1' }])
  assert.throws(() => parseVisionRoutes('[{"id":"bad","path":"//attacker.example/"}]'))
  assert.throws(() => parseVisionRoutes('[{"id":"same","path":"/a"},{"id":"same","path":"/b"}]'))
})

test('compares equal screenshots without a false regression', () => {
  const first = image(2, 2, [255, 255, 255, 255])
  const result = comparePngBuffers(first, first)
  assert.equal(result.comparable, true)
  assert.equal(result.changed_pixels, 0)
  assert.equal(result.difference_ratio, 0)
})

test('detects pixel and dimension changes', () => {
  const white = image(2, 2, [255, 255, 255, 255])
  const black = image(2, 2, [0, 0, 0, 255])
  const changed = comparePngBuffers(black, white)
  assert.equal(changed.comparable, true)
  assert.equal(changed.changed_pixels, 4)
  assert.equal(changed.difference_ratio, 1)
  const resized = comparePngBuffers(image(3, 2, [255, 255, 255, 255]), white)
  assert.equal(resized.comparable, false)
  assert.equal(resized.reason, 'dimension_mismatch')
})

test('creates a revision-bound protected reference manifest', () => {
  const manifest = makeReferenceManifest({
    revision: 'a'.repeat(40),
    observedAt: '2026-01-01T00:00:00.000Z',
    screenshots: [{ route_id: 'home', viewport_id: 'mobile', filename: 'home-mobile.png', sha256: 'b'.repeat(64), width: 390, height: 844 }],
  })
  assert.equal(manifest.evidence_source, 'BROWSER_RENDERED_PLAYWRIGHT')
  assert.equal(manifest.immutable_reference_key, 'prodverity-vision-reference-v1')
  assert.equal(manifest.screenshots[0].sha256, 'b'.repeat(64))
})

test('bounds visual-difference policy values', () => {
  assert.equal(boundedRatio('0.03', 0.02), 0.03)
  assert.equal(boundedRatio('9', 0.02), 0.5)
  assert.equal(boundedRatio('invalid', 0.02), 0.02)
})

SHA-256: 36a46d8d737da1cf5add22df7643df253b766432f8f05a550ebe991b6fbebcaf