Incognito-Wiki/tests/matomo-build.test.mjs
2026-08-16 12:03:59 +02:00

76 lines
3.6 KiB
JavaScript

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 includes local UI assets and 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);
const scripts = document.querySelectorAll(
'script[src="/Incognito-Wiki/matomo-consent.js"]',
);
assert.equal(scripts.length, 1, 'every page should load one base-aware local controller');
assert.equal(scripts[0].hasAttribute('defer'), true);
await 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.match(css, /#incognito-analytics-consent/);
assert.match(css, /\.incognito-consent-action/);
assert.match(css, /#incognito-privacy-settings/);
} finally {
await rm(output, { recursive: true, force: true });
}
});