okami.mom/strata

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

Niko1221App: set up a model on an Intel Arc card (Linux, SYCL engine)1130d824a

main
5.7 KiB88 linesraw
1import './helpers/isolate-profile.mjs';   // a test run never writes the owner's real Strata profile
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { EventEmitter } from 'node:events';
5import { mkdtempSync, mkdirSync, writeFileSync, rmSync, realpathSync } from 'node:fs';
6import { join } from 'node:path';
7import { tmpdir as osTmpdir } from 'node:os';
8import { bundledPython, createControl, findControl } from '../server/control-runtime.mjs';
9import { createNativeControl } from '../server/native-control.mjs';
10
11// ARCFIX (0.2.13): a Control-only feed update (`strata update`, or Settings > Updates > Strata) writes <components>/control/active.json.
12// The packaged app must then run the model setup (tools/strata_mcp.py and the setup.py it starts) from THAT Control, not from the bundled one.
13const isWin = process.platform === 'win32';
14const tmp = () => realpathSync(osTmpdir());
15
16function controlDir(parent, name, version) {
17  const dir = join(parent, name);
18  mkdirSync(join(dir, 'python', ...(isWin ? [] : ['bin'])), { recursive: true });
19  mkdirSync(join(dir, 'strata_control'), { recursive: true });
20  mkdirSync(join(dir, 'tools'), { recursive: true });
21  mkdirSync(join(dir, 'serve'), { recursive: true });
22  writeFileSync(bundledPython(dir), 'x');
23  writeFileSync(join(dir, 'strata.py'), '# cli');
24  writeFileSync(join(dir, 'tools', 'strata_mcp.py'), '# mcp');
25  writeFileSync(join(dir, 'setup.py'), '# setup');
26  writeFileSync(join(dir, 'serve', 'server.py'), 'port\nparallel\n/v1/responses');
27  writeFileSync(join(dir, 'BUNDLE.json'), JSON.stringify({ version }));
28  return dir;
29}
30
31function fixture() {
32  const root = mkdtempSync(join(tmp(), 'strata-arcfix-'));
33  const install = join(root, 'install');                          // the AppImage's / installer's folder: <install>/host + <install>/control
34  mkdirSync(join(install, 'host'), { recursive: true });
35  const bundled = controlDir(install, 'control', '0.1.59');
36  const components = join(root, 'components');
37  mkdirSync(join(components, 'control'), { recursive: true });
38  const updated = controlDir(join(components, 'control'), '0.1.59.1', '0.1.59.1');
39  writeFileSync(join(components, 'control', 'active.json'), JSON.stringify({ version: '0.1.59.1', folder: '0.1.59.1', sha256: 'x' }));
40  return { root, install, bundled, components, updated, hostDir: join(install, 'host') };
41}
42
43test('a four-part Control (0.1.59.1) from the feed beats the bundled 0.1.59, whether the shell names the bundled one or it sits beside the host', () => {
44  const f = fixture();
45  try {
46    // the Tauri shell sets STRATA_CONTROL_DIR to the bundled folder (apps/desktop/src-tauri/src/main.rs apply_runtime_policy)
47    const viaShell = findControl({ env: { STRATA_CONTROL_DIR: f.bundled }, hostDir: f.hostDir, components: f.components });
48    assert.equal(viaShell.dir, f.updated);
49    assert.equal(viaShell.bundle.version, '0.1.59.1');
50    assert.equal(findControl({ env: {}, hostDir: f.hostDir, components: f.components }).dir, f.updated);
51    // the same Control, the same version: the bundled one stays (a tie goes to the first), so a feed that offers 0.1.59 again changes nothing
52    writeFileSync(join(f.updated, 'BUNDLE.json'), JSON.stringify({ version: '0.1.59' }));
53    writeFileSync(join(f.components, 'control', 'active.json'), JSON.stringify({ version: '0.1.59', folder: '0.1.59.1' }));
54    assert.equal(findControl({ env: { STRATA_CONTROL_DIR: f.bundled }, hostDir: f.hostDir, components: f.components }).dir, f.bundled);
55  } finally { rmSync(f.root, { recursive: true, force: true }); }
56});
57
58test('the model setup of the packaged app runs from the updated Control: strata_mcp.py (and with it --src, the folder setup.py is started from) is the updated one', async () => {
59  const f = fixture();
60  try {
61    const found = findControl({ env: { STRATA_CONTROL_DIR: f.bundled }, hostDir: f.hostDir, components: f.components });
62    const spawned = [];
63    const spawnProcess = (command, args, options) => {
64      spawned.push({ command, args, options });
65      const child = new EventEmitter(); child.stdout = new EventEmitter(); child.stderr = new EventEmitter(); child.exitCode = null;
66      child.kill = () => true;
67      child.stdin = { write(line) {
68        const message = JSON.parse(line); if (!message.id) return;
69        const result = message.method === 'tools/list' ? { tools: [{ name: 'strata_status' }] } : { protocolVersion: '2024-11-05' };
70        queueMicrotask(() => child.stdout.emit('data', Buffer.from(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\n')));
71      } };
72      return child;
73    };
74    const data = join(f.root, 'data');
75    const control = { ...createControl({ control: found, run: async () => ({}) }),
76      async installedModels() { return { install_dir: f.root, models: [] }; },
77      async engine() { return { usable: true, backend: 'sycl', engine: { version: '0.1.59', platform: 'linux-x86_64', backend: 'sycl', exe: '/e/strata', root: '/e' } }; } };
78    const native = createNativeControl({ control, directory: data, spawnProcess, fetcher: async () => Response.json({}) });
79    try { await native.refresh(true); } finally { await native.close(); }
80    const installer = spawned.find(s => String(s.args[0]).endsWith('strata_mcp.py'));
81    assert.ok(installer, 'the host started the installer process');
82    assert.equal(installer.command, bundledPython(f.updated));
83    assert.equal(installer.args[0], join(f.updated, 'tools', 'strata_mcp.py'));
84    assert.equal(installer.args[installer.args.indexOf('--src') + 1], f.updated);
85    assert.equal(installer.options.cwd, f.updated);
86    assert.equal(installer.options.env.STRATA_CONTROL_DIR, f.updated);
87  } finally { rmSync(f.root, { recursive: true, force: true }); }
88});