okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c163678f7
main
1/** Item 4: the Compare view can show pictures side by side, so the host lists them as pictures and serves them safely. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from 'node:fs' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { diffRun , imageTypeOf , runImagePath } from '../server/timeskip.mjs' ; 8 9function fixture () { 10const dir = mkdtempSync ( join ( tmpdir (), 'strata-cmp-' )); 11const base = join ( dir , 'base' ), upd = join ( dir , 'upd' ), outside = join ( dir , 'secret.png' ); 12mkdirSync ( join ( base , 'img' ), { recursive :true }); mkdirSync ( join ( upd , 'img' ), { recursive :true }); 13writeFileSync ( outside , 'not for you' ); 14writeFileSync ( join ( base , 'index.html' ), '<h1>Old</h1>' ); writeFileSync ( join ( upd , 'index.html' ), '<h1>New</h1>' ); 15writeFileSync ( join ( base , 'img' , 'logo.png' ), Buffer . from ([ 137 , 80 , 78 , 71 , 0 , 1 , 2 ])); writeFileSync ( join ( upd , 'img' , 'logo.png' ), Buffer . from ([ 137 , 80 , 78 , 71 , 0 , 9 , 9 , 9 ])); 16writeFileSync ( join ( base , 'same.png' ), Buffer . from ([ 137 , 80 , 78 , 71 , 0 ])); writeFileSync ( join ( upd , 'same.png' ), Buffer . from ([ 137 , 80 , 78 , 71 , 0 ])); 17writeFileSync ( join ( upd , 'new.svg' ), '<svg xmlns="http://www.w3.org/2000/svg"/>' ); 18const record = { checkpoints :[{ id :'cp1' , label :'baseline' , path :base }], selected_checkpoint :null , attempts :[{ index :1 , path :upd }] }; 19return { dir, record}; 20} 21 22test ( 'pictures are listed as pictures (changed ones only), text files keep their text' , () => { 23const { dir, record} = fixture (); 24try { 25const found = diffRun ( record ); 26const by = Object . fromEntries ( found . files . map ( f => [ f . path , f ])); 27assert . equal ( by [ 'img/logo.png' ]. image , true ); assert . equal ( by [ 'img/logo.png' ]. status , 'changed' ); assert . equal ( by [ 'img/logo.png' ]. original , undefined ); 28assert . equal ( by [ 'new.svg' ]. image , true ); assert . equal ( by [ 'new.svg' ]. status , 'added' ); 29assert . equal ( by [ 'same.png' ], undefined , 'an unchanged picture is not listed' ); 30assert . equal ( by [ 'index.html' ]. original , '<h1>Old</h1>' ); assert . equal ( by [ 'index.html' ]. updated , '<h1>New</h1>' ); 31} finally { rmSync ( dir , { recursive :true , force :true }); } 32}); 33 34test ( 'a picture is served only from inside the run, only as a picture' , () => { 35const { dir, record} = fixture (); 36try { 37assert . ok ( runImagePath ( record , 'img/logo.png' , 'original' )?. endsWith ( 'logo.png' )); 38assert . ok ( runImagePath ( record , 'img/logo.png' , 'updated' )); 39assert . equal ( runImagePath ( record , 'img/logo.png' , 'other' ), null ); 40assert . equal ( runImagePath ( record , '../secret.png' , 'original' ), null , 'no way out of the folder' ); 41assert . equal ( runImagePath ( record , 'img/../../secret.png' , 'updated' ), null ); 42assert . equal ( runImagePath ( record , 'index.html' , 'updated' ), null , 'a page is not a picture' ); 43assert . equal ( runImagePath ( record , 'missing.png' , 'updated' ), null ); 44assert . equal ( imageTypeOf ( 'a/b/C.JPG' ), 'image/jpeg' ); assert . equal ( imageTypeOf ( 'a.txt' ), null ); 45} finally { rmSync ( dir , { recursive :true , force :true }); } 46}); 47 48test ( 'the page that was made comes first; a browser profile and other housekeeping come last, without their contents' , () => { 49const dir = mkdtempSync ( join ( tmpdir (), 'strata-ts-rank-' )); 50try { 51const base = join ( dir , 'base' ), cand = join ( dir , 'cand' ); 52for ( const root of [ base , cand ]) { mkdirSync ( join ( root , '.chrome-profile' , 'Default' , 'Cache' ), { recursive :true }); mkdirSync ( join ( root , 'notes' ), { recursive :true }); } 53for ( let i = 0 ; i < 12 ; i ++ ) { writeFileSync ( join ( base , '.chrome-profile' , 'Default' , 'Cache' , 'f' + i ), 'a' + i ); writeFileSync ( join ( cand , '.chrome-profile' , 'Default' , 'Cache' , 'f' + i ), 'b' + i ); } 54writeFileSync ( join ( cand , 'voxel-pagoda.html' ), '<canvas></canvas>' ); 55writeFileSync ( join ( base , 'notes' , 'a.md' ), 'old' ); writeFileSync ( join ( cand , 'notes' , 'a.md' ), 'new' ); 56const found = diffRun ({ checkpoints :[{ id :'cp1-baseline' , label :'baseline' , path :base }], selected_checkpoint :'cp1-baseline' , attempts :[{ index :1 , path :cand }] }); 57assert . deepEqual ( found . files . slice ( 0 , 2 ). map ( f => f . path ), [ 'voxel-pagoda.html' , 'notes/a.md' ], 'the page first, then other readable work' ); 58assert . equal ( found . files [ 0 ]. updated , '<canvas></canvas>' ); 59const noise = found . files . filter ( f => f . noise ); 60assert . equal ( noise . length , 5 , 'only a few housekeeping files are listed' ); assert . ok ( noise . every ( f => f . unreadable && f . original === undefined )); 61assert . equal ( found . more , 7 , 'the rest is counted, not listed' ); 62assert . ok ( found . files . findIndex ( f => f . noise ) > 1 , 'housekeeping is last' ); 63rmSync ( join ( cand , 'voxel-pagoda.html' )); writeFileSync ( join ( cand , 'notes' , 'a.md' ), 'old' ); 64const onlyNoise = diffRun ({ checkpoints :[{ id :'cp1-baseline' , label :'baseline' , path :base }], selected_checkpoint :'cp1-baseline' , attempts :[{ index :1 , path :cand }] }); 65assert . deepEqual ( onlyNoise . files , [], 'when only housekeeping changed nothing is shown as changed' ); assert . equal ( onlyNoise . housekeeping , 12 ); 66} finally { rmSync ( dir , { recursive :true , force :true }); } 67}); 68 69test ( 'a big file that did not change is not listed as changed; one that did is' , () => { 70const dir = mkdtempSync ( join ( tmpdir (), 'strata-ts-big-' )); 71try { 72const base = join ( dir , 'base' ), cand = join ( dir , 'cand' ); mkdirSync ( base ); mkdirSync ( cand ); 73const big = Buffer . alloc ( 400 * 1024 , 7 ); 74writeFileSync ( join ( base , 'archive.bin' ), big ); writeFileSync ( join ( cand , 'archive.bin' ), big ); 75writeFileSync ( join ( base , 'data.json' ), Buffer . alloc ( 300 * 1024 , 65 )); writeFileSync ( join ( cand , 'data.json' ), Buffer . alloc ( 300 * 1024 , 66 )); 76writeFileSync ( join ( cand , 'page.html' ), '<p>x</p>' ); 77const found = diffRun ({ checkpoints :[{ id :'cp1-baseline' , label :'baseline' , path :base }], selected_checkpoint :'cp1-baseline' , attempts :[{ index :1 , path :cand }] }); 78assert . deepEqual ( found . files . map ( f => f . path ), [ 'page.html' , 'data.json' ], 'the untouched big file is gone from the list; the changed one is last (too big to show)' ); 79assert . equal ( found . files [ 1 ]. unreadable , true ); 80} finally { rmSync ( dir , { recursive :true , force :true }); } 81});