okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
743a6b3c6
main
1/** Item 4: the A versus B view adapts to the kind of file and keeps one window size. The pure parts. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFile } from 'node:fs/promises' ; 5import { build } from 'esbuild' ; 6import { fileURLToPath } from 'node:url' ; 7 8async function load ( file ) { 9const result = await build ({ entryPoints :[ fileURLToPath ( new URL ( `../src/lib/ ${ file } ` , import . meta. url ))], bundle :true , format :'esm' , platform :'node' , write :false , alias :{ react :fileURLToPath ( new URL ( './helpers/react-stub.mjs' , import . meta. url )) }, logLevel :'silent' , loader :{ '.json' :'json' } }); 10return import ( `data:text/javascript;base64, ${ Buffer . from ( result . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 11} 12globalThis . window ??= globalThis ; globalThis . localStorage ??= { getItem :() => null , setItem :() => {} }; 13globalThis . navigator ??= { language :'en' }; 14const cmp = await load ( 'time-skip-compare.ts' ); 15const act = await load ( 'time-skip-activity.ts' ); 16 17test ( 'each kind of file gets its own view' , () => { 18const k = ( path , file ) => cmp . fileKind ( path , file ); 19assert . equal ( k ( 'notes/plan.md' ), 'markdown' ); assert . equal ( k ( 'a.txt' ), 'text' ); assert . equal ( k ( 'site/index.html' ), 'html' ); assert . equal ( k ( 'b.HTM' ), 'html' ); 20assert . equal ( k ( 'img/logo.PNG' ), 'image' ); assert . equal ( k ( 'x.svg' ), 'image' ); assert . equal ( k ( 'weird' , { image :true }), 'image' ); 21assert . equal ( k ( 'src/app.py' ), 'code' ); assert . equal ( k ( 'data.bin' , { unreadable :true }), 'other' ); 22assert . equal ( k ( 'photo.jpg' , { unreadable :true }), 'image' , 'a picture is not "unreadable": it is shown from the file' ); 23}); 24 25test ( 'changed words are marked, unchanged text is identical on both sides, and nothing is lost' , () => { 26const a = 'Dear Hiring Manager, I am writing to express my sincere interest in the role.' ; 27const b = 'Hello team, I am writing because I love the role.' ; 28const d = cmp . wordDiff ( a , b ); 29assert . equal ( d . original . map ( s => s . text ). join ( '' ), a ); assert . equal ( d . updated . map ( s => s . text ). join ( '' ), b ); 30assert . ok ( d . original . some ( s => s . changed && / Dear / . test ( s . text )) && d . updated . some ( s => s . changed && / Hello / . test ( s . text ))); 31assert . ok ( d . original . some ( s => ! s . changed && / I am writing / . test ( s . text )), 'what stayed is not marked' ); 32assert . deepEqual ( cmp . wordDiff ( 'same text' , 'same text' ). updated , [{ text :'same text' , changed :false }]); 33const lines = cmp . segmentLines ( cmp . wordDiff ( '# Title\n- one\n' , '# Title\n- two\n' ). updated ); 34assert . equal ( lines [ 0 ]. map ( s => s . text ). join ( '' ), '# Title' ); assert . equal ( lines [ 1 ]. map ( s => s . text ). join ( '' ), '- two' ); 35assert . ok ( lines [ 1 ]. some ( s => s . changed )); 36}); 37 38test ( 'a huge pair of texts does not freeze the page: the work is capped' , () => { 39const big = Array . from ({ length :4000 }, ( _ , i ) => `word ${ i } ` ). join ( ' ' ); 40const t = Date . now (); 41const d = cmp . wordDiff ( big , big + ' extra' ); 42assert . ok ( Date . now () - t < 3000 ); 43assert . ok ( d . updated . length >= 1 ); 44}); 45 46test ( 'code and other files get a short line diff with the unchanged stretches folded' , () => { 47const a = Array . from ({ length :40 }, ( _ , i ) => `line ${ i } ` ). join ( '\n' ); 48const b = a . replace ( 'line 20' , 'line twenty' ) + '\nline 40' ; 49const rows = cmp . lineDiff ( a , b ); 50const stats = cmp . diffStats ( rows ); 51assert . deepEqual ( stats , { added :2 , removed :1 }); 52assert . ok ( rows . length < 14 , `folded to ${ rows . length } rows` ); 53assert . ok ( rows . some ( r => r . t === 'gap' && r . n > 10 )); 54assert . deepEqual ( cmp . lineDiff ( '' , 'a\nb' ). map ( r => r . t ), [ 'add' , 'add' ], 'a new file is all additions' ); 55assert . deepEqual ( cmp . lineDiff ( 'a' , '' ). map ( r => r . t ), [ 'del' ], 'a removed file is all removals' ); 56}); 57 58test ( 'an HTML page is shown with no way to reach out: a policy is added to the page itself' , () => { 59const withHead = cmp . htmlPreviewDocument ( '<html><head><title>x</title></head><body><script>alert(1)</script><img src="https://evil.example/a.png"></body></html>' ); 60assert . match ( withHead , / Content-Security-Policy / ); assert . match ( withHead , / default-src 'none' / ); assert . ok ( withHead . indexOf ( 'Content-Security-Policy' ) < withHead . indexOf ( '<script>' ), 'the policy comes first' ); 61const bare = cmp . htmlPreviewDocument ( '<h1>Hi</h1>' ); 62assert . match ( bare , / <body><h1>Hi<\/h1><\/body> / ); assert . match ( bare , / Content-Security-Policy / ); 63assert . equal ( cmp . timeSkipImageUrl ( 'abc12345' , 'img/a b.png' , 'updated' ), '/api/timeskip/file?id=abc12345&path=img%2Fa%20b.png&version=updated' ); 64}); 65 66test ( 'the working view says what is happening, from the host\'s words or the phase' , async () => { 67assert . equal ( act . activityWords ({ phase :'generating' , activity :{ code :'trying-version' , text :'x' , args :{ step :'Writing notes.md…' } } }), 'Writing notes.md…' ); 68assert . equal ( act . activityWords ({ phase :'generating' , activity :{ code :'trying-version' , text :'x' } }), 'Trying an improved version…' ); 69assert . equal ( act . activityWords ({ phase :'checking' , activity :{ code :'checking-result' , text :'x' } }), 'Checking the result…' ); 70assert . equal ( act . activityWords ({ phase :'queued' }), 'Reading your files…' ); 71assert . equal ( act . activityWords ({ phase :'checking' }), 'Checking the result…' ); 72assert . ok ( act . phaseFraction ( 'generating' ) > act . phaseFraction ( 'checkpointing' ) && act . phaseFraction ( 'checking' ) > act . phaseFraction ( 'generating' )); 73const panel = await readFile ( new URL ( '../src/components/TimeSkipCompare.tsx' , import . meta. url ), 'utf8' ); 74// A page that draws itself with a script (a 3D scene) must run, or it shows as an empty dark page; the frame stays isolated and offline. 75assert . equal ( cmp . HTML_PREVIEW_SANDBOX , 'allow-scripts' ); 76assert . match ( panel , / sandbox= { HTML_PREVIEW_SANDBOX} / ); 77assert . ok ( ! /allow-same-origin | allow-forms | allow-popups | allow-top-navigation / . test ( panel + cmp . HTML_PREVIEW_SANDBOX )); 78const policy = cmp . htmlPreviewDocument ( '<p>x</p>' ); 79assert . match ( policy , / script-src 'unsafe-inline' / ); assert . ok ( ! /connect-src | https ? :\/\/ / . test ( policy . split ( '<body>' )[ 0 ]), 'nothing may be fetched' ); 80assert . equal ( cmp . needsOutsideFiles ( '<script src="https://cdn.example/three.js"></script>' ), true ); 81assert . equal ( cmp . needsOutsideFiles ( '<script>draw()</script><a href="https://x.example">link</a>' ), false ); 82});