import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, readdirSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { injectAnalytics } from '../src/analytics-inject.js'; import { validateJob } from '../src/validate.js'; import { loadConfig } from '../src/config.js'; import { createJobs } from '../src/jobs.js'; const BASE = 'https://project.webklar.com/a/'; const KEY = 'abcdef0123456789abcd'; const A = (o = {}) => ({ siteKey: KEY, color: '#2b6cb0', privacy: { company: 'Muster GmbH', address: 'Weg 1, 12345 Ort', email: 'info@muster.de' }, ...o }); const TAG = ``; const dir = () => mkdtempSync(join(tmpdir(), 'inj-')); const put = (d, f, c) => { mkdirSync(join(d, f, '..'), { recursive: true }); writeFileSync(join(d, f), c); }; const get = (d, f) => readFileSync(join(d, f), 'utf8'); const count = (s, sub) => s.split(sub).length - 1; test('mehrere Dateien inkl. Unterordner: Tag vor , node_modules/Punktordner übersprungen', async () => { const d = dir(); put(d, 'index.html', '
b
'); put(d, 'node_modules/p/x.html', ''); put(d, '.hidden/x.html', ''); await injectAnalytics(d, A(), { baseUrl: BASE }); assert.equal(get(d, 'index.html'), `b
`); assert.equal(get(d, 'node_modules/p/x.html'), ''); assert.equal(get(d, '.hidden/x.html'), ''); }); test('zweimal ausführen: kein Doppel; alter Tag wird ersetzt', async () => { const d = dir(); put(d, 'index.html', ''); await injectAnalytics(d, A(), { baseUrl: BASE }); await injectAnalytics(d, A(), { baseUrl: BASE }); assert.equal(count(get(d, 'index.html'), 'data-wk-analytics'), 1); put(d, 'b.html', ''); await injectAnalytics(d, A(), { baseUrl: BASE }); assert.equal(get(d, 'b.html'), `${TAG}`); }); test('ohne head: nach ; ohne body: am Dateianfang; großes ', async () => { const d = dir(); put(d, 'a.html', 'hi'); put(d, 'b.html', 'nur text
'); put(d, 'c.html', 'nur text
`); assert.equal(get(d, 'c.html'), `