fix(chat): track replies by queued batch

This commit is contained in:
2026-06-26 11:42:17 +08:00
parent ceaa3ebfcf
commit 58236453ed
6 changed files with 126 additions and 34 deletions
@@ -87,6 +87,7 @@ function createTestChatMachine(
httpMessageQueue: fromCallback<ChatEvent>(({ receive, sendBack }) => {
receive((event) => {
if (event.type !== "ChatSendMessage") return;
sendBack({ type: "ChatQueuedSendStarted" });
sendBack({
type: "ChatQueuedHttpDone",
output: {
@@ -266,4 +267,96 @@ describe("chatMachine transitions", () => {
actor.stop();
});
it("tracks replying state by queued batch instead of user message count", async () => {
const machine = chatMachine.provide({
actors: {
loadHistory: fromPromise<LoadHistoryOutput>(async () => ({
messages: [],
hasMore: false,
newOffset: 0,
localOverwritten: true,
localCount: 0,
networkCount: 0,
})),
loadMoreHistory: fromPromise<LoadMoreHistoryOutput, { offset: number }>(
async () => ({
messages: [],
hasMore: false,
newOffset: 0,
}),
),
sendMessageHttp: fromPromise<
SendMessageHttpOutput,
{ content: string }
>(async () => ({
response: makeChatSendResponse(),
reply: null,
})),
sendMessageWs: fromPromise<void, { content: string }>(async () => {}),
httpMessageQueue: fromCallback<ChatEvent>(({ receive, sendBack }) => {
const pending: string[] = [];
receive((event) => {
if (event.type !== "ChatSendMessage") return;
pending.push(event.content);
if (pending.length !== 2) return;
sendBack({ type: "ChatQueuedSendStarted" });
sendBack({
type: "ChatQueuedHttpDone",
output: {
response: makeChatSendResponse(),
reply: null,
},
});
});
return () => undefined;
}),
wsPreferredMessageQueue: fromCallback<ChatEvent>(({ receive }) => {
receive(() => undefined);
return () => undefined;
}),
unlockPrivateMessage: fromPromise<
UnlockPrivateOutput,
{ messageId: string }
>(async ({ input }) => ({
messageId: input.messageId,
response: UnlockPrivateResponse.from({
unlocked: true,
content: "unlocked",
showUpgrade: false,
paywallTriggered: false,
privateFreeLimit: 0,
privateUsedToday: 0,
reason: "ok",
}),
})),
chatWebSocket: fromCallback(({ sendBack }) => {
sendBack({
type: "ChatWebSocketConnected",
userId: "user-1",
});
return () => undefined;
}),
},
});
const actor = createActor(machine).start();
actor.send({ type: "ChatNonVipLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
);
actor.send({ type: "ChatSendMessage", content: "first" });
actor.send({ type: "ChatSendMessage", content: "second" });
await waitFor(
actor,
(snapshot) => snapshot.context.messages.length === 2,
);
expect(actor.getSnapshot().context.pendingReplyCount).toBe(0);
expect(actor.getSnapshot().context.isReplyingAI).toBe(false);
actor.stop();
});
});