// The kit's every-control claims in docs/VUE-MIGRATION.md, read from every // SFC outside the kit: every select is a UiSelect, every pressed toggle a // UiButton with `pressed` (no template hand-writes `aria-pressed` on any // element, as an attribute or inside a `v-bind` object, apart from the // exceptions below), and every close, dismiss and clear button a // UiCloseButton. // Controls built in script are not read here. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync, readdirSync } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const SRC_ROOT = fileURLToPath(new URL('../../', import.meta.url)); const KIT_ROOT = fileURLToPath(new URL('./', import.meta.url)); const REPO_ROOT = path.resolve(SRC_ROOT, '..'); // The pressed toggles that still hand-write `aria-pressed`, by file: each // pattern matches exactly one of the file's tags, and no other tag may. const HAND_WRITTEN_PRESSED = { // The cinematic button renders the attributes the Bhote Koshi layer // writes for it, `aria-pressed` among them. 'src/ui/views/panels/BhoteKoshiPanel.vue': [/^/g, '') .replace(//g, ''); } // An opening tag's attributes, quoted values may hold `>` (`() => x`). const ATTRS = String.raw`(?:[^>"']|"[^"]*"|'[^']*')*`; const RAW_SELECT = /`, 'g'); // An attribute's name, outside the quoted values. const names = (attrs) => attrs.replace(/"[^"]*"|'[^']*'/g, '""'); const PRESSED = /(?:^|\s)(?::|v-bind:)?aria-pressed(?=[\s=/]|$)/; // A `v-bind` object that names aria-pressed. const BOUND_PRESSED = /(?:^|\s)(?:v-bind|:)=(?:"[^"]*aria-?pressed[^"]*"|'[^']*aria-?pressed[^']*')/i; const TIMES = String.raw`×|×|×|×`; const CLOSE_GLYPH = [ TIMES, String.raw`\s*(?:${TIMES})\s*`, String.raw`]*\bmaterial-symbols-outlined\b[^>]*>\s*close\s*`, String.raw``, ].join('|'); const HAND_WRITTEN_CLOSE = new RegExp( String.raw`<(button|UiButton)\b${ATTRS}>\s*(?:${CLOSE_GLYPH})\s*`, 'gi', ); const tidy = (markup) => markup.replace(/\s+/g, ' '); /** Raw selects in an SFC's template. */ const rawSelects = (source) => [...template(source).matchAll(RAW_SELECT)].map((m) => m[0]); /** Tags that hand-write aria-pressed. */ function handWrittenPressed(source) { const found = []; for (const m of template(source).matchAll(ANY_TAG)) if (PRESSED.test(names(m[1])) || BOUND_PRESSED.test(m[1])) found.push(tidy(m[0])); return found; } /** Hand-written close buttons in an SFC's template. */ const handWrittenCloses = (source) => [...template(source).matchAll(HAND_WRITTEN_CLOSE)].map((m) => tidy(m[0])); const views = sfcFiles().map((file) => ({ file: path.relative(REPO_ROOT, file).split(path.sep).join('/'), source: readFileSync(file, 'utf8'), })); test('the scans find each hand-written control', () => { for (const markup of [ '', '', ]) assert.equal(rawSelects(markup).length, 1, markup); for (const markup of [ '', '', '', ]) assert.equal(rawSelects(markup).length, 0, markup); for (const markup of [ '', '', '', '', '', '', '', ]) assert.equal(handWrittenPressed(markup).length, 1, markup); for (const markup of [ '', '', '', ]) assert.equal(handWrittenPressed(markup).length, 0, markup); for (const markup of [ '', '', '', ]) assert.equal(handWrittenCloses(markup).length, 1, markup); for (const markup of [ '', '', ]) assert.equal(handWrittenCloses(markup).length, 0, markup); }); test('the views are read: the scan reaches the views and skips the kit', () => { const files = views.map((view) => view.file); assert.ok(files.includes('src/ui/views/panels/ChatPanel.vue')); assert.ok(files.includes('src/ui/layerPanel/RowRange.vue')); assert.ok(!files.some((file) => file.startsWith('src/ui/kit/'))); }); test('every select in the views is a UiSelect', () => { const found = []; for (const { file, source } of views) for (const markup of rawSelects(source)) found.push(`${file}: ${markup}`); assert.deepEqual(found, []); }); test('no view hand-writes aria-pressed on a button outside the documented exceptions', () => { const unexpected = []; for (const { file, source } of views) { const allowed = [...(HAND_WRITTEN_PRESSED[file] ?? [])]; for (const markup of handWrittenPressed(source)) { const at = allowed.findIndex((pattern) => pattern.test(markup)); if (at === -1) unexpected.push(`${file}: ${markup}`); else allowed.splice(at, 1); } assert.deepEqual(allowed, [], `${file}: an exception no longer matches`); } assert.deepEqual(unexpected, []); }); test('no view hand-writes a close button; each is a UiCloseButton', () => { const found = []; for (const { file, source } of views) for (const markup of handWrittenCloses(source)) found.push(`${file}: ${markup}`); assert.deepEqual(found, []); });