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 , readFileSync , existsSync } from 'node:fs' ; 4import { tmpdir } from 'node:os' ; 5import { join } from 'node:path' ; 6import { createUploads , parseMultipart } from '../server/chat-uploads.mjs' ; 7import { createAttachments } from '../server/chat-files.mjs' ; 8import { imageParts } from '../server/agent/agent-bridge.mjs' ; 9 10const PNG = Buffer . concat ([ Buffer . from ([ 137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 ]), Buffer . alloc ( 40 , 1 )]); 11const multipart = ( parts , boundary = 'XBOUND' ) => ({ 12contentType :'multipart/form-data; boundary=' + boundary , 13body :Buffer . concat ([ ...parts . flatMap ( p => [ Buffer . from ( '--' + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + p . name + '"\r\nContent-Type: ' + p . type + '\r\n\r\n' ), p . bytes , Buffer . from ( '\r\n' )]), Buffer . from ( '--' + boundary + '--\r\n' )]), 14}); 15 16test ( 'pictures and files arrive as multipart, a data address or raw bytes; pictures get an id, files land in the chat folder' , () => { 17const dir = mkdtempSync ( join ( tmpdir (), 'strata-up-' )); 18try { 19const proj = join ( dir , 'proj' ); mkdirSync ( proj ); 20const att = createAttachments (); 21const up = createUploads ({ dir :join ( dir , 'uploads' ), folderOf :() => proj , attachments :att }); 22const m = multipart ([{ name :'a.png' , type :'image/png' , bytes :PNG }, { name :'notes.md' , type :'text/markdown' , bytes :Buffer . from ( '# hi\r\nthere' ) }]); 23assert . equal ( parseMultipart ( m . body , m . contentType ). length , 2 ); 24const out = up . accept ( 'c1' , m ); 25assert . equal ( out [ 0 ]. kind , 'image' ); assert . match ( out [ 0 ]. url , / ^\/api\/chat\/upload\/ [ 0 - 9a - f- ]{ 36 } $ / ); 26assert . equal ( out [ 1 ]. kind , 'file' ); assert . ok ( existsSync ( join ( proj , '.strata' , 'attachments' , 'notes.md' ))); assert . deepEqual ( att . list ( 'c1' ), [ out [ 1 ]. path ]); 27assert . equal ( readFileSync ( out [ 1 ]. path , 'utf8' ), '# hi\r\nthere' , 'the bytes are kept exactly' ); 28const j = up . accept ( 'c1' , { body :Buffer . from ( JSON . stringify ({ name :'p.png' , dataUrl :'data:image/png;base64,' + PNG . toString ( 'base64' ) })), contentType :'application/json' }); 29assert . equal ( j [ 0 ]. kind , 'image' ); 30const raw = up . accept ( 'c1' , { body :PNG , contentType :'image/png' , name :'x.png' }); 31assert . equal ( up . image ( raw [ 0 ]. id ). type , 'image/png' ); 32assert . equal ( up . dataUrls ( 'c1' , [ raw [ 0 ]. id ])[ 0 ]. url . slice ( 0 , 22 ), 'data:image/png;base64,' ); 33assert . throws (() => up . dataUrls ( 'other-chat' , [ raw [ 0 ]. id ]), / no longer available / , 'a picture belongs to its chat' ); 34assert . throws (() => up . dataUrls ( 'c1' , [ '1' , '2' , '3' , '4' , '5' ]), / up to 4 / ); 35// the first bytes decide: a renamed non-picture is a file, a picture type the model cannot read is refused 36assert . equal ( up . accept ( 'c1' , { body :Buffer . from ( 'plain' ), contentType :'text/plain' , name :'renamed.png' })[ 0 ]. kind , 'file' ); 37assert . throws (() => up . accept ( 'c1' , { body :Buffer . from ( 'plain' ), contentType :'image/png' , name :'fake.png' }), / cannot read / ); 38assert . throws (() => up . accept ( 'c1' , { body :Buffer . from ( 'GIF89a....' ), contentType :'image/gif' , name :'a.gif' }), / PNG, JPEG or WebP / ); 39const bigPng = Buffer . concat ([ PNG , Buffer . alloc ( 7 * 1024 * 1024 )]); 40assert . throws (() => up . accept ( 'c1' , { body :bigPng , contentType :'image/png' , name :'big.png' }), / bigger than 6 MB / ); 41assert . throws (() => up . accept ( 'c1' , { body :Buffer . alloc ( 13 * 1024 * 1024 ), contentType :'application/pdf' , name :'huge.pdf' }), / bigger than 12 MB / ); 42const evil = up . accept ( 'c1' , { body :Buffer . from ( 'x' ), contentType :'text/plain' , name :'..' + String . fromCharCode ( 92 ) + '..' + String . fromCharCode ( 92 ) + 'evil.txt' }); 43assert . ok ( evil [ 0 ]. path . startsWith ( join ( proj , '.strata' , 'attachments' )), 'a file name cannot climb out of the attachments folder' ); 44} finally { rmSync ( dir , { recursive :true , force :true }); } 45}); 46 47test ( 'pictures become the Harness image parts; anything else is dropped' , () => { 48const parts = imageParts ([{ url :'data:image/png;base64,AAAA' , name :'a.png' }, { url :'data:image/gif;base64,AAAA' }, { url :'nope' }, null ]); 49assert . deepEqual ( parts , [{ type :'image' , mediaType :'image/png' , data :'AAAA' , name :'a.png' }]); 50assert . deepEqual ( imageParts ( undefined ), []); 51});