okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c11eb58b
main
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 => { 12const { code} = await transform ( src ( name ), { loader :'ts' , format :'esm' }); 13return 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 () => { 17const { parkedReady} = await load ( 'lib/while-busy.ts' ); 18assert . equal ( parkedReady ( 'chat-1' , 'chat-1' , false , 1 ), true , 'the chat on screen is the active one and its answer is done' ); 19assert . equal ( parkedReady ( 'chat-1' , 'chat-2' , false , 1 ), false , 'the side bar already made chat 2 active: chat 1 must not send now' ); 20assert . equal ( parkedReady ( 'chat-1' , 'chat-1' , true , 1 ), false , 'still answering' ); 21assert . equal ( parkedReady ( 'chat-1' , 'chat-1' , false , 0 ), false , 'nothing parked' ); 22assert . equal ( parkedReady ( undefined , 'chat-1' , false , 1 ), false ); 23assert . 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' , () => { 27const chat = src ( 'screens/Chat.tsx' ); 28assert . match ( chat , / import \{ busyPlan, parkedReady \} from '\.\.\/lib\/while-busy'; / ); 29assert . 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});