import { createMockFacade } from './facade/mockFacade'
import { runPerformanceBudget } from './facade/performanceBudget'
type PerformanceReport = { schemaVersion: 1; status: 'pass' | 'failed'; browserId: 'chrome'; crossOriginIsolated: boolean; benchmark?: { objects: number; triangles: number; tableCells: number; objectMs: number; triangleMs: number; tableMs: number; heapBytes: number | null; pass: boolean }; persistence?: { mode: string; byteLength: number; roundTrip: boolean; released: boolean }; opfs?: { markerSuite: string; triangles: number; markerRemoved: boolean }; afterRelease?: { shapeCount: number; kernelReferenceCount: number }; error?: string }
const sha256 = async (bytes: Uint8Array) => [...new Uint8Array(await crypto.subtle.digest('SHA-256', Uint8Array.from(bytes).buffer))].map((value) => value.toString(16).padStart(2, '0')).join('')
const run = async (): Promise<PerformanceReport> => { const facade = createMockFacade(); const report: PerformanceReport = { schemaVersion: 1, status: 'failed', browserId: 'chrome', crossOriginIsolated: globalThis.crossOriginIsolated === true }; try { if (!report.crossOriginIsolated || !navigator.storage?.getDirectory) throw new Error('Chrome performance evidence requires isolated OPFS.'); await facade.project.list(); const benchmark = runPerformanceBudget(); const payload = new TextEncoder().encode(JSON.stringify(benchmark)); const stored = await facade.project.resource.put(payload, 'application/vnd.bitbybit.performance+json'); const loaded = await facade.project.resource.get(stored.hash); const roundTrip = loaded !== null && await sha256(loaded) === await sha256(payload); await facade.project.resource.release(stored.hash); const released = await facade.project.resource.get(stored.hash) === null; const directory = await navigator.storage.getDirectory(); const markerName = 'performance-chrome-marker.json'; const marker = await directory.getFileHandle(markerName, { create: true }); const writable = await marker.createWritable(); await writable.write(JSON.stringify({ suite: 'QA-05', triangles: benchmark.triangles.count })); await writable.close(); const markerPayload = JSON.parse(await (await marker.getFile()).text()) as { suite: string; triangles: number }; await directory.removeEntry(markerName); let markerRemoved = false; try { await directory.getFileHandle(markerName) } catch (error) { markerRemoved = error instanceof DOMException && error.name === 'NotFoundError' }; report.benchmark = { objects: benchmark.objects.count, triangles: benchmark.triangles.count, tableCells: benchmark.table.cells, objectMs: benchmark.objects.durationMs, triangleMs: benchmark.triangles.durationMs, tableMs: benchmark.table.durationMs, heapBytes: benchmark.heapBytes, pass: benchmark.pass }; report.persistence = { mode: facade.project.capabilities().mode, byteLength: stored.byteLength, roundTrip, released }; report.opfs = { markerSuite: markerPayload.suite, triangles: markerPayload.triangles, markerRemoved }; report.status = report.benchmark.objects === 1000 && report.benchmark.triangles === 1_000_000 && report.benchmark.tableCells === 100_000 && report.benchmark.pass && report.persistence.mode === 'sqlite-opfs' && report.persistence.roundTrip && report.persistence.released && report.opfs.markerSuite === 'QA-05' && report.opfs.triangles === 1_000_000 && report.opfs.markerRemoved ? 'pass' : 'failed' } catch (error) { report.error = error instanceof Error ? error.stack || error.message : String(error) } finally { facade.geometry.dispose(); await new Promise((resolve) => setTimeout(resolve, 0)); const capabilities = facade.geometry.capabilities(); report.afterRelease = { shapeCount: capabilities.shapeCount, kernelReferenceCount: capabilities.kernelReferenceCount }; if (capabilities.shapeCount !== 0 || capabilities.kernelReferenceCount !== 0) { report.status = 'failed'; report.error = `${report.error ? `${report.error} ` : ''}Shape ownership gate failed.` } } return report }
run().then((report) => { ;(window as Window & { __bitbybitPerformanceReport?: PerformanceReport }).__bitbybitPerformanceReport = report; document.documentElement.dataset.status = report.status; document.querySelector('#result')!.textContent = JSON.stringify(report, null, 2) }).catch((error) => { document.documentElement.dataset.status = 'failed'; document.querySelector('#result')!.textContent = error instanceof Error ? error.stack || error.message : String(error) })
export {}
