test(FN-4835): cover streamed-text precedence in chat hooks

Fusion-Task-Id: FN-4835
Fusion-Task-Lineage: 09eb6339-a476-4cf1-b7e5-e60fa6c263ce
This commit is contained in:
Fusion (runfusion.ai)
2026-05-16 23:41:50 -07:00
committed by gsxdsm
parent 7351852a61
commit f8457d1130
2 changed files with 27 additions and 9 deletions

View File

@@ -610,7 +610,23 @@ describe("useChat", () => {
});
});
it("prefers accumulated streamed text over done payload snapshot when both exist", async () => {
it.each([
{
name: "single sentence boundary",
chunks: ["Hello.", " World."],
expected: "Hello. World.",
},
{
name: "multiple sentence boundaries across three chunks",
chunks: ["One.", " Two.", " Three.", " Four."],
expected: "One. Two. Three. Four.",
},
{
name: "trailing whitespace-only delta before done",
chunks: ["Trailing", " "],
expected: "Trailing ",
},
])("prefers streamed text over done snapshot (%s)", async ({ chunks, expected }) => {
const session = makeSession({ id: "session-001", agentId: "agent-001" });
mockFetchChatSessions.mockResolvedValueOnce({ sessions: [session] });
mockFetchChatMessages.mockResolvedValueOnce({ messages: [] });
@@ -639,15 +655,16 @@ describe("useChat", () => {
act(() => {
result.current.sendMessage("Hello!");
textHandler?.("Hello.");
textHandler?.(" World.");
for (const chunk of chunks) {
textHandler?.(chunk);
}
doneHandler?.({
messageId: "msg-003",
message: {
id: "msg-003",
sessionId: "session-001",
role: "assistant",
content: "Hello.World.",
content: expected.replace(/\s+/g, ""),
thinkingOutput: null,
metadata: null,
createdAt: "2026-01-01T00:00:00.000Z",
@@ -658,7 +675,7 @@ describe("useChat", () => {
await waitFor(() => {
expect(result.current.messages.at(-1)).toEqual(expect.objectContaining({
id: "msg-003",
content: "Hello. World.",
content: expected,
}));
});
});

View File

@@ -167,7 +167,7 @@ describe("useQuickChat", () => {
});
});
it("prefers done payload assistant snapshot over streamed text", async () => {
it("prefers accumulated streamed text over done payload snapshot when both exist", async () => {
const session = makeSession({ id: "session-001", agentId: "agent-001" });
mockFetchResumeChatSession.mockResolvedValue({ session });
mockFetchChatMessages.mockResolvedValue({ messages: [] });
@@ -188,14 +188,15 @@ describe("useQuickChat", () => {
act(() => {
void result.current.sendMessage("Hello");
onText?.("streamed text");
onText?.("Quick.");
onText?.(" Chat.");
onDone?.({
messageId: "msg-002",
message: {
id: "msg-002",
sessionId: "session-001",
role: "assistant",
content: "snapshot wins",
content: "Quick.Chat.",
thinkingOutput: null,
metadata: null,
createdAt: "2026-01-01T00:00:00.000Z",
@@ -206,7 +207,7 @@ describe("useQuickChat", () => {
await waitFor(() => {
expect(result.current.messages.at(-1)).toEqual(expect.objectContaining({
id: "msg-002",
content: "snapshot wins",
content: "Quick. Chat.",
}));
});
});