summaryrefslogtreecommitdiffhomepage
path: root/src/core/chunks/local-echo.test.ts
blob: 565017cfcb9919f18c2af2bac137fb952b2160a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { describe, expect, it } from "vitest";
import { appendUserMessage, applyHistory, foldEvent, initialState, removeLocalEcho } from "./index";

describe("request-local optimistic echo", () => {
  it("retains identity through turn-start tags without conflating identical sends", () => {
    let state = appendUserMessage(initialState(), "same", [{ url: "data:first" }], "r1");
    state = appendUserMessage(state, "same", [{ url: "data:second" }], "r2");
    state = foldEvent(state, { type: "turn-start", conversationId: "c1", turnId: "t1" });
    state = removeLocalEcho(state, "r1");
    expect(state.provisional.map((p) => p.localRequestId)).toEqual(["r2", "r2"]);
    expect(state.provisional.map((p) => p.chunk)).toEqual([
      { type: "text", text: "same" },
      { type: "image", url: "data:second" },
    ]);
    expect(state.generating).toBe(true);
    expect(removeLocalEcho(state, "r1")).toBe(state);
  });

  it("a live user-message confirms only its deduplicated text, not every identical local send", () => {
    let state = appendUserMessage(initialState(), "same", [{ url: "data:first" }], "r1");
    state = appendUserMessage(state, "same", [{ url: "data:second" }], "r2");
    state = foldEvent(state, { type: "turn-start", conversationId: "c1", turnId: "t1" });
    state = foldEvent(state, {
      type: "user-message",
      conversationId: "c1",
      turnId: "t1",
      text: "same",
    });
    state = removeLocalEcho(removeLocalEcho(state, "r1"), "r2");
    expect(state.provisional).toEqual([
      { role: "user", turnId: "t1", opening: true, chunk: { type: "text", text: "same" } },
    ]);
  });

  it("does not remove committed rows even if their content is identical", () => {
    let state = appendUserMessage(initialState(), "same", undefined, "r1");
    state = applyHistory(state, [{ seq: 1, role: "user", chunk: { type: "text", text: "same" } }]);
    const committed = state.committed;
    state = removeLocalEcho(state, "r1");
    expect(state.committed).toBe(committed);
    expect(state.provisional).toEqual([]);
    expect(removeLocalEcho(state, "r1")).toBe(state);
    expect(removeLocalEcho(initialState(), "r1")).toEqual(initialState());
  });
});