fix(chat): keep single locked history message unchanged

This commit is contained in:
2026-07-10 10:48:40 +08:00
parent a1a93a51fb
commit 1b85f3a410
3 changed files with 10 additions and 39 deletions
@@ -453,7 +453,7 @@ describe("chatMachine transitions", () => {
actor.stop(); actor.stop();
}); });
it("auto unlocks a single locked image message after payment", async () => { it("keeps a single locked image message locked after payment", async () => {
const actor = createActor( const actor = createActor(
createTestChatMachine({ createTestChatMachine({
historyMessages: [ historyMessages: [
@@ -495,16 +495,15 @@ describe("chatMachine transitions", () => {
); );
actor.send({ type: "ChatPaymentSucceeded" }); actor.send({ type: "ChatPaymentSucceeded" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ userSession: "ready" }),
);
expect(actor.getSnapshot().matches({ userSession: "ready" })).toBe(true);
expect(actor.getSnapshot().context.unlockHistoryPromptVisible).toBe(false); expect(actor.getSnapshot().context.unlockHistoryPromptVisible).toBe(false);
expect(actor.getSnapshot().context.messages).toMatchObject([ expect(actor.getSnapshot().context.messages).toMatchObject([
{ {
id: "msg-image-locked", id: "msg-image-locked",
imageUrl: "https://example.com/unlocked.jpg", imageUrl: "https://example.com/locked.jpg",
locked: false, imagePaywalled: true,
locked: true,
}, },
]); ]);
+5 -29
View File
@@ -36,7 +36,6 @@ import {
applyHistoryLoadedOutput, applyHistoryLoadedOutput,
applyNetworkHistoryLoadedOutput, applyNetworkHistoryLoadedOutput,
countLockedHistoryMessages, countLockedHistoryMessages,
shouldAutoUnlockHistory,
shouldPromptUnlockHistory, shouldPromptUnlockHistory,
} from "./chat-machine.helpers"; } from "./chat-machine.helpers";
import { import {
@@ -290,17 +289,6 @@ export const chatMachine = setup({
actions: "clearChatSession", actions: "clearChatSession",
}, },
ChatNetworkHistoryLoaded: [ ChatNetworkHistoryLoaded: [
{
guard: ({ context, event }) =>
event.type === "ChatNetworkHistoryLoaded" &&
context.paymentUnlockPending &&
shouldAutoUnlockHistory(event.output.messages),
target: ".unlockingHistory",
actions: [
"applyNetworkHistoryLoadedAndClearPayment",
"markUnlockHistoryStarted",
],
},
{ {
guard: ({ context, event }) => guard: ({ context, event }) =>
event.type === "ChatNetworkHistoryLoaded" && event.type === "ChatNetworkHistoryLoaded" &&
@@ -309,6 +297,11 @@ export const chatMachine = setup({
target: ".ready", target: ".ready",
actions: "showUnlockHistoryPromptFromNetwork", actions: "showUnlockHistoryPromptFromNetwork",
}, },
{
guard: ({ context }) => context.paymentUnlockPending,
target: ".ready",
actions: "applyNetworkHistoryLoadedAndClearPayment",
},
{ {
guard: ({ context }) => guard: ({ context }) =>
!context.isUnlockingHistory && !context.isUnlockingMessage, !context.isUnlockingHistory && !context.isUnlockingMessage,
@@ -328,12 +321,6 @@ export const chatMachine = setup({
actions: "appendQueuedSendErrorMessage", actions: "appendQueuedSendErrorMessage",
}, },
ChatPaymentSucceeded: [ ChatPaymentSucceeded: [
{
guard: ({ context }) =>
context.historyLoaded && shouldAutoUnlockHistory(context.messages),
target: ".unlockingHistory",
actions: ["clearUpgradePrompt", "markUnlockHistoryStarted"],
},
{ {
guard: ({ context }) => guard: ({ context }) =>
context.historyLoaded && context.historyLoaded &&
@@ -369,17 +356,6 @@ export const chatMachine = setup({
actions: "applyLocalHistoryLoaded", actions: "applyLocalHistoryLoaded",
}, },
ChatNetworkHistoryLoaded: [ ChatNetworkHistoryLoaded: [
{
guard: ({ context, event }) =>
event.type === "ChatNetworkHistoryLoaded" &&
context.paymentUnlockPending &&
shouldAutoUnlockHistory(event.output.messages),
target: "unlockingHistory",
actions: [
"applyNetworkHistoryLoadedAndClearPayment",
"markUnlockHistoryStarted",
],
},
{ {
guard: ({ context, event }) => guard: ({ context, event }) =>
event.type === "ChatNetworkHistoryLoaded" && event.type === "ChatNetworkHistoryLoaded" &&
-4
View File
@@ -43,10 +43,6 @@ function shouldApplySingleUnlock(message: UiMessage, messageId: string): boolean
); );
} }
export function shouldAutoUnlockHistory(messages: readonly UiMessage[]): boolean {
return countLockedHistoryMessages(messages) === 1;
}
export function shouldPromptUnlockHistory(messages: readonly UiMessage[]): boolean { export function shouldPromptUnlockHistory(messages: readonly UiMessage[]): boolean {
return countLockedHistoryMessages(messages) > 1; return countLockedHistoryMessages(messages) > 1;
} }