okami.mom/strata

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

Niko1221Merge f-027-gh-app: GitHub app and Terminal items for 0.2.73cabb9e3f

main
5.0 KiB101 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFileSync, writeFileSync } from 'node:fs';
4import fs from 'node:fs';
5import { syncBuiltinESMExports } from 'node:module';
6import { join } from 'node:path';
7import { nativeFixture, finishJob } from './helpers/native-fixture.mjs';
8
9const configOf = f => join(f.repo, 'strata-iq2_xs.json');
10
11for (const validFirst of [true, false]) test(`a load ${validFirst ? 'uses a valid' : 'refuses an invalid'} captured config even if the file changes after reading`, async t => {
12  const f = nativeFixture();
13  try {
14    await f.control.refresh(true);
15    const path = configOf(f), good = JSON.parse(readFileSync(path, 'utf8'));
16    good.parallel = 2;
17    const valid = '\uFEFF' + JSON.stringify(good), invalid = JSON.stringify({ ...good, parallel: 99 });
18    writeFileSync(path, validFirst ? valid : invalid);
19    // Let status discovery finish normally. Replace the real file immediately after
20    // the first read following the model-path check, before a second read can occur.
21    const realpath = fs.realpathSync, read = fs.readFileSync;
22    let capture = false;
23    t.mock.method(fs, 'realpathSync', (...args) => {
24      const result = realpath(...args);
25      if (args[0] === path) capture = true;
26      return result;
27    });
28    t.mock.method(fs, 'readFileSync', (...args) => {
29      const result = read(...args);
30      if (capture && args[0] === path) {
31        capture = false;
32        writeFileSync(path, validFirst ? invalid : valid);
33      }
34      return result;
35    });
36    syncBuiltinESMExports();
37    const done = await finishJob(f.control, f.control.operation('load', { model: 'iq2_xs' }).id);
38    assert.equal(done.state, validFirst ? 'done' : 'error', done.error);
39    if (validFirst) {
40      const instance = f.control.resolveInstance(done.result.instanceId);
41      assert.equal(JSON.parse(read(instance.configPath, 'utf8')).parallel, 2);
42    } else {
43      assert.match(done.error, /"parallel" must be a whole number/);
44      assert.equal(f.control.state().instances.length, 0);
45      assert.equal(f.calls.some(c => c.args.some(a => /serve[/\\]server\.py$/.test(String(a)))), false);
46    }
47  } finally {
48    t.mock.restoreAll();
49    syncBuiltinESMExports();
50    await f.cleanup();
51  }
52});
53
54test('the settings file is found, checked, and opened in the text editor', async () => {
55  const f = nativeFixture();
56  try {
57    await f.control.refresh();
58    const info = f.control.settingsFile('iq2_xs');
59    assert.equal(info.path, configOf(f));
60    assert.equal(info.name, 'strata-iq2_xs.json');
61    assert.equal(info.ok, true);
62    assert.match(info.note, /apply the next time the model starts/);
63    assert.ok(info.issues.every(i => i.level === 'warning'), 'the fixture’s own extra key is a warning at most');
64    const seen = [];
65    const opened = await f.control.openSettingsFile('iq2_xs', { recorder: seen, platform: 'win32' });
66    assert.deepEqual(seen, [['notepad.exe', info.path]]);
67    assert.equal(opened.name, 'strata-iq2_xs.json');
68    assert.throws(() => f.control.settingsFile('../etc/passwd'), /installed model/);
69  } finally { await f.cleanup(); }
70});
71
72test('a mistake in the file holds the start back with its line; a typo’d key only warns', async () => {
73  const f = nativeFixture();
74  try {
75    await f.control.refresh(true);
76    const path = configOf(f), good = readFileSync(path, 'utf8');
77    // 1. a syntax slip (missing comma): not valid JSON, said with the line, and nothing is started
78    writeFileSync(path, JSON.stringify(JSON.parse(good), null, 2).replace('"gpu": 0,', '"gpu": 0\n  "x": 1,'));
79    assert.equal(f.control.settingsFile('iq2_xs').ok, false);
80    let done = await finishJob(f.control, f.control.operation('load', { model: 'iq2_xs' }).id);
81    assert.equal(done.state, 'error');
82    assert.match(done.error, /^The settings file for this model has a mistake, so the model was not started\. Line \d+: The settings file is not valid JSON/);
83    assert.equal(f.control.state().instances.length, 0, 'no engine was started');
84    assert.equal(f.calls.filter(c => /strata\.py|serve/.test(JSON.stringify(c.args))).length, 0);
85    // 2. a bad value
86    const config = JSON.parse(good); config.parallel = 99; config.lazy_load = 'yes';
87    writeFileSync(path, JSON.stringify(config, null, 2));
88    done = await finishJob(f.control, f.control.operation('load', { model: 'iq2_xs' }).id);
89    assert.equal(done.state, 'error');
90    assert.match(done.error, /"parallel" must be a whole number from 1 to 8, not 99/);
91    assert.match(done.error, /"lazy_load" must be true or false, not "yes"/);
92    // 3. an unknown key only warns and the model loads
93    const typo = JSON.parse(good); typo.paralel = 2;
94    writeFileSync(path, JSON.stringify(typo, null, 2));
95    const info = f.control.settingsFile('iq2_xs');
96    assert.equal(info.ok, true);
97    assert.ok(info.issues.some(i => i.code === 'unknown-key-near' && i.params.near === 'parallel'));
98    done = await finishJob(f.control, f.control.operation('load', { model: 'iq2_xs' }).id);
99    assert.equal(done.state, 'done', done.error);
100  } finally { await f.cleanup(); }
101});