|
| 1 | +const assert = require('node:assert/strict'); |
| 2 | +const fs = require('node:fs'); |
| 3 | +const path = require('node:path'); |
| 4 | +const test = require('node:test'); |
| 5 | + |
| 6 | +const root = path.join(__dirname, '..'); |
| 7 | + |
| 8 | +function readFile(name) { |
| 9 | + return fs.readFileSync(path.join(root, name), 'utf8'); |
| 10 | +} |
| 11 | + |
| 12 | +test('README source table matches code source registry', () => { |
| 13 | + const html = readFile('index.html'); |
| 14 | + const readme = readFile('README.md'); |
| 15 | + |
| 16 | + const sourceMatch = html.match(/var SOURCES\s*=\s*\{([\s\S]*?)\n\s*\};/); |
| 17 | + assert.ok(sourceMatch, 'SOURCES registry should exist in index.html'); |
| 18 | + |
| 19 | + const nameMatches = sourceMatch[1].matchAll(/name:\s*'([^']+)'/g); |
| 20 | + const codeSourceNames = Array.from(nameMatches, (m) => m[1]).sort(); |
| 21 | + |
| 22 | + assert.ok(codeSourceNames.length >= 6, 'Should have at least 6 sources in code'); |
| 23 | + |
| 24 | + for (const name of codeSourceNames) { |
| 25 | + assert.ok( |
| 26 | + readme.includes(name), |
| 27 | + `README should mention source "${name}" from the code registry` |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + const sourceTableMatch = readme.match(/\| Source \| Method.*\n\|[-:|]+\n([\s\S]*?)(?:\n\n|\n---)/); |
| 32 | + assert.ok(sourceTableMatch, 'README should have a source table'); |
| 33 | + |
| 34 | + const readmeSourceLines = sourceTableMatch[1].trim().split('\n').filter((l) => l.startsWith('|')); |
| 35 | + assert.equal( |
| 36 | + readmeSourceLines.length, |
| 37 | + codeSourceNames.length, |
| 38 | + `README source table rows (${readmeSourceLines.length}) should match code source count (${codeSourceNames.length})` |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +test('each source declares page size in code', () => { |
| 43 | + const html = readFile('index.html'); |
| 44 | + const sourceMatch = html.match(/var SOURCES\s*=\s*\{([\s\S]*?)\n\s*\};/); |
| 45 | + assert.ok(sourceMatch); |
| 46 | + |
| 47 | + const idMatches = sourceMatch[1].matchAll(/(\w+):\s*\{\s*id:/g); |
| 48 | + const sourceIds = Array.from(idMatches, (m) => m[1]); |
| 49 | + |
| 50 | + for (const id of sourceIds) { |
| 51 | + const pageSizeRx = new RegExp(id + '.*?pageSize:\\s*(\\d+)', 's'); |
| 52 | + const match = sourceMatch[1].match(pageSizeRx); |
| 53 | + assert.ok(match, `Source "${id}" should declare a pageSize`); |
| 54 | + assert.ok(parseInt(match[1]) > 0, `Source "${id}" pageSize should be positive`); |
| 55 | + } |
| 56 | +}); |
0 commit comments