okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8667a3dbf
main
1// Files dropped on the app window on Linux and macOS: the shell hands the paths to the host, the page fetches each file once. 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { spawn } from 'node:child_process' ; 5import { mkdtemp , mkdir , rm , writeFile , readFile } from 'node:fs/promises' ; 6import { createServer } from 'node:net' ; 7import { tmpdir } from 'node:os' ; 8import { join , resolve } from 'node:path' ; 9import { transform } from 'esbuild' ; 10import { createNativeDrops , typeOfName , MAX_FILES } from '../server/native-drop.mjs' ; 11 12const root = resolve ( import . meta. dirname , '..' ); 13async function loadTs ( path ) { 14const { code} = await transform ( await readFile ( new URL ( path , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 15return import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 16} 17const { readDroppedRefs, fetchDropped, dragHasFiles} = await loadTs ( '../src/lib/native-drop.ts' ); 18 19test ( 'the host keeps only regular files it can read, each for one fetch, for a short time' , async () => { 20const dir = await mkdtemp ( join ( tmpdir (), 'strata-drop-' )); 21try { 22await writeFile ( join ( dir , 'a.PNG' ), Buffer . from ([ 1 , 2 , 3 ])); await writeFile ( join ( dir , 'notes.md' ), '# hi' ); await writeFile ( join ( dir , 'thing.bin' ), 'x' ); 23await mkdir ( join ( dir , 'folder' )); 24let clock = 1000 ; 25const drops = createNativeDrops ({ now :() => clock }); 26const got = await drops . add ([ join ( dir , 'a.PNG' ), join ( dir , 'folder' ), join ( dir , 'missing.txt' ), 'relative/path.txt' , 42 , join ( dir , 'notes.md' ), join ( dir , 'thing.bin' )]); 27assert . deepEqual ( got . map ( f => [ f . name , f . type , f . size ]), [[ 'a.PNG' , 'image/png' , 3 ], [ 'notes.md' , 'text/markdown' , 4 ], [ 'thing.bin' , 'application/octet-stream' , 1 ]]); 28assert . match ( got [ 0 ]. id , / ^ [ 0 - 9a - f- ]{ 36 } $ / ); 29assert . deepEqual ([ ...drops . take ( got [ 0 ]. id ). bytes ], [ 1 , 2 , 3 ]); 30assert . equal ( drops . take ( got [ 0 ]. id ), null , 'one fetch per file' ); 31clock += 3 * 60_000 ; 32assert . equal ( drops . take ( got [ 1 ]. id ), null , 'gone after a couple of minutes' ); 33assert . deepEqual ( await drops . add ( 'not a list' ), []); 34const many = await drops . add ( Array . from ({ length :30 }, () => join ( dir , 'notes.md' ))); 35assert . equal ( many . length , MAX_FILES ); 36const big = createNativeDrops ({ info :async () => ({ isFile :() => true , size :26 * 1024 * 1024 }) }); 37assert . deepEqual ( await big . add ([ join ( dir , 'notes.md' )]), [], 'a file over 25 MB is left out' ); 38assert . equal ( typeOfName ( 'x.JPG' ), 'image/jpeg' ); 39} finally { await rm ( dir , { recursive :true , force :true , maxRetries :3 }); } 40}); 41 42test ( 'only the app shell can hand files over; the page fetches each file once' , { timeout :60000 }, async () => { 43const home = await mkdtemp ( join ( tmpdir (), 'strata-drop-host-' )); 44const socket = createServer (); await new Promise ( r => socket . listen ( 0 , '127.0.0.1' , r )); 45const port = socket . address (). port ; await new Promise ( r => socket . close ( r )); 46const secret = 'a' . repeat ( 40 ); 47const child = spawn ( process . execPath , [ 'server/index.mjs' ], { cwd :root , env :{ ...process . env , STRATA_HOME :home , STRATA_MODELS_DIR :join ( home , 'models' ), STRATA_PORT :String ( port ), STRATA_REAL_ENGINE :'0' , STRATA_WORKSPACE :join ( home , 'work' ), STRATA_SHUTDOWN_TOKEN :secret }, stdio :'ignore' , windowsHide :true }); 48const base = `http://127.0.0.1: ${ port } ` ; 49try { 50for ( let i = 0 ; i < 100 ; i ++ ) { try { if (( await fetch ( base + '/api/skills' )). ok ) break ; } catch { /* not up */ } await new Promise ( r => setTimeout ( r , 100 )); } 51const picture = join ( home , 'qa.png' ); await writeFile ( picture , Buffer . from ([ 137 , 80 , 78 , 71 ])); 52const post = ( headers ) => fetch ( base + '/api/app/drop' , { method :'POST' , headers :{ 'content-type' :'application/json' , ...headers }, body :JSON . stringify ({ paths :[ picture ] }) }); 53assert . equal (( await post ({})). status , 403 , 'a page cannot hand over paths' ); 54assert . equal (( await post ({ 'x-strata-shutdown' :'b' . repeat ( 40 ) })). status , 403 ); 55const ok = await post ({ 'x-strata-shutdown' :secret }); 56assert . equal ( ok . status , 200 ); 57const [ file ] = ( await ok . json ()). files ; 58assert . deepEqual ([ file . name , file . type , file . size ], [ 'qa.png' , 'image/png' , 4 ]); 59const first = await fetch ( ` ${ base } /api/app/drop/ ${ file . id } ` ); 60assert . equal ( first . status , 200 ); assert . equal ( first . headers . get ( 'content-type' ), 'image/png' ); 61assert . deepEqual ([ ...new Uint8Array ( await first . arrayBuffer ())], [ 137 , 80 , 78 , 71 ]); 62assert . equal (( await fetch ( ` ${ base } /api/app/drop/ ${ file . id } ` )). status , 404 , 'once' ); 63assert . equal (( await fetch ( ` ${ base } /api/app/drop/not-an-id` )). status , 404 ); 64} finally { child . kill (); await new Promise ( r => setTimeout ( r , 300 )); await rm ( home , { recursive :true , force :true , maxRetries :3 }). catch (() => {}); } 65}); 66 67test ( 'the page reads what the shell announced and makes Files of it' , async () => { 68const id = '123e4567-e89b-12d3-a456-426614174000' ; 69assert . deepEqual ( readDroppedRefs ([{ id, name :'a.png' , type :'image/png' , size :3 }, { id :'bad' , name :'x' }, { id, name :'' }, null , 7 ]), [{ id, name :'a.png' , type :'image/png' }]); 70assert . deepEqual ( readDroppedRefs ( 'nope' ), []); 71const files = await fetchDropped ([{ id, name :'a.png' , type :'image/png' }, { id :id . replace ( '0' , '1' ), name :'gone.png' , type :'image/png' }], 72async url => url . endsWith ( id ) ?{ ok :true , blob :async () => new Blob ([ new Uint8Array ([ 1 , 2 ])]) } :{ ok :false , blob :async () => new Blob ([]) }); 73assert . deepEqual ( files . map ( f => [ f . name , f . type , f . size ]), [[ 'a.png' , 'image/png' , 2 ]]); 74assert . equal ( dragHasFiles ([ 'text/uri-list' , 'text/html' ]), true ); assert . equal ( dragHasFiles ([ 'Files' ]), true ); assert . equal ( dragHasFiles ([ 'text/plain' ]), false ); assert . equal ( dragHasFiles ( null ), false ); 75}); 76 77test ( 'the window passes the drop on (Linux and macOS) and ignores a file dropped outside the chat' , async () => { 78const rust = await readFile ( new URL ( '../../desktop/src-tauri/src/main.rs' , import . meta. url ), 'utf8' ); 79assert . match ( rust , / #\[cfg\(windows\)\]\s + let builder = builder\.disable_drag_drop_handler\(\); / ); 80assert . match ( rust , / window\.on_webview_event\( / ); assert . match ( rust , / \/api\/app\/drop / ); 81const zone = await readFile ( new URL ( '../src/components/DropZone.tsx' , import . meta. url ), 'utf8' ); 82assert . match ( zone , / NATIVE_DROP_EVENT / ); assert . match ( zone , / export function installDropGuard / ); 83assert . match ( await readFile ( new URL ( '../src/main.tsx' , import . meta. url ), 'utf8' ), / installDropGuard\(\); / ); 84});