okami.mom/strata

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

Niko1221Merge f-0213-guidata (4c4596746) into f-0213-int22a3d7068

main
4.6 KiB90 linesraw
1// #5079: after the desktop shell loads the page again because its renderer died, the page starts with the sidebar previews closed, once.
2// The shell side (which failures reload, the limit of two in two minutes, the address) is unit-tested in apps/desktop/src-tauri/src/recovery.rs.
3import assert from 'node:assert/strict';
4import test from 'node:test';
5import { readFileSync } from 'node:fs';
6import { resolve } from 'node:path';
7import { transform } from 'esbuild';
8
9const root = resolve(import.meta.dirname, '..');
10const { code } = await transform(readFileSync(resolve(root, 'src/lib/recovery.ts'), 'utf8'), { loader: 'ts', format: 'esm' });
11const lib = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
12
13function fakeWindow(url) {
14  const u = new URL(url);
15  const calls = [];
16  globalThis.window = {
17    location: { pathname: u.pathname, search: u.search, hash: u.hash },
18    history: { state: null, replaceState: (state, title, address) => { calls.push(address); } },
19  };
20  return calls;
21}
22
23test('the recovery number is read from the query, and only a single digit counts', () => {
24  assert.equal(lib.parseRecovery('?recovery=1'), 1);
25  assert.equal(lib.parseRecovery('?x=1&recovery=2'), 2);
26  for (const bad of ['', '?', '?recovery=', '?recovery=0', '?recovery=10', '?recovery=x', '?recovery=-1', '?recovery=1.5', '?recoveryx=1'])
27    assert.equal(lib.parseRecovery(bad), 0, bad);
28});
29
30test('the mark is taken out of the query and nothing else is', () => {
31  assert.equal(lib.withoutRecovery('?recovery=1'), '');
32  assert.equal(lib.withoutRecovery('?recovery=1&a=b'), '?a=b');
33  assert.equal(lib.withoutRecovery('?a=b'), '?a=b');
34  assert.equal(lib.withoutRecovery(''), '');
35});
36
37test('an ordinary start defers nothing and leaves the address alone', () => {
38  const calls = fakeWindow('http://127.0.0.1:47831/#/chat/s_1');
39  assert.equal(lib.startRecovery(), 0);
40  assert.deepEqual(calls, []);
41  assert.equal(lib.previewsDeferred('s_1'), false);
42  assert.equal(lib.previewsDeferred(), false);
43});
44
45test('a recovery start defers the previews of the chat that was open, removes the mark and keeps the screen', () => {
46  const calls = fakeWindow('http://127.0.0.1:47831/?recovery=1#/chat/s_1');
47  assert.equal(lib.startRecovery(), 1);
48  assert.deepEqual(calls, ['/#/chat/s_1'], 'the address loses the mark, keeps the hash');
49  assert.equal(lib.previewsDeferred(), true);
50  assert.equal(lib.previewsDeferred('s_1'), true);
51  assert.equal(lib.previewsDeferred('s_1'), true, 'asked again for the same chat (the effects run more than once)');
52  // another chat is an ordinary one, and the deferral is over for good
53  assert.equal(lib.previewsDeferred('s_2'), false);
54  assert.equal(lib.previewsDeferred('s_1'), false);
55});
56
57test('opening a preview ends the deferral; a second start in the same page is ordinary again', () => {
58  fakeWindow('http://127.0.0.1:47831/?recovery=2');
59  assert.equal(lib.startRecovery(), 2);
60  assert.equal(lib.previewsDeferred('s_1'), true);
61  lib.endPreviewDeferral();
62  assert.equal(lib.previewsDeferred('s_1'), false);
63  fakeWindow('http://127.0.0.1:47831/');
64  assert.equal(lib.startRecovery(), 0);
65  assert.equal(lib.previewsDeferred('s_1'), false);
66});
67
68test('no address to read is an ordinary start, not an error', () => {
69  globalThis.window = undefined;
70  assert.equal(lib.startRecovery(), 0);
71});
72
73test('the page reads the mark before the first render, and the chat honours it', () => {
74  const main = readFileSync(resolve(root, 'src/main.tsx'), 'utf8');
75  assert.ok(main.indexOf('startRecovery();') > 0 && main.indexOf('startRecovery();') < main.indexOf('createRoot(el).render'));
76  const chat = readFileSync(resolve(root, 'src/screens/Chat.tsx'), 'utf8');
77  assert.match(chat, /setSelectedPreview\(session && previewsDeferred\(session\.id\) \|\|/);
78  assert.match(chat, /function openPreview\(item: PreviewArtifact\) \{\s*endPreviewDeferral\(\);/);
79  assert.match(chat, /function selectPreview\(id: string\) \{\s*endPreviewDeferral\(\);/);
80});
81
82test('the shell hooks: Windows ProcessFailed, macOS web content process, the same recovery path, no new text', () => {
83  const main = readFileSync(resolve(root, '../desktop/src-tauri/src/main.rs'), 'utf8');
84  assert.match(main, /fn hook_renderer_failures\(window: &tauri::WebviewWindow, port: u16\)/);
85  assert.match(main, /add_ProcessFailed\(&handler, &mut token\)/);
86  assert.match(main, /on_web_content_process_terminate\(/);
87  assert.match(main, /recovery::plan\(/);
88  const cargo = readFileSync(resolve(root, '../desktop/src-tauri/Cargo.toml'), 'utf8');
89  assert.match(cargo, /\[target\.'cfg\(windows\)'\.dependencies\][\s\S]*webview2-com = "0\.39"/);
90});