okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c5755ed83
main
1/** 2* Headless check of the approval card, light and dark, against a real host, the real Harness and a scripted model: 3* 4* STRATA_HARNESS_DIR=... node scripts/approval-flow.mjs [shots-dir] 5* 6* For each command the scripted model asks to run in Manual mode it checks that the card asks in plain words, says nothing of 7* tool names or JSON in its headline, and keeps the exact command under Details; then it approves and checks the row that remains. 8*/ 9import { spawn , spawnSync } from 'node:child_process' ; 10import { existsSync , mkdirSync , mkdtempSync , rmSync } from 'node:fs' ; 11import { createServer } from 'node:net' ; 12import { tmpdir } from 'node:os' ; 13import { join , resolve } from 'node:path' ; 14import { pathToFileURL } from 'node:url' ; 15import { startScriptedWorker } from '../tests/helpers/scripted-worker.mjs' ; 16 17const HOST_DIR = resolve ( import . meta. dirname , '..' ); 18const SHOTS = resolve ( process . argv [ 2 ] ?? 'approval-shots' ); 19mkdirSync ( SHOTS , { recursive :true }); 20const { chromium} = await import ( pathToFileURL ( join ( HOST_DIR , 'node_modules/playwright-core/index.mjs' )). href ); 21const CHROME = process . env . CHROME ?? [ 'C:/Program Files/Google/Chrome/Application/chrome.exe' , '/usr/bin/google-chrome' ]. find ( existsSync ); 22const freePort = () => new Promise ( r => { const s = createServer (); s . listen ( 0 , '127.0.0.1' , () => { const p = s . address (). port ; s . close (() => r ( p )); }); }); 23const sleep = ms => new Promise ( r => setTimeout ( r , ms )); 24let failed = false ; 25const check = ( ok , label , extra = '' ) => { console . log ( ` ${ ok ? 'ok ' : 'FAIL' } ${ label } ${ extra ? ' ' + extra : '' } ` ); if ( ! ok ) failed = true ; }; 26 27const worker = await startScriptedWorker ({ model :'cloud-model' }); 28const home = mkdtempSync ( join ( tmpdir (), 'strata-appr-' )); 29const ws = join ( home , 'workspace' ); mkdirSync ( ws , { recursive :true }); 30const port = await freePort (); 31const env = { ...process . env , STRATA_HOME :home , STRATA_PORT :String ( port ), STRATA_WORKSPACE :ws , STRATA_REAL_ENGINE :'0' , STRATA_COMPONENTS_DIR :join ( home , 'components' ) }; 32const child = spawn ( process . execPath , [ join ( HOST_DIR , 'server' , 'index.mjs' )], { cwd :HOST_DIR , env, stdio :'ignore' , windowsHide :true }); 33const base = `http://127.0.0.1: ${ port } ` ; 34for ( let i = 0 ; i < 100 ; i ++ ) { try { if (( await fetch ( ` ${ base } /api/agent/status` )). ok ) break ; } catch { /* starting */ } await sleep ( 200 ); } 35const saved = await ( await fetch ( ` ${ base } /api/model-providers` , { method :'POST' , headers :{ 'content-type' :'application/json' }, body :JSON . stringify ({ kind :'custom' , name :'Scripted API' , baseUrl :worker . url , protocol :'openai-chat' , apiKey :'sk-approval-test-0123456789' , models :[{ id :'cloud-model' , name :'Cloud model' , contextMax :64000 }] }) })). json (); 36check ( !! saved . connections ?.[ 0 ]?. id , 'a scripted API model is connected' ); 37 38const CASES = [ 39{ call :{ command :'Start-Process explorer.exe -ArgumentList "C:\\Users\\Ana\\Documents"' , description :'Open Documents' }, tool :'pwsh' , question :/ Open your Documents folder in File Explorer\? / , details :/ Start-Process explorer\.exe / }, 40{ call :{ file_path :join ( ws , 'notes.txt' ). replaceAll ( '\\' , '/' ), content :'a\nb\nc\n' }, tool :'write' , question :/ Create the file notes\.txt\? / , details :/ notes\.txt / }, 41{ call :{ command :'Start-Process "C:\\Users\\Ana\\Downloads\\setup-1.4.2.exe" -ArgumentList "/S"' , description :'Run the installer' }, tool :'pwsh' , question :/ Run a program: setup-1\.4\.2\.exe\? / , details :/ setup-1\.4\.2\.exe / }, 42]; 43 44const browser = await chromium . launch ({ executablePath :CHROME , headless :true }); 45for ( const scheme of [ 'light' , 'dark' ]) { 46const context = await browser . newContext ({ viewport :{ width :1280 , height :860 }, colorScheme :scheme }); 47const page = await context . newPage (); 48const problems = []; page . on ( 'pageerror' , e => problems . push ( e . message )); 49await page . goto ( ` ${ base } /#/chat` ); 50await page . waitForTimeout ( 2000 ); 51const skip = page . getByText ( 'Go straight to the app' ); 52if ( await skip . count ()) { await skip . click (); await page . waitForTimeout ( 600 ); } 53await page . getByRole ( 'button' , { name :'New chat' }). first (). click (); 54await page . waitForTimeout ( 600 ); 55await page . getByRole ( 'button' , { name :/ Choose model / }). first (). click (); 56await page . getByRole ( 'menuitemradio' , { name :/ Cloud model / }). first (). click (); 57let n = 0 ; 58for ( const c of CASES ) { 59n += 1 ; 60await page . locator ( 'textarea' ). first (). fill ( `SCRIPT:call ${ c . tool } ${ JSON . stringify ( c . call )} ` ); 61await page . getByRole ( 'button' , { name :'Send' }). click (); 62const card = page . locator ( '.askcard' ). first (); 63await card . waitFor ({ timeout :120000 }); 64const head = (( await card . locator ( '.askcard__q' ). textContent ()) ?? '' ). trim (); 65check ( c . question . test ( head ), ` ${ scheme } : the card asks " ${ head } "` ); 66check ( ! /\b ( pwsh | bash | write | sandbox | json | powershell ) \b / i . test ( head ) || / Run a program / . test ( head ), ` ${ scheme } : the headline has no tool name or jargon` ); 67const box = await card . boundingBox (); 68check ( !! box && box . height < 110 , ` ${ scheme } : the card is compact` , String ( Math . round ( box ?. height ?? 0 )) + ' px' ); 69check ( await card . locator ( 'details' ). evaluate ( d => ! d . open ), ` ${ scheme } : Details starts closed` ); 70await card . locator ( 'summary' ). click (); 71const exact = (( await card . locator ( 'pre' ). textContent ()) ?? '' ); 72check ( c . details . test ( exact ), ` ${ scheme } : the exact command is under Details` , exact . slice ( 0 , 80 )); 73if ( n === 1 ) await page . screenshot ({ path :join ( SHOTS , `approval- ${ scheme } .png` ) }); 74await page . getByRole ( 'button' , { name :'Deny' }). click (); 75await page . waitForFunction (() => document . documentElement . dataset . streaming !== 'true' , null , { timeout :120000 }). catch (() => {}); 76await page . waitForTimeout ( 400 ); 77} 78check ( problems . length === 0 , ` ${ scheme } : no page error` , problems . join ( '; ' )); 79await context . close (); 80} 81await browser . close (); 82if ( process . platform === 'win32' ) spawnSync ( 'taskkill' , [ '/PID' , String ( child . pid ), '/T' , '/F' ], { windowsHide :true }); else child . kill ( 'SIGTERM' ); 83await worker . close (); 84try { rmSync ( home , { recursive :true , force :true , maxRetries :3 }); } catch { /* the Harness sandbox leaves entries; best effort */ } 85process . exit ( failed ?1 :0 );