okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFile , mkdtemp , mkdir , writeFile , symlink , rm , realpath } from 'node:fs/promises' ; 4import { tmpdir } from 'node:os' ; 5import { join } from 'node:path' ; 6import { transform } from 'esbuild' ; 7import { openArtifact } from '../server/artifacts.mjs' ; 8const code = await readFile ( new URL ( '../src/lib/chat-metrics.ts' , import . meta. url ), 'utf8' ); 9const { code :js } = await transform ( code ,{ loader :'ts' , format :'esm' }); 10const { chatMetrics} = await import ( `data:text/javascript;base64, ${ Buffer . from ( js ). toString ( 'base64' )} ` ); 11const { code :previewJs } = await transform ( await readFile ( new URL ( '../src/lib/preview-tabs.ts' , import . meta. url ), 'utf8' ),{ loader :'ts' , format :'esm' }); 12const { closePreviewTab} = await import ( `data:text/javascript;base64, ${ Buffer . from ( previewJs ). toString ( 'base64' )} ` ); 13test ( 'closing a preview preserves selection or selects its nearest remaining neighbor' ,() => { 14const tabs = [{ id :'a' },{ id :'b' },{ id :'c' }]; 15assert . deepEqual ( closePreviewTab ( tabs , 'b' , 'a' ),{ items :[{ id :'b' },{ id :'c' }], selected :'b' }); 16assert . equal ( closePreviewTab ( tabs , 'a' , 'a' ). selected , 'b' ); 17assert . equal ( closePreviewTab ( tabs , 'b' , 'b' ). selected , 'c' ); 18assert . equal ( closePreviewTab ( tabs , 'c' , 'c' ). selected , 'b' ); 19assert . deepEqual ( closePreviewTab ([{ id :'a' }], 'a' , 'a' ),{ items :[], selected :null }); 20assert . deepEqual ( tabs ,[{ id :'a' },{ id :'b' },{ id :'c' }]); 21}); 22const session = { id :'chat-a' , statsAvailable :true , tokensIn :1000 , tokensOut :100 , messages :[{ role :'user' , content :'hi' },{ role :'assistant' , inputTokens :1000 , tokens :100 , contextMax :10000 , tps :50 , ttftMs :200 , durationMs :2000 , tools :[{ durationMs :100 }]}]}; 23test ( 'chat metrics ignore another session and all-request totals' ,() => { 24const m = chatMetrics ( session ,{ phase :'generating' , outputTps :900 , totals :{ tokensIn :80000000 }, context :{ sessionId :'chat-b' , usedTokens :9500 , maxTokens :10000 }}); 25assert . equal ( m . total , 1100 ); assert . equal ( m . speed , 50 ); assert . equal ( m . pct , 11 ); assert . equal ( m . ttft , 200 ); assert . equal ( m . toolMs , 100 ); 26const active = chatMetrics ( session ,{ phase :'generating' , outputTps :60 , context :{ sessionId :'chat-a' , usedTokens :2000 , maxTokens :10000 }}); 27assert . equal ( active . speed , 60 ); assert . equal ( active . pct , 20 ); 28assert . equal ( chatMetrics ({ ...session , statsAvailable :false }, null ). total , null ); 29assert . equal ( chatMetrics ( null , null ). pct , null ); 30}); 31test ( 'native file actions use explicit argument arrays and reject traversal and symlinks' , async () => { 32const root = await mkdtemp ( join ( tmpdir (), 'strata-artifact-test-' )); 33try { 34const examplesDir = join ( root , 'examples' ), previewsDir = join ( root , 'copies' ); await mkdir ( examplesDir ); 35await writeFile ( join ( examplesDir , 'a file.md' ), 'Example' ); await writeFile ( join ( root , 'private.md' ), 'Outside' ); await symlink ( join ( root , 'private.md' ), join ( examplesDir , 'outside.md' )); 36const calls = []; const deps = { examplesDir, previewsDir, platform :'darwin' , launch :async ( ...args ) => calls . push ( args )}; 37await openArtifact ({ action :'reveal' , path :'/examples/a file.md' }, deps ); 38assert . deepEqual ( calls [ 0 ],[ 'open' ,[ '-R' , await realpath ( join ( examplesDir , 'a file.md' ))]]); 39await openArtifact ({ action :'open' , name :'../notes.md' , content :'Exported preview' }, deps ); 40assert . equal ( await readFile ( calls [ 1 ][ 1 ][ 0 ], 'utf8' ), 'Exported preview' ); 41await assert . rejects ( openArtifact ({ action :'open' , path :'/examples/../private.md' }, deps )); 42await assert . rejects ( openArtifact ({ action :'open' , path :'/examples/outside.md' }, deps )); 43await assert . rejects ( openArtifact ({ action :'open' , path :'/etc/passwd' }, deps )); 44await assert . rejects ( openArtifact ({ action :'open' , name :'bad.app' , content :'No' }, deps )); 45assert . equal ( calls . length , 2 ); 46} finally { await rm ( root ,{ recursive :true , force :true });} 47}); 48 49test ( 'API replies use their own context limit and never inherit local engine readings' ,() => { 50const remote = { ...session , messages :[ ...session . messages ,{ role :'assistant' , remoteModel :true , inputTokens :50 , tokens :10 , contextMax :64000 , tps :12 }]}; 51const monitor = { phase :'generating' , outputTps :900 , context :{ sessionId :'chat-a' , usedTokens :9000 , maxTokens :10000 }}; 52const metrics = chatMetrics ( remote , monitor , 32768 ); 53assert . equal ( metrics . max , 64000 ); assert . equal ( metrics . used , 60 ); assert . equal ( metrics . speed , 12 ); 54assert . equal ( chatMetrics ({ ...remote , messages :[{ role :'assistant' , remoteModel :true , inputTokens :50 , tokens :10 }]}, monitor , 32768 ). max , null ); 55}); 56 57test ( 'a web address from a reply opens in the default browser with an argument array, and only http(s) (#4721)' , async () => { 58const calls = []; const launch = async ( ...args ) => calls . push ( args ); 59await openArtifact ({ action :'open' , url :'https://github.com/Niko1221/Strata/issues/3818?a=1&b=2' },{ platform :'win32' , launch}); 60assert . deepEqual ( calls [ 0 ],[ 'rundll32.exe' ,[ 'url.dll,FileProtocolHandler' , 'https://github.com/Niko1221/Strata/issues/3818?a=1&b=2' ]]); 61await openArtifact ({ action :'open' , url :'http://example.com/x' },{ platform :'linux' , launch}); 62assert . deepEqual ( calls [ 1 ],[ 'xdg-open' ,[ 'http://example.com/x' ]]); 63await openArtifact ({ action :'open' , url :'http://example.com/x' },{ platform :'darwin' , launch}); 64assert . deepEqual ( calls [ 2 ],[ 'open' ,[ 'http://example.com/x' ]]); 65for ( const bad of [ 'file:///C:/Windows/system32/calc.exe' , 'javascript:alert(1)' , 'ms-msdt:/id x' , 'not a url' , 'https://x.test/' + 'a' . repeat ( 5000 )]) 66await assert . rejects ( openArtifact ({ action :'open' , url :bad },{ platform :'win32' , launch})); 67await assert . rejects ( openArtifact ({ action :'reveal' , url :'https://example.com/' },{ platform :'win32' , launch})); 68assert . equal ( calls . length , 3 ); 69});