okami.mom/strata

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

Niko1221Merge f-022-gpuroles: Main / Helper / Off per graphics card in the app, TUI, launch, setup, server and Dockere28889d12

main
4.2 KiB74 linesraw
1/** GPU roles (Main / Helper / Off): src/lib/gpu-roles.ts and server/gpu-roles.mjs run the vectors strata_control/gpu_roles.py runs (tests/data/gpu_roles_vectors.json). */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { readFile } from 'node:fs/promises';
5import { transform } from 'esbuild';
6
7const { code } = await transform(await readFile(new URL('../src/lib/gpu-roles.ts', import.meta.url), 'utf8'), { loader: 'ts', format: 'esm' });
8const R = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
9const S = await import('../server/gpu-roles.mjs');
10const V = JSON.parse(await readFile(new URL('../../../tests/data/gpu_roles_vectors.json', import.meta.url), 'utf8'));
11const roles = d => d === null || d === undefined ? d : Object.fromEntries(Object.entries(d).map(([k, v]) => [Number(k), v]));
12
13test('recommend: the best card is Main, others Helper, 8 GB or less / built-in / unusable start Off', () => {
14  for (const c of V.recommend) {
15    const r = R.recommend(V.hw[c.hw]);
16    assert.deepEqual(r.roles, roles(c.roles), c.hw);
17    assert.equal(r.main, c.main, c.hw);
18    assert.equal(r.why, c.why, c.hw);
19  }
20});
21
22test('setRole: one click, exactly one Main among the cards that are on', () => {
23  for (const c of V.set_role) {
24    const out = R.setRole(roles(c.roles), V.hw[c.hw], c.card, c.role);
25    assert.deepEqual(out.roles, roles(c.after), JSON.stringify(c));
26    assert.equal(out.note, c.note, JSON.stringify(c));
27  }
28});
29
30test('order: Helpers first, Main LAST, Off left out; one card is a number in the mirror', () => {
31  for (const c of V.order) {
32    assert.deepEqual(R.order(roles(c.roles), V.hw[c.hw]), c.gpu, JSON.stringify(c));
33    assert.deepEqual(S.order(roles(c.roles), V.hw[c.hw]), c.gpu, 'server copy: ' + JSON.stringify(c));
34  }
35  const m = R.mirror({ 0: 'off', 1: 'main' }, V.hw.two);
36  assert.equal(m.gpu, 1);
37  assert.equal(m.gpu_order, 'as_given');
38  assert.deepEqual(R.mirror({ 0: 'helper', 1: 'main' }, V.hw.two).gpu, [0, 1]);
39  const none = R.mirror({ 0: 'off', 1: 'off' }, V.hw.two);
40  assert.deepEqual([none.gpu, none.gpu_none], [[], true]);
41  assert.deepEqual(S.mirror({ 0: 'helper', 1: 'main' }, V.hw.two), R.mirror({ 0: 'helper', 1: 'main' }, V.hw.two));
42  assert.deepEqual(S.mirror({ 0: 'off', 1: 'off' }, V.hw.two), none);
43});
44
45test('warnings: only built-in graphics as Main, a card the engine cannot run, too little memory, CPU only', () => {
46  for (const c of V.warnings) {
47    const w = R.warnings(roles(c.roles), V.hw[c.hw], { spillGb: c.spill_gb ?? 0 });
48    assert.deepEqual(w.map(x => x.code), c.codes, JSON.stringify(c));
49    if (c.fix) assert.deepEqual(w[0].fix, c.fix);
50    for (const x of w) assert.ok(x.text.length < 90, x.text);
51  }
52  // a slower card as Main is understood by people: no warning
53  assert.deepEqual(R.warnings({ 0: 'main', 1: 'helper' }, V.hw.two), []);
54  assert.equal(R.warnings({ 0: 'off', 1: 'off' }, V.hw.two, { engineRunsCpuOnly: true })[0].text, 'Performance is degraded on CPU only.');
55  assert.equal(R.warnings({ 0: 'off', 1: 'off' }, V.hw.two)[0].text, R.NO_CARD_LINE);
56});
57
58test('legacy gpu settings show the roles the engine really uses', () => {
59  for (const c of V.legacy) assert.deepEqual(R.fromLegacy(c.gpu, V.hw[c.hw], c.gpu_order), roles(c.roles), JSON.stringify(c));
60});
61
62test('saved roles: parse; the server copy agrees on which are usable', () => {
63  assert.deepEqual(R.parseSaved({ 0: 'helper', 1: 'main' }), { 0: 'helper', 1: 'main' });
64  for (const bad of [{ 0: 'boss' }, { 0: 'main', 1: 'main' }, 'x', {}, null]) { assert.equal(R.parseSaved(bad), null); assert.equal(S.parseSaved(bad), null); }
65});
66
67test('words: the sentence and the one-line summary', () => {
68  assert.equal(R.sentence(V.hw.one), 'Strata will use your RTX 4070 Ti.');
69  assert.match(R.sentence(V.hw.three), /RTX 4070 Ti is Main: it is faster\./);
70  assert.match(R.sentence(V.hw.three), /RTX 2060 starts Off: 8 GB or less\./);
71  assert.equal(R.describe({ 0: 'helper', 1: 'main' }, V.hw.two), 'Main: GPU 2 RTX 4070 Ti. Helper: GPU 1 RTX 3060.');
72  assert.equal(R.noteText('old_main_helper:1', V.hw.two), 'RTX 4070 Ti is now a Helper.');
73  assert.equal(R.noteText('only_main', V.hw.two), 'The only card that is on has to be Main.');
74});