okami.mom/strata

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

Niko1221host: no /usr/bin/tar, /usr/bin/env or HOME/.local/state on Windows236cca71b

main
3.5 KiB59 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFileSync } from 'node:fs';
4import { fileURLToPath } from 'node:url';
5import { stateRoot, tuningDir, timeSkipDir, tarBinary, whichCommand } from '../server/platform-paths.mjs';
6import { which } from '../server/util.mjs';
7
8const read = (rel) => readFileSync(fileURLToPath(new URL(rel, import.meta.url)), 'utf8');
9
10test('Windows keeps tuning and Time-skip state under %LOCALAPPDATA%\\Strata, never HOME/.local/state', () => {
11  const env = { LOCALAPPDATA: 'C:\\Users\\me\\AppData\\Local', HOME: '/home/ignored', XDG_STATE_HOME: '/ignored' };
12  assert.equal(stateRoot(env, 'win32'), 'C:\\Users\\me\\AppData\\Local\\Strata');
13  assert.equal(tuningDir(env, 'win32'), 'C:\\Users\\me\\AppData\\Local\\Strata\\tuning');
14  assert.equal(timeSkipDir(env, 'win32'), 'C:\\Users\\me\\AppData\\Local\\Strata\\time-skip');
15});
16
17test('without LOCALAPPDATA, Windows falls back to the profile folder', () => {
18  assert.equal(stateRoot({ USERPROFILE: 'C:\\Users\\me' }, 'win32', 'C:\\Users\\other'), 'C:\\Users\\me\\AppData\\Local\\Strata');
19  assert.equal(stateRoot({}, 'win32', 'C:\\Users\\other'), 'C:\\Users\\other\\AppData\\Local\\Strata');
20});
21
22test('explicit directories win on every platform', () => {
23  assert.equal(tuningDir({ STRATA_TUNE_DIR: 'D:\\tune' }, 'win32'), 'D:\\tune');
24  assert.equal(timeSkipDir({ STRATA_TIMESKIP_DIR: '/t/ts' }, 'linux'), '/t/ts');
25});
26
27test('POSIX is unchanged: XDG_STATE_HOME, else ~/.local/state', () => {
28  assert.equal(stateRoot({ XDG_STATE_HOME: '/x' }, 'linux'), '/x/strata');
29  assert.equal(tuningDir({ HOME: '/home/me' }, 'linux'), '/home/me/.local/state/strata/tuning');
30  assert.equal(timeSkipDir({ HOME: '/home/me' }, 'darwin'), '/home/me/.local/state/strata/time-skip');
31});
32
33test('the archive tool is the one the platform ships', () => {
34  assert.equal(tarBinary({ SystemRoot: 'C:\\Windows' }, 'win32'), 'C:\\Windows\\System32\\tar.exe');
35  assert.equal(tarBinary({}, 'win32'), 'C:\\Windows\\System32\\tar.exe');
36  assert.equal(tarBinary({}, 'linux'), '/usr/bin/tar');
37  assert.equal(tarBinary({}, 'darwin'), '/usr/bin/tar');
38});
39
40test('which asks where.exe on Windows (first match) and /usr/bin/env which elsewhere', async () => {
41  assert.deepEqual(whichCommand('llama-server', { SystemRoot: 'C:\\Windows' }, 'win32'),
42    { command: 'C:\\Windows\\System32\\where.exe', args: ['llama-server'] });
43  assert.deepEqual(whichCommand('llama-server', {}, 'linux'), { command: '/usr/bin/env', args: ['which', 'llama-server'] });
44  const seen = [];
45  const win = await which('cloudflared', { platform: 'win32', env: { SystemRoot: 'C:\\Windows' },
46    run: async (command, args) => { seen.push([command, args]); return 'C:\\tools\\cloudflared.exe\r\nC:\\other\\cloudflared.exe'; } });
47  assert.equal(win, 'C:\\tools\\cloudflared.exe');
48  assert.deepEqual(seen[0], ['C:\\Windows\\System32\\where.exe', ['cloudflared']]);
49  assert.equal(await which('nothing', { platform: 'linux', run: async () => '' }), null);
50  assert.equal(await which('sh', { platform: 'linux', run: async () => '/usr/bin/sh' }), '/usr/bin/sh');
51});
52
53test('the host no longer hard-codes /usr/bin/tar, /usr/bin/env or HOME/.local/state', () => {
54  assert.ok(!read('../server/engine-install.mjs').includes('/usr/bin/tar'));
55  assert.ok(!read('../server/util.mjs').includes("'/usr/bin/env'"));
56  assert.ok(!read('../server/timeskip.mjs').includes('.local'));
57  assert.ok(!read('../server/index.mjs').includes("'.local', 'state'"));
58  assert.match(read('../server/engine-install.mjs'), /tarBinary\(\)/);
59});