okami.mom/strata

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

Niko1221Merge f-0213-preview (68bead108) into f-0213-inte82c70698

main
9.4 KiB152 linesraw
1// The sidebar preview kept a file's FIRST content after the file changed on disk (QAGUI, 4d482e627). Three places had to change together:
2// openPreview dropped the freshly read file, the panel's loading effect had no content version, and the effect that re-adds a message's own
3// artifacts put the stored first copy back over the newer one. A fix of the first two alone ran the renderer out of memory: a ping-pong
4// between the message copy and the opened copy. These tests pin the rules (lib/preview-merge.ts) and that nothing can loop.
5import assert from 'node:assert/strict';
6import test from 'node:test';
7import { readFileSync, writeFileSync, mkdtempSync, utimesSync } from 'node:fs';
8import { tmpdir } from 'node:os';
9import { join, resolve } from 'node:path';
10import { build } from 'esbuild';
11import { createRequire } from 'node:module';
12
13const root = resolve(import.meta.dirname, '..');
14const bundle = await build({ stdin: { contents: `export * from './src/lib/preview-merge.ts'; export { textRevision } from './src/lib/preview-render.ts';`, resolveDir: root, loader: 'ts' }, bundle: true, write: false, platform: 'node', format: 'cjs' });
15const mod = { exports: {} };
16new Function('require', 'module', 'exports', bundle.outputFiles[0].text)(createRequire(import.meta.url), mod, mod.exports);
17const { previewVersion, openItem, syncArtifacts, messageArtifacts } = mod.exports;
18const { previewFile } = await import('../server/chat-files.mjs');
19
20const md = (content, at, extra = {}) => ({ id: 'file:/w/notes.md', kind: 'markdown', title: 'notes.md', content, at, ...extra });
21const T = (n) => new Date(Date.UTC(2026, 10, 4, 10, 0, n)).toISOString();
22
23test('the version follows the content: the text, else the host version, else the address', () => {
24  assert.equal(previewVersion(md('# One')), previewVersion(md('# One')));
25  assert.notEqual(previewVersion(md('# One')), previewVersion(md('# Two')));
26  assert.notEqual(previewVersion({ url: '/a.png', version: '1:5' }), previewVersion({ url: '/a.png', version: '2:5' }));
27  assert.equal(previewVersion({ url: '/a.png', version: '1:5' }), previewVersion({ url: '/a.png', version: '1:5' }));
28  assert.notEqual(previewVersion({ url: '/a.png' }), previewVersion({ url: '/b.png' }));
29  assert.notEqual(previewVersion({ content: '' }), previewVersion({ url: '' }));
30});
31
32test('opening a file that changed replaces its tab in place (it used to be dropped)', () => {
33  const old = [md('# Version One', 1000), { id: 'other', kind: 'code', title: 'x.ts', content: 'x' }];
34  const next = openItem(old, md('# Version Two', 2000));
35  assert.equal(next.length, 2);
36  assert.equal(next[0].content, '# Version Two');
37  assert.equal(next[1].id, 'other', 'its place in the strip is kept');
38  assert.equal(old[0].content, '# Version One', 'the old array is not mutated');
39});
40
41test('opening a file that did not change, or a new file, behaves as before; the same array comes back when nothing changed', () => {
42  const old = [md('# Version One', 1000)];
43  assert.equal(openItem(old, md('# Version One', 5000)), old);
44  const added = openItem(old, { id: 'file:/w/b.md', kind: 'markdown', title: 'b.md', content: 'b' });
45  assert.equal(added.length, 2);
46  assert.equal(added[0], old[0]);
47});
48
49test('the QAGUI case: the message still carries Version One, the file was opened as Version Two, and Version Two stays', () => {
50  const messages = [{ at: T(0), artifacts: [md('# Version One')] }];
51  let tabs = syncArtifacts([], messageArtifacts(messages), []);          // the first message arrives: the tab shows Version One
52  assert.equal(tabs[0].content, '# Version One');
53  tabs = openItem(tabs, md('# Version Two', Date.parse(T(60))));         // "Open in sidebar" reads the file again
54  assert.equal(tabs[0].content, '# Version Two');
55  for (let i = 0; i < 5; i++) {                                           // a later message count change runs the sync again
56    const again = syncArtifacts(tabs, messageArtifacts(messages), []);
57    assert.equal(again, tabs, 'the same array: nothing to update');
58    assert.equal(again[0].content, '# Version Two', 'the message copy did not come back');
59  }
60});
61
62test('a message newer than the copy on show, with other content, does replace it; an older or equal one never does', () => {
63  const shown = [md('# Version Two', Date.parse(T(60)))];
64  const older = syncArtifacts(shown, messageArtifacts([{ at: T(30), artifacts: [md('# Version One')] }]), []);
65  assert.equal(older, shown);
66  const equal = syncArtifacts(shown, messageArtifacts([{ at: T(60), artifacts: [md('# Version Three')] }]), []);
67  assert.equal(equal, shown);
68  const newer = syncArtifacts(shown, messageArtifacts([{ at: T(90), artifacts: [md('# Version Three')] }]), []);
69  assert.equal(newer[0].content, '# Version Three');
70  assert.equal(shown[0].content, '# Version Two');
71  // the same content in a newer message is not a change
72  assert.equal(syncArtifacts(shown, messageArtifacts([{ at: T(90), artifacts: [md('# Version Two')] }]), []), shown);
73});
74
75test('a missing tab is added, a closed one is removed and not brought back', () => {
76  const items = messageArtifacts([{ at: T(0), artifacts: [md('a'), { id: 'b', kind: 'code', title: 'b', content: 'b' }] }]);
77  const added = syncArtifacts([], items, []);
78  assert.deepEqual(added.map(a => a.id), ['file:/w/notes.md', 'b']);
79  const closed = syncArtifacts(added, items, ['b']);
80  assert.deepEqual(closed.map(a => a.id), ['file:/w/notes.md']);
81  assert.deepEqual(syncArtifacts(closed, items, ['b']).map(a => a.id), ['file:/w/notes.md'], 'and it stays closed');
82  assert.equal(syncArtifacts(closed, items, ['b']), closed);
83  // a tab opened by hand that no message knows stays
84  const mine = [{ id: 'mine', kind: 'code', title: 'm', content: 'm' }];
85  assert.equal(syncArtifacts(mine, [], []), mine);
86});
87
88test('message artifacts: stamped with their message time, the last of an id wins, a bad time is left unstamped', () => {
89  const list = messageArtifacts([{ at: T(0), artifacts: [md('one')] }, { at: T(30), artifacts: [md('two')] }, { at: 'not a time', artifacts: [{ id: 'z', kind: 'code', title: 'z', content: 'z' }] }, {}]);
90  assert.equal(list.length, 2);
91  assert.equal(list[0].content, 'two');
92  assert.equal(list[0].at, Date.parse(T(30)));
93  assert.equal(list[1].at, undefined);
94  assert.deepEqual(messageArtifacts(undefined), []);
95});
96
97test('no loop: the open and the sync handed to each other 2000 times change the tabs only when the content changes', () => {
98  const messages = [{ at: T(0), artifacts: [md('# Version One')] }];
99  let tabs = [], changes = 0, loads = 0, lastKey = '';
100  const set = next => { if (next !== tabs) changes++; tabs = next; };
101  // what the panel does: reload its text only when (id, version) of the shown tab changes
102  const panel = () => { const cur = tabs[0]; const key = cur ? cur.id + '|' + previewVersion(cur) : ''; if (key !== lastKey) { loads++; lastKey = key; } };
103  set(syncArtifacts(tabs, messageArtifacts(messages), [])); panel();
104  for (let i = 0; i < 2000; i++) {
105    // the chat renders for every streamed token; the sync runs when the artifact count changes, the open when a run finishes
106    set(openItem(tabs, md('# Version Two', Date.parse(T(60)))));
107    set(syncArtifacts(tabs, messageArtifacts(messages), []));
108    set(openItem(tabs, md('# Version Two', Date.parse(T(60 + i)))));
109    panel();
110  }
111  assert.equal(changes, 2, 'the first tab, then Version Two once');
112  assert.equal(loads, 2, 'the panel loaded its text twice: Version One, then Version Two');
113});
114
115test('no loop: a file that changes every second is one load per change, not one per render', () => {
116  let tabs = [], loads = 0, lastKey = '';
117  for (let s = 0; s < 300; s++) {
118    for (let render = 0; render < 20; render++) {                           // 20 renders per second of streamed tokens
119      if (render === 0) tabs = openItem(tabs, md('# v' + s, 1000 + s));
120      const cur = tabs[0]; const key = cur.id + '|' + previewVersion(cur);
121      if (key !== lastKey) { loads++; lastKey = key; }
122    }
123  }
124  assert.equal(loads, 300);
125});
126
127test('the host sends the file version: it moves when the file is rewritten', () => {
128  const dir = mkdtempSync(join(tmpdir(), 'strata-prev-'));
129  writeFileSync(join(dir, 'n.md'), '# Version One\n');
130  const a = previewFile(dir, 'n.md', 's');
131  writeFileSync(join(dir, 'n.md'), '# Version Two\n');
132  utimesSync(join(dir, 'n.md'), new Date(Date.now() + 5000), new Date(Date.now() + 5000));
133  const b = previewFile(dir, 'n.md', 's');
134  assert.equal(typeof a.mtime, 'number');
135  assert.notEqual(a.mtime, b.mtime);
136  assert.equal(b.text, '# Version Two\n');
137});
138
139test('the three places are changed together', () => {
140  const chat = readFileSync(resolve(root, 'src/screens/Chat.tsx'), 'utf8');
141  assert.match(chat, /setPreviews\(old => openItem\(old, item\)\)/);
142  assert.match(chat, /setPreviews\(old => syncArtifacts\(old, items, session\?\.closedPreviews\)\)/);
143  assert.doesNotMatch(chat, /old\.some\(a => a\.id === item\.id\) \? old/);
144  assert.doesNotMatch(chat, /\[\.\.\.old\.filter\(a => !items\.some/);
145  const panel = readFileSync(resolve(root, 'src/components/ArtifactPanel.tsx'), 'utf8');
146  assert.match(panel, /\}, \[current\?\.id, version, revision\]\);/);
147  assert.match(panel, /const version = useMemo\(\(\) => current \? previewVersion\(current\) : '', \[current\]\)/);
148  // the reading view is reset by the file, not by its content
149  assert.match(panel, /setMode\('preview'\);[\s\S]*?\}, \[current\?\.id\]\);/);
150  const deliver = readFileSync(resolve(root, 'src/lib/deliverables.ts'), 'utf8');
151  assert.match(deliver, /version = a\.mtime !== undefined/);
152});