okami.mom/strata

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

Niko1221host tests: fileURLToPath replaces the regex that stripped the slash before a drive letter (replaces 439339ab1; the pathname form also left %20 in paths with spaces)44d885874

main
3.8 KiB57 linesraw
1import { fileURLToPath } from 'node:url';
2import assert from 'node:assert/strict';
3import { test } from 'node:test';
4import { readFile } from 'node:fs/promises';
5import { transform } from 'esbuild';
6import { migrateSetup } from '../server/setup.mjs';
7const { code } = await transform(await readFile(new URL('../src/lib/lifecycle.ts', import.meta.url), 'utf8'), { loader: 'ts', format: 'esm' });
8const { lifecyclePolicy } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
9
10test('old setup steps map once and preserve selections and completed work', () => {
11  const choices = { variantId:'fixture', profile:'performance', contextLength:'8k', batch:1024, batchAuto:false };
12  const history = [{step:3, at:'fixture'}];
13  for (const [oldStep, expected] of [[1,1],[2,2],[3,2],[4,3],[5,4]]) {
14    const original = { step:oldStep, choices, history, dismissed:true, complete:false };
15    const migrated = migrateSetup(original);
16    assert.equal(migrated.step, expected); assert.equal(migrated.flowVersion, 2);
17    assert.deepEqual(migrated.choices, choices); assert.deepEqual(migrated.history, history);
18    assert.equal(migrated.dismissed, true); assert.equal(migrated.complete, false);
19    assert.deepEqual(migrateSetup(migrated), migrated);
20    assert.equal(original.step, oldStep);
21  }
22});
23
24test('shared lifecycle policy blocks queued, active, disconnected and unknown status', () => {
25  const idle = {connected:true, streaming:false, stats:{running:0, waiting:0}, busy:{}};
26  assert.equal(lifecyclePolicy(idle).allowed, true);
27  for (const patch of [
28    {stats:{running:0, waiting:2}}, {stats:{running:1, waiting:0}}, {streaming:true},
29    {connected:false}, {stats:null}, {stats:{running:0}}, {stats:{running:0,waiting:NaN}},
30    {busy:{restart:true}}, {busy:{unload:true}}, {busy:{'load:fixture':true}},
31  ]) assert.equal(lifecyclePolicy({...idle, ...patch}).allowed, false, JSON.stringify(patch));
32  assert.match(lifecyclePolicy({...idle, stats:{running:0,waiting:2}}).reason, /2 queued/);
33  assert.match(lifecyclePolicy({...idle, stats:null}).reason, /unavailable/);
34  assert.equal(lifecyclePolicy({...idle, busy:{'install:fixture':true}}).allowed, true);
35});
36
37test('blocked lifecycle writes do not change config; save-only still works', async () => {
38  const { build } = await import('esbuild');
39  const { createRequire } = await import('node:module');
40  const { outputFiles } = await build({entryPoints:[fileURLToPath(new URL('../src/lib/store.ts',import.meta.url))],bundle:true,write:false,platform:'node',format:'cjs'});
41  const mod={exports:{}}; new Function('require','module','exports','matchMedia',outputFiles[0].text)(createRequire(import.meta.url),mod,mod.exports,()=>({addEventListener(){},matches:false}));
42  const store=mod.exports; const originalFetch=globalThis.fetch; const requests=[];
43  globalThis.fetch=async (url, options) => {requests.push([url,options]); return new Response(JSON.stringify({expert:[],beginner:{contextLength:'recommended'},engine:{running:false}}),{status:200});};
44  try {
45    for (const stats of [null,{running:0,waiting:2}]) {
46      Object.assign(store.getState(),{connection:'live',serving:stats ? {stats} : null,library:{mode:'real',variants:[],loaded:'fixture'},busy:{}});
47      await assert.rejects(store.saveEngine({expert:[]},true,true),/unavailable|queued/);
48      assert.equal(await store.loadVariant('fixture'),false);
49      assert.equal(await store.unloadModel(),false);
50      assert.equal(await store.restartEngine(),false);
51      assert.equal(requests.length,0);
52      for(const toast of store.getState().toasts) store.holdToast(toast.id);
53    }
54    await store.saveEngine({expert:[]},true,false);
55    assert.equal(requests.length,1); assert.equal(requests[0][1].method,'PATCH');
56  } finally {globalThis.fetch=originalFetch; for(const toast of store.getState().toasts)store.holdToast(toast.id);}
57});