okami.mom/strata

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

Niko1221Merge f-0213-life (final 576d06553) into f-0213-int66331f9bc

main
6.8 KiB112 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFile } from 'node:fs/promises';
4import { sensorPace, ACTIVE_MS, IDLE_MS, WATCH_MS } from '../server/sensor-pace.mjs';
5import { readGpuTelemetry } from '../server/device-probes.mjs';
6
7test('an idle app with the Monitor out of sight asks the card rarely (#4066)', () => {
8  const idle = sensorPace({ live: false, watchedUntil: 0, now: 1_000 });
9  assert.equal(idle.intervalMs, IDLE_MS);
10  assert.equal(idle.transfer, false);
11  assert.ok(IDLE_MS >= 10 * ACTIVE_MS);
12});
13
14test('a running model or an open Monitor keeps the 3 s pace', () => {
15  assert.equal(sensorPace({ live: true, now: 1_000 }).intervalMs, ACTIVE_MS);
16  assert.equal(sensorPace({ live: true, now: 1_000 }).transfer, true);
17  const watched = sensorPace({ live: false, watchedUntil: 1_000 + WATCH_MS, now: 1_000 });
18  assert.equal(watched.intervalMs, ACTIVE_MS);
19  assert.equal(watched.transfer, false, 'the PCIe sample only means something while a model runs');
20  assert.equal(sensorPace({ live: false, watchedUntil: 1_000, now: 1_000 + 1 }).intervalMs, IDLE_MS);
21});
22
23const gpus = [{ id: 'GPU-1', vendor: 'nvidia', index: 0, sensorIndex: 0 }];
24const calls = [];
25const run = async (cmd, args) => {
26  calls.push(args[0]);
27  return args[0] === 'dmon' ? '# gpu rxpci txpci\n# Idx MB/s MB/s\n 0 12 3' : 'GPU-1, 41, 5, 1024, 30.5, 200, 4, 4, 16';
28};
29
30test('without the PCIe sample one nvidia-smi process is started, with it two', async () => {
31  calls.length = 0;
32  const idle = await readGpuTelemetry(gpus, { transfer: false, run });
33  assert.deepEqual(calls.map(c => c === 'dmon' ? 'dmon' : 'query'), ['query']);
34  assert.equal(idle[0].temperatureC, 41);
35  calls.length = 0;
36  await readGpuTelemetry(gpus, { run });
37  assert.deepEqual(calls.map(c => c === 'dmon' ? 'dmon' : 'query'), ['query', 'dmon']);
38});
39
40test('the monitor uses the pace, the Monitor screen asks while it is open, and /api/monitor marks it watched', async () => {
41  const monitor = await readFile(new URL('../server/monitor.mjs', import.meta.url), 'utf8');
42  assert.match(monitor, /sensorPace\(/);
43  assert.doesNotMatch(monitor, /sensorsAt>=3000/);
44  const index = await readFile(new URL('../server/index.mjs', import.meta.url), 'utf8');
45  assert.match(index, /monitor\.watch\(\); if \(url\.searchParams\.get\('drives'\) === '1'\) monitor\.watchDrives\(\); return json\(res, 200, monitor\.snapshot\(\)\)/);
46  assert.match(index, /monitor\.setLiveProbe\(/);
47  const screen = await readFile(new URL('../src/screens/Monitor.tsx', import.meta.url), 'utf8');
48  assert.match(screen, /api\.get\('\/api\/monitor\?drives=1'\)/);
49});
50
51test('the drive-temperature reader starts only while the Monitor is watched and ends after it is gone (#4661)', async () => {
52  const monitor = await readFile(new URL('../server/monitor.mjs', import.meta.url), 'utf8');
53  assert.match(monitor, /STRATA_REAL_ENGINE==='1'&&Date\.now\(\)<drivesUntil&&Date\.now\(\)-hostStartedAt>DRIVES_AFTER_MS\?readTemperatures\(\)/);
54  assert.match(monitor, /stopTemperatures\(\)/);
55  const { readTemperatures, stopTemperatures } = await import('../server/temperature-telemetry.mjs');
56  assert.equal(typeof stopTemperatures, 'function');
57  // nothing is started by importing or by stopping: on a PC that never opens the Monitor no PowerShell and no disk query exists
58  stopTemperatures();
59  assert.deepEqual(await readTemperatures({ platform: 'darwin' }), { cpuTempC: null, ssdTempC: null });
60});
61
62test('#4661: the drive reader asks NVMe drives only, leaves a marker across a crash, and gives up a stuck first pass', async () => {
63  const { readFile } = await import('node:fs/promises');
64  const { EventEmitter } = await import('node:events');
65  const { createWindowsSsdReader } = await import('../server/temperature-telemetry.mjs');
66  const source = await readFile(new URL('../server/temperature-telemetry.mjs', import.meta.url), 'utf8');
67  assert.doesNotMatch(source, /foreach\(\$n in 0\.\.15\)/, 'no blind walk over PhysicalDrive0..15');
68  assert.match(source, /BusType -eq 'NVMe'/);
69  const fake = () => Object.assign(new EventEmitter(), { stdout: Object.assign(new EventEmitter(), { unref() {} }), unref() {}, kill() { this.killed = true; } });
70  const marker = () => { const m = { has: false, exists: () => m.has, write: () => { m.has = true; }, remove: () => { m.has = false; } }; return m; };
71  // a healthy first pass: the marker is there while the drives are asked and gone after the first answer
72  let clock = 1000, child = fake(), m = marker();
73  let reader = createWindowsSsdReader({ spawnFn: () => child, now: () => clock, sentinel: m });
74  assert.equal(reader.read(), null);
75  assert.equal(m.has, true, 'the marker is left before the first question');
76  child.stdout.emit('data', 'ssd 44\r\n');
77  assert.equal(m.has, false, 'the first answer clears it');
78  assert.equal(reader.read(), 44);
79  // the host (or the PC) died in the middle of a question: the marker is still there at the next start, and the drives are never asked again
80  let spawned = 0;
81  m = marker(); m.has = true;
82  reader = createWindowsSsdReader({ spawnFn: () => { spawned++; return fake(); }, now: () => clock, sentinel: m });
83  assert.equal(reader.read(), null);
84  assert.equal(spawned, 0, 'a marker from a crashed question means nothing is started');
85  assert.equal(reader.off, true);
86  // a first pass that never answers is ended for good and the marker stays
87  child = fake(); m = marker(); clock = 1000;
88  reader = createWindowsSsdReader({ spawnFn: () => child, now: () => clock, sentinel: m, firstPassMs: 45000 });
89  reader.read();
90  clock += 46000;
91  assert.equal(reader.read(), null);
92  assert.equal(child.killed, true);
93  assert.equal(reader.off, true);
94  assert.equal(m.has, true);
95  // a marker that cannot be written means no question either
96  spawned = 0;
97  reader = createWindowsSsdReader({ spawnFn: () => { spawned++; return fake(); }, now: () => clock, sentinel: { exists: () => false, write: () => { throw new Error('read-only'); }, remove() {} } });
98  assert.equal(reader.read(), null);
99  assert.equal(spawned, 0);
100});
101
102test('#4661: only the Monitor screen asks for the drives; the start-up fetch and the Models page do not', async () => {
103  const { readFile, readdir } = await import('node:fs/promises');
104  const { join } = await import('node:path');
105  const hits = [];
106  const walk = async dir => { for (const entry of await readdir(dir, { withFileTypes: true })) { const p = join(dir, entry.name); if (entry.isDirectory()) await walk(p); else if (/\.(ts|tsx)$/.test(entry.name) && /drives=1/.test(await readFile(p, 'utf8'))) hits.push(entry.name); } };
107  await walk(new URL('../src', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1'));
108  assert.deepEqual(hits, ['Monitor.tsx']);
109  const monitor = await import('../server/monitor.mjs');
110  assert.equal(typeof monitor.watchDrives, 'function');
111  assert.ok(monitor.DRIVES_AFTER_MS >= 60000);
112});