okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
12.5 KiB182 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFileSync as read } from 'node:fs';
4import { build } from 'esbuild';
5import { listEngines, changeEngine } from '../server/engine-library.mjs';
6import { plainEngineError } from '../server/update-messages.mjs';
7import { createControl } from '../server/control-runtime.mjs';
8import { checkComponentUpdates } from '../server/updates.mjs';
9
10const src = name => read(new URL(`../${name}`, import.meta.url), 'utf8');
11const json = name => JSON.parse(src(`src/locales/${name}.json`));
12const built = await build({ stdin: { contents: "export * from './src/lib/engine-list';", resolveDir: process.cwd() }, bundle: true, write: false, format: 'esm', platform: 'node', loader: { '.json': 'json' } });
13const { versionLess, engineSize, engineMake, modelsNeedingNewer } = await import(`data:text/javascript;base64,${Buffer.from(built.outputFiles[0].text).toString('base64')}`);
14
15const listing = { ok: true, families: [{ family: 'qwen', title: 'Qwen and other models', engines: [{ version: '0.1.55', folder: 'a', active: true }, { version: '0.1.54', folder: 'b' }] }], total_bytes: 2_000_000_000, serving: false };
16
17test('the list: no Control (a source checkout) says so; otherwise it is Control\'s list; a failure is one plain sentence with the raw reason behind it', async () => {
18  assert.deepEqual((await listEngines(null)).body, { available: false, families: [], total_bytes: 0 });
19  const ok = await listEngines({ engines: async () => listing });
20  assert.equal(ok.status, 200);
21  assert.equal(ok.body.available, true);
22  assert.equal(ok.body.families[0].engines.length, 2);
23  assert.equal(ok.body.total_bytes, 2_000_000_000);
24  const bad = await listEngines({ engines: async () => { throw new Error('exit 2: traceback'); } });
25  assert.equal(bad.status, 500);
26  assert.equal(bad.body.error, "The installed engines couldn't be read.");
27  assert.match(bad.body.details, /traceback/);
28});
29
30test('switch: asks Control for the chosen engine only, and answers at once with one next step while a model is loaded', async () => {
31  const calls = [];
32  const control = { useEngine: async a => { calls.push(['use', a]); return { status: 'switched', to: '0.1.54' }; }, removeEngine: async a => { calls.push(['remove', a]); return { status: 'deleted' }; } };
33  const busy = await changeEngine(control, 'use', { family: 'qwen', folder: 'b' }, { modelsRunning: () => true });
34  assert.equal(busy.status, 409);
35  assert.match(busy.body.error, /A model is running, so nothing was changed\. Unload the model in Models/);
36  assert.deepEqual(calls, [], 'nothing was asked of Control');
37  const ok = await changeEngine(control, 'use', { family: 'qwen', folder: 'b' }, { modelsRunning: () => false });
38  assert.equal(ok.status, 200);
39  assert.deepEqual(calls, [['use', { family: 'qwen', folder: 'b' }]]);
40  const deleted = await changeEngine(control, 'remove', { family: 'qwen', folder: 'c' }, { modelsRunning: () => true });
41  assert.equal(deleted.status, 200, 'deleting an engine nothing uses is not stopped by a loaded model; Control refuses the one in use');
42  assert.deepEqual(calls.at(-1), ['remove', { family: 'qwen', folder: 'c' }]);
43});
44
45test('only a family and a plain folder name from the list are accepted: a path, a hidden folder or another family is refused before Control is asked', async () => {
46  const control = { useEngine: async () => assert.fail('must not be called'), removeEngine: async () => assert.fail('must not be called') };
47  for (const body of [{ family: 'x', folder: 'a' }, { family: 'deepseek', folder: 'a' }, { family: 'qwen', folder: '../a' }, { family: 'qwen', folder: 'a/b' }, { family: 'qwen', folder: 'a\\b' }, { family: 'qwen', folder: '.rejected' }, { family: 'qwen', folder: '--force' }, { family: 'qwen', folder: '-x' }, { family: 'qwen', folder: 'a b' }, { family: 'qwen' }, {}]) {
48    for (const action of ['use', 'remove']) assert.equal((await changeEngine(control, action, body)).status, 400, JSON.stringify(body));
49  }
50  assert.equal((await changeEngine({}, 'use', { family: 'qwen', folder: 'a' })).status, 501);
51});
52
53test('the engine in use cannot be deleted: Control\'s refusal reaches the person in plain words, the raw text behind Details', async () => {
54  const refusing = { removeEngine: async () => { throw Object.assign(new Error('This is the engine in use. Switch to another one first.'), { exitCode: 2 }); }, useEngine: async () => ({}) };
55  const out = await changeEngine(refusing, 'remove', { family: 'qwen', folder: 'a' });
56  assert.equal(out.status, 400);
57  assert.equal(out.body.error, 'This is the engine in use. Switch to another one first, then delete it.');
58  assert.match(out.body.details, /Switch to another one first/);
59  const serving = await changeEngine({ useEngine: async () => { throw Object.assign(new Error('A model is running, so the engine was not switched.'), { exitCode: 7 }); } }, 'use', { family: 'qwen', folder: 'a' });
60  assert.equal(serving.status, 409);
61  assert.match(serving.body.error, /^A model is running, so nothing was changed/);
62});
63
64test('plain engine answers: nothing says exit codes, folders or hashes outside the details', () => {
65  const cases = [
66    [{ message: 'x', exitCode: 7 }, 'switch', /A model is running/],
67    [{ message: 'a running program is using it', exitCode: 2 }, 'delete', /still using this engine/],
68    [{ message: 'that engine is not installed (it may have been deleted already)', exitCode: 2 }, 'delete', /already gone/],
69    [{ message: 'engine 0.1.1 can no longer be used: sig', exitCode: 5 }, 'switch', /can't run on this computer/],
70    [{ message: 'something odd' }, 'switch', /wasn't switched\. Nothing was changed/],
71    [{ message: 'something odd' }, 'delete', /wasn't deleted/],
72  ];
73  for (const [error, action, expected] of cases) {
74    const plain = plainEngineError(error, action);
75    assert.match(plain.message, expected);
76    assert.doesNotMatch(plain.message, /exit|SHA|\.json|[A-Z]:\\|\/home\//i);
77  }
78});
79
80test('createControl asks the same commands the Terminal runs, and none of them acquires', async () => {
81  const seen = [];
82  const control = createControl({ control: { dir: '/c', python: '/p', cli: '/c/strata.py' }, env: {}, run: async (args, options) => { seen.push([args.join(' '), !!options.acquire]); return { code: 0, json: { ok: true, families: [] } }; } });
83  await control.engines();
84  await control.useEngine({ family: 'qwen', folder: '0.1.54-linux-x86_64-cuda-cuda13' });
85  await control.removeEngine({ family: 'qwen', folder: '0.1.50-linux-x86_64-cuda-cuda13' });
86  assert.deepEqual(seen, [
87    ['components engines --output json', false],
88    ['components use 0.1.54-linux-x86_64-cuda-cuda13 --family qwen --output json', false],
89    ['components remove 0.1.50-linux-x86_64-cuda-cuda13 --family qwen --output json', false],
90  ]);
91});
92
93test('About: an engine already on this computer is offered as an update that switches to it, with nothing to download', async () => {
94  const control = { version: '0.1.55', updateCheck: async () => ({ feed: {}, control: { status: 'current', installed: '0.1.55' }, app: { status: 'current' },
95    engine: { status: 'available', installed: { version: '0.1.54' }, offered: { version: '0.1.55' }, download_bytes: 0, installed_locally: '0.1.55-linux-x86_64-cuda-cuda13' } }) };
96  const result = await checkComponentUpdates({ control, appVersion: '0.2.9' });
97  assert.equal(result.engine.status, 'available');
98  assert.equal(result.engine.canInstall, true);
99  assert.match(result.engine.message, /already on this computer.*nothing is downloaded/);
100  assert.equal(result.engine.installedVersion, '0.1.54');
101});
102
103test('engine helpers: sizes, makes and version order', () => {
104  assert.equal(engineSize(1_020_000_000, 'en'), '1.0 GB');
105  assert.equal(engineSize(640_000_000, 'en'), '640 MB');
106  assert.equal(engineSize(10, 'en'), '1 MB');
107  assert.equal(engineMake('cuda'), 'NVIDIA');
108  assert.equal(engineMake('hip'), 'AMD');
109  assert.equal(engineMake('sycl'), 'Intel');
110  assert.equal(versionLess('0.1.9', '0.1.10'), true);
111  assert.equal(versionLess('0.1.49', '0.1.49.1'), true);
112  assert.equal(versionLess('0.1.55', '0.1.55'), false);
113  assert.equal(versionLess('0.1.56', '0.1.55.1'), false);
114});
115
116test('a switch to an older engine warns about the installed models that need a newer one, and does not refuse', () => {
117  const native = { installed: { models: [{ model: 'gyro', model_name: 'Gyro' }, { model: 'plain', model_name: 'Plain' }] },
118    catalog: { families: [{ family: 'qwen', title: 'Qwen', sizes: [{ id: 'gyro', model: 'gyro', engine_min: '0.1.45.1' }, { id: 'plain', model: 'plain', engine_min: null }] }] } };
119  assert.deepEqual(modelsNeedingNewer(native, { family: 'qwen', version: '0.1.45' }), ['Gyro']);
120  assert.deepEqual(modelsNeedingNewer(native, { family: 'qwen', version: '0.1.46' }), []);
121  assert.deepEqual(modelsNeedingNewer(null, { family: 'qwen', version: '0.1.1' }), []);
122  const page = src('src/components/SettingsEngine.tsx');
123  assert.match(page, /disabled=\{pending \|\| running\}/, 'only a loaded model stops the button; the warning does not');
124});
125
126test('the Engine tab lists and switches, and has no update; About has the update and no rollback', () => {
127  const engine = src('src/components/SettingsEngine.tsx');
128  assert.match(src('src/components/NativeOperations.tsx'), /<SettingsEngine \/>/, 'the Engine tab shows the list');
129  assert.match(engine, /Installed engines/);
130  assert.doesNotMatch(engine, /Check for updates|update-engine|fetchUpdateCheck/);
131  const ops = src('src/components/NativeOperations.tsx');
132  assert.doesNotMatch(ops, /Update component|Check for updates|Update engine\?|fetchUpdateCheck|update-engine/);
133  assert.match(ops, /View logs/, 'the logs stay on the Engine tab');
134  const about = src('src/components/SettingsUpdates.tsx');
135  assert.doesNotMatch(about, /Roll back|rollback|Restore previous engine|Previous engine available/i);
136  assert.match(about, /Check for updates/);
137  assert.match(about, /'update-engine'/);
138  assert.match(about, /'update-strata'/);
139});
140
141test('the engine in use has no Delete button; every other row has Use this (when it can run here) and Delete', () => {
142  const page = src('src/components/SettingsEngine.tsx');
143  assert.match(page, /engine\.active \? <span className="engine-row__state">\{tr\("In use"\)\}<\/span> : <>/);
144  assert.match(page, /engine\.can_use \? <Btn[\s\S]*?\{tr\("Use this"\)\}/);
145  assert.match(page, /disabled=\{busy \|\| !engine\.can_delete\}/);
146  assert.match(page, /run\('remove', del\)/);
147});
148
149test('the engine catalogs: same keys and placeholders in nine languages, none still English, none in another catalog, and every sentence of the host is in them', () => {
150  const languages = ['en', 'zh', 'ja', 'de', 'es', 'ru', 'pt', 'ko', 'fr'];
151  const en = json('engine-en');
152  const slots = text => [...text.matchAll(/\{\d+\}/g)].map(m => m[0]).sort();
153  const others = new Set();
154  for (const name of ['en', 'tune-en', 'tools-en', 'fix-en', 'gh-en', 'ui-en']) for (const v of Object.values(json(name))) others.add(v);
155  for (const l of languages) {
156    const catalog = json(`engine-${l}`);
157    assert.deepEqual(Object.keys(catalog), Object.keys(en), l);
158    for (const [id, source] of Object.entries(en)) {
159      assert.ok(catalog[id].trim(), `${l}:${id}`);
160      assert.deepEqual(slots(catalog[id]), slots(source), `${l}:${id}`);
161      if (l !== 'en') assert.notEqual(catalog[id], source, `${l}:${id} is still English`);
162    }
163  }
164  for (const [id, text] of Object.entries(en)) assert.ok(!others.has(text), `already in another catalog: ${id} ${text}`);
165  const known = new Set(Object.values(en));
166  for (const sentence of [
167    'A model is running, so nothing was changed. Unload the model in Models, then try again.',
168    'This is the engine in use. Switch to another one first, then delete it.',
169    'A program is still using this engine, so it was not deleted. Unload your models and try again.',
170    'That engine is already gone. The list has been refreshed.',
171    'An update is running. Wait for it to finish, then try again.',
172    "This engine can't run on this computer, so nothing was changed.",
173    "The engine wasn't deleted. Nothing else was changed.",
174    "The engine wasn't switched. Nothing was changed.",
175    'Choose an engine from the list.',
176    'Engines can be switched and deleted in the installed Strata app.',
177    'A newer engine is already on this computer. Update switches to it; nothing is downloaded.',
178  ]) {
179    assert.ok(known.has(sentence), `not in the engine catalog: ${sentence}`);
180    assert.ok([src('server/engine-library.mjs'), src('server/update-messages.mjs'), src('server/updates.mjs')].some(file => file.includes(sentence)), `no longer said by the host: ${sentence}`);
181  }
182});