fix(chat): stabilize message identities

This commit is contained in:
2026-07-20 18:30:21 +08:00
parent 159e8bfd59
commit 1e13f94b5d
33 changed files with 718 additions and 212 deletions
@@ -38,13 +38,13 @@ describe("chat history flow", () => {
resolveNetwork = resolve;
});
const localMessage: UiMessage = {
id: "local-msg",
displayId: "local-msg",
content: "cached local message",
isFromAI: true,
date: "2026-07-02",
};
const networkMessage: UiMessage = {
id: "network-msg",
displayId: "network-msg",
content: "fresh network message",
isFromAI: true,
date: "2026-07-02",
@@ -67,6 +67,7 @@ describe("chat history flow", () => {
type: "ChatNetworkHistoryLoaded",
output: {
messages: [networkMessage],
localDisplayIds: [localMessage.displayId],
localOverwritten: true,
localCount: 1,
networkCount: 1,
@@ -89,17 +90,17 @@ describe("chat history flow", () => {
);
expect(actor.getSnapshot().context.messages).toMatchObject([
{ id: "local-msg", content: "cached local message" },
{ displayId: "local-msg", content: "cached local message" },
]);
resolveNetwork();
await waitFor(
actor,
(snapshot) => snapshot.context.messages[0]?.id === "network-msg",
(snapshot) => snapshot.context.messages[0]?.displayId === "network-msg",
);
expect(actor.getSnapshot().context.messages).toMatchObject([
{ id: "network-msg", content: "fresh network message" },
{ displayId: "network-msg", content: "fresh network message" },
]);
actor.stop();
@@ -108,7 +109,7 @@ describe("chat history flow", () => {
it("loads older pages until total is exhausted and keeps existing messages", async () => {
const requests: LoadMoreHistoryActorEvent[] = [];
const latestMessage: UiMessage = {
id: "latest",
displayId: "latest",
content: "current unlocked message",
isFromAI: true,
date: "2026-07-15",
@@ -173,8 +174,8 @@ describe("chat history flow", () => {
);
expect(requests[0]).toMatchObject({ offset: 50, limit: 50 });
expect(actor.getSnapshot().context.messages).toMatchObject([
{ id: "older-50" },
{ id: "latest", content: "current unlocked message" },
{ displayId: "older-50" },
{ displayId: "latest", content: "current unlocked message" },
]);
actor.send({ type: "ChatLoadMoreHistoryRequested" });
@@ -290,10 +291,10 @@ describe("chat history flow", () => {
});
});
function createHistoryMessage(id: string): UiMessage {
function createHistoryMessage(displayId: string): UiMessage {
return {
id,
content: `Message ${id}`,
displayId,
content: `Message ${displayId}`,
isFromAI: true,
date: "2026-07-15",
};