okami.mom/strata

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

Niko1221Trial2 merge f-0291-gh2-app (0dfa2d9b1)2e8fb221c

main
6.9 KiB117 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { createEngineStep } from '../server/engine-step.mjs';
4
5const GB = 1e9;
6function control({ usable = false, check, update, checkError = null } = {}) {
7  const calls = [];
8  return {
9    calls,
10    async engine() { calls.push('engine'); return { usable }; },
11    async updateCheck() { calls.push('updateCheck'); if (checkError) throw new Error(checkError); return check; },
12    async update(options) { calls.push(['update', options.engine, options.control]); return update(options); },
13  };
14}
15const offer = (status = 'install', extra = {}) => ({ engine: { status, installed: null, offered: { version: '0.1.46' }, download_bytes: 0.5 * GB, ...extra } });
16const updated = { ok: true, results: [{ component: 'engine', status: 'updated', installed_after: { version: '0.1.46' } }] };
17
18test('no engine installed: the step downloads it with the model and reports the stages in order', async () => {
19  const seen = [];
20  const c = control({
21    check: offer(),
22    update: async ({ onEvent }) => {
23      onEvent({ event: 'progress', percent: 50, done: 0.25 * GB, total: 0.5 * GB });
24      onEvent({ event: 'phase', phase: 'verify', message: 'Checking' });
25      onEvent({ event: 'phase', phase: 'test', message: 'Test-starting' });
26      onEvent({ event: 'phase', phase: 'switch', message: 'Switching' });
27      return updated;
28    },
29  });
30  const step = createEngineStep({ control: c, onChange: p => seen.push(p.state) });
31  const part = await step.run();
32  assert.deepEqual(c.calls, ['engine', 'updateCheck', ['update', true, false]], 'the engine only: Control and the app are not part of a model install');
33  assert.deepEqual([...new Set(seen)], ['checking', 'downloading', 'verifying', 'testing', 'switching', 'done']);
34  assert.equal(part.state, 'done');
35  assert.equal(part.version, '0.1.46');
36  assert.equal(step.summaryNote(), null);
37});
38
39test('one combined total: model and engine bytes add up, the model-only numbers stay in the parts list', async () => {
40  const c = control({ check: offer(), update: async ({ onEvent }) => { onEvent({ event: 'progress', percent: 40, done: 0.2 * GB, total: 0.5 * GB }); return new Promise(() => {}); } });
41  const step = createEngineStep({ control: c });
42  void step.run();
43  await new Promise(r => setTimeout(r, 5));
44  const install = { running: true, step: 'Downloading', download: { file: 'model-00001.gguf', percent: 10, done_gb: 6, remaining_gb: 60 } };
45  const merged = step.merge(install);
46  assert.equal(merged.download.done_gb, 6.2);
47  assert.equal(merged.download.remaining_gb, 60.3);
48  assert.equal(merged.download.percent, Math.round(100 * 6.2 / 66.5));
49  assert.equal(merged.download.file, 'model-00001.gguf', 'the model is still the part that is moving');
50  assert.deepEqual(merged.parts.map(p => p.kind), ['model', 'engine']);
51  assert.deepEqual([merged.parts[0].done_gb, merged.parts[0].total_gb], [6, 66]);
52  assert.equal(merged.engine.state, 'downloading');
53  assert.equal(install.download.done_gb, 6, 'merge does not change its input');
54});
55
56test('a failed engine download keeps the model and says how to retry', async () => {
57  const c = control({ check: offer(), update: async () => { throw new Error('the connection dropped'); } });
58  const step = createEngineStep({ control: c });
59  const part = await step.run();
60  assert.equal(part.state, 'failed');
61  assert.equal(part.retry, 'Update engine');
62  assert.match(part.message, /^Your model is ready\. The engine for this PC couldn't be downloaded\. .*Update engine in Settings/);
63  assert.doesNotMatch(part.message, /dropped|HTTP|exit|sha/i);
64  assert.match(part.details, /connection dropped/, 'the reason is kept for the Details disclosure');
65  assert.equal(step.summaryNote(), part.message);
66  const merged = step.merge({ running: false, download: { file: 'm', percent: 100, done_gb: 10, remaining_gb: 0 } });
67  assert.equal(merged.download.remaining_gb, 0, 'a failed engine does not leave the progress bar short');
68});
69
70test('a feed that cannot be reached is a failure only when there is no engine yet', async () => {
71  const none = await createEngineStep({ control: control({ usable: false, checkError: 'could not be read' }) }).run();
72  assert.equal(none.state, 'failed');
73  const have = await createEngineStep({ control: control({ usable: true, checkError: 'could not be read' }) }).run();
74  assert.equal(have.state, 'skipped');
75  assert.match(have.message, /engine for this PC is installed/);
76  assert.equal(have.details, undefined, 'nothing to say when an engine is installed');
77});
78
79test('an engine that is already current downloads nothing; one with no package for this PC is a failure', async () => {
80  const c = control({ usable: true, check: { engine: { status: 'current', installed: { version: '0.1.46' } } } });
81  const part = await createEngineStep({ control: c }).run();
82  assert.equal(part.state, 'current');
83  assert.deepEqual(c.calls, ['engine', 'updateCheck']);
84  const none = await createEngineStep({ control: control({ usable: false, check: { engine: { status: 'none-for-this-pc', reasons: ['0.1.46 is for linux-x86_64, this PC is windows-x86_64'] } } }) }).run();
85  assert.equal(none.state, 'failed');
86  assert.doesNotMatch(none.message, /windows-x86_64/);
87  assert.match(none.details, /windows-x86_64/);
88});
89
90test('without a component updater the step is skipped and never throws', async () => {
91  const part = await createEngineStep({ control: {} }).run();
92  assert.equal(part.state, 'skipped');
93  assert.equal(createEngineStep({ control: {} }).merge(null), null);
94});
95
96test('#4823: an engine that already works is not replaced without saying from which version to which, and that the old one is kept', async () => {
97  const messages = [];
98  const c = control({
99    usable: true,
100    check: offer('available', { installed: { version: '0.1.54' }, offered: { version: '0.1.55' } }),
101    update: async ({ onEvent }) => { onEvent({ event: 'progress', percent: 30, done: 0.15 * GB, total: 0.5 * GB }); return { ok: true, results: [{ component: 'engine', status: 'updated', installed_after: { version: '0.1.55' } }] }; },
102  });
103  const step = createEngineStep({ control: c, onChange: p => messages.push(p.message) });
104  const part = await step.run();
105  assert.match(messages.find(m => /Updating/.test(m)), /^Updating the engine from 0\.1\.54 to 0\.1\.55\. The engine you have now is kept, and Settings can go back to it\.$/);
106  assert.ok(messages.includes('Updating the engine from 0.1.54 to 0.1.55: 30%'));
107  assert.equal(part.message, 'The engine was updated from 0.1.54 to 0.1.55. Settings can go back to the one you had.');
108});
109
110test('a first engine keeps the plain download words', async () => {
111  const messages = [];
112  const c = control({ check: offer(), update: async () => updated });
113  const step = createEngineStep({ control: c, onChange: p => messages.push(p.message) });
114  await step.run();
115  assert.ok(messages.includes('Downloading the engine for this PC…'));
116  assert.ok(!messages.some(m => /Updating the engine from/.test(m)));
117});