okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-clean-7 (045843071): the app loads screens, languages, Prism and Time-skip on first use; main chunk 3,123,528 -> 755,418 B, start screen 248.5 -> 143.4 ms (p2006 Chrome, median of 10); nothing users see changes (cleanup step 7)1631a9fde

main
4.0 KiB66 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import ts from 'typescript';
4import { readdir, readFile } from 'node:fs/promises';
5import { join } from 'node:path';
6import { build } from 'esbuild';
7
8const languages = ['en', 'zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko', 'fr'];
9const read = async name => JSON.parse(await readFile(`src/locales/${name}.json`, 'utf8'));
10const built = await build({ stdin: { contents: "export * from './src/lib/i18n';", resolveDir: process.cwd() }, bundle: true, write: false, format: 'esm', platform: 'node', loader: { '.json': 'json' } });
11const { tr, setTranslationLanguage, loadCatalog } = await import(`data:text/javascript;base64,${Buffer.from(built.outputFiles[0].text).toString('base64')}`);
12for (const l of ['zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko', 'fr']) await loadCatalog(l);  // the non-English catalogs load on first use (src/lib/i18n.ts loadCatalog); load them before the first tr()
13
14test('every tr("...") literal and every command hint has a key in the English catalog, so no language is stuck in English (#3479)', async () => {
15  const known = new Set();
16  for (const name of ['en', 'tune-en', 'tools-en', 'fix-en', 'gh-en', 'ui-en', 'engine-en', 'pick-en', 'df2-en', 'upd-en', 'r3-en', 'pic-en', 'gui-en', 'sav-en']) for (const v of Object.values(await read(name))) known.add(String(v));
17  const missing = [];
18  const scan = async dir => {
19    for (const entry of await readdir(dir, { withFileTypes: true })) {
20      const path = join(dir, entry.name);
21      if (entry.isDirectory()) { if (entry.name !== 'locales') await scan(path); continue; }
22      if (!/\.tsx?$/.test(path)) continue;
23      const source = await readFile(path, 'utf8'), sf = ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true, path.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
24      const visit = node => {
25        if (ts.isCallExpression(node) && node.expression.getText(sf) === 'tr' && node.arguments[0] && (ts.isStringLiteral(node.arguments[0]) || ts.isNoSubstitutionTemplateLiteral(node.arguments[0]))) {
26          const text = node.arguments[0].text.trim();
27          if (text && /[a-zA-Z]{2}/.test(text) && !known.has(text)) missing.push(`${path}: ${text}`);
28        }
29        if (ts.isPropertyAssignment(node) && node.name.getText(sf) === 'hint' && ts.isStringLiteral(node.initializer) && !path.endsWith('chat-showcase.ts')) {
30          const text = node.initializer.text.trim();
31          if (text && !known.has(text)) missing.push(`${path}: hint ${text}`);
32        }
33        ts.forEachChild(node, visit);
34      };
35      visit(sf);
36    }
37  };
38  await scan('src');
39  assert.deepEqual(missing, []);
40});
41
42test('the fix catalogs have the same keys and placeholders in all nine languages', async () => {
43  const en = await read('fix-en');
44  for (const language of languages) {
45    const catalog = await read(`fix-${language}`);
46    assert.deepEqual(Object.keys(catalog), Object.keys(en), language);
47    for (const [id, text] of Object.entries(catalog)) assert.deepEqual((text.match(/\{\d+\}/g) ?? []).sort(), (en[id].match(/\{\d+\}/g) ?? []).sort(), `${language} ${id}`);
48  }
49});
50
51test('the /plan, /research and /new hints and the four "Open ..." hints are translated (#3479, #3825)', () => {
52  for (const language of ['zh', 'ru']) {
53    setTranslationLanguage(language);
54    for (const hint of ['Have the assistant write a plan first; nothing runs until you approve it', 'Search the web, read the pages and answer with sources', 'Start a new conversation; keep this one'])
55      assert.notEqual(tr(hint), hint, `${language}: ${hint}`);
56    for (const label of ['Model settings', 'Serving', 'Models', 'Skills']) {
57      const hint = tr(`Open ${label}`);
58      assert.notEqual(hint, `Open ${label}`, `${language}: ${label} template`);
59      assert.ok(!hint.includes(label), `${language}: the label is translated too: ${hint}`);
60    }
61  }
62  setTranslationLanguage('zh');
63  assert.equal(tr('Open Skills'), '打开 技能');
64  setTranslationLanguage('en');
65  assert.equal(tr('Open Skills'), 'Open Skills');
66});