fix(chat): queue outgoing messages

This commit is contained in:
2026-06-26 11:11:43 +08:00
parent 2858ceccdc
commit fe85af0cd7
8 changed files with 240 additions and 60 deletions
@@ -34,6 +34,7 @@ function makeChatState(overrides: Partial<ChatState> = {}): ChatState {
return {
messages: [],
isReplyingAI: true,
pendingReplyCount: 1,
wsConnected: false,
upgradePromptVisible: false,
upgradeReason: null,
@@ -7,6 +7,7 @@ import {
type UiMessage,
} from "@/data/dto/chat";
import { chatMachine } from "@/stores/chat/chat-machine";
import type { ChatEvent } from "@/stores/chat/chat-events";
interface LoadHistoryOutput {
messages: UiMessage[];
@@ -83,6 +84,23 @@ function createTestChatMachine(
reply: null,
})),
sendMessageWs: fromPromise<void, { content: string }>(async () => {}),
httpMessageQueue: fromCallback<ChatEvent>(({ receive, sendBack }) => {
receive((event) => {
if (event.type !== "ChatSendMessage") return;
sendBack({
type: "ChatQueuedHttpDone",
output: {
response: makeChatSendResponse(),
reply: null,
},
});
});
return () => undefined;
}),
wsPreferredMessageQueue: fromCallback<ChatEvent>(({ receive }) => {
receive(() => undefined);
return () => undefined;
}),
unlockPrivateMessage: fromPromise<
UnlockPrivateOutput,
{ messageId: string }
@@ -220,4 +238,32 @@ describe("chatMachine transitions", () => {
actor.stop();
});
it("allows multiple messages to be queued without leaving ready state", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatNonVipLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
);
actor.send({ type: "ChatSendMessage", content: "hello" });
actor.send({ type: "ChatSendMessage", content: "still there?" });
expect(actor.getSnapshot().matches({ nonVipUserSession: "ready" })).toBe(
true,
);
expect(actor.getSnapshot().context.messages).toMatchObject([
{ content: "hello", isFromAI: false },
{ content: "still there?", isFromAI: false },
]);
await waitFor(
actor,
(snapshot) => snapshot.context.pendingReplyCount === 0,
);
expect(actor.getSnapshot().context.isReplyingAI).toBe(false);
actor.stop();
});
});