okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
788e0b731
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { mkdtempSync , mkdirSync , rmSync , writeFileSync } from 'node:fs' ; 4import { tmpdir } from 'node:os' ; 5import { join } from 'node:path' ; 6import { previewFile , rawFile , parseCsv , createAttachments , withAttachments } from '../server/chat-files.mjs' ; 7 8const NL = String . fromCharCode ( 10 ); 9 10test ( 'previews: html and markdown as text, csv as a table, pictures and pdf as raw addresses, nothing outside the folder' , () => { 11const dir = mkdtempSync ( join ( tmpdir (), 'strata-pv-' )); 12try { 13const root = join ( dir , 'p' ); mkdirSync ( root ); 14writeFileSync ( join ( root , 'a.html' ), '<h1>x</h1>' ); writeFileSync ( join ( root , 'n.md' ), '# T' ); writeFileSync ( join ( root , 'd.csv' ), [ 'name,qty' , '"Tea, green",2' , '"say ""hi""",3' , '' ]. join ( NL )); 15writeFileSync ( join ( root , 'p.png' ), Buffer . from ([ 137 , 80 , 78 , 71 , 0 ])); writeFileSync ( join ( root , 'x.pdf' ), Buffer . from ( '%PDF-1.4' )); writeFileSync ( join ( root , 'z.zip' ), Buffer . from ([ 80 , 75 , 0 , 0 ])); writeFileSync ( join ( dir , 'secret.txt' ), 's' ); 16assert . deepEqual ([ previewFile ( root , 'a.html' , 's' ). kind , previewFile ( root , 'a.html' , 's' ). text ], [ 'html' , '<h1>x</h1>' ]); 17assert . equal ( previewFile ( root , 'n.md' , 's' ). text , '# T' ); 18assert . deepEqual ( previewFile ( root , 'd.csv' , 's' ). rows , [[ 'name' , 'qty' ], [ 'Tea, green' , '2' ], [ 'say "hi"' , '3' ]]); 19const png = previewFile ( root , 'p.png' , 'chat 1' ); assert . equal ( png . mime , 'image/png' ); assert . match ( png . rawUrl , / ^\/api\/files\/raw\?sessionId=chat%201&path= / ); 20assert . equal ( previewFile ( root , 'x.pdf' , 's' ). mime , 'application/pdf' ); 21assert . match ( previewFile ( root , 'z.zip' , 's' ). note , / no preview / ); 22assert . equal ( rawFile ( root , 'p.png' ). mime , 'image/png' ); assert . throws (() => rawFile ( root , 'a.html' ), / Only pictures and PDFs / ); 23assert . throws (() => previewFile ( root , join ( '..' , 'secret.txt' ), 's' ), / outside / ); 24assert . throws (() => rawFile ( root , join ( '..' , 'secret.txt' )), / outside / ); 25assert . deepEqual ( parseCsv ( 'a;b\n1;2' , ';' ), [[ 'a' , 'b' ], [ '1' , '2' ]]); 26} finally { rmSync ( dir , { recursive :true , force :true }); } 27}); 28 29test ( 'attach as context: the files are named in the next message once, then forgotten' , () => { 30const a = createAttachments (); 31a . add ( 'c' , '/p/a.html' ); a . add ( 'c' , '/p/a.html' ); a . add ( 'c' , '/p/with space.md' ); 32assert . deepEqual ( a . list ( 'c' ), [ '/p/a.html' , '/p/with space.md' ]); 33assert . equal ( withAttachments ( 'improve it' , a . take ( 'c' )), '@/p/a.html @"/p/with space.md" improve it' ); 34assert . deepEqual ( a . take ( 'c' ), []); assert . equal ( withAttachments ( 'hi' , []), 'hi' ); 35a . add ( 'd' , 'x' ); assert . deepEqual ( a . remove ( 'd' , 'x' ), []); 36});