import { isDeepStrictEqual } from 'node:util'
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const report = JSON.parse(await readFile(resolve(root, 'config/chrome-property-colorlist-verification.json'), 'utf8'))
const fail = (message) => { throw new Error(`Chrome PropertyColorList check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || !/Chrome\//.test(report.userAgent || '') || report.crossOriginIsolated !== true) fail('browser boundary is invalid')
const requiredWorkerRequests = ['initialize', 'save-document', 'load-document', 'load-checkpoint', 'recovery-report', 'put-resource', 'get-resource', 'release-resource', 'dispose']
if (!report.worker?.constructed?.some(({ name }) => name === 'bitbybit-persistence') || !requiredWorkerRequests.every((type) => report.worker.requestTypes?.includes(type)) || report.worker.terminated?.includes('bitbybit-persistence') !== true) fail('persistence Worker lifecycle is incomplete')
const target = [[0.2, 0.4, 0.6, 0.8], [17 / 255, 34 / 255, 51 / 255, 0.4]]
if (!isDeepStrictEqual(report.ui?.rgbInputTypes, ['color', 'color']) || !isDeepStrictEqual(report.ui?.alphaInputTypes, ['number', 'number']) || !isDeepStrictEqual(report.ui?.rgbLabels, ['Colour list 1 RGB', 'Colour list 2 RGB']) || !isDeepStrictEqual(report.ui?.alphaLabels, ['Colour list 1 alpha', 'Colour list 2 alpha']) || report.ui?.addLabel !== 'Add Colour list color' || report.ui?.removeLabel !== 'Remove Colour list 3' || !isDeepStrictEqual(report.ui?.beforeHex, ['#000000']) || !isDeepStrictEqual(report.ui?.afterHex, ['#336699', '#112233']) || !isDeepStrictEqual(report.ui?.beforeAlpha, ['1']) || !isDeepStrictEqual(report.ui?.afterAlpha, [0.8, 0.4]) || !isDeepStrictEqual(report.ui?.beforeValue, [[0, 0, 0, 1]]) || !isDeepStrictEqual(report.ui?.afterValue, target) || report.ui?.beforeItemCount !== 1 || report.ui?.peakItemCount !== 3 || report.ui?.afterItemCount !== 2 || report.ui?.documentVersion !== 8 || report.ui?.dirty !== true || report.ui?.objectState !== 'touched' || report.ui?.errors?.length !== 0) fail('production UI edit evidence is incomplete')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.sqliteWasm !== true || report.persistence.opfs !== true || report.persistence.savedMode !== 'sqlite-opfs' || !isDeepStrictEqual(report.persistence.loadedValue, target) || report.persistence.loadedType !== 'App::PropertyColorList' || report.persistence.checkpointVersion !== 8 || report.persistence.recoveryIntegrity !== 'ok' || report.persistence.fcstdElement !== 'ColorList' || report.persistence.fcstdValue !== JSON.stringify(target) || report.persistence.fcstdResourcePath !== 'ColourList' || report.persistence.fcstdResourceBytes !== 12) fail('OPFS/FCStd persistence evidence is incomplete')
if (!(report.resource?.byteLength > 0) || report.resource.roundTrip !== true || report.resource.released !== true || report.resource.markerRemoved !== true || report.release?.shapeCount !== 0 || report.release.kernelReferenceCount !== 0 || report.release.workerTerminated !== true) fail('resource release evidence is incomplete')
console.log(JSON.stringify({ status: 'chrome-property-colorlist-pass', ui: report.ui, persistence: report.persistence, workerRequests: report.worker.requestTypes, resource: report.resource, release: report.release }, null, 2))
