okami.mom/strata

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

Niko1221merge f-split-ui5 e9a2ca44a (round-7 UI, time-skip fixes, mode switch mid-goal, /plan, queued messages, uninstaller icon; includes agent6 eecb29593) into f-split-int743a6b3c6

main
6.0 KiB82 linesraw
1/** Item 4: the A versus B view adapts to the kind of file and keeps one window size. The pure parts. */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { readFile } from 'node:fs/promises';
5import { build } from 'esbuild';
6import { fileURLToPath } from 'node:url';
7
8async function load(file) {
9  const result = await build({ entryPoints: [fileURLToPath(new URL(`../src/lib/${file}`, import.meta.url))], bundle: true, format: 'esm', platform: 'node', write: false, alias: { react: fileURLToPath(new URL('./helpers/react-stub.mjs', import.meta.url)) }, logLevel: 'silent', loader: { '.json': 'json' } });
10  return import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
11}
12globalThis.window ??= globalThis; globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };
13globalThis.navigator ??= { language: 'en' };
14const cmp = await load('time-skip-compare.ts');
15const act = await load('time-skip-activity.ts');
16
17test('each kind of file gets its own view', () => {
18  const k = (path, file) => cmp.fileKind(path, file);
19  assert.equal(k('notes/plan.md'), 'markdown'); assert.equal(k('a.txt'), 'text'); assert.equal(k('site/index.html'), 'html'); assert.equal(k('b.HTM'), 'html');
20  assert.equal(k('img/logo.PNG'), 'image'); assert.equal(k('x.svg'), 'image'); assert.equal(k('weird', { image: true }), 'image');
21  assert.equal(k('src/app.py'), 'code'); assert.equal(k('data.bin', { unreadable: true }), 'other');
22  assert.equal(k('photo.jpg', { unreadable: true }), 'image', 'a picture is not "unreadable": it is shown from the file');
23});
24
25test('changed words are marked, unchanged text is identical on both sides, and nothing is lost', () => {
26  const a = 'Dear Hiring Manager, I am writing to express my sincere interest in the role.';
27  const b = 'Hello team, I am writing because I love the role.';
28  const d = cmp.wordDiff(a, b);
29  assert.equal(d.original.map(s => s.text).join(''), a); assert.equal(d.updated.map(s => s.text).join(''), b);
30  assert.ok(d.original.some(s => s.changed && /Dear/.test(s.text)) && d.updated.some(s => s.changed && /Hello/.test(s.text)));
31  assert.ok(d.original.some(s => !s.changed && /I am writing/.test(s.text)), 'what stayed is not marked');
32  assert.deepEqual(cmp.wordDiff('same text', 'same text').updated, [{ text: 'same text', changed: false }]);
33  const lines = cmp.segmentLines(cmp.wordDiff('# Title\n- one\n', '# Title\n- two\n').updated);
34  assert.equal(lines[0].map(s => s.text).join(''), '# Title'); assert.equal(lines[1].map(s => s.text).join(''), '- two');
35  assert.ok(lines[1].some(s => s.changed));
36});
37
38test('a huge pair of texts does not freeze the page: the work is capped', () => {
39  const big = Array.from({ length: 4000 }, (_, i) => `word${i}`).join(' ');
40  const t = Date.now();
41  const d = cmp.wordDiff(big, big + ' extra');
42  assert.ok(Date.now() - t < 3000);
43  assert.ok(d.updated.length >= 1);
44});
45
46test('code and other files get a short line diff with the unchanged stretches folded', () => {
47  const a = Array.from({ length: 40 }, (_, i) => `line ${i}`).join('\n');
48  const b = a.replace('line 20', 'line twenty') + '\nline 40';
49  const rows = cmp.lineDiff(a, b);
50  const stats = cmp.diffStats(rows);
51  assert.deepEqual(stats, { added: 2, removed: 1 });
52  assert.ok(rows.length < 14, `folded to ${rows.length} rows`);
53  assert.ok(rows.some(r => r.t === 'gap' && r.n > 10));
54  assert.deepEqual(cmp.lineDiff('', 'a\nb').map(r => r.t), ['add', 'add'], 'a new file is all additions');
55  assert.deepEqual(cmp.lineDiff('a', '').map(r => r.t), ['del'], 'a removed file is all removals');
56});
57
58test('an HTML page is shown with no way to reach out: a policy is added to the page itself', () => {
59  const withHead = cmp.htmlPreviewDocument('<html><head><title>x</title></head><body><script>alert(1)</script><img src="https://evil.example/a.png"></body></html>');
60  assert.match(withHead, /Content-Security-Policy/); assert.match(withHead, /default-src 'none'/); assert.ok(withHead.indexOf('Content-Security-Policy') < withHead.indexOf('<script>'), 'the policy comes first');
61  const bare = cmp.htmlPreviewDocument('<h1>Hi</h1>');
62  assert.match(bare, /<body><h1>Hi<\/h1><\/body>/); assert.match(bare, /Content-Security-Policy/);
63  assert.equal(cmp.timeSkipImageUrl('abc12345', 'img/a b.png', 'updated'), '/api/timeskip/file?id=abc12345&path=img%2Fa%20b.png&version=updated');
64});
65
66test('the working view says what is happening, from the host\'s words or the phase', async () => {
67  assert.equal(act.activityWords({ phase: 'generating', activity: { code: 'trying-version', text: 'x', args: { step: 'Writing notes.md…' } } }), 'Writing notes.md…');
68  assert.equal(act.activityWords({ phase: 'generating', activity: { code: 'trying-version', text: 'x' } }), 'Trying an improved version…');
69  assert.equal(act.activityWords({ phase: 'checking', activity: { code: 'checking-result', text: 'x' } }), 'Checking the result…');
70  assert.equal(act.activityWords({ phase: 'queued' }), 'Reading your files…');
71  assert.equal(act.activityWords({ phase: 'checking' }), 'Checking the result…');
72  assert.ok(act.phaseFraction('generating') > act.phaseFraction('checkpointing') && act.phaseFraction('checking') > act.phaseFraction('generating'));
73  const panel = await readFile(new URL('../src/components/TimeSkipCompare.tsx', import.meta.url), 'utf8');
74  // A page that draws itself with a script (a 3D scene) must run, or it shows as an empty dark page; the frame stays isolated and offline.
75  assert.equal(cmp.HTML_PREVIEW_SANDBOX, 'allow-scripts');
76  assert.match(panel, /sandbox={HTML_PREVIEW_SANDBOX}/);
77  assert.ok(!/allow-same-origin|allow-forms|allow-popups|allow-top-navigation/.test(panel + cmp.HTML_PREVIEW_SANDBOX));
78  const policy = cmp.htmlPreviewDocument('<p>x</p>');
79  assert.match(policy, /script-src 'unsafe-inline'/); assert.ok(!/connect-src|https?:\/\//.test(policy.split('<body>')[0]), 'nothing may be fetched');
80  assert.equal(cmp.needsOutsideFiles('<script src="https://cdn.example/three.js"></script>'), true);
81  assert.equal(cmp.needsOutsideFiles('<script>draw()</script><a href="https://x.example">link</a>'), false);
82});