okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
4.7 KiB77 linesraw
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() {
13  const bundle = await build({
14    stdin: { 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 },
15    bundle: true, write: false, platform: 'node', format: 'cjs', jsx: 'automatic', external: ['katex', 'katex/dist/katex.min.css'],
16    plugins: [{ name: 'copy-control-stub', setup(b) {
17      b.onResolve({ filter: /^\.\/ui$/ }, () => ({ path: 'ui', namespace: 'test-stub' }));
18      b.onLoad({ filter: /.*/, namespace: 'test-stub' }, () => ({ contents: 'export const IconBtn = () => null; export const Btn = () => null; export const Icon = () => null;' }));
19    } }],
20  });
21  const mod = { exports: {} };
22  new Function('require', 'module', 'exports', bundle.outputFiles[0].text)(createRequire(import.meta.url), mod, mod.exports);
23  return mod.exports;
24}
25
26test('a data: PNG, JPEG, WebP or GIF renders as an <img>', async () => {
27  const { render } = await load();
28  const html = render(`Here it is: ![dot](${PNG}) done`);
29  assert.match(html, /<img [^>]*src="data:image\/png;base64,/);
30  assert.match(html, /alt="dot"/);
31  assert.match(html, /referrerPolicy|referrerpolicy="no-referrer"/i);
32  for (const type of ['jpeg', 'jpg', 'webp', 'gif']) assert.match(render(`![x](data:image/${type};base64,QUJD)`), /<img /, type);
33});
34
35test('SVG data, script addresses, file: and other schemes never become an <img>', async () => {
36  const { render } = await load();
37  for (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)']) {
39    const html = render(`![x](${src})`, 'chat-1');
40    assert.ok(!/<img /.test(html), src);
41    assert.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 () => {
46  const { render, imagePlan } = await load();
47  const html = render('![Monthly sales](https://example.com/sales.png)', 'chat-1');
48  assert.ok(!/<img /.test(html));
49  assert.match(html, /<a [^>]*href="https:\/\/example.com\/sales.png"[^>]*>Monthly sales<\/a>/);
50  assert.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 () => {
54  const { render, imagePlan } = await load();
55  const html = render('![chart](plots/sales.png)', 'chat 1');
56  assert.match(html, /<img [^>]*src="\/api\/files\/raw\?sessionId=chat%201&amp;path=plots%2Fsales.png"/);
57  assert.match(render('![chart](C:\\work\\sales.png)', 'c1'), /<img [^>]*path=C%3A%5Cwork%5Csales.png/);
58  assert.ok(!/<img /.test(render('![chart](plots/sales.png)')));                 // no chat: shown as its alt text
59  assert.match(render('![chart](plots/sales.png)'), /chart/);
60  assert.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 () => {
64  const { render } = await load();
65  const html = render('See [the report](https://example.com/r) and `code` and **bold**. ![a](' + PNG + ')');
66  assert.match(html, /<a [^>]*>the report<\/a>/);
67  assert.match(html, /<code class="md-code">code<\/code>/);
68  assert.match(html, /<strong>bold<\/strong>/);
69  assert.ok(!html.includes('>!<') && !/!<a /.test(html));
70  assert.ok(!/<img /.test(render('`![x](' + PNG + ')`')));        // inside a code span it stays code
71});
72
73test('every incomplete stream prefix of an answer with a picture renders', async () => {
74  const { render } = await load();
75  const text = `Chart:\n\n![sales](${PNG})\n\nand ![b](https://example.com/b.png) end`;
76  for (let i = 0; i <= text.length; i += 7) assert.equal(typeof render(text.slice(0, i), 'c1'), 'string');
77});