Incognito-Wiki/tests/matomo-build.test.mjs
msa46 84d213a9f6
Some checks are pending
Deploy to GitHub Pages / build (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Blocked by required conditions
feat: integrate privacy-first Matomo analytics
2026-08-15 15:06:36 +02:00

50 lines
1.9 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 the local consent controller and its UI styles', 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 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 });
}
});