🌳 test: Assert the Message Tree a Host Write Leaves After a Replaced Start Rolls Back (#17548)

This commit is contained in:
Marco Beretta 2026-10-28 22:54:12 +01:00 • committed by GitHub
parent 658712c2df
commit 76b4a80fd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,4 @@
import { buildTree } from 'librechat-data-provider';
import type { TMessage, TPayload, ChatEvent } from 'librechat-data-provider';
import type { ChatStream, ChatSendResult, ChatTransport } from '../types';
import type { ChatState } from '../chat';
@ -83,6 +84,15 @@ const createChat = (state: ChatState, transport: Partial<ChatTransport> = {}) =>
}),
});
type TreeNode = { messageId: string; children?: TreeNode[] };
/** Every root-to-leaf path of a tree, so a row filed under the wrong parent shows as its own root. */
const toBranches = (nodes: TreeNode[], prefix = ''): string[] =>
nodes.flatMap((node) => {
const path = prefix ? `${prefix}>${node.messageId}` : node.messageId;
return node.children?.length ? toBranches(node.children, path) : [path];
});
describe('Chat.version', () => {
it('counts a write that keeps the same list', async () => {
const chat = createChat(createState([earlier]));
@ -168,21 +178,33 @@ describe('Chat.setMessages', () => {
(current: TMessage[]) =>
current.map((m) => (m.messageId === 'earlier-1' ? { ...m, text: 'Edited' } : m)),
['earlier-1:Edited'],
['earlier-1'],
],
[
'a row the turn added',
'a row the turn added, keeping the turn rows above it',
(current: TMessage[]) =>
current.map((m) => (m.messageId === 'response-1' ? { ...m, text: 'Kept' } : m)),
['earlier-1:Earlier', 'response-1:Kept'],
['earlier-1:Earlier', 'user-1:Earlier', 'response-1:Kept'],
['earlier-1>user-1>response-1'],
],
[
'a row appended under the turn, keeping the turn rows above it',
(current: TMessage[]) => [
...current,
{ ...placeholder, messageId: 'note-1', parentMessageId: 'response-1', text: 'Note' },
],
['earlier-1:Earlier', 'user-1:Earlier', 'response-1:', 'note-1:Note'],
['earlier-1>user-1>response-1>note-1'],
],
[
'a removal of a row the turn did not touch',
(current: TMessage[]) => current.filter((m) => m.messageId !== 'earlier-1'),
[],
[],
],
])(
'keeps its write to %s when the server replaces the start and the turn is undone',
async (_label, edit, expected) => {
async (_label, edit, expected, branches) => {
let answer: ((result: ChatSendResult) => void) | undefined;
const state = createState([earlier]);
const chat = createChat(state, {
@ -210,6 +232,7 @@ describe('Chat.setMessages', () => {
expect(chat.messages.map((message) => `${message.messageId}:${message.text}`)).toEqual(
expected,
);
expect(toBranches(buildTree({ messages: chat.messages }) ?? [])).toEqual(branches);
},
);
});