Files
jobtrackingapp/docs/performance/font-startup/evidence/font-startup-probe.mjs.txt
T

52 lines
3.8 KiB
Plaintext

import { createRequire } from 'node:module';
import path from 'node:path';
import fs from 'node:fs';
import assert from 'node:assert/strict';
const { chromium } = createRequire(path.join(process.cwd(), 'package.json'))('@playwright/test');
const [phase, output] = process.argv.slice(2);
assert.ok(['before', 'after'].includes(phase));
const states = [];
const browser = await chromium.launch();
try {
for (const width of [390, 1440]) for (const language of ['en', 'nb']) for (const mode of ['light', 'dark']) {
const context = await browser.newContext({ viewport: { width, height: 1000 }, reducedMotion: 'reduce' });
const page = await context.newPage();
const external = [], writes = [], errors = [];
page.on('pageerror', e => errors.push(e.message));
await page.addInitScript(({ language, mode }) => { localStorage.setItem('uiLanguage', language); localStorage.setItem('jobtracker.themeMode', mode); }, { language, mode });
await page.route('**/*', route => {
const request = route.request(), url = new URL(request.url());
if (!['localhost', '127.0.0.1'].includes(url.hostname)) {
external.push(url.href);
if (url.hostname === 'fonts.googleapis.com') return route.fulfill({ contentType: 'text/css', body: '/* synthetic unavailable font stylesheet: no remote font or provider access */' });
return route.abort();
}
if (!url.pathname.startsWith('/api/')) return route.continue();
if (request.method() !== 'GET') { writes.push(url.pathname); return route.abort(); }
const fixtures = {
'/api/auth/config': { requireAuth: true },
'/api/auth/me': { id: 'font-probe-owner', sessionContext: 'font-probe-session', firstName: 'Alex', displayName: 'Alex Sample', roles: [], uiLanguage: language === 'nb' ? 'nb-NO' : 'en' },
'/api/jobapplications/reminders': [], '/api/notifications/unread-count': { count: 0 }, '/api/job-discovery/saved-searches': [], '/api/companies': []
};
return route.fulfill({ status: Object.hasOwn(fixtures, url.pathname) ? 200 : 404, contentType: 'application/json', body: JSON.stringify(fixtures[url.pathname] ?? {}) });
});
await page.goto('http://localhost:4338/discover');
await page.locator('h1').waitFor();
await page.evaluate(() => document.fonts.ready);
const actual = await page.evaluate(() => ({
families: [...new Set([...document.querySelectorAll('body,h1,h2,button,input,label')].map(e => getComputedStyle(e).fontFamily))].sort(),
links: [...document.querySelectorAll('link[href*="fonts.googleapis.com"],link[href*="fonts.gstatic.com"]')].map(e => ({ rel: e.rel, href: e.href })),
heading: (() => { const h = document.querySelector('h1'), r = h.getBoundingClientRect(); return { text: h.textContent, width: r.width, height: r.height, family: getComputedStyle(h).fontFamily }; })(),
overflow: document.documentElement.scrollWidth > innerWidth + 1
}));
assert.ok(actual.families.every(f => !/archivo/i.test(f)), 'Archivo must be unreferenced by rendered app typography');
assert.equal(external.length, phase === 'before' ? 1 : 0);
assert.equal(actual.links.length, phase === 'before' ? 4 : 0);
assert.deepEqual(errors, []); assert.deepEqual(writes, []); assert.equal(actual.overflow, false);
states.push({ width, language, mode, ...actual, external, errors, writes });
await context.close();
}
} finally { await browser.close(); }
fs.writeFileSync(output, JSON.stringify({ phase, limits: ['Synthetic API only', 'External font stylesheet replaced with inert CSS; no Google request sent', 'Request count and typography are observed; no live-network latency claim'], states }, null, 2));
console.log(`${phase}: ${states.length} states, ${states.reduce((n,s)=>n+s.external.length,0)} intercepted external stylesheet requests`);