import assert from 'node:assert/strict'; import { execFile } from 'node:child_process'; import { access, mkdtemp, readFile, readdir, rm } from 'node:fs/promises'; import { join } from 'node:path'; import { promisify } from 'node:util'; import test from 'node:test'; import { parseHTML } from 'linkedom'; const execFileAsync = promisify(execFile); async function collectCss(directory) { const entries = await readdir(directory, { withFileTypes: true }); const chunks = []; for (const entry of entries) { const path = join(directory, entry.name); if (entry.isDirectory()) chunks.push(await collectCss(path)); if (entry.isFile() && entry.name.endsWith('.css')) chunks.push(await readFile(path, 'utf8')); } return chunks.join('\n'); } test('subpath production build excludes analytics and includes focused programme navigation', async () => { const output = await mkdtemp(join(process.cwd(), '.matomo-build-')); try { await execFileAsync('node_modules/.bin/astro', ['build', '--outDir', output], { env: { ...process.env, SITE: 'https://example.github.io', BASE: '/Incognito-Wiki', }, maxBuffer: 10 * 1024 * 1024, }); const html = await readFile(join(output, 'index.html'), 'utf8'); const { document } = parseHTML(html); assert.equal( document.querySelector('script[src*="matomo"]'), null, 'pages should not load Matomo or its consent controller', ); await assert.rejects(access(join(output, 'matomo-consent.js'))); const computerScienceHtml = await readFile(join(output, 'computer-science/index.html'), 'utf8'); const { document: computerScienceDocument } = parseHTML(computerScienceHtml); const computerScienceSidebar = computerScienceDocument.querySelector('#starlight__sidebar'); assert.ok(computerScienceSidebar); assert.match(computerScienceSidebar.textContent, /Home/); assert.match(computerScienceSidebar.textContent, /Previous exams and documents/); assert.match(computerScienceSidebar.textContent, /Computer Science/); assert.match(computerScienceSidebar.textContent, /Useful Information/); assert.doesNotMatch(computerScienceSidebar.textContent, /About Incognito/); assert.doesNotMatch(computerScienceSidebar.textContent, /Data Science & AI/); assert.doesNotMatch(computerScienceSidebar.textContent, /Master AI/); assert.doesNotMatch(computerScienceSidebar.textContent, /Master DSDM/); const dataScienceHtml = await readFile(join(output, 'data-science-and-ai/index.html'), 'utf8'); const { document: dataScienceDocument } = parseHTML(dataScienceHtml); const dataScienceSidebar = dataScienceDocument.querySelector('#starlight__sidebar'); assert.ok(dataScienceSidebar); assert.match(dataScienceSidebar.textContent, /Home/); assert.match(dataScienceSidebar.textContent, /Previous exams and documents/); assert.match(dataScienceSidebar.textContent, /Data Science & AI/); assert.match(dataScienceSidebar.textContent, /Useful Information/); assert.doesNotMatch(dataScienceSidebar.textContent, /About Incognito/); assert.doesNotMatch(dataScienceSidebar.textContent, /Computer Science/); assert.doesNotMatch(dataScienceSidebar.textContent, /Master AI/); assert.doesNotMatch(dataScienceSidebar.textContent, /Master DSDM/); const css = await collectCss(output); assert.doesNotMatch(css, /#incognito-analytics-consent/); assert.doesNotMatch(css, /\.incognito-consent-action/); assert.doesNotMatch(css, /#incognito-privacy-settings/); } finally { await rm(output, { recursive: true, force: true }); } });