okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c5755ed83
main
1/** 2* The first workspace is never the app's own folder, and any workspace can be renamed (a display name; the path stays). 3* A real host process with a scratch user profile; no STRATA_WORKSPACE, started inside the host folder like the installed app. 4*/ 5import test from 'node:test' ; 6import assert from 'node:assert/strict' ; 7import { spawn , spawnSync } from 'node:child_process' ; 8import { existsSync , mkdirSync , mkdtempSync , rmSync , writeFileSync } from 'node:fs' ; 9import { createServer } from 'node:net' ; 10import { tmpdir } from 'node:os' ; 11import { join , resolve } from 'node:path' ; 12 13const HOST_DIR = resolve ( import . meta. dirname , '..' ); 14const freePort = () => new Promise ( r => { const s = createServer (); s . listen ( 0 , '127.0.0.1' , () => { const p = s . address (). port ; s . close (() => r ( p )); }); }); 15const sleep = ms => new Promise ( r => setTimeout ( r , ms )); 16 17async function start ( t , { user, home}) { 18const port = await freePort (); 19const env = { ...process . env , STRATA_HOME :home , STRATA_PORT :String ( port ), USERPROFILE :user , HOME :user , STRATA_REAL_ENGINE :'0' , STRATA_COMPONENTS_DIR :join ( home , 'components' ) }; 20delete env . STRATA_WORKSPACE ; 21const child = spawn ( process . execPath , [ join ( HOST_DIR , 'server' , 'index.mjs' )], { cwd :HOST_DIR , env, stdio :[ 'ignore' , 'pipe' , 'pipe' ], windowsHide :true }); 22let log = '' ; child . stdout . on ( 'data' , d => { log += d ; }); child . stderr . on ( 'data' , d => { log += d ; }); 23t . after ( async () => { 24if ( child . exitCode !== null ) return ; 25if ( process . platform === 'win32' ) spawnSync ( 'taskkill' , [ '/PID' , String ( child . pid ), '/T' , '/F' ], { windowsHide :true }); else child . kill ( 'SIGTERM' ); 26await new Promise ( r => child . once ( 'exit' , r )); 27}); 28const base = `http://127.0.0.1: ${ port } ` ; 29for ( let i = 0 ; i < 100 ; i ++ ) { 30try { if (( await fetch ( ` ${ base } /api/workspaces` )). ok ) break ; } catch { /* not up yet */ } 31if ( child . exitCode !== null ) throw new Error ( `the host exited: ${ log . slice ( - 400 )} ` ); 32await sleep ( 200 ); 33} 34const call = async ( method , path , body ) => { const r = await fetch ( base + path , { method, headers :{ 'content-type' :'application/json' }, body :body ?JSON . stringify ( body ) :undefined }); return { status :r . status , body :await r . json (). catch (() => null ) }; }; 35return { call}; 36} 37 38const scratch = () => { const dir = mkdtempSync ( join ( tmpdir (), 'strata-ws-' )); const user = join ( dir , 'user' ); const home = join ( dir , 'home' ); mkdirSync ( user , { recursive :true }); mkdirSync ( home , { recursive :true }); return { dir, user, home}; }; 39 40test ( 'item 6: with nothing chosen the first workspace is Documents\\Strata, never the app folder' , async t => { 41const s = scratch (); t . after (() => rmSync ( s . dir , { recursive :true , force :true })); 42const host = await start ( t , s ); 43const { body} = await host . call ( 'GET' , '/api/workspaces' ); 44const first = body . workspaces [ 0 ]; 45assert . equal ( first . id , 'root' ); 46assert . equal ( resolve ( first . path ), join ( s . user , 'Documents' , 'Strata' , 'Workspace' )); 47assert . equal ( first . name , 'Strata' ); 48assert . ok ( existsSync ( first . path ), 'the folder is created' ); 49assert . notEqual ( resolve ( first . path ), HOST_DIR ); 50}); 51 52test ( 'item 6: a saved first workspace that is the app folder moves to the default; its chats follow through an alias' , async t => { 53const s = scratch (); t . after (() => rmSync ( s . dir , { recursive :true , force :true })); 54mkdirSync ( join ( s . home , 'data' ), { recursive :true }); 55writeFileSync ( join ( s . home , 'data' , 'workspaces.json' ), JSON . stringify ([{ id :'root' , name :'host' , path :HOST_DIR }, { id :'ws_a' , name :'Mine' , path :s . user }])); 56const host = await start ( t , s ); 57const { body} = await host . call ( 'GET' , '/api/workspaces' ); 58assert . equal ( resolve ( body . workspaces [ 0 ]. path ), join ( s . user , 'Documents' , 'Strata' , 'Workspace' )); 59assert . equal ( body . workspaces [ 0 ]. name , 'Strata' ); 60assert . deepEqual ( body . workspaces [ 0 ]. aliases , [ HOST_DIR ]); 61assert . equal ( body . workspaces [ 1 ]. path , s . user , 'a folder the person added is left alone' ); 62}); 63 64test ( 'item 6: renaming changes the display name only' , async t => { 65const s = scratch (); t . after (() => rmSync ( s . dir , { recursive :true , force :true })); 66const host = await start ( t , s ); 67const before = ( await host . call ( 'GET' , '/api/workspaces' )). body . workspaces [ 0 ]; 68const done = await host . call ( 'PATCH' , '/api/workspaces/root' , { name :' My projects ' }); 69assert . equal ( done . status , 200 ); 70assert . equal ( done . body . name , 'My projects' ); assert . equal ( done . body . path , before . path ); 71assert . equal (( await host . call ( 'GET' , '/api/workspaces' )). body . workspaces [ 0 ]. name , 'My projects' , 'it is saved' ); 72assert . equal (( await host . call ( 'PATCH' , '/api/workspaces/root' , { name :' ' })). status , 400 ); 73assert . equal (( await host . call ( 'PATCH' , '/api/workspaces/nope' , { name :'x' })). status , 404 ); 74});