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
21.0 KiB320 linesraw
1/** The engine inside the installer: installed when missing or older, never over a newer or equal one, never without a matching manifest. */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, readdirSync, rmSync } from 'node:fs';
5import { createHash } from 'node:crypto';
6import { tmpdir } from 'node:os';
7import { join } from 'node:path';
8import { installBundledEngine, removeVerifiedBundle, cleanupAfterInstall, compareVersions, wrongMakeNotice } from '../server/bundled-engine.mjs';
9
10// the make of card is read from this computer unless a test names it; the existing tests are written for an NVIDIA PC
11process.env.STRATA_ENGINE_BACKEND = 'cuda';
12const sha = buffer => createHash('sha256').update(buffer).digest('hex');
13function makePackage(root, version, { body = 'engine-' + version, backend = 'cuda', platform = 'windows-x86_64', variant = 'cuda13', minDriver = null } = {}) {
14  const name = `${version}-${platform}-${backend}-${variant}`;
15  const dir = join(root, name);
16  mkdirSync(dir, { recursive: true });
17  const files = { 'strata.exe': Buffer.from(body), 'strata_kq.dll': Buffer.from('kq-' + body), 'BUILD.json': Buffer.from('{}') };
18  for (const [file, data] of Object.entries(files)) writeFileSync(join(dir, file), data);
19  writeFileSync(join(dir, 'engine-component.json'), JSON.stringify({
20    schema_version: 2, id: 'strata-engine', name: 'strata-engine', version, platform, backend, archs: ['sm_86'], ...(minDriver ? { min_driver: { nvidia: minDriver } } : {}), build_id: 'x', entrypoint: 'strata.exe',
21    files: Object.entries(files).map(([path, data]) => ({ path, sha256: sha(data), bytes: data.length })), signature: null }));
22  return name;
23}
24const setup = () => {
25  const base = mkdtempSync(join(tmpdir(), 'st-bundled-engine-'));
26  const bundle = join(base, 'engine-bundle'), components = join(base, 'components');
27  mkdirSync(bundle); mkdirSync(components);
28  return { base, bundle, components, engine: join(components, 'engine'), cleanup: () => rmSync(base, { recursive: true, force: true }) };
29};
30
31test('versions compare by number, not text', () => {
32  assert.equal(compareVersions('0.1.45', '0.1.9'), 1);
33  assert.equal(compareVersions('0.1.45', '0.1.45.1'), -1);
34  assert.equal(compareVersions('0.1.45', '0.1.45'), 0);
35});
36
37test('no engine installed: the bundled one is installed, hash-checked, with nothing left over', async () => {
38  const t = setup();
39  try {
40    const name = makePackage(t.bundle, '0.1.45');
41    const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components });
42    assert.equal(result.action, 'installed');
43    assert.equal(readFileSync(join(t.engine, name, 'strata.exe'), 'utf8'), 'engine-0.1.45');
44    assert.ok(existsSync(join(t.engine, name, 'engine-component.json')));
45    assert.deepEqual(readdirSync(t.engine), [name], 'no temporary folder remains');
46    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'kept', 'a second start does nothing');
47  } finally { t.cleanup(); }
48});
49
50test('an older engine is installed: the bundled one is added beside it', async () => {
51  const t = setup();
52  try {
53    const old = makePackage(t.engine, '0.1.44');
54    const name = makePackage(t.bundle, '0.1.45');
55    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'installed');
56    assert.deepEqual(readdirSync(t.engine).sort(), [old, name].sort());
57  } finally { t.cleanup(); }
58});
59
60test('a newer or equal engine is never overwritten or touched', async () => {
61  const t = setup();
62  try {
63    const newer = makePackage(t.engine, '0.1.46', { body: 'mine' });
64    makePackage(t.bundle, '0.1.45');
65    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'kept');
66    assert.deepEqual(readdirSync(t.engine), [newer]);
67    assert.equal(readFileSync(join(t.engine, newer, 'strata.exe'), 'utf8'), 'mine');
68    // same version, other build: the installed one stays
69    const same = makePackage(t.engine, '0.1.45', { body: 'installed-build' });
70    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'kept');
71    assert.equal(readFileSync(join(t.engine, same, 'strata.exe'), 'utf8'), 'installed-build');
72  } finally { t.cleanup(); }
73});
74
75test('a damaged copy of the same package is put right; an engine for another backend does not count', async () => {
76  const t = setup();
77  try {
78    const name = makePackage(t.bundle, '0.1.45');
79    makePackage(t.engine, '0.1.45');
80    writeFileSync(join(t.engine, name, 'strata.exe'), 'cut');          // wrong size
81    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'repaired');
82    assert.equal(readFileSync(join(t.engine, name, 'strata.exe'), 'utf8'), 'engine-0.1.45');
83    const t2 = setup();
84    try {
85      makePackage(t2.engine, '0.1.50', { backend: 'cpu' });
86      makePackage(t2.bundle, '0.1.45');
87      assert.equal((await installBundledEngine({ bundleDir: t2.bundle, components: t2.components })).action, 'installed');
88    } finally { t2.cleanup(); }
89  } finally { t.cleanup(); }
90});
91
92test('a bundle that does not match its manifest is refused and installs nothing', async () => {
93  const t = setup();
94  try {
95    const name = makePackage(t.bundle, '0.1.45');
96    writeFileSync(join(t.bundle, name, 'strata_kq.dll'), 'kq-engine-0.1.4X');   // same size, other bytes
97    const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components });
98    assert.equal(result.action, 'refused');
99    assert.equal(existsSync(join(t.engine, name)), false);
100    assert.equal((await installBundledEngine({ bundleDir: join(t.base, 'nope'), components: t.components })).action, 'none');
101  } finally { t.cleanup(); }
102});
103
104test('CUDA 13 and CUDA 12 builds of one version are different engines: both are installed when the driver runs both', async () => {
105  const t = setup();
106  try {
107    const n13 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65', body: 'thirteen' });
108    const n12 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51', body: 'twelve' });
109    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: '580.178.04' })).action, 'installed');
110    assert.deepEqual(readdirSync(t.engine).sort(), [n12, n13].sort());
111    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: '580.178.04' })).action, 'kept', 'a second start does nothing');
112  } finally { t.cleanup(); }
113});
114
115test('an older driver gets only the build it can run; a driver below both gets the lowest build so the app can name the driver', async () => {
116  const t = setup(), u = setup(), v = setup();
117  try {
118    const n13 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65' });
119    const n12 = makePackage(t.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51' });
120    await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: '575.57.08' });
121    assert.deepEqual(readdirSync(t.engine), [n12], 'driver 575 runs CUDA 12 only');
122    for (const x of [u]) { makePackage(x.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65' }); makePackage(x.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51' }); }
123    await installBundledEngine({ bundleDir: u.bundle, components: u.components, driver: '550.120' });
124    assert.deepEqual(readdirSync(u.engine), [n12], 'driver 550 runs neither: the CUDA 12 build is installed so Control can say 575 is needed');
125    // no driver at all (a PC without NVIDIA, or nvidia-smi cannot be read): nothing is held back
126    makePackage(v.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda13', minDriver: '580.65' }); makePackage(v.bundle, '0.1.45', { platform: 'linux-x86_64', variant: 'cuda12', minDriver: '575.51' });
127    await installBundledEngine({ bundleDir: v.bundle, components: v.components, driver: null });
128    assert.deepEqual(readdirSync(v.engine).sort(), [n12, n13].sort());
129  } finally { t.cleanup(); u.cleanup(); v.cleanup(); }
130});
131
132// The app update (latest.json) is built without engine-bundle (tauri.update.conf.json), so one update fits every installer variant.
133test('an engine-less app update (no engine-bundle folder, or one holding only .gitignore) leaves the installed engines exactly as they are', async () => {
134  const t = setup();
135  try {
136    const amd = makePackage(t.engine, '0.1.45.3', { platform: 'windows-x86_64', backend: 'hip', variant: 'rx9070' });
137    const before = readFileSync(join(t.engine, amd, 'engine-component.json'), 'utf8');
138    assert.equal((await installBundledEngine({ bundleDir: join(t.base, 'no-such-engine-bundle'), components: t.components })).action, 'none');
139    writeFileSync(join(t.bundle, '.gitignore'), '*\n!.gitignore\n');
140    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, platform: 'windows-x86_64' })).action, 'none');
141    assert.deepEqual(readdirSync(t.engine), [amd]);
142    assert.equal(readFileSync(join(t.engine, amd, 'engine-component.json'), 'utf8'), before);
143  } finally { t.cleanup(); }
144});
145
146test('why the update carries no engine: a CUDA installer on an AMD PC would copy its CUDA engine beside the AMD one (no NVIDIA driver to filter by)', async () => {
147  const t = setup();
148  try {
149    const amd = makePackage(t.engine, '0.1.45.3', { platform: 'windows-x86_64', backend: 'hip', variant: 'rx9070' });
150    const cuda = makePackage(t.bundle, '0.1.45.3', { platform: 'windows-x86_64', variant: 'cuda13', minDriver: '580.65' });
151    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, driver: null })).action, 'installed');
152    assert.deepEqual(readdirSync(t.engine).sort(), [amd, cuda].sort(), 'the AMD engine stays; a useless CUDA engine is added');
153  } finally { t.cleanup(); }
154});
155
156test('an AMD build on an NVIDIA PC is not unpacked and the sentence names the right download', async () => {
157  const t = setup();
158  try {
159    const name = makePackage(t.bundle, '0.2.0', { platform: 'linux-x86_64', backend: 'hip', variant: 'rx9070' });
160    const manifest = JSON.parse(readFileSync(join(t.bundle, name, 'engine-component.json'), 'utf8'));
161    manifest.archs = ['gfx1201'];
162    writeFileSync(join(t.bundle, name, 'engine-component.json'), JSON.stringify(manifest));
163    const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components, backend: 'cuda' });
164    assert.equal(result.action, 'wrong-make');
165    assert.equal(result.notice, 'This Strata is for AMD RX 9000 cards. Your PC has an NVIDIA card: download the NVIDIA version from https://github.com/Niko1221/Strata/releases');
166    assert.equal(existsSync(t.engine), false, 'nothing was copied');
167    // the same build on an AMD PC is unpacked; an unknown card is not a reason to refuse
168    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, backend: 'hip' })).action, 'installed');
169    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components, backend: null })).action, 'kept');
170  } finally { t.cleanup(); }
171});
172
173test('wrongMakeNotice names the make, not a list of reasons', () => {
174  assert.equal(wrongMakeNotice({ backend: 'cuda', archs: ['sm_86'] }, 'hip'), 'This Strata is for NVIDIA cards. Your PC has an AMD card: download the AMD version from https://github.com/Niko1221/Strata/releases');
175});
176
177test('Windows: the display adapters decide the make, and a leftover nvidia-smi does not outvote them (#3688)', async () => {
178  const { gpuBackendHere, windowsDisplayVendors } = await import('../server/bundled-engine.mjs');
179  assert.equal(gpuBackendHere({}, { vendors: new Set(['1002']) }), 'hip');
180  assert.equal(gpuBackendHere({}, { vendors: new Set(['10de', '1002']) }), 'cuda');
181  assert.equal(gpuBackendHere({}, { vendors: new Set(['8086']) }), 'sycl');
182  assert.equal(gpuBackendHere({ STRATA_ENGINE_BACKEND: 'cuda' }, { vendors: new Set(['1002']) }), 'cuda', 'an explicit choice wins');
183  const out = 'MatchingDeviceId    REG_SZ    PCI/VEN_1002&DEV_7551&SUBSYS_0' + String.fromCharCode(10) + 'MatchingDeviceId    REG_SZ    PCI/VEN_8086&DEV_46A6';
184  assert.deepEqual([...windowsDisplayVendors({ platform: 'win32', run: () => out })].sort(), ['1002', '8086']);
185  assert.equal(windowsDisplayVendors({ platform: 'linux', run: () => { throw new Error('must not run'); } }).size, 0);
186  assert.equal(windowsDisplayVendors({ platform: 'win32', run: () => { throw new Error('no registry'); } }).size, 0);
187});
188
189test('after an update, the engine bundle of the old install folder is removed only when the same engine is complete in the store', async () => {
190  const t = setup();
191  try {
192    const name = makePackage(t.bundle, '0.1.48');
193    writeFileSync(join(t.bundle, '.gitignore'), 'ignore-all');
194    const lines = [];
195    // 1. nothing in the store yet: kept, and one line says why
196    let r = await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components, log: l => lines.push(l) });
197    assert.equal(r.removed, false);
198    assert.equal(lines.length, 1);
199    assert.match(lines[0], /kept in the install folder.*not \(or not completely\) in the engine store/);
200    assert.ok(existsSync(join(t.bundle, name)));
201    // 2. the engine is copied (the app's own first start does this)
202    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'installed');
203    // 3. a store file with the right size and the wrong content: kept (the hash is checked, not only the size)
204    const target = join(t.engine, name, 'strata.exe');
205    const good = readFileSync(target);
206    writeFileSync(target, Buffer.alloc(good.length, 'x'));
207    lines.length = 0;
208    r = await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components, log: l => lines.push(l) });
209    assert.equal(r.removed, false);
210    assert.equal(lines.length, 1, 'exactly one line');
211    assert.ok(existsSync(join(t.bundle, name)));
212    // 4. a store file with the wrong size: kept
213    writeFileSync(target, Buffer.concat([good, Buffer.from('extra')]));
214    assert.equal((await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components })).removed, false);
215    // 5. the store copy is whole: the bundle goes, its .gitignore stays, and the store is untouched
216    writeFileSync(target, good);
217    lines.length = 0;
218    r = await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components, log: l => lines.push(l) });
219    assert.equal(r.removed, true);
220    assert.equal(existsSync(join(t.bundle, name)), false);
221    assert.ok(existsSync(join(t.bundle, '.gitignore')));
222    assert.equal(readFileSync(target, 'utf8'), 'engine-0.1.48');
223    assert.match(lines.at(-1), /was removed; the same engine is complete in the engine store/);
224    // 6. nothing left to do
225    assert.equal((await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components })).removed, false);
226    // 7. a different version in the store does not count
227    const other = makePackage(t.bundle, '0.1.49');
228    assert.equal((await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components })).removed, false);
229    assert.ok(existsSync(join(t.bundle, other)));
230  } finally { t.cleanup(); }
231});
232
233test('the bundle cleanup lines reach the output of the host (the desktop log copies it), for "removed" and for "kept"', async () => {
234  const t = setup();
235  const seen = [], real = console.log;
236  try {
237    const name = makePackage(t.bundle, '0.1.48');
238    console.log = line => { seen.push(String(line)); };
239    // kept: the engine is not in the store yet
240    await cleanupAfterInstall(Promise.resolve({ action: 'kept' }), { bundleDir: t.bundle, components: t.components });
241    assert.equal(seen.length, 1);
242    assert.match(seen[0], /^engine bundle: kept in the install folder/);
243    // removed: the engine is in the store and whole
244    const installed = await installBundledEngine({ bundleDir: t.bundle, components: t.components });
245    assert.equal(installed.action, 'installed');
246    seen.length = 0;
247    await cleanupAfterInstall(Promise.resolve({ action: 'kept' }), { bundleDir: t.bundle, components: t.components });
248    assert.equal(seen.length, 1);
249    assert.match(seen[0], /^engine bundle: the copy that came with the installer \(.*\) was removed/);
250    assert.equal(existsSync(join(t.bundle, name)), false);
251    // nothing to say when the engine step did nothing (no bundle) or was refused
252    seen.length = 0;
253    await cleanupAfterInstall(Promise.resolve({ action: 'none' }), { bundleDir: t.bundle, components: t.components });
254    await cleanupAfterInstall(Promise.resolve({ action: 'wrong-make' }), { bundleDir: t.bundle, components: t.components });
255    await cleanupAfterInstall(Promise.reject(new Error('boom')), { bundleDir: t.bundle, components: t.components });
256    assert.deepEqual(seen, []);
257  } finally { console.log = real; t.cleanup(); }
258});
259
260test('macOS: the engine bundle inside a signed .app stays (deleting it breaks the signature); outside an .app, or on another system, it is removed as before', async () => {
261  const t = setup();
262  const platform = Object.getOwnPropertyDescriptor(process, 'platform');
263  const as = value => Object.defineProperty(process, 'platform', { value, configurable: true });
264  try {
265    const app = join(t.base, 'Strata.app', 'Contents', 'Resources', 'engine-bundle');
266    mkdirSync(app, { recursive: true });
267    const inApp = makePackage(app, '0.1.48');
268    const outside = makePackage(t.bundle, '0.1.48');
269    // the engine is in the store (the app's own first start copies it), so the bundle would be removable
270    assert.equal((await installBundledEngine({ bundleDir: t.bundle, components: t.components })).action, 'installed');
271    const lines = [];
272    // 1. macOS, bundle inside Strata.app: kept, with one line that says why
273    as('darwin');
274    let r = await removeVerifiedBundle({ bundleDir: app, components: t.components, log: l => lines.push(l) });
275    assert.equal(r.removed, false);
276    assert.ok(existsSync(join(app, inApp, 'strata.exe')), 'the signed app keeps its engine files');
277    assert.equal(lines.length, 1);
278    assert.match(lines[0], /kept in the install folder.*macOS app bundles must remain unchanged after signing/);
279    // 2. macOS, bundle outside an .app: removed as before
280    r = await removeVerifiedBundle({ bundleDir: t.bundle, components: t.components, log: () => {} });
281    assert.equal(r.removed, true);
282    assert.equal(existsSync(join(t.bundle, outside)), false);
283    // 3. another system, bundle inside a folder called .app: removed as before (the guard is for macOS only)
284    as('linux');
285    r = await removeVerifiedBundle({ bundleDir: app, components: t.components, log: () => {} });
286    assert.equal(r.removed, true);
287    assert.equal(existsSync(join(app, inApp)), false);
288  } finally {
289    Object.defineProperty(process, 'platform', platform);
290    t.cleanup();
291  }
292});
293
294test('a card taken out of the PC does not decide the make; two present makes accept a build for either (#3892)', async () => {
295  const { gpuBackendsHere, windowsDisplayVendors } = await import('../server/bundled-engine.mjs');
296  const nl = String.fromCharCode(10);
297  const registry = 'MatchingDeviceId    REG_SZ    PCI/VEN_10DE&DEV_2882' + nl + 'MatchingDeviceId    REG_SZ    PCI/VEN_1002&DEV_7550';
298  // the NVIDIA card was swapped for a Radeon: Windows still lists its driver entry, but only the AMD card is present
299  assert.deepEqual([...windowsDisplayVendors({ platform: 'win32', run: () => registry, present: () => new Set(['1002']) })], ['1002']);
300  assert.equal(gpuBackendsHere({}, { vendors: new Set(['1002']) })[0], 'hip');
301  // both cards present: both makes count, NVIDIA first
302  assert.deepEqual([...windowsDisplayVendors({ platform: 'win32', run: () => registry, present: () => new Set(['1002', '10de']) })].sort(), ['1002', '10de']);
303  assert.deepEqual(gpuBackendsHere({}, { vendors: new Set(['10de', '1002']) }), ['cuda', 'hip']);
304  // present cannot be read: the registry answer stands
305  assert.equal(windowsDisplayVendors({ platform: 'win32', run: () => registry, present: () => new Set() }).size, 2);
306  // the second source is only asked when two makes are listed
307  assert.equal(windowsDisplayVendors({ platform: 'win32', run: () => 'MatchingDeviceId    REG_SZ    PCI/VEN_1002&DEV_7550', present: () => { throw new Error('must not run'); } }).size, 1);
308  assert.deepEqual(gpuBackendsHere({ STRATA_ENGINE_BACKEND: 'hip' }, { vendors: new Set(['10de', '1002']) }), ['hip']);
309
310  const t = setup();
311  try {
312    const name = makePackage(t.bundle, '0.2.0', { platform: 'windows-x86_64', backend: 'hip', variant: 'rx9070' });
313    const manifest = JSON.parse(readFileSync(join(t.bundle, name, 'engine-component.json'), 'utf8'));
314    manifest.archs = ['gfx1201'];
315    writeFileSync(join(t.bundle, name, 'engine-component.json'), JSON.stringify(manifest));
316    // an AMD build on a PC with an NVIDIA desktop card and an AMD model card is unpacked, not refused
317    const result = await installBundledEngine({ bundleDir: t.bundle, components: t.components, backend: ['cuda', 'hip'] });
318    assert.equal(result.action, 'installed');
319  } finally { t.cleanup(); }
320});