import { createMockFacade } from './facade/mockFacade'
import { checkAccessibilitySemantics, formatLength, localeSnapshot, supportedLocales } from './facade/locale'
type Qa08Report = { schemaVersion: 1; status: 'pass' | 'failed'; browserId: 'chrome'; crossOriginIsolated: boolean; locales?: { supported: number; snapshots: number; longLabel: boolean; decimalSamples: string[] }; accessibility?: { checked: number; missingNames: number; duplicates: number; unnamedFocusable: number; pass: boolean }; persistence?: { mode: string; byteLength: number; roundTrip: boolean; released: boolean }; opfs?: { markerSuite: string; localeCount: 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<Qa08Report> => { const facade = createMockFacade(); const report: Qa08Report = { schemaVersion: 1, status: 'failed', browserId: 'chrome', crossOriginIsolated: globalThis.crossOriginIsolated === true }; try { if (!report.crossOriginIsolated || !navigator.storage?.getDirectory) throw new Error('Chrome QA-08 evidence requires isolated OPFS.'); await facade.project.list(); const locales = supportedLocales(); const snapshots = locales.map(localeSnapshot); const semantics = checkAccessibilitySemantics([{ id: 'workbench', role: 'combobox', accessibleName: 'Workbench', focusable: true }, { id: 'save', role: 'button', accessibleName: 'Save project', focusable: true }, { id: 'viewport', role: 'img', accessibleName: '3D viewport' }, { id: 'status', role: 'status', accessibleName: 'Ready' }]); const payload = new TextEncoder().encode(JSON.stringify(snapshots)); const stored = await facade.project.resource.put(payload, 'application/vnd.bitbybit.qa08+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 = 'qa08-chrome-marker.json'; const marker = await directory.getFileHandle(markerName, { create: true }); const writable = await marker.createWritable(); await writable.write(JSON.stringify({ suite: 'QA-08', localeCount: locales.length })); await writable.close(); const markerPayload = JSON.parse(await (await marker.getFile()).text()) as { suite: string; localeCount: number }; await directory.removeEntry(markerName); let markerRemoved = false; try { await directory.getFileHandle(markerName) } catch (error) { markerRemoved = error instanceof DOMException && error.name === 'NotFoundError' }; report.locales = { supported: locales.length, snapshots: snapshots.length, longLabel: snapshots.every((entry) => entry.messages.longLabel.length > 20), decimalSamples: snapshots.map((entry) => entry.samples.decimal) }; report.accessibility = { checked: semantics.checked, missingNames: semantics.missingNames.length, duplicates: semantics.duplicateIds.length, unnamedFocusable: semantics.unnamedFocusable.length, pass: semantics.pass }; report.persistence = { mode: facade.project.capabilities().mode, byteLength: stored.byteLength, roundTrip, released }; report.opfs = { markerSuite: markerPayload.suite, localeCount: markerPayload.localeCount, markerRemoved }; report.status = report.locales.supported === 3 && report.locales.snapshots === 3 && report.locales.longLabel && report.locales.decimalSamples.every((sample) => sample.includes('mm')) && report.accessibility.checked === 4 && report.accessibility.pass && report.persistence.mode === 'sqlite-opfs' && report.persistence.roundTrip && report.persistence.released && report.opfs.markerSuite === 'QA-08' && report.opfs.localeCount === 3 && 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 & { __bitbybitQa08Report?: Qa08Report }).__bitbybitQa08Report = 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 {}
