rob-revisited/ide/ui/js/docs-model.test.mjs

137 lines
7.5 KiB
JavaScript

// The Doc browser's model, and the documentation itself (docs/ide/): every page of the table of contents exists, every link between
// pages leads to a page (and to a heading of it), every picture exists.
// Run: node --test ide/ui/js/docs-model.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { buildToc, tocIds, resolveLink, resolveImage, search, slug, headings, pagesIn, isSoon, terms, History, TOOL_PAGES } from './docs-model.js';
const here = dirname(fileURLToPath(import.meta.url));
const DOCS = join(here, '..', '..', '..', 'docs', 'ide');
const page = (id, lang, text) => ({ id, lang, title: (/^#\s+(.+)$/m.exec(text) || [, id])[1], text });
test('slugs are the anchors GitHub makes', () => {
assert.equal(slug('The PHP version'), 'the-php-version');
assert.equal(slug('`.env` files'), 'env-files');
assert.equal(slug('Réglages : thème'), 'reglages--theme');
assert.equal(slug('Docker: a remote engine'), 'docker-a-remote-engine');
});
test('links: pages, anchors, the rest of the docs online, web addresses', () => {
const en = page('git', 'en', '# Git');
const fr = page('git', 'fr', '# Git');
assert.deepEqual(resolveLink('tests.md', en), { page: 'tests', anchor: '' });
assert.deepEqual(resolveLink('tests.md#coverage', en), { page: 'tests', anchor: 'coverage' });
assert.deepEqual(resolveLink('#branches', en), { page: 'git', anchor: 'branches' });
assert.deepEqual(resolveLink('tests.md', fr), { page: 'tests', anchor: '' });
assert.deepEqual(resolveLink('../tests.md', fr), { page: 'tests', anchor: '' });
assert.deepEqual(resolveLink('../web.md#the-php-version', en), { url: 'https://jumphp.net/docs/web#the-php-version' });
assert.deepEqual(resolveLink('../../web.md', fr), { url: 'https://jumphp.net/docs/web' });
assert.deepEqual(resolveLink('https://example.org/x', en), { url: 'https://example.org/x' });
});
test('pictures: only the documentation\'s own, from English and translated pages', () => {
assert.equal(resolveImage('images/git-log.png', page('git', 'en', '')), 'images/git-log.png');
assert.equal(resolveImage('../images/git-log.png', page('git', 'fr', '')), 'images/git-log.png');
assert.equal(resolveImage('../../secret.png', page('git', 'en', '')), null);
assert.equal(resolveImage('images/../../x.png', page('git', 'en', '')), null);
assert.equal(resolveImage('https://x/y.png', page('git', 'en', '')), null);
});
test('the table of contents follows toc.json, in the language when a page is translated, the others last', () => {
const pages = [page('index', 'en', '# Overview'), page('git', 'en', '# Git'), page('git', 'fr', '# Git (fr)'), page('extra', 'en', '# Extra'),
page('soon', 'en', '# Soon\n\n> **Coming soon**: being built')];
const toc = { groups: [{ id: 'start', title: { en: 'Start', fr: 'Début' }, pages: ['index', 'git', 'missing'] }, { id: 'later', title: { en: 'Later' }, pages: ['soon'] }] };
const fr = buildToc(toc, pages, 'fr');
assert.equal(fr[0].title, 'Début');
assert.deepEqual(fr[0].pages.map((p) => [p.id, p.title, p.translated]), [['index', 'Overview', false], ['git', 'Git (fr)', true]]);
assert.equal(fr[1].title, 'Later');
assert.equal(fr[1].pages[0].soon, true);
assert.deepEqual(fr[2].pages.map((p) => p.id), ['extra']);
assert.equal(pagesIn(pages, 'de').get('git').lang, 'en');
});
test('search: every word, accents ignored, titles first, the heading above the match', () => {
const pages = [
page('a', 'en', '# Tests\n\nRun the tests.\n\n## Coverage\n\nThe coverage in the gutter.'),
page('b', 'en', '# Git\n\nCommit, then the tests run on the server.'),
page('b', 'fr', '# Git\n\nValider, puis les tests tournent sur le serveur. Réseau.'),
];
const r = search(pages, 'tests', 'en');
assert.deepEqual(r.map((x) => x.id), ['a', 'b']);
assert.deepEqual(search(pages, 'coverage gutter', 'en').map((x) => [x.id, x.anchor]), [['a', 'coverage']]);
assert.deepEqual(search(pages, 'reseau', 'fr').map((x) => x.id), ['b']);
assert.deepEqual(search(pages, 'reseau', 'en'), []);
assert.deepEqual(terms('"the gutter" x tests'), ['the gutter', 'tests']);
assert.deepEqual(search(pages, '', 'en'), []);
});
test('headings skip code blocks; history goes back and forward', () => {
assert.deepEqual(headings('# A\n```\n# not\n```\n## B').map((h) => h.anchor), ['a', 'b']);
const h = new History();
h.push({ page: 'a', anchor: '' }); h.push({ page: 'b', anchor: '' }); h.push({ page: 'b', anchor: '' });
assert.equal(h.items.length, 2);
assert.equal(h.back().page, 'a');
assert.equal(h.forward().page, 'b');
h.back(); h.push({ page: 'c', anchor: '' });
assert.equal(h.canForward(), false);
assert.equal(isSoon('# X\n\n> **Coming soon**: y'), true);
});
// the documentation itself ------------------------------------------------------------------------------------------------------
function load() {
const pages = [];
for (const [dir, lang] of [[DOCS, 'en'], ...readdirSync(DOCS).filter((d) => /^[a-z]{2}$/.test(d)).map((d) => [join(DOCS, d), d])]) {
for (const f of readdirSync(dir).filter((n) => n.endsWith('.md'))) pages.push(page(f.slice(0, -3), lang, readFileSync(join(dir, f), 'utf8')));
}
return pages;
}
test('docs/ide: every page of the table of contents exists in English, and every English page is in it', () => {
const toc = JSON.parse(readFileSync(join(DOCS, 'toc.json'), 'utf8'));
const pages = load();
const en = new Set(pages.filter((p) => p.lang === 'en').map((p) => p.id));
const ids = tocIds(toc);
assert.deepEqual(ids.filter((id) => !en.has(id)), [], 'pages named by toc.json but missing');
assert.deepEqual([...en].filter((id) => !ids.includes(id)), [], 'pages missing from toc.json');
assert.equal(new Set(ids).size, ids.length, 'a page twice in toc.json');
for (const p of pages.filter((x) => x.lang !== 'en')) assert.ok(en.has(p.id), `${p.lang}/${p.id}.md has no English page`);
for (const g of toc.groups) assert.ok(g.title.en && g.title.fr, `group ${g.id}: titles in English and French`);
for (const id of Object.values(TOOL_PAGES)) assert.ok(en.has(id), `TOOL_PAGES names ${id}, which does not exist`);
});
test('docs/ide: every link leads to a page and its heading, every picture exists', () => {
const pages = load();
const bad = [];
for (const p of pages) {
let inCode = false;
for (const line of p.text.split('\n')) {
if (/^```/.test(line)) { inCode = !inCode; continue; }
if (inCode) continue;
for (const m of line.replace(/`[^`]*`/g, '').matchAll(/(!?)\[[^\]]*\]\(([^)\s]+)\)/g)) {
const where = `${p.lang}/${p.id}.md: ${m[2]}`;
if (m[1]) {
const img = resolveImage(m[2], p);
if (!img || !existsSync(join(DOCS, img))) bad.push(`picture ${where}`);
continue;
}
const to = resolveLink(m[2], p);
if (to.url) {
if (to.url.startsWith('https://jumphp.net/docs/') && !to.url.startsWith('https://jumphp.net/docs/ide/')) {
const name = to.url.slice('https://jumphp.net/docs/'.length).split('#')[0];
if (!existsSync(join(DOCS, '..', name + '.md'))) bad.push(`docs page ${where}`);
}
continue;
}
const target = pagesIn(pages, p.lang).get(to.page);
if (!target) { bad.push(`page ${where}`); continue; }
if (to.anchor && !headings(target.text).some((h) => h.anchor === to.anchor)) bad.push(`anchor ${where}`);
}
}
}
assert.deepEqual(bad, []);
});