Oregami
Repositories/oxedyne/daimond

oxedyne/daimond/dev/verify_starters.mjs

2.1 KiB, 1 run

created by r2519314175:693, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// The starter tags, in every language.
2//
3// They are the one place the interface puts a word INTO the user's data. A tag
4// is lowercased and space-collapsed by the store, so a starter that is not
5// already in that form would draw a chip reading one thing and file another --
6// and the chip would then not match the tag it just made, so clicking it again
7// would offer to add it a second time. The app normalises them defensively;
8// this makes sure no locale is relying on that.
9import fs from 'node:fs';
10import path from 'node:path';
11import { fileURLToPath } from 'node:url';
12
13const HERE = path.dirname(fileURLToPath(import.meta.url));
14const I18N = path.join(HERE, '..', 'www', 'i18n');
15const LOCALES = ['en', 'de', 'es', 'fr', 'ja', 'ko', 'pt-BR', 'zh-Hans'];
16
17let bad = 0;
18const check = (ok, what) => { console.log(`${ok ? 'PASS' : 'FAIL'} ${what}`); if (!ok) bad++; };
19
20/// What the store would make of a tag: trimmed, inner whitespace collapsed,
21/// lowercased. Mirrors normalise_tags in src/diamond_meta.rs.
22const norm = (s) => s.split(/\s+/).filter(Boolean).join(' ').toLowerCase();
23
24const MAX_TAG_LEN = 32; // src/diamond_meta.rs
25
26for (const loc of LOCALES) {
27 const src = fs.readFileSync(path.join(I18N, `${loc}.js`), 'utf8');
28 const m = src.match(/'tag\.starters':\s*'((?:[^'\\]|\\.)*)'/);
29 check(!!m, `${loc}: declares tag.starters`);
30 if (!m) continue;
31 const raw = m[1].replace(/\\'/g, "'").split(',').map(s => s.trim()).filter(Boolean);
32 check(raw.length === 4, `${loc}: offers four starters (${raw.length}): ${raw.join(' / ')}`);
33 for (const tag of raw) {
34 check(norm(tag) === tag,
35 `${loc}: ${JSON.stringify(tag)} is already as the store would file it${norm(tag) === tag ? '' : ` (would become ${JSON.stringify(norm(tag))})`}`);
36 check([...tag].length <= MAX_TAG_LEN,
37 `${loc}: ${JSON.stringify(tag)} fits the ${MAX_TAG_LEN}-character cap (${[...tag].length})`);
38 }
39 // Distinct after normalisation, or the pool would offer the same tag twice.
40 const set = new Set(raw.map(norm));
41 check(set.size === raw.length, `${loc}: the four are distinct`);
42}
43
44console.log(bad ? `\n${bad} FAILED` : `\nALL PASS`);
45process.exit(bad ? 1 : 0);