From 934b92502b8a40eae8ab6e2c4249bda8e1afef34 Mon Sep 17 00:00:00 2001 From: chenhang Date: Mon, 29 Jun 2026 11:27:39 +0800 Subject: [PATCH] fix(chat): clear limit prompt after payment --- .../chat-machine.transitions.test.ts | 48 +++++++++++++++++++ src/stores/chat/chat-machine.ts | 16 +++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/stores/chat/__tests__/chat-machine.transitions.test.ts b/src/stores/chat/__tests__/chat-machine.transitions.test.ts index f1d96d61..57bf9bdd 100644 --- a/src/stores/chat/__tests__/chat-machine.transitions.test.ts +++ b/src/stores/chat/__tests__/chat-machine.transitions.test.ts @@ -354,4 +354,52 @@ describe("chatMachine transitions", () => { actor.stop(); }); + + it("clears the daily limit prompt after payment succeeds", async () => { + const actor = createActor(createTestChatMachine()).start(); + + actor.send({ type: "ChatUserLogin", token: "token" }); + await waitFor(actor, (snapshot) => + snapshot.matches({ userSession: "ready" }), + ); + + actor.send({ + type: "ChatQueuedHttpDone", + output: { + response: ChatSendResponse.from({ + reply: "", + audioUrl: "", + messageId: "", + isGuest: false, + timestamp: Date.now(), + image: { type: null, url: null }, + lockDetail: { + locked: true, + showContent: false, + showUpgrade: true, + reason: "daily_limit", + hint: "Free message limit reached.", + detail: { + type: "daily_msg_limit", + usedToday: 3, + limit: 3, + }, + }, + }), + reply: null, + }, + }); + + expect(actor.getSnapshot().context.upgradePromptVisible).toBe(true); + expect(actor.getSnapshot().context.upgradeReason).toBe("daily_limit"); + + actor.send({ type: "ChatPaymentSucceeded" }); + + expect(actor.getSnapshot().context.upgradePromptVisible).toBe(false); + expect(actor.getSnapshot().context.upgradeReason).toBeNull(); + expect(actor.getSnapshot().context.upgradeHint).toBeNull(); + expect(actor.getSnapshot().context.upgradeDetail).toBeNull(); + + actor.stop(); + }); }); diff --git a/src/stores/chat/chat-machine.ts b/src/stores/chat/chat-machine.ts index 617c33cb..86dc79ed 100644 --- a/src/stores/chat/chat-machine.ts +++ b/src/stores/chat/chat-machine.ts @@ -218,6 +218,13 @@ export const chatMachine = setup({ return applyHttpSendOutput(context, event.output); }), + clearUpgradePrompt: assign(() => ({ + upgradePromptVisible: false, + upgradeReason: null, + upgradeHint: null, + upgradeDetail: null, + })), + markPaymentUnlockPending: assign(() => ({ paymentUnlockPending: true, unlockHistoryError: null, @@ -290,7 +297,6 @@ export const chatMachine = setup({ initial: "initializing", states: { initializing: { - // 本地游客额度逻辑已停用:只等待历史加载完成。 always: [ { target: "ready", @@ -376,21 +382,21 @@ export const chatMachine = setup({ guard: ({ context }) => context.historyLoaded && shouldAutoUnlockHistory(context.messages), target: ".unlockingHistory", - actions: "markUnlockHistoryStarted", + actions: ["clearUpgradePrompt", "markUnlockHistoryStarted"], }, { guard: ({ context }) => context.historyLoaded && shouldPromptUnlockHistory(context.messages), - actions: "showUnlockHistoryPrompt", + actions: ["clearUpgradePrompt", "showUnlockHistoryPrompt"], }, { guard: ({ context }) => !context.historyLoaded, - actions: "markPaymentUnlockPending", + actions: ["clearUpgradePrompt", "markPaymentUnlockPending"], }, { guard: ({ context }) => context.historyLoaded, - actions: "dismissUnlockHistoryPrompt", + actions: ["clearUpgradePrompt", "dismissUnlockHistoryPrompt"], }, ], ChatUnlockHistoryConfirmed: {