okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1// Needs: a throwaway host (STRATA_UI_BASE, default http://127.0.0.1:18791) and tests/helpers/scripted-worker.mjs listening on 18899 as the model (see ui-drive.mjs ensureMock). 2// Pictures in the real app page: paste, "+" and drag-and-drop attach a picture; the OFF switch (per API model) refuses it with a plain message. 3import { deflateSync } from 'node:zlib' ; 4import { writeFileSync , mkdirSync } from 'node:fs' ; 5import { ensureMock , open , api } from './ui-drive.mjs' ; 6 7import { tmpdir } from 'node:os' ; 8import { join } from 'node:path' ; 9const OUT = process . env . PICS_OUT ?? join ( tmpdir (), 'pics-ui' ). split ( String . fromCharCode ( 92 )). join ( '/' ); 10mkdirSync ( OUT , { recursive :true }); 11const crc = (() => { const t = new Int32Array ( 256 ). map (( _ , n ) => { let c = n ; for ( let k = 0 ; k < 8 ; k ++ ) c = c & 1 ?0xedb88320 ^ ( c >>> 1 ) :c >>> 1 ; return c ; }); return b => { let c = - 1 ; for ( const x of b ) c = t [( c ^ x ) & 255 ] ^ ( c >>> 8 ); return ( c ^ - 1 ) >>> 0 ; }; })(); 12function png ( w , h , rgb ) { 13const chunk = ( type , data ) => { const len = Buffer . alloc ( 4 ); len . writeUInt32BE ( data . length ); const body = Buffer . concat ([ Buffer . from ( type ), data ]); const c = Buffer . alloc ( 4 ); c . writeUInt32BE ( crc ( body )); return Buffer . concat ([ len , body , c ]); }; 14const raw = Buffer . alloc (( w * 3 + 1 ) * h ); for ( let y = 0 ; y < h ; y ++ ) for ( let x = 0 ; x < w ; x ++ ) { const o = y * ( w * 3 + 1 ) + 1 + x * 3 ; raw [ o ] = rgb [ 0 ]; raw [ o + 1 ] = rgb [ 1 ]; raw [ o + 2 ] = rgb [ 2 ]; } 15const ihdr = Buffer . alloc ( 13 ); ihdr . writeUInt32BE ( w , 0 ); ihdr . writeUInt32BE ( h , 4 ); ihdr [ 8 ] = 8 ; ihdr [ 9 ] = 2 ; 16return Buffer . concat ([ Buffer . from ([ 137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 ]), chunk ( 'IHDR' , ihdr ), chunk ( 'IDAT' , deflateSync ( raw )), chunk ( 'IEND' , Buffer . alloc ( 0 ))]); 17} 18const file = ` ${ OUT } /blue.png` ; writeFileSync ( file , png ( 64 , 64 , [ 30 , 80 , 220 ])); 19const b64 = png ( 48 , 48 , [ 220 , 40 , 40 ]). toString ( 'base64' ); 20let failed = false ; 21const ok = ( name , cond , detail = '' ) => { if ( ! cond ) failed = true ; console . log ( ` ${ cond ? 'ok ' : 'FAIL' } ${ name } ${ detail ? ' ' + detail : '' } ` ); }; 22 23const connection = await ensureMock (); 24const { browser, page} = await open (); 25await page . locator ( '.chat-model-picker' ). first (). click (); 26await page . locator ( '.model-picker-api' ). first (). click (); 27await page . waitForTimeout ( 500 ); 28const photos = () => page . locator ( '.composer .chat-photos img' ). count (); 29const textarea = page . locator ( '.composer textarea' ); 30 31// 1. paste from the clipboard (a screenshot arrives as image.png) 32await textarea . focus (); 33await page . evaluate ( b64 => { 34const bytes = Uint8Array . from ( atob ( b64 ), c => c . charCodeAt ( 0 )); 35const data = new DataTransfer (); data . items . add ( new File ([ bytes ], 'image.png' , { type :'image/png' })); 36document . querySelector ( '.composer textarea' ). dispatchEvent ( new ClipboardEvent ( 'paste' , { clipboardData :data , bubbles :true , cancelable :true })); 37}, b64 ); 38await page . waitForTimeout ( 700 ); 39ok ( 'paste: the picture is attached' , await photos () === 1 , ` ${ await photos ()} picture(s)` ); 40ok ( 'paste: it has its own name' , / Pasted picture / . test ( await page . locator ( '.composer .chat-photos span' ). first (). innerText ())); 41await page . screenshot ({ path :` ${ OUT } /1-paste.png` }); 42 43// 2. the "+" menu 44const chooser = page . waitForEvent ( 'filechooser' ); 45await page . locator ( '.composer-add' ). click (); 46await page . getByText ( 'Add files or photos' ). click (); 47( await chooser ). setFiles ( file ); 48await page . waitForTimeout ( 700 ); 49ok ( '+: the picture is attached' , await photos () === 2 , ` ${ await photos ()} picture(s)` ); 50 51// 3. drag and drop onto the conversation 52await page . evaluate ( b64 => { 53const bytes = Uint8Array . from ( atob ( b64 ), c => c . charCodeAt ( 0 )); 54const data = new DataTransfer (); data . items . add ( new File ([ bytes ], 'dropped.png' , { type :'image/png' })); 55const target = document . querySelector ( '.chatcol' ); 56for ( const type of [ 'dragenter' , 'dragover' , 'drop' ]) target . dispatchEvent ( new DragEvent ( type , { dataTransfer :data , bubbles :true , cancelable :true })); 57}, b64 ); 58await page . waitForTimeout ( 700 ); 59ok ( 'drop: the picture is attached' , await photos () === 3 , ` ${ await photos ()} picture(s)` ); 60await page . screenshot ({ path :` ${ OUT } /2-three.png` }); 61 62// 4. send: the model's request carries the pictures 63await textarea . fill ( 'what is in the pictures SCRIPT:echo seen' ); 64await page . getByRole ( 'button' , { name :'Send' }). click (); 65await page . waitForTimeout ( 6000 ); 66const seen = ( await ( await fetch ( 'http://127.0.0.1:18899/__requests' )). json ()). requests ; 67const withImages = seen . filter ( r => ( r . raw . match ( / "type":"image_url" / g ) ?? []). length || ( r . raw . match ( / "image_url" / g ) ?? []). length ); 68ok ( 'send: the model received the pictures as image input' , withImages . length >= 1 , ` ${ withImages . length } request(s) with image input` ); 69await page . screenshot ({ path :` ${ OUT } /3-sent.png` }); 70 71// 5. the OFF switch in Settings > Models 72await page . getByText ( 'More' , { exact :true }). first (). click (); 73await page . getByText ( 'Settings' , { exact :true }). first (). click (); 74await page . locator ( '#settings-tab-Models' ). click (); 75await page . locator ( '#settings-panel-Models' ). getByRole ( 'button' , { name :/ Edit / }). first (). click (); 76const box = page . locator ( '#settings-panel-Models .provider-selected input[type=checkbox]' ). first (); 77await box . scrollIntoViewIfNeeded (); 78ok ( 'off switch: on by default' , await box . isChecked ()); 79await page . screenshot ({ path :` ${ OUT } /4-settings-on.png` }); 80await box . uncheck (); 81await page . locator ( '#settings-panel-Models' ). getByRole ( 'button' , { name :'Apply' }). first (). click (); 82await page . waitForTimeout ( 1000 ); 83const saved = ( await api ( '/api/model-providers' )). body . connections . find ( c => c . id === connection . id ). models [ 0 ]; 84ok ( 'off switch: saved as off' , saved . vision === false && saved . reads === false , JSON . stringify ({ vision :saved . vision , reads :saved . reads })); 85await page . keyboard . press ( 'Escape' ); 86await page . waitForTimeout ( 500 ); 87await page . locator ( '.composer-add' ). click (). catch (() => {}); 88await page . keyboard . press ( 'Escape' ); 89await page . evaluate ( b64 => { 90const bytes = Uint8Array . from ( atob ( b64 ), c => c . charCodeAt ( 0 )); 91const data = new DataTransfer (); data . items . add ( new File ([ bytes ], 'image.png' , { type :'image/png' })); 92document . querySelector ( '.composer textarea' ). dispatchEvent ( new ClipboardEvent ( 'paste' , { clipboardData :data , bubbles :true , cancelable :true })); 93}, b64 ); 94await page . waitForTimeout ( 700 ); 95const message = await page . locator ( '.composer .verror, .composer [role=alert]' ). first (). innerText (). catch (() => '' ); 96ok ( 'off: a pasted picture is refused with a plain message and a next step' , / turned off / . test ( message ) && / Models / . test ( message ), message . replace ( / \s +/ g , ' ' ). slice ( 0 , 160 )); 97ok ( 'off: nothing was attached' , await photos () === 0 ); 98await page . screenshot ({ path :` ${ OUT } /5-off.png` }); 99await browser . close (); 100process . exit ( failed ?1 :0 );