okami.mom/strata

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

Niko1221apps/host: the owner's UI and the Node App Host0b3299a01

main
6.1 KiB75 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFile } from 'node:fs/promises';
4import { transform } from 'esbuild';
5async function loadTs(path) {
6  const {code}=await transform(await readFile(new URL(path,import.meta.url),'utf8'),{loader:'ts',format:'esm'});
7  return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
8}
9const profiles=await loadTs('../src/lib/run-profiles.ts');
10const memory=await loadTs('../src/lib/run-memory.ts');
11const engine={expert:[{id:'ctx.length',recommended:'128K'},{id:'perf.threads',recommended:8},{id:'mem.mmap',recommended:true}]};
12const hardware={host:{name:'Test PC',platform:'darwin',arch:'arm64'},cpu:{model:'Test CPU',cores:10},memory:{totalGb:16},gpus:[{id:'a',name:'Unified GPU',vramGb:16,backend:'metal',unified:true}],accelerator:{backend:'metal',unified:true}};
13const experience={source:'demo',config:{placement:'Automatic'},reading:{low:100,high:120},answer:{low:20,high:30},memory:{weights:19,cache:2.7,runtime:2.5,total:24.2}};
14
15test('saved settings retain the optional image choice and older profiles restore Off',()=>{
16  const config=profiles.recommendedConfig(engine,hardware);
17  const profile={id:'images',name:'With images',variantId:'q4',hardware:profiles.computerIdentity(hardware),engineVersion:'1',config,imageModel:'q6',updatedAt:'2026-10-12'};
18  const saved=profiles.saveProfile(profiles.emptyProfiles(),profile,true);
19  assert.equal(profiles.readProfiles(JSON.stringify(saved)).profiles[0].imageModel,'q6');
20  const older=structuredClone(saved);delete older.profiles[0].imageModel;
21  assert.equal(profiles.readProfiles(JSON.stringify(older)).profiles[0].imageModel,'off');
22  older.profiles[0].imageModel='unknown-version';
23  assert.equal(profiles.readProfiles(JSON.stringify(older)).profiles[0].imageModel,'off');
24});
25
26test('profiles retain custom expert values and defaults belong to an exact version on a specific computer',()=>{
27  const config=profiles.recommendedConfig(engine,hardware,'256k');config.expert['sample.temp']=.37;
28  const computer=profiles.computerIdentity(hardware);
29  const profile={id:'daily',name:'Everyday',variantId:'q4',hardware:computer,engineVersion:'1',config,updatedAt:'2026-10-10'};
30  const store=profiles.saveProfile(profiles.emptyProfiles(),profile,true);
31  config.expert['sample.temp']=1; // A later draft must not alter the saved profile.
32  const restored=profiles.readProfiles(JSON.stringify(store));
33  assert.equal(restored.profiles[0].config.expert['sample.temp'],.37);
34  assert.equal(restored.profiles[0].config.expert['ctx.length'],'256K');
35  assert.equal(restored.defaults[profiles.defaultProfileKey(computer,'q4')],'daily');
36  assert.equal(restored.defaults[profiles.defaultProfileKey(computer,'q2')],undefined);
37  const different=profiles.computerIdentity({...hardware,memory:{totalGb:32}});
38  assert.equal(restored.defaults[profiles.defaultProfileKey(different,'q4')],undefined);
39  assert.equal(profiles.saveProfile(restored,restored.profiles[0],false).defaults[profiles.defaultProfileKey(computer,'q4')],undefined);
40});
41test('resource choices preserve expert tuning and reset returns to 128K with hardware-aware CPU headroom',()=>{
42  const config=profiles.recommendedConfig(engine,hardware,'512k');config.expert['sample.temp']=.3;
43  const full=profiles.resourceConfig(config,'full',hardware);
44  assert.equal(full.expert['perf.threads'],10);assert.equal(full.expert['ctx.length'],'512K');assert.equal(full.expert['sample.temp'],.3);
45  const recommended=profiles.recommendedConfig(engine,hardware);
46  assert.equal(recommended.beginner.contextLength,'recommended');assert.equal(recommended.expert['ctx.length'],'128K');assert.equal(recommended.expert['perf.threads'],8);
47  const dual=profiles.resourceConfig(config,'shared',{...hardware,cpu:{...hardware.cpu,cores:48}});
48  assert.equal(dual.expert['perf.threads'],38);
49});
50test('unified memory is counted once; SSD plans change with context and computer use',()=>{
51  const shared=memory.planMemory(experience,hardware,'shared');const full=memory.planMemory(experience,hardware,'full');
52  assert.equal(shared.gpu,0);assert.equal(shared.budget,12);assert.equal(full.budget,14);
53  assert.ok(Math.abs(shared.ram+shared.ssd-shared.total)<.001);
54  assert.ok(full.ssd<shared.ssd);
55  const shorter=memory.planMemory({...experience,memory:{...experience.memory,cache:.7,total:22.2}},hardware,'shared');
56  assert.ok(shorter.ssd<shared.ssd);
57  const tooMuchCache=memory.planMemory({...experience,memory:{...experience.memory,cache:20}},hardware,'shared');
58  assert.equal(tooMuchCache.cacheFits,false);assert.equal(tooMuchCache.ssd,0,'KV cache overflow must not be represented as disk-backed weights');
59  const parallel=memory.planMemory(experience,hardware,'shared',4);assert.equal(parallel.cacheFits,false);
60  const locked=memory.planMemory(experience,hardware,'shared',1,false);assert.equal(locked.ssd,0);assert.ok(locked.total>locked.budget,'Locked model memory must not be presented as an SSD spill plan');
61});
62test('multi-GPU planning retains individual budgets and respects CPU-only and unknown partial placement',()=>{
63  const discrete={...hardware,memory:{totalGb:32},accelerator:{backend:'cuda',unified:false},gpus:[{id:'a',name:'GPU A',vramGb:24,backend:'cuda'},{id:'b',name:'GPU B',vramGb:12,backend:'cuda'}]};
64  const plan=memory.planMemory(experience,discrete,'shared');
65  assert.equal(plan.devices.length,2);assert.equal(plan.budget,24+21.6+10.8);assert.equal(plan.ssd,0);
66  const cpu=memory.planMemory({...experience,config:{placement:'CPU only'}},discrete,'shared');assert.equal(cpu.gpu,0);assert.equal(cpu.devices.length,0);assert.equal(cpu.budget,24);
67  assert.equal(memory.planMemory({...experience,config:{placement:'Half'}},discrete,'shared'),null);
68});
69test('illustrative spill penalties never modify or impersonate measured performance',()=>{
70  const plan=memory.planMemory(experience,hardware,'shared');
71  assert.ok(memory.demoRunExperience(experience,plan,'shared').answer.low<experience.answer.low);
72  const measured={...experience,source:'measured'};
73  assert.strictEqual(memory.demoRunExperience(measured,plan,'full'),measured);
74  assert.deepEqual(experience.answer,{low:20,high:30});
75});