refactor(chat): remove vip websocket session flow

This commit is contained in:
2026-06-26 18:35:26 +08:00
parent 18bd9eaa47
commit e9fc001a6f
7 changed files with 40 additions and 567 deletions
@@ -83,7 +83,6 @@ function createTestChatMachine(
response: makeChatSendResponse(),
reply: null,
})),
sendMessageWs: fromPromise<void, { content: string }>(async () => {}),
httpMessageQueue: fromCallback<ChatEvent>(({ receive, sendBack }) => {
receive((event) => {
if (event.type !== "ChatSendMessage") return;
@@ -98,10 +97,6 @@ function createTestChatMachine(
});
return () => undefined;
}),
wsPreferredMessageQueue: fromCallback<ChatEvent>(({ receive }) => {
receive(() => undefined);
return () => undefined;
}),
unlockPrivateMessage: fromPromise<
UnlockPrivateOutput,
{ messageId: string }
@@ -117,19 +112,12 @@ function createTestChatMachine(
reason: "ok",
}),
})),
chatWebSocket: fromCallback(({ sendBack }) => {
sendBack({
type: "ChatWebSocketConnected",
userId: "user-1",
});
return () => undefined;
}),
},
});
}
describe("chatMachine transitions", () => {
it("keeps guest logout disabled while allowing upgrade to non-VIP login", async () => {
it("keeps guest logout disabled while allowing upgrade to user login", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatGuestLogin" });
@@ -140,9 +128,9 @@ describe("chatMachine transitions", () => {
actor.send({ type: "ChatLogout" });
expect(actor.getSnapshot().matches({ guestSession: "ready" })).toBe(true);
actor.send({ type: "ChatNonVipLogin", token: "token" });
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
snapshot.matches({ userSession: "ready" }),
);
actor.send({ type: "ChatLogout" });
@@ -151,24 +139,16 @@ describe("chatMachine transitions", () => {
actor.stop();
});
it("enters VIP ready only after history and websocket are ready", async () => {
it("enters user ready after history is loaded", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatVipLogin", token: "token" });
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ vipUserSession: "ready" }),
snapshot.matches({ userSession: "ready" }),
);
const snapshot = actor.getSnapshot();
expect(snapshot.context.historyLoaded).toBe(true);
expect(snapshot.context.wsConnected).toBe(true);
actor.send({ type: "ChatNonVipLogin", token: "token" });
await waitFor(actor, (nextSnapshot) =>
nextSnapshot.matches({ nonVipUserSession: "ready" }),
);
expect(actor.getSnapshot().context.wsConnected).toBe(false);
actor.stop();
});
@@ -176,23 +156,13 @@ describe("chatMachine transitions", () => {
it("ignores guest login while an authenticated user session is active", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatNonVipLogin", token: "token" });
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
snapshot.matches({ userSession: "ready" }),
);
actor.send({ type: "ChatGuestLogin" });
expect(actor.getSnapshot().matches({ nonVipUserSession: "ready" })).toBe(
true,
);
actor.send({ type: "ChatVipLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ vipUserSession: "ready" }),
);
actor.send({ type: "ChatGuestLogin" });
expect(actor.getSnapshot().matches({ vipUserSession: "ready" })).toBe(true);
expect(actor.getSnapshot().matches({ userSession: "ready" })).toBe(true);
actor.stop();
});
@@ -217,9 +187,9 @@ describe("chatMachine transitions", () => {
}),
).start();
actor.send({ type: "ChatNonVipLogin", token: "token" });
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
snapshot.matches({ userSession: "ready" }),
);
actor.send({ type: "ChatUnlockPrivateMessage", messageId: "private-1" });
@@ -243,17 +213,15 @@ describe("chatMachine transitions", () => {
it("allows multiple messages to be queued without leaving ready state", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatNonVipLogin", token: "token" });
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
snapshot.matches({ userSession: "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().matches({ userSession: "ready" })).toBe(true);
expect(actor.getSnapshot().context.messages).toMatchObject([
{ content: "hello", isFromAI: false },
{ content: "still there?", isFromAI: false },
@@ -293,7 +261,6 @@ describe("chatMachine transitions", () => {
response: makeChatSendResponse(),
reply: null,
})),
sendMessageWs: fromPromise<void, { content: string }>(async () => {}),
httpMessageQueue: fromCallback<ChatEvent>(({ receive, sendBack }) => {
const pending: string[] = [];
receive((event) => {
@@ -311,10 +278,6 @@ describe("chatMachine transitions", () => {
});
return () => undefined;
}),
wsPreferredMessageQueue: fromCallback<ChatEvent>(({ receive }) => {
receive(() => undefined);
return () => undefined;
}),
unlockPrivateMessage: fromPromise<
UnlockPrivateOutput,
{ messageId: string }
@@ -330,20 +293,13 @@ describe("chatMachine transitions", () => {
reason: "ok",
}),
})),
chatWebSocket: fromCallback(({ sendBack }) => {
sendBack({
type: "ChatWebSocketConnected",
userId: "user-1",
});
return () => undefined;
}),
},
});
const actor = createActor(machine).start();
actor.send({ type: "ChatNonVipLogin", token: "token" });
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ nonVipUserSession: "ready" }),
snapshot.matches({ userSession: "ready" }),
);
actor.send({ type: "ChatSendMessage", content: "first" });