/** * first-use-layout — `gstack-first-use layout`: measures the play spec's crowded screens (its full_house state file, frozen * at start) with gstack-design-lint's app profile under --full-house, so text-clipped and text-overlap run with no model, * and writes one write-once receipt bound to the build and the frozen files (docs/first-use-check.md). Only those two * rules decide the receipt's verdict; the app profile's spacing rules stay advice for the builder's own lint runs. decide() in * first-use.ts only reads the receipts; this module is the only part of the first-use check that starts a browser. */ import * as crypto from 'node:crypto'; import * as fs from 'node:fs'; import * as path from 'node:path'; import { parseViewport } from './design-lint/app-input'; import { lintApp, type AppOptions } from './design-lint/render-app'; import { readManifest, type FirstUseError, type LayoutReceipt } from './first-use'; const VERSION_FILE = path.resolve(import.meta.dir, '..', 'VERSION'); export const FULL_HOUSE_RULES = ['text-clipped', 'text-overlap']; export async function runLayout(dir: string, deps: Pick = {}, now: () => Date = () => new Date()): Promise<{ receipt: LayoutReceipt; file: string } | { error: FirstUseError; nothing?: true }> { const read = readManifest(dir); if ('error' in read) return read; const m = read.manifest, fh = m.full_house; if (!fh) return { nothing: true, error: { code: 'FIRST_USE_RECORD_INVALID', detail: `this run's play spec has no crowded screens to measure (full_house: ${typeof m.play.full_house === 'string' ? m.play.full_house : 'none'})` } }; const viewports = fh.viewports?.map(parseViewport); const r = await lintApp({ states: fh.states_parsed, within: fh.within, waitFor: fh.wait_for, viewports, waivers: fh.waivers_parsed, fullHouse: true, ...deps }); let version = 'unknown'; try { version = fs.readFileSync(VERSION_FILE, 'utf8').trim(); } catch { /* not a checkout */ } const ours = r.findings.filter(f => f.app && FULL_HOUSE_RULES.includes(f.rule)); const open = ours.filter(f => !f.waiver).length; const verdict = r.app.verdict === 'incomplete' ? 'incomplete' : open ? 'open' : 'clean'; const at = now(); const receipt: LayoutReceipt = { schema_version: 1, kind: 'first-use-layout', at: at.toISOString(), build_fingerprint: m.build.fingerprint, states_sha256: fh.states_sha256, waivers_sha256: fh.waivers_sha256, viewports: r.app.viewports, within: fh.within, origins: [...new Set(fh.states_parsed.map(s => { try { return new URL(s.url).origin; } catch { return s.url; } }))], gstack_version: version, verdict, code: verdict === 'incomplete' ? r.app.code : open ? 'OPEN' : 'OK', open, waived: ours.length - open, findings: ours.map(f => ({ rule: f.rule, evidence: f.evidence, ...(f.app ? { locator: f.app.locator } : {}), ...(f.waiver ? { waiver: f.waiver } : {}) })), incomplete: r.app.states.filter(s => s.status === 'incomplete').map(s => ({ state: s.name, viewport: s.viewport, code: s.code, detail: s.detail ?? '' })), }; const file = path.join(dir, 'layout', `${at.toISOString().replace(/[-:]/g, '')}-${crypto.randomBytes(3).toString('hex')}.json`); try { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, JSON.stringify(receipt, null, 2) + '\n', { flag: 'wx' }); } catch (e) { return { error: { code: 'FIRST_USE_RECORD_INVALID', detail: `cannot write ${file}: ${(e as Error).message}` } }; } return { receipt, file }; }