okami.mom/strata

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

Niko1221rc1 merge tui-update (3165c2e8a)a1cc285fa

main
4.5 KiB65 linesraw
1/** Settings shows the last known update check from this computer and never asks the network when it opens. */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { readFile } from 'node:fs/promises';
5import { transform } from 'esbuild';
6
7const load = async name => {
8  const { code } = await transform(await readFile(new URL(`../src/lib/${name}.ts`, import.meta.url), 'utf8'), { loader: 'ts', format: 'esm' });
9  return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
10};
11const { readUpdateCache, saveUpdateCache, clearUpdateCache } = await load('update-cache');
12
13const store = new Map();
14globalThis.localStorage = { getItem: k => store.get(k) ?? null, setItem: (k, v) => void store.set(k, String(v)), removeItem: k => void store.delete(k) };
15const result = (status = 'available') => ({ checkedAt: '2026-10-19T10:00:00Z', strata: { status: 'current', message: 'ok' }, engine: { status, message: 'x' } });
16
17test('a saved check is read back for the same installed versions', () => {
18  clearUpdateCache();
19  assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45' }), null, 'never checked');
20  saveUpdateCache(result(), { appVersion: '0.2.0', engineVersion: '0.1.45' });
21  assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45' }).engine.status, 'available');
22});
23
24test('a check made for other versions is ignored, so an old "Update available" does not linger after an update', () => {
25  saveUpdateCache(result(), { appVersion: '0.2.0', engineVersion: '0.1.45' });
26  assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.46' }), null);
27  assert.equal(readUpdateCache({ appVersion: '0.2.1', engineVersion: '0.1.45' }), null);
28});
29
30test('a new Control version, or a version that is not known yet, hides the old result', () => {
31  saveUpdateCache(result(), { appVersion: '0.2.0', engineVersion: '0.1.45', controlVersion: '0.1.45' });
32  assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45', controlVersion: '0.1.45' }).engine.status, 'available');
33  assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45', controlVersion: '0.1.46' }), null, 'Control changed');
34  assert.equal(readUpdateCache({ appVersion: null, engineVersion: '0.1.45', controlVersion: '0.1.45' }), null, 'app version still loading');
35  assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45' }), null, 'Control version not known');
36});
37
38test('unreadable or blocked storage is harmless', () => {
39  store.set('strata.update-check.v1', '{not json');
40  assert.equal(readUpdateCache({}), null);
41  const saved = globalThis.localStorage;
42  globalThis.localStorage = { getItem() { throw new Error('blocked'); }, setItem() { throw new Error('blocked'); }, removeItem() { throw new Error('blocked'); } };
43  assert.equal(readUpdateCache({}), null);
44  assert.doesNotThrow(() => saveUpdateCache(result(), {}));
45  assert.doesNotThrow(() => clearUpdateCache());
46  globalThis.localStorage = saved;
47});
48
49test('opening the Engine and Update settings makes no update call: only a button press does', async () => {
50  const src = async name => (await readFile(new URL(`../src/${name}`, import.meta.url), 'utf8')).replace(/\r\n/g, '\n');
51  const ops = await src('components/NativeOperations.tsx'), upd = await src('components/SettingsUpdates.tsx'), lib = await src('lib/update-check.ts');
52  assert.doesNotMatch(ops, /\/api\/updates\/check/, 'NativeOperations has no direct call');
53  assert.doesNotMatch(upd, /\/api\/updates\/check/);
54  assert.equal(lib.match(/\/api\/updates\/check/g).length, 1, 'one place asks the host');
55  // every caller of the check sits inside a function that only a click starts
56  // Settings > Engine no longer checks or updates at all (it lists, switches and deletes installed engines); About does
57  assert.doesNotMatch(ops, /fetchUpdateCheck|checkForUpdates/, 'the Engine tab has no update check');
58  for (const [name, text] of [['SettingsUpdates', upd]]) {
59    const body = text.replace(/async function (checkForUpdates|check)\(\) \{[\s\S]*?\n    \}\n/, '');
60    assert.doesNotMatch(body, /fetchUpdateCheck\(/, `${name}: the check runs only inside the button handler`);
61    assert.match(text, /void (checkForUpdates|check)\(\)/, `${name}: a button starts it`);
62  }
63  // nothing in either component runs a check from an effect
64  for (const text of [ops, upd]) for (const effect of text.match(/useEffect\(\(\) => \{[\s\S]*?\n    \}, \[[^\]]*\]\);/g) ?? []) assert.doesNotMatch(effect, /fetchUpdateCheck|updates\/check/);
65});