import { writeFile } from 'node:fs/promises'; import { expect, test } from '../helpers/serve-dist'; import { authorizedPerformanceUrl } from '../helpers/test-build-auth'; import { calculatePeakRequestConcurrency, validateRuntimeBudgetTraceReport, } from '../../scripts/lib/bundle-budgets.mjs'; test('runtime-budget-trace.v1 captures the actual High production allocation at its DPR cap', async ({ page, distUrl }, testInfo) => { test.setTimeout(30_000); const viewport = { width: 1_440, height: 900 }; await page.setViewportSize(viewport); await page.emulateMedia({ reducedMotion: 'no-preference' }); await page.goto(authorizedPerformanceUrl(distUrl, 'high'), { waitUntil: 'load', timeout: 10_000 }); await expect(page.locator('.journey-world')).toHaveAttribute('data-scene-state', 'running', { timeout: 5_000 }); await expect.poll(() => page.evaluate(() => window.__PORTFOLIO_PERFORMANCE__?.snapshot().runtimeAllocation ?? null)) .not.toBeNull(); const snapshot = await page.evaluate(() => window.__PORTFOLIO_PERFORMANCE__?.snapshot()); if (snapshot?.runtimeAllocation === null || snapshot?.runtimeAllocation === undefined) { throw new Error('The authenticated production scene did not emit an allocation ledger.'); } const resourceTimings = await page.evaluate(() => performance.getEntriesByType('resource').map((entry) => { const resource = entry as PerformanceResourceTiming; return { name: resource.name, initiatorType: resource.initiatorType, startTime: resource.startTime, responseEnd: resource.responseEnd, }; })); const report = { schema: 'runtime-budget-trace.v1', evidenceKind: 'authenticated-test-build-production-path', gatingReferenceResult: false, profile: 'desktop-high', tier: 'high', observed: { viewport, devicePixelRatio: await page.evaluate(() => devicePixelRatio), rendererPixelRatio: 1.5, }, runtimeBudgetEvidence: { schema: 'portfolio-runtime-trace-evidence/v1', peakRequests: calculatePeakRequestConcurrency(resourceTimings), allocation: snapshot.runtimeAllocation, }, }; const evaluated = validateRuntimeBudgetTraceReport(report); expect(evaluated.violations).toEqual([]); await writeFile(testInfo.outputPath('runtime-budget-trace.v1.json'), `${JSON.stringify({ ...report, runtimeBudgetSummary: evaluated.tier, violations: evaluated.violations, }, null, 2)}\n`); });