okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// O3 (tools and images review, 2026-10-22): a Markdown image in an answer renders as a picture when it is already on this computer or inside 2// the message, and never fetches anything from the internet by default. 3import assert from 'node:assert/strict' ; 4import test from 'node:test' ; 5import { resolve } from 'node:path' ; 6import { createRequire } from 'node:module' ; 7import { build } from 'esbuild' ; 8 9const root = resolve ( import . meta. dirname , '..' ); 10const PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==' ; 11 12async function load () { 13const bundle = await build ({ 14stdin :{ contents :`import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { Markdown } from './src/components/Markdown.tsx'; import { imagePlan } from './src/lib/markdown-images.ts'; export { imagePlan }; export const render = (text, imageSession) => renderToStaticMarkup(React.createElement(Markdown, { text, imageSession }));` , resolveDir :root }, 15bundle :true , write :false , platform :'node' , format :'cjs' , jsx :'automatic' , external :[ 'katex' , 'katex/dist/katex.min.css' ], 16plugins :[{ name :'copy-control-stub' , setup ( b ) { 17b . onResolve ({ filter :/ ^\.\/ui$ / }, () => ({ path :'ui' , namespace :'test-stub' })); 18b . onLoad ({ filter :/ . */ , namespace :'test-stub' }, () => ({ contents :'export const IconBtn = () => null; export const Btn = () => null; export const Icon = () => null;' })); 19} }], 20}); 21const mod = { exports :{} }; 22new Function ( 'require' , 'module' , 'exports' , bundle . outputFiles [ 0 ]. text )( createRequire ( import . meta. url ), mod , mod . exports ); 23return mod . exports ; 24} 25 26test ( 'a data: PNG, JPEG, WebP or GIF renders as an <img>' , async () => { 27const { render} = await load (); 28const html = render ( `Here it is:  done` ); 29assert . match ( html , / <img [ ^ > ] * src="data:image\/png;base64, / ); 30assert . match ( html , / alt="dot" / ); 31assert . match ( html , / referrerPolicy | referrerpolicy="no-referrer" / i ); 32for ( const type of [ 'jpeg' , 'jpg' , 'webp' , 'gif' ]) assert . match ( render ( `` ), / <img / , type ); 33}); 34 35test ( 'SVG data, script addresses, file: and other schemes never become an <img>' , async () => { 36const { render} = await load (); 37for ( const src of [ 'data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=' , 'data:text/html;base64,QUJD' , 'javascript:alert(1)' , 'file:///C:/secret.png' , 38'blob:https://x/1' , 'ftp://x/y.png' , '//host/share/x.png' , '\\\\host\\share\\x.png' , 'data:image/png;base64,QUJD" onerror="alert(1)' ]) { 39const html = render ( `` , 'chat-1' ); 40assert . ok ( ! /<img / . test ( html ), src ); 41assert . ok ( ! /<a / . test ( html ), src ); 42} 43}); 44 45test ( 'a remote picture is a link, not a request: nothing is fetched from the internet by default' , async () => { 46const { render, imagePlan} = await load (); 47const html = render ( '' , 'chat-1' ); 48assert . ok ( ! /<img / . test ( html )); 49assert . match ( html , / <a [ ^ > ] * href="https:\/\/example.com\/sales.png" [ ^ > ] * >Monthly sales<\/a> / ); 50assert . deepEqual ( imagePlan ( 'http://example.com/a.png' ), { kind :'link' , href :'http://example.com/a.png' }); 51}); 52 53test ( 'a path in the chat folder is read through the host, and only when the chat is known' , async () => { 54const { render, imagePlan} = await load (); 55const html = render ( '' , 'chat 1' ); 56assert . match ( html , / <img [ ^ > ] * src="\/api\/files\/raw\?sessionId=chat%201&path=plots%2Fsales.png" / ); 57assert . match ( render ( '' , 'c1' ), / <img [ ^ > ] * path=C%3A%5Cwork%5Csales.png / ); 58assert . ok ( ! /<img / . test ( render ( '' ))); // no chat: shown as its alt text 59assert . match ( render ( '' ), / chart / ); 60assert . deepEqual ( imagePlan ( '/api/files/raw?sessionId=a&path=b' ), { kind :'img' , src :'/api/files/raw?sessionId=a&path=b' }); 61}); 62 63test ( 'links, code and emphasis around a picture are unchanged, and the old text-with-a-bang is gone' , async () => { 64const { render} = await load (); 65const html = render ( 'See [the report](https://example.com/r) and `code` and **bold**. ' ); 66assert . match ( html , / <a [ ^ > ] * >the report<\/a> / ); 67assert . match ( html , / <code class="md-code">code<\/code> / ); 68assert . match ( html , / <strong>bold<\/strong> / ); 69assert . ok ( ! html . includes ( '>!<' ) && ! /!<a / . test ( html )); 70assert . ok ( ! /<img / . test ( render ( '``' ))); // inside a code span it stays code 71}); 72 73test ( 'every incomplete stream prefix of an answer with a picture renders' , async () => { 74const { render} = await load (); 75const text = `Chart:\n\n\n\nand  end` ; 76for ( let i = 0 ; i <= text . length ; i += 7 ) assert . equal ( typeof render ( text . slice ( 0 , i ), 'c1' ), 'string' ); 77});