fix(auth): return to guest session on logout

This commit is contained in:
2026-07-03 16:14:04 +08:00
parent b22f23bcc4
commit 8f17ea9c9f
11 changed files with 97 additions and 26 deletions
@@ -164,7 +164,7 @@ function createLoadHistoryCallback(
}
describe("chatMachine transitions", () => {
it("keeps guest logout disabled while allowing upgrade to user login", async () => {
it("keeps guest logout disabled and supports user to guest transition", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatGuestLogin" });
@@ -180,6 +180,22 @@ describe("chatMachine transitions", () => {
snapshot.matches({ userSession: "ready" }),
);
actor.send({ type: "ChatGuestLogin" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ guestSession: "ready" }),
);
actor.stop();
});
it("allows explicit logout from user session", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatUserLogin", token: "token" });
await waitFor(actor, (snapshot) =>
snapshot.matches({ userSession: "ready" }),
);
actor.send({ type: "ChatLogout" });
expect(actor.getSnapshot().matches("idle")).toBe(true);
@@ -270,7 +286,7 @@ describe("chatMachine transitions", () => {
actor.stop();
});
it("ignores guest login while an authenticated user session is active", async () => {
it("switches to guest session when guest login arrives during user session", async () => {
const actor = createActor(createTestChatMachine()).start();
actor.send({ type: "ChatUserLogin", token: "token" });
@@ -279,7 +295,9 @@ describe("chatMachine transitions", () => {
);
actor.send({ type: "ChatGuestLogin" });
expect(actor.getSnapshot().matches({ userSession: "ready" })).toBe(true);
await waitFor(actor, (snapshot) =>
snapshot.matches({ guestSession: "ready" }),
);
actor.stop();
});
+4
View File
@@ -235,6 +235,10 @@ export const chatMachine = setup({
},
],
on: {
ChatGuestLogin: {
target: "#chat.guestSession",
actions: "startGuestSession",
},
ChatLogout: {
target: "#chat.idle",
actions: "clearChatSession",