import { createMockFacade } from './facade/mockFacade'
import { createAddonManager, createSignedAddonPackage, type AddonManifest } from './facade/addonGovernance'
type AddonReport = { schemaVersion: 1; status: 'pass' | 'failed'; browserId: 'chrome'; crossOriginIsolated: boolean; catalog?: { installed: number; revision: number; rollbackVersion: string }; security?: { forgedRejected: boolean; permissionRejected: boolean; removed: boolean }; persistence?: { mode: string; byteLength: number; roundTrip: boolean; released: boolean }; opfs?: { markerSuite: string; markerInstalled: 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<AddonReport> => { const facade = createMockFacade(); const report: AddonReport = { schemaVersion: 1, status: 'failed', browserId: 'chrome', crossOriginIsolated: globalThis.crossOriginIsolated === true }; try { if (!report.crossOriginIsolated || !navigator.storage?.getDirectory) throw new Error('Chrome addon evidence requires isolated OPFS.'); await facade.project.list(); const manager = createAddonManager({ trustedKeys: { release: 'chrome-secret' } }); const base: AddonManifest = { id: 'measure-addon', version: '1.0.0', name: 'Measure', entrypoint: 'facade.measure', permissions: ['geometry.read'], dependencies: [] }; const first = createSignedAddonPackage(base, 'commands:v1', 'release', 'chrome-secret'); manager.install(first); const second = createSignedAddonPackage({ ...base, version: '1.1.0' }, 'commands:v2', 'release', 'chrome-secret'); manager.update(second); const rolled = manager.rollback('measure-addon'); let forgedRejected = false; try { manager.install({ ...first, payload: 'tampered' }) } catch { forgedRejected = true }; let permissionRejected = false; try { manager.install(createSignedAddonPackage({ ...base, id: 'network-addon', permissions: ['network'] }, 'network', 'release', 'chrome-secret')) } catch { permissionRejected = true }; const snapshot = manager.snapshot(); const payload = new TextEncoder().encode(manager.exportManifest()); const stored = await facade.project.resource.put(payload, 'application/vnd.bitbybit.addon+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 = 'addon-chrome-marker.json'; const marker = await directory.getFileHandle(markerName, { create: true }); const writable = await marker.createWritable(); await writable.write(JSON.stringify({ suite: 'ADDON-ALL', installed: snapshot.catalog.length })); await writable.close(); const markerPayload = JSON.parse(await (await marker.getFile()).text()) as { suite: string; installed: number }; await directory.removeEntry(markerName); let markerRemoved = false; try { await directory.getFileHandle(markerName) } catch (error) { markerRemoved = error instanceof DOMException && error.name === 'NotFoundError' }; manager.remove('measure-addon'); report.catalog = { installed: snapshot.catalog.length, revision: rolled.revision, rollbackVersion: rolled.manifest.version }; report.security = { forgedRejected, permissionRejected, removed: manager.snapshot().catalog.length === 0 }; report.persistence = { mode: facade.project.capabilities().mode, byteLength: stored.byteLength, roundTrip, released }; report.opfs = { markerSuite: markerPayload.suite, markerInstalled: markerPayload.installed, markerRemoved }; report.status = report.catalog.installed === 1 && report.catalog.revision === 1 && report.catalog.rollbackVersion === '1.0.0' && report.security.forgedRejected && report.security.permissionRejected && report.security.removed && report.persistence.mode === 'sqlite-opfs' && report.persistence.byteLength > 0 && report.persistence.roundTrip && report.persistence.released && report.opfs.markerSuite === 'ADDON-ALL' && report.opfs.markerInstalled === 1 && 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 & { __bitbybitAddonReport?: AddonReport }).__bitbybitAddonReport = 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 {}
