Closes the 3rd PR #138 review. Warning fix: the render-phase reconciler only disarms the error-path adoption fallback when activeChatId actually changes. Pressing 'New chat' while ALREADY in a new chat keeps activeChatId === null (a no-op atom write), so the reconciler never fired and a stale armed fallback could adopt the just-failed chat from a late refetch, yanking the user out of their fresh chat. useChatSession now returns cancelPendingAdoption(); the window calls it from startNewChat AND selectChat. (The hook call moved above those callbacks so they can reference it.) Added a hook test that fails without the explicit disarm, plus a test for the existing-chat onTurnFinished branch (no adoption + per-chat invalidation). Cleanups: removed the dead pickNewlyCreatedChatId (the fallback effect uses newlyAddedChatIds directly with the 0/1/>1 decision inline) and its tests/doc mention; inlined the two invalidation closures (onTurnFinished is read live by useChat's onFinish, never in an effect dep array, so memoizing them was needless ceremony). Verified: tsc clean, 127 ai-chat tests green; live (z.ai glm-5.2) new chat + 2nd turn recalled the number in the SAME row (1 chat / 4 messages), no page errors. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import {
|
|
resolveAdoptedChatId,
|
|
newlyAddedChatIds,
|
|
extractServerChatId,
|
|
} from "./adopt-chat-id";
|
|
|
|
describe("resolveAdoptedChatId", () => {
|
|
it("adopts the server id for a brand-new chat (activeChatId null + id)", () => {
|
|
expect(resolveAdoptedChatId(null, "chat-1")).toBe("chat-1");
|
|
});
|
|
|
|
it("returns null for an existing chat even with a server id", () => {
|
|
expect(resolveAdoptedChatId("chat-existing", "chat-1")).toBeNull();
|
|
});
|
|
|
|
it("returns null for a new chat with no server id", () => {
|
|
expect(resolveAdoptedChatId(null, undefined)).toBeNull();
|
|
expect(resolveAdoptedChatId(null, null)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("newlyAddedChatIds", () => {
|
|
it("returns the single new id", () => {
|
|
expect([...newlyAddedChatIds(["a", "b"], ["a", "b", "c"])]).toEqual(["c"]);
|
|
});
|
|
|
|
it("returns an empty set when nothing was added", () => {
|
|
expect(newlyAddedChatIds(["a", "b"], ["b", "a"]).size).toBe(0);
|
|
});
|
|
|
|
it("returns both new ids when two were added", () => {
|
|
expect(newlyAddedChatIds(["a"], ["a", "b", "c"])).toEqual(
|
|
new Set(["b", "c"]),
|
|
);
|
|
});
|
|
|
|
it("keeps only the new id across an add+delete in the same window", () => {
|
|
// before [a,b] -> after [b,new]: a was deleted, new was added.
|
|
expect([...newlyAddedChatIds(["a", "b"], ["b", "new"])]).toEqual(["new"]);
|
|
});
|
|
|
|
it("dedupes a repeated new id to a single entry", () => {
|
|
expect(newlyAddedChatIds(["a"], ["a", "new", "new"])).toEqual(
|
|
new Set(["new"]),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("extractServerChatId", () => {
|
|
it("returns the chatId when present on metadata", () => {
|
|
expect(extractServerChatId({ metadata: { chatId: "chat-1" } })).toBe(
|
|
"chat-1",
|
|
);
|
|
});
|
|
|
|
it("returns undefined when the message has no metadata", () => {
|
|
expect(extractServerChatId({})).toBeUndefined();
|
|
});
|
|
|
|
it("returns undefined when metadata lacks chatId", () => {
|
|
expect(extractServerChatId({ metadata: { other: 1 } })).toBeUndefined();
|
|
});
|
|
|
|
it("returns undefined for a non-string chatId", () => {
|
|
expect(extractServerChatId({ metadata: { chatId: 42 } })).toBeUndefined();
|
|
});
|
|
|
|
it("returns undefined for an undefined message", () => {
|
|
expect(extractServerChatId(undefined)).toBeUndefined();
|
|
});
|
|
});
|