Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "astro dev",
"build": "node scripts/gen_sdk_ref.mjs && node scripts/gen_errors.mjs && astro build && node scripts/postbuild.mjs",
"verify": "node scripts/verify-fresh.mjs && node scripts/verify-figures.mjs && node scripts/verify-workflows.mjs && node scripts/verify-urls.mjs && node scripts/verify-security-txt.mjs && node scripts/verify-first-lines.mjs && node scripts/verify-public-words.mjs && node scripts/verify-mcp.mjs && node scripts/verify-cors.mjs && node scripts/verify-toolkit.mjs && node scripts/verify-shapes.mjs && node scripts/verify-limits.mjs && node scripts/verify-stability.mjs && node scripts/mutants-stability.mjs &&node scripts/verify-guide.mjs && node scripts/verify-sd-counts.mjs && node scripts/verify-identifiers.mjs && node scripts/verify-examples.mjs &&node scripts/verify-practice.mjs && node scripts/verify-run.mjs && node scripts/mutants-run.mjs",
"verify": "node scripts/verify-fresh.mjs && node scripts/verify-figures.mjs && node scripts/verify-workflows.mjs && node scripts/verify-urls.mjs && node scripts/verify-security-txt.mjs && node scripts/verify-first-lines.mjs && node scripts/verify-public-words.mjs && node scripts/verify-mcp.mjs && node scripts/verify-cors.mjs && node scripts/verify-toolkit.mjs && node scripts/verify-shapes.mjs && node scripts/verify-limits.mjs && node scripts/verify-stability.mjs && node scripts/mutants-stability.mjs &&node scripts/verify-guide.mjs && node scripts/verify-definitions.mjs && node scripts/mutants-definitions.mjs && node scripts/verify-sd-counts.mjs && node scripts/verify-identifiers.mjs && node scripts/verify-examples.mjs &&node scripts/verify-practice.mjs && node scripts/verify-run.mjs && node scripts/mutants-run.mjs",
"check": "npm run build && npm run verify"
},
"dependencies": {
Expand Down
63 changes: 63 additions & 0 deletions scripts/mutants-definitions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node
// mutants-definitions.mjs: verify-definitions watched failing. Each fault is planted in a copy of the
// schema pages, the check is run on the copy (--root), and it must end red with the line named here; the
// untouched copy must pass. A plant that changes nothing in its file stops the run.
//
// Usage: node scripts/mutants-definitions.mjs
import { cpSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const HERE = dirname(fileURLToPath(import.meta.url));
const SRC = join(HERE, '..', 'src', 'content', 'docs', 'docs', 'schema');
const REL = ['src', 'content', 'docs', 'docs', 'schema'];

const ROW = (type) => new RegExp(`^(\\| \\[[A-Za-z]+\\]\\(/docs/schema/element_categories/${type}\\) \\| )(.*)( \\|)$`, 'm');
const PLANTS = [
{ name: 'a table row reworded', file: 'index.mdx', exit: 1, says: 'DRIFT character: the table says',
edit: (t) => t.replace(ROW('character'), (_, a, b, c) => a + b.replace(/\.$/, ' of note.') + c) },
{ name: "a page's opening sentence reworded", file: 'element_categories/trait.md', exit: 1, says: 'DRIFT trait: the table says',
edit: (t) => t.replace(/^(Traits? [^\n]*?)\. /m, '$1 indeed. ') },
{ name: 'a row removed', file: 'index.mdx', exit: 1, says: 'DRIFT zone: its page has no row',
edit: (t) => t.replace(new RegExp(ROW('zone').source + '\\r?\\n', 'm'), '') },
{ name: 'a row for a type with no page', file: 'index.mdx', exit: 1, says: 'DRIFT vehicle: a row in the Schema table with no type page',
edit: (t) => t.replace(ROW('zone'), (line) => `${line}\n| [Vehicle](/docs/schema/element_categories/vehicle) | A Vehicle carries things. |`) },
{ name: "a Pin's row made its first sentence", file: 'index.mdx', exit: 1, says: 'DRIFT pin: the table says "A Pin is a special Map element."',
edit: (t) => t.replace(ROW('pin'), (_, a, b, c) => `${a}A Pin is a special Map element.${c}`) },
{ name: 'a type page gone', file: 'element_categories/law.md', exit: 2, says: 'CANNOT SEE: 21 type pages', edit: () => null },
];

const dir = mkdtempSync(join(tmpdir(), 'ow-defs-'));
const run = (root) => {
const r = spawnSync(process.execPath, [join(HERE, 'verify-definitions.mjs'), '--root', root], { encoding: 'utf8' });
return { code: r.status, out: r.stdout + r.stderr };
};
const fresh = (name) => {
const root = join(dir, name);
cpSync(SRC, join(root, ...REL), { recursive: true });
return root;
};
let bad = 0;
try {
const control = run(fresh('control'));
if (control.code !== 0) { console.log(` FAIL the control (an untouched copy) ended ${control.code}`); bad++; }
PLANTS.forEach((p, i) => {
const root = fresh(`m${i}`);
const file = join(root, ...REL, ...p.file.split('/'));
const before = readFileSync(file, 'utf8');
const after = p.edit(before);
if (after === null) rmSync(file);
else if (after === before) { console.log(` FAIL ${p.name}: the plant changed nothing in ${p.file}`); bad++; return; }
else writeFileSync(file, after);
const r = run(root);
const ok = r.code === p.exit && r.out.includes(p.says);
if (!ok) bad++;
console.log(` ${ok ? 'red as named' : 'FAIL'}: ${p.name}${ok ? '' : ` (exit ${r.code}, wanted ${p.exit} with "${p.says}")`}`);
});
} finally {
rmSync(dir, { recursive: true, force: true });
}
if (bad) { console.log(`mutants-definitions: FAIL: ${bad} of ${PLANTS.length + 1} runs did not end as named`); process.exit(1); }
console.log(`mutants-definitions: PASS: ${PLANTS.length} planted faults each failed verify-definitions as named; the control passed`);
62 changes: 62 additions & 0 deletions scripts/verify-definitions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env node
// verify-definitions.mjs: the Schema page's table of the 22 element types against the type pages.
//
// A type's definition is the opening paragraph of its own page (docs/schema/element_categories/<type>.md).
// The Schema page's table gives one sentence per type, and that sentence is the page's own: its first
// sentence, or for the two types whose page opens by placing them ("A Pin is a special Map element.") its
// second. The table is written by hand, so nothing kept it to the pages: a reworded intro left the table
// saying the old words. This check fails on a row that is not its page's sentence, a page with no row and
// a row with no page.
//
// Usage: node scripts/verify-definitions.mjs [--root <dir>] (reads the page sources, no build needed)
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const at = process.argv.indexOf('--root');
const ROOT = at > -1 ? process.argv[at + 1] : join(dirname(fileURLToPath(import.meta.url)), '..');
const SCHEMA = join(ROOT, 'src', 'content', 'docs', 'docs', 'schema');
const PAGES = join(SCHEMA, 'element_categories');
const TABLE = join(SCHEMA, 'index.mdx');
// The types whose table row is their page's second sentence: the first only says where the type belongs.
const SECOND = new Set(['pin', 'marker']);

if (!existsSync(PAGES) || !existsSync(TABLE)) { console.log(`CANNOT SEE: ${existsSync(TABLE) ? PAGES : TABLE} is missing`); process.exit(2); }

const intro = (text) => {
const body = text.startsWith('---') ? text.split('---').slice(2).join('---') : text;
for (const block of body.trim().split(/\r?\n\s*\r?\n/)) {
const b = block.trim();
if (b && !/^(import |<|#|:::)/.test(b)) return b.replace(/\s+/g, ' ');
}
return '';
};
const sentences = (s) => s.split(/(?<=\.)\s+/).filter(Boolean);

const pages = new Map();
for (const f of readdirSync(PAGES).filter((f) => /\.mdx?$/.test(f) && !f.startsWith('index.')).sort()) {
pages.set(f.replace(/\.mdx?$/, ''), intro(readFileSync(join(PAGES, f), 'utf8')));
}
const rows = new Map();
for (const line of readFileSync(TABLE, 'utf8').split(/\r?\n/)) {
const m = line.match(/^\| \[[A-Za-z]+\]\(\/docs\/schema\/element_categories\/([a-z]+)\) \| (.*) \|$/);
if (m) rows.set(m[1], m[2].trim());
}
if (pages.size !== 22 || rows.size === 0) {
console.log(`CANNOT SEE: ${pages.size} type pages (22 expected) and ${rows.size} table rows read`);
process.exit(2);
}

const bad = [];
for (const [type, full] of pages) {
const row = rows.get(type);
if (row === undefined) { bad.push(`${type}: its page has no row in the Schema table`); continue; }
const want = sentences(full)[SECOND.has(type) ? 1 : 0];
if (!want) { bad.push(`${type}: its page's opening paragraph has no ${SECOND.has(type) ? 'second ' : ''}sentence to compare`); continue; }
if (row !== want) bad.push(`${type}: the table says "${row}" and the page's ${SECOND.has(type) ? 'second' : 'first'} sentence is "${want}"`);
}
for (const type of rows.keys()) if (!pages.has(type)) bad.push(`${type}: a row in the Schema table with no type page`);

for (const b of bad) console.log(` DRIFT ${b}`);
if (bad.length) { console.log(`FAIL: ${bad.length} of ${pages.size} definitions differ between the Schema table and the type pages`); process.exit(1); }
console.log(`definitions: PASS — the Schema table's ${rows.size} rows are each their type page's own sentence (the first; the second for ${[...SECOND].sort().join(' and ')})`);
Loading