// node --test tools/symbols/check-no-nc.test.mjs — planted files. import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { randomBytes } from 'node:crypto'; import { copyFileSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import test from 'node:test'; import { gzipSync, brotliCompressSync } from 'node:zlib'; import { check, PACK_MAGIC } from './check-no-nc.mjs'; function site(files, prefix = '') { const dir = mkdtempSync(join(tmpdir(), 'check-no-nc-')); const base = { 'index.html': '
arasaac
'), 'pkg/s.js.br': brotliCompressSync('sclera'), }); assert.deepEqual(check(dir).named.sort(), ['index.html.gz', 'pkg/aac-web.0123456789ab.wasm', 'pkg/s.js.br', 'symbols/LICENSES.md']); rmSync(dir, { recursive: true }); }); test('a file named for a set is caught in every folder, third-party ones too', () => { const dir = site({ 'vendor/arasaac-loader.js': 'export {};', 'voices/c/Sclera.txt': '', 'models/lang/arasaac/w.bin': 'x', 'pkg/sclera.js': 'export {};', }); assert.deepEqual(check(dir).named.sort(), ['models/lang/arasaac/w.bin', 'pkg/sclera.js', 'vendor/arasaac-loader.js', 'voices/c/Sclera.txt']); rmSync(dir, { recursive: true }); }); test('under /app/, the third-party folders are the app\'s', () => { const dir = site({ 'app/pkg/x.js': 'sclera', 'app/voices/y.txt': 'sclera' }, 'app/'); assert.deepEqual(check(dir, 'app/').named, ['app/pkg/x.js']); // The same folders outside the app are not third-party data. const other = site({ 'voices/x.txt': 'sclera' }, 'app/'); assert.deepEqual(check(other, 'app/').named, ['voices/x.txt']); rmSync(dir, { recursive: true }); rmSync(other, { recursive: true }); }); test('run as a command, as prerender-static.sh does, from any path, it checks', () => { const script = fileURLToPath(new URL('./check-no-nc.mjs', import.meta.url)); const tmp = mkdtempSync(join(tmpdir(), 'check-no-nc-cli-')); const spaced = join(tmp, 'with space'); mkdirSync(spaced); copyFileSync(script, join(spaced, 'check-no-nc.mjs')); symlinkSync(dirname(script), join(tmp, 'linked')); const ways = [ { name: 'as prerender-static.sh', args: [script] }, { name: 'from a path with a space', args: [join(spaced, 'check-no-nc.mjs')] }, { name: 'through a symlinked directory', args: [join(tmp, 'linked', 'check-no-nc.mjs')] }, { name: 'relative, inside the spaced path', args: ['check-no-nc.mjs'], cwd: spaced }, ]; const planted = site({ 'index.html': 'ARASAAC
' }); const clean = site({}); for (const way of ways) { const run = (dir) => spawnSync(process.execPath, [...way.args, dir], { cwd: way.cwd, encoding: 'utf8' }); const bad = run(planted); assert.equal(bad.status, 1, `${way.name}: ${bad.stdout}${bad.stderr}`); assert.match(bad.stderr, /names them in 1 file\(s\):\n index\.html/, way.name); const good = run(clean); assert.equal(good.status, 0, `${way.name}: ${good.stdout}${good.stderr}`); assert.match(good.stdout, /✓ no symbol pack/, way.name); } for (const dir of [tmp, planted, clean]) rmSync(dir, { recursive: true }); });