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
9.6 KiB150 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFile } from 'node:fs/promises';
4import { build } from 'esbuild';
5import { KNOWN_KEYS, checkAndParseRunConfig, checkRunConfig, describeIssues, nearestKey } from '../server/run-config-check.mjs';
6import { NATIVE_FIELDS } from '../server/native-settings.mjs';
7import { openTextFile, textEditorCommand } from '../server/open-text-file.mjs';
8
9const good = `{
10  "exe": "/opt/strata/strata",
11  "args": ["--native", "/m/a.gguf", "--max-context", "65536"],
12  "env": {"STRATA_PCIE_FRAC_DEFAULT": "old"},
13  "sampling": {"temperature": 0.7, "repeat_penalty": 1.1},
14  "parallel": 2,
15  "aliases": "chat, coding",
16  "gpu": [0, 1],
17  "layer_split": "16,32",
18  "port": 8080
19}`;
20
21test('a normal run config has no issues', () => {
22  assert.deepEqual(checkRunConfig(good), { ok: true, issues: [] });
23  assert.deepEqual(checkRunConfig('\uFEFF{}'), { ok: true, issues: [] }, 'Notepad’s byte-order mark is fine');
24});
25
26test('the checked configuration is parsed from the same captured text', () => {
27  const text = '\uFEFF{"parallel":2,"args":["--ple-io","mmap"]}';
28  const checked = checkAndParseRunConfig(text);
29  assert.deepEqual(checked, {
30    ok: true,
31    issues: [],
32    config: { parallel: 2, args: ['--ple-io', 'mmap'] },
33  });
34  const refused = checkAndParseRunConfig('{"parallel":99}');
35  assert.equal(refused.ok, false);
36  assert.equal(refused.config, null);
37  assert.equal(refused.issues[0].code, 'whole-range');
38});
39
40test('every key the app edits is a known key', () => {
41  for (const field of NATIVE_FIELDS) assert.ok(KNOWN_KEYS.has(field.id), field.id);
42});
43
44test('every key setup itself writes into a run config is a known key, so a freshly set-up model shows no warning in the settings file panel (QA 0213: "args_written" was flagged as unknown)', async () => {
45  const setup = await readFile(new URL('../../../setup.py', import.meta.url), 'utf8');
46  const block = /^SETUP_KEYS = frozenset\(\{([^}]*)\}\)/m.exec(setup);
47  assert.ok(block, 'SETUP_KEYS is in setup.py');
48  const keys = [...block[1].matchAll(/"([a-z_]+)"/g)].map(m => m[1]);
49  assert.ok(keys.includes('args_written') && keys.length > 10);
50  for (const key of [...keys, 'user_args']) assert.ok(KNOWN_KEYS.has(key), `${key} is written by setup/the app but the settings check calls it unknown`);
51  const text = JSON.stringify({ exe: 'e', args: ['--native', 'm'], args_written: ['--native', 'm'], user_args: ['--pool-workers', '8'], port: 8080 });
52  assert.deepEqual(checkRunConfig(text), { ok: true, issues: [] });
53});
54
55test('broken JSON is reported with its line, in plain words', () => {
56  const text = '{\n  "args": ["--ple-io" "mmap"],\n  "port": 8080\n}';
57  const result = checkRunConfig(text);
58  assert.equal(result.ok, false);
59  assert.equal(result.issues[0].code, 'json');
60  assert.equal(result.issues[0].level, 'error');
61  assert.equal(result.issues[0].line, 2);
62  assert.match(describeIssues(result.issues)[0], /^Line 2: The settings file is not valid JSON; look near line 2\. Check for a missing comma/);
63  assert.equal(checkRunConfig('[1,2]').issues[0].code, 'not-object');
64  assert.equal(checkRunConfig('').ok, false);
65});
66
67test('an unknown key is a warning with the nearest real name, and does not hold the start back', () => {
68  const result = checkRunConfig('{\n  "paralel": 2,\n  "idle_unload_secs": 5,\n  "totally_made_up_thing": 1\n}');
69  assert.equal(result.ok, true);
70  assert.deepEqual(result.issues.map(i => [i.level, i.code, i.key, i.params.near, i.line]), [
71    ['warning', 'unknown-key-near', 'paralel', 'parallel', 2],
72    ['warning', 'unknown-key-near', 'idle_unload_secs', 'idle_unload_s', 3],
73    ['warning', 'unknown-key', 'totally_made_up_thing', '', 4]]);
74  assert.match(result.issues[0].message, /"paralel" is not a setting Strata reads, so it is ignored\. Did you mean "parallel"\?/);
75  assert.equal(nearestKey('Lazy-Load'), 'lazy_load');
76  assert.equal(nearestKey('zzzzzzzz'), null);
77});
78
79test('a bad value is an error that names the key, the line, what is allowed and what was found', () => {
80  const text = '{\n  "parallel": 12,\n  "lazy_load": "yes",\n  "anthropic_thinking": "sometimes",\n  "idle_unload_s": 1.5,\n  "port": 70000,\n  "args": ["--ple-io", 4],\n  "env": {"PATH": "/x", "STRATA_Y": 2},\n  "sampling": {"temperature": -1, "top_k": 0.5, "banana": 3},\n  "gpu": "first",\n  "layer_split": "32,16",\n  "reasoning_effort": "max"\n}';
81  const result = checkRunConfig(text);
82  assert.equal(result.ok, false);
83  const by = key => result.issues.filter(i => i.key === key);
84  assert.deepEqual(by('parallel').map(i => [i.code, i.line, i.params.min, i.params.max, i.params.value]), [['whole-range', 2, 1, 8, '12']]);
85  assert.equal(by('lazy_load')[0].code, 'bool');
86  assert.deepEqual([by('anthropic_thinking')[0].code, by('anthropic_thinking')[0].params.choices], ['choice', 'model, on_request']);
87  assert.equal(by('idle_unload_s')[0].code, 'whole-range');
88  assert.equal(by('port')[0].code, 'number-range');
89  assert.equal(by('args')[0].code, 'args-type');
90  assert.deepEqual(by('env').map(i => i.code), ['env-value'], 'names other than STRATA_ (PATH) are kept, only a non-text value is wrong');
91  assert.deepEqual(by('sampling').map(i => [i.code, i.params.key]), [['number-range', 'sampling.temperature'], ['whole-range', 'sampling.top_k'], ['unknown-key', 'sampling.banana']]);
92  assert.equal(by('gpu')[0].code, 'gpu');
93  assert.equal(by('layer_split')[0].code, 'layer-split');
94  assert.equal(by('reasoning_effort')[0].code, 'choice');
95  assert.ok(result.issues.filter(i => i.level === 'error').every(i => i.message.length > 20));
96});
97
98test('null and empty values mean "use the default" and are never an error', () => {
99  assert.deepEqual(checkRunConfig('{"parallel": null, "lazy_load": null, "reasoning_budget_tokens": ""}'), { ok: true, issues: [] });
100});
101
102test('--host, --port and --api-key in args are a warning, not a refusal', () => {
103  const r = checkRunConfig('{"args": ["--port", "9"]}');
104  assert.equal(r.ok, true);
105  assert.equal(r.issues[0].code, 'args-managed');
106});
107
108test('the text editor: Notepad on Windows, text mode on a Mac, the system text-file program on Linux', async () => {
109  assert.deepEqual(await textEditorCommand('C:\\s\\strata-q.json', { platform: 'win32' }), ['notepad.exe', ['C:\\s\\strata-q.json']]);
110  assert.deepEqual(await textEditorCommand('/s/strata-q.json', { platform: 'darwin' }), ['open', ['-t', '/s/strata-q.json']]);
111  const ask = async (command, args) => command === 'xdg-mime' ? 'org.gnome.gedit.desktop\n' : args[1] === 'command -v gtk-launch' ? '/usr/bin/gtk-launch\n' : '';
112  assert.deepEqual(await textEditorCommand('/s/strata-q.json', { platform: 'linux', ask }), ['/usr/bin/gtk-launch', ['org.gnome.gedit.desktop', '/s/strata-q.json']]);
113  const noEditor = async () => '';
114  const seen = [];
115  await openTextFile('/s/strata-q.json', { platform: 'linux', ask: noEditor, recorder: seen });
116  assert.deepEqual(seen, [['xdg-open', '/s/strata-q.json']], 'with no text editor known the file’s own program opens it');
117});
118
119test('every issue sentence exists in all nine languages and keeps its slots', async () => {
120  const languages = ['en', 'zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko', 'fr'];
121  const compiled = await build({ stdin: { contents: "export * from './src/lib/run-config-issues'; export * from './src/lib/i18n';", resolveDir: process.cwd() }, bundle: true, write: false, platform: 'node', format: 'esm' });
122  const { issueText, setTranslationLanguage, loadCatalog } = await import(`data:text/javascript;base64,${Buffer.from(compiled.outputFiles[0].text).toString('base64')}`);
123  for (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()
124  const samples = [
125    { code: 'json', line: 3, params: { line: 3 } }, { code: 'json', line: null, params: { line: 0 } }, { code: 'not-object', params: {} },
126    { code: 'unknown-key-near', params: { key: 'paralel', near: 'parallel' } }, { code: 'unknown-key', params: { key: 'zzz' } },
127    { code: 'args-type', params: {} }, { code: 'args-managed', params: {} }, { code: 'env-type', params: {} },
128    { code: 'env-value', params: { name: 'STRATA_X' } }, { code: 'sampling-type', params: {} }, { code: 'bool', params: { key: 'lazy_load', value: '"yes"' } },
129    { code: 'number-range', params: { key: 'port', min: 1, max: 65535, value: '0' } }, { code: 'whole-range', params: { key: 'parallel', min: 1, max: 8, value: '12' } },
130    { code: 'choice', params: { key: 'gpu_order', choices: 'auto, as_given', value: '"x"' } }, { code: 'text', params: { key: 'aliases' } },
131    { code: 'layer-split', params: {} }, { code: 'gpu', params: {} },
132  ].map(s => ({ level: 'error', key: 'k', message: 'ENGLISH FALLBACK', line: s.line === undefined ? 5 : s.line, ...s }));
133  const english = {};
134  setTranslationLanguage('en');
135  for (const s of samples) english[`${s.code}:${s.line}`] = issueText(s);
136  for (const s of samples) assert.notEqual(english[`${s.code}:${s.line}`], 'ENGLISH FALLBACK', s.code);
137  for (const language of languages.filter(l => l !== 'en')) {
138    setTranslationLanguage(language);
139    for (const s of samples) {
140      const text = issueText(s);
141      assert.notEqual(text, english[`${s.code}:${s.line}`], `${language}:${s.code} is not translated`);
142      assert.ok(!/\{\d+\}/.test(text), `${language}:${s.code} left a slot`);
143    }
144  }
145  setTranslationLanguage('en');
146});
147
148test('the notes the app itself writes into the file (ui_resource_baseline, ui_vision_device, ...) are not reported', () => {
149  assert.deepEqual(checkRunConfig('{"ui_resource_baseline": {"parallel": 2}, "ui_vision_device": "cpu", "ui_resource_profile": "work"}'), { ok: true, issues: [] });
150});