import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { createServer, type Server } from 'node:http' import { tmpdir } from 'node:os' import path from 'node:path' import { gzipSync } from 'node:zlib' import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest' import { runDoctor } from '../src/modules/doctor/run.js' import { IMAGE_RULES, overBudget, PERF_BUDGETS, perfBudgets } from '../src/modules/performance/budgets.js' const packageRoot = path.resolve(import.meta.dirname, '..') const read = (rel: string) => readFileSync(path.join(packageRoot, rel), 'utf8') let dirs: string[] = [] afterEach(() => { for (const d of dirs) {rmSync(d, { force: true, recursive: true })} dirs = [] }) function project(files: Record): string { const root = mkdtempSync(path.join(tmpdir(), 'iapl-perf-')) dirs.push(root) const all: Record = { 'node_modules/@intecion/ipal-kit/package.json': JSON.stringify({ name: '@intecion/ipal-kit', version: '1.18.0' }), 'package.json': JSON.stringify({ name: 'examplepl-web', dependencies: { '@intecion/ipal-kit': '^1.18.0', next: '16.4.6' } }), ...files, } for (const [rel, content] of Object.entries(all)) { mkdirSync(path.dirname(path.join(root, rel)), { recursive: true }) writeFileSync(path.join(root, rel), content) } return root } const doctor = (root: string, only: string[], extra: { config?: object; deepStatus?: object; url?: string } = {}) => runDoctor({ offline: true, only, packageRoot, root, today: '2026-12-01', writeSeen: false, ...extra }) /** Each line starts with the expected text, in order. */ const startWith = (lines: string[], expected: string[]) => { expect(lines).toHaveLength(expected.length) expected.forEach((text, i) => expect(lines[i].startsWith(text), `${lines[i]}\n should start with: ${text}`).toBe(true)) } const found = async (root: string, only: string[], extra = {}) => (await doctor(root, only, extra)).findings.map((f) => `${f.rule}: ${f.message}`) describe('doctor: performance group (static)', () => { test('a clean site has no findings', async () => { const root = project({ 'e2e/performance.spec.ts': 'perfBudgets({})', 'next.config.ts': 'images: buildImagesConfig()', 'src/app/(frontend)/[locale]/layout.tsx': "import { Inter } from 'next/font/google'\nconst body = Inter({ display: 'swap' })\n', '', ].join('\n'), 'src/app/(frontend)/styles.css': '@import url("https://cdn.example/x.css");\n@font-face { font-family: X; src: url(x.woff2) }\n.menu { transition: height 200ms }\n@keyframes spin { to { transform: rotate(1turn) } }', 'src/components/Map.tsx': '