okami.mom/strata

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

Niko1221Merge f-0215-parked: chat: a parked message is sent only from the chat that is also the active one (#5806)7c11eb58b

main
2.2 KiB30 linesraw
1// #5806: a message waiting to be sent in one chat (Send after / Interrupt) went into ANOTHER chat when the person switched chats.
2// A click in the side bar makes the other chat the store's active one (selectSession) a moment before the page shows it; in that moment
3// the chat on screen was still the first one, its answer read as finished (the store's streaming is the active chat's), so it sent its
4// parked message, and sendMessage sends to the active chat: the other one.
5import test from 'node:test';
6import assert from 'node:assert/strict';
7import { readFileSync } from 'node:fs';
8import { transform } from 'esbuild';
9
10const src = name => readFileSync(new URL(`../src/${name}`, import.meta.url), 'utf8').replace(/\r\n/g, '\n');
11const load = async name => {
12  const { code } = await transform(src(name), { loader: 'ts', format: 'esm' });
13  return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
14};
15
16test('a parked message is sent only from the chat that is also the active one, and only when it is quiet', async () => {
17  const { parkedReady } = await load('lib/while-busy.ts');
18  assert.equal(parkedReady('chat-1', 'chat-1', false, 1), true, 'the chat on screen is the active one and its answer is done');
19  assert.equal(parkedReady('chat-1', 'chat-2', false, 1), false, 'the side bar already made chat 2 active: chat 1 must not send now');
20  assert.equal(parkedReady('chat-1', 'chat-1', true, 1), false, 'still answering');
21  assert.equal(parkedReady('chat-1', 'chat-1', false, 0), false, 'nothing parked');
22  assert.equal(parkedReady(undefined, 'chat-1', false, 1), false);
23  assert.equal(parkedReady('chat-1', null, false, 1), false, 'no active chat yet: sendMessage would start a new one');
24});
25
26test('the chat screen asks parkedReady with the active chat and runs again when the active chat changes', () => {
27  const chat = src('screens/Chat.tsx');
28  assert.match(chat, /import \{ busyPlan, parkedReady \} from '\.\.\/lib\/while-busy';/);
29  assert.match(chat, /if \(!session \|\| !parkedReady\(session\.id, activeId, streaming, parked\?\.length \?\? 0\)\)\s*return;\s*const next = takeParked\(session\.id\);\s*if \(next !== undefined\)\s*void sendMessage\(next\);\s*\}, \[streaming, parked\?\.length, session\?\.id, activeId\]\);/);
30});