okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1cc285fa
main
1#!/usr/bin/env node 2/** 3* Motion capture and interruptibility check. 4* 5* node scripts/motion.mjs 6* 7* Two jobs: 8* 9* 1. Record the canonical interactions and sample frames from each recording 10* into a contact sheet (`.shots/motion-<name>.png`), so choreography can be 11* reviewed as pictures rather than argued about. 12* 2. Assert the thing a screenshot cannot show: that a surface toggled 13* mid-flight *reverses* rather than queueing or jumping to the end. 14* 15* Requires the dev server and a cached Chromium. The bundled ffmpeg that ships 16* with Playwright is used to extract frames. 17*/ 18import { chromium } from 'playwright-core' ; 19import { execFile } from 'node:child_process' ; 20import { promisify } from 'node:util' ; 21import { existsSync , mkdirSync , rmSync , readdirSync } from 'node:fs' ; 22import { join , resolve } from 'node:path' ; 23import { homedir } from 'node:os' ; 24import { requireChromium } from './browser.mjs' ; 25 26const run = promisify ( execFile ); 27const BASE = process . env . STRATA_URL ?? 'http://localhost:5199' ; 28const ROOT = resolve ( process . env . STRATA_SHOTS ?? '.shots' ); 29const TMP = join ( ROOT , 'motion-frames' ); 30mkdirSync ( TMP , { recursive :true }); 31 32const executablePath = requireChromium (); 33 34const ffmpeg = [ 35join ( homedir (), 'Library/Caches/ms-playwright/ffmpeg-1011/ffmpeg-mac' ), 36'/opt/homebrew/bin/ffmpeg' , 37]. find (( p ) => existsSync ( p )); 38 39if ( ! executablePath ) { 40console . error ( 'No cached Chromium found.' ); 41process . exit ( 2 ); 42} 43 44const W = 1280 ; 45const H = 720 ; 46const failures = []; 47 48/** Record one interaction, then sample frames from the resulting webm. */ 49async function clip ( browser , name , script ) { 50const dir = join ( TMP , name ); 51rmSync ( dir , { recursive :true , force :true }); 52mkdirSync ( dir , { recursive :true }); 53 54const t0 = Date . now (); 55const context = await browser . newContext ({ 56viewport :{ width :W , height :H }, 57recordVideo :{ dir, size :{ width :W , height :H } }, 58}); 59const page = await context . newPage (); 60page . on ( 'pageerror' , ( e ) => failures . push ( ` ${ name } : page error — ${ e . message } ` )); 61await page . goto ( ` ${ BASE } /#/chat` , { waitUntil :'domcontentloaded' }); 62await page . waitForTimeout ( 1200 ); 63// Recording starts at context creation, so the first second is page load. 64// Each clip reports when its interaction actually happened, and frames are 65// sampled from that window — otherwise every sheet shows an idle app. 66const started = Date . now (); 67await script ( page ); 68const window = { from :( started - t0 ) / 1000 , to :( Date . now () - t0 ) / 1000 }; 69const video = page . video (); 70await context . close (); 71 72const videoPath = video ?await video . path () :null ; 73if ( ! videoPath || ! existsSync ( videoPath )) { 74failures . push ( ` ${ name } : no recording produced` ); 75return null ; 76} 77 78if ( ! ffmpeg ) { 79console . log ( ` ${ name } : recorded, but no ffmpeg found to sample frames` ); 80return videoPath ; 81} 82 83// The ffmpeg Playwright bundles is a stripped build (no `fps` or `hstack` 84// filters), so frames are taken by seeking to a timestamp and grabbing one. 85const span = Math . max ( 0.6 , window . to - window . from ); 86const stamps = Array . from ({ length :6 }, ( _ , i ) => Number (( window . from + ( span * i ) / 5 ). toFixed ( 2 ))); 87const frames = []; 88for ( const t of stamps ) { 89const out = join ( dir , `t ${ String ( t ). replace ( '.' , '_' )} .png` ); 90await run ( ffmpeg , [ '-y' , '-ss' , String ( t ), '-i' , videoPath , '-frames:v' , '1' , out ]). catch (() => {}); 91if ( existsSync ( out )) frames . push ( out ); 92} 93if ( ! frames . length ) failures . push ( ` ${ name } : ffmpeg produced no frames` ); 94return { videoPath, frames, window}; 95} 96 97/** 98* Stitch sampled frames into one image. Done in Python rather than ffmpeg 99* because the bundled build has no `hstack` filter. 100*/ 101async function contactSheet ( name , frames ) { 102if ( ! frames ?. length ) return ; 103const out = join ( ROOT , `motion- ${ name } .png` ); 104const script = ` 105import sys 106from PIL import Image 107paths, out = sys.argv[1:-1], sys.argv[-1] 108ims = [Image.open(p) for p in paths] 109w = sum(i.width for i in ims) 110h = max(i.height for i in ims) 111sheet = Image.new('RGB', (w, h), '#ffffff') 112x = 0 113for im in ims: 114sheet.paste(im, (x, 0)) 115x += im.width 116sheet.save(out) 117` ; 118await run ( 'python3' , [ '-c' , script , ...frames , out ]). catch (() => 119failures . push ( ` ${ name } : could not build the contact sheet` ), 120); 121if ( existsSync ( out )) console . log ( ` ${ name } : ${ frames . length } frames -> ${ out } ` ); 122} 123 124const browser = await chromium . launch ({ executablePath, args :[ '--no-sandbox' ] }); 125console . log ( 'recording interactions\n' ); 126 127const clips = { 128'route-change' :async ( page ) => { 129await page . keyboard . press ( 'Meta+3' ); 130await page . waitForTimeout ( 700 ); 131await page . keyboard . press ( 'Meta+5' ); 132await page . waitForTimeout ( 700 ); 133}, 134'drawer-toggle' :async ( page ) => { 135await page . click ( 'button.nav-new' ); 136await page . waitForTimeout ( 400 ); 137await page . getByRole ( 'button' , { name :'Add a task' , exact :true }). click (); 138await page . waitForTimeout ( 600 ); 139await page . getByRole ( 'button' , { name :/ Close . * task / i }). click (); 140await page . waitForTimeout ( 600 ); 141}, 142'palette-toggle' :async ( page ) => { 143await page . keyboard . press ( 'Meta+k' ); 144await page . waitForTimeout ( 600 ); 145await page . keyboard . press ( 'Escape' ); 146await page . waitForTimeout ( 600 ); 147}, 148disclosure :async ( page ) => { 149await page . click ( 'button.nav-new' ); 150await page . waitForTimeout ( 300 ); 151await page . fill ( 'textarea' , 'read the readme' ); 152await page . keyboard . press ( 'Enter' ); 153await page . waitForTimeout ( 7000 ); 154const details = page . locator ( '.st-collapse' ). first (); 155if ( await details . count ()) { 156await details . locator ( 'summary' ). click (); 157await page . waitForTimeout ( 400 ); 158await details . locator ( 'summary' ). click (); 159await page . waitForTimeout ( 500 ); 160} 161}, 162}; 163 164for ( const [ name , script ] of Object . entries ( clips )) { 165const result = await clip ( browser , name , script ); 166if ( result && typeof result === 'object' ) { 167console . log ( ` ${ name } : sampling ${ result . window . from . toFixed ( 2 )} s– ${ result . window . to . toFixed ( 2 )} s` ); 168await contactSheet ( name , result . frames ); 169} 170} 171 172/* ---------------------------------------------------- interruptibility ---- */ 173 174/** 175* Open a surface, reverse it mid-flight, and confirm it comes to rest in the 176* reversed state. A queued or keyframe-driven animation fails this: it finishes 177* the entrance first and only then plays the exit. 178*/ 179async function reversesMidFlight ( label , open , close , readState ) { 180const page = await browser . newPage ({ viewport :{ width :W , height :H } }); 181await page . goto ( ` ${ BASE } /#/chat` , { waitUntil :'domcontentloaded' }); 182await page . waitForTimeout ( 1200 ); 183await open ( page ); 184await page . waitForTimeout ( 60 ); // deliberately inside the entrance 185await close ( page ); 186await page . waitForTimeout ( 500 ); // longer than any transition on the scale 187const settled = await readState ( page ); 188await page . close (); 189if ( ! settled ) failures . push ( ` ${ label } : did not settle in the reversed state` ); 190console . log ( ` ${ label } : reversed mid-flight — ${ settled ? 'settled closed' : 'STUCK OPEN' } ` ); 191} 192 193/* A step that throws must still leave the other checks reported — a crash 194before the summary tells the reader nothing about what did pass. */ 195try { 196await reversesMidFlight ( 197'task dialog' , 198( p ) => p . click ( 'button.nav-new' ). then (() => p . waitForTimeout ( 300 )). then (() => p . getByRole ( 'button' , { name :'Add a task' , exact :true }). click ()), 199( p ) => p . getByRole ( 'button' , { name :/ Close . * task / i }). click (), 200( p ) => 201p . evaluate (() => { 202const el = document . querySelector ( '.dialog-layer' ); 203if ( ! el ) return true ; 204return getComputedStyle ( el ). visibility === 'hidden' ; 205}), 206); 207 208await reversesMidFlight ( 209'command palette' , 210( p ) => p . keyboard . press ( 'Meta+k' ), 211( p ) => p . keyboard . press ( 'Escape' ), 212( p ) => 213p . evaluate (() => { 214const el = document . querySelector ( '.modal' ); 215return !! el && ! el . matches ( '[data-open="true"]' ) && getComputedStyle ( el ). visibility === 'hidden' ; 216}), 217); 218 219/** 220* One loop per region. 221* 222* Several indefinite animations running in the same part of the screen read as 223* a fault rather than as activity — two pulsing things side by side have no 224* hierarchy, and the eye cannot settle on either. Each screen is checked for 225* regions containing more than one looping animation. 226* 227* "Region" is the nearest ancestor that is a card, a panel or the page body, so 228* a spinner inside a list row is judged against that row rather than against 229* everything on the screen. 230*/ 231async function loopBudget ( route , theme ) { 232const page = await browser . newPage ({ viewport :{ width :1512 , height :1110 } }); 233await page . goto ( ` ${ BASE } /#/ ${ route } ` , { waitUntil :'domcontentloaded' }); 234await page . waitForTimeout ( 1500 ); 235if ( theme === 'dark' ) { 236/* Set the theme the way the app does rather than by clicking a control a 237modal may be covering — the setup screen renders one over the whole 238window, and clicking through it timed out. */ 239await page . evaluate (() => { 240const raw = JSON . parse ( localStorage . getItem ( 'strata.prefs.v2' ) ?? '{}' ); 241localStorage . setItem ( 'strata.prefs.v2' , JSON . stringify ({ ...raw , theme :'dark' })); 242}); 243await page . reload ({ waitUntil :'domcontentloaded' }); 244await page . waitForTimeout ( 1200 ); 245} 246const out = await page . evaluate (() => { 247const loops = [ ...document . querySelectorAll ( '*' )]. filter (( el ) => { 248const cs = getComputedStyle ( el ); 249if ( ! cs . animationIterationCount . split ( ',' ). some (( v ) => v . trim () === 'infinite' )) return false ; 250const r = el . getBoundingClientRect (); 251return r . width > 0 && r . height > 0 && cs . visibility !== 'hidden' ; 252}); 253const regions = new Map (); 254for ( const el of loops ) { 255const region = 256el . closest ( '.st-card, .st-drawer, section, aside, table' ) ?? document . querySelector ( '.main__body' ) ?? document . body ; 257const label = region . className ?. toString (). split ( ' ' ). slice ( 0 , 2 ). join ( '.' ) || region . tagName . toLowerCase (); 258regions . set ( label , ( regions . get ( label ) ?? 0 ) + 1 ); 259} 260return [ ...regions . entries ()]. map (([ label , n ]) => ({ label, n})); 261}); 262await page . close (); 263return out ; 264} 265 266for ( const theme of [ 'light' , 'dark' ]) { 267// Every route a reader can reach, not just the ones the reverse checks visit. 268for ( const route of [ 'chat' , 'models' , 'tuning' , 'monitor' , 'serving' , 'workspace' , 'skills' , 'settings' , 'setup' ]) { 269const regions = await loopBudget ( route , theme ); 270const over = regions . filter (( r ) => r . n > 1 ); 271for ( const r of over ) failures . push ( ` ${ theme } · ${ route } : ${ r . n } loops at once in " ${ r . label } "` ); 272} 273} 274} catch ( err ) { 275failures . push ( `a step threw before finishing: ${ err instanceof Error ? err . message . split ( '\n' )[ 0 ] : err } ` ); 276} 277 278await browser . close (); 279 280/* The summary has to name what was checked. Saying "every surface reverses 281mid-flight" when the failure was two spinners in one card would send the 282reader looking in the wrong place. */ 283console . log ( 284failures . length 285 ?`\nmotion: ${ failures . length } failure(s)\n ${ failures . join ( '\n ' )} ` 286 :[ 287'' , 288'motion: every surface reverses mid-flight' , 289'motion: one loop per region on every screen, both themes' , 290]. join ( '\n' ), 291); 292process . exit ( failures . length ?1 :0 );