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-fileincluded-verification.json'), 'utf8'))
const fail = (message) => { throw new Error(`Chrome PropertyFileIncluded 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 value = report.ui?.afterValue
if (report.ui?.inputType !== 'file' || report.ui.label !== 'Embedded file' || report.ui.beforeName !== 'Empty' || report.ui.afterName !== 'chrome-resource.bin' || value?.schemaVersion !== 1 || value.archiveName !== 'chrome-resource.bin' || !/^[a-f0-9]{64}$/.test(value.resourceHash || '') || value.byteLength !== 16 || value.mediaType !== 'application/octet-stream' || report.ui.documentVersion !== 2 || report.ui.dirty !== true || report.ui.objectState !== 'touched' || !isDeepStrictEqual(report.ui.notices, ['chrome-resource.bin embedded'])) fail('production UI included-file 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, value) || report.persistence.loadedType !== 'App::PropertyFileIncluded' || report.persistence.checkpointVersion !== 2 || report.persistence.recoveryIntegrity !== 'ok' || report.persistence.fcstdElement !== 'FileIncluded' || report.persistence.fcstdResourcePath !== 'chrome-resource.bin' || report.persistence.fcstdResourceBytes !== 16 || report.persistence.fcstdResourceMatches !== true) fail('OPFS/FCStd persistence evidence is incomplete')
if (report.resource?.documentByteLength !== 16 || report.resource.documentRoundTrip !== true || !(report.resource.independentByteLength > 0) || report.resource.independentRoundTrip !== true || report.resource.independentReleased !== 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-fileincluded-pass', ui: report.ui, persistence: report.persistence, workerRequests: report.worker.requestTypes, resource: report.resource, release: report.release }, null, 2))
