|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import type { ChatSessionPersistedState } from "../src/v3/chat.js"; |
| 2 | +import { TriggerChatTransport } from "../src/v3/chat.js"; |
3 | 3 | import { seedTranscriptCursor } from "../src/v3/chat-react.js"; |
4 | 4 |
|
5 | | -function fakeTransport(initial?: ChatSessionPersistedState) { |
6 | | - const sessions = new Map<string, ChatSessionPersistedState>(); |
7 | | - if (initial) sessions.set("chat-1", initial); |
8 | | - const calls: Array<{ chatId: string; session: ChatSessionPersistedState }> = []; |
9 | | - return { |
10 | | - calls, |
11 | | - sessions, |
12 | | - getSession: (chatId: string) => sessions.get(chatId), |
13 | | - setSession: (chatId: string, session: ChatSessionPersistedState) => { |
14 | | - calls.push({ chatId, session }); |
15 | | - sessions.set(chatId, session); |
16 | | - }, |
17 | | - }; |
| 5 | +function transportWithStart() { |
| 6 | + return new TriggerChatTransport({ |
| 7 | + task: "my-chat", |
| 8 | + accessToken: () => "pat", |
| 9 | + startSession: async () => ({ publicAccessToken: "pat-from-start" }), |
| 10 | + }); |
18 | 11 | } |
19 | 12 |
|
20 | 13 | describe("seedTranscriptCursor", () => { |
21 | | - it("moves the transport's resume cursor to the transcript's lastOutEventId", () => { |
22 | | - const transport = fakeTransport({ |
23 | | - publicAccessToken: "pat", |
24 | | - lastEventId: "3", |
25 | | - activeInputSeq: 2, |
26 | | - isStreaming: false, |
27 | | - }); |
28 | | - |
29 | | - expect(seedTranscriptCursor(transport, "chat-1", { lastOutEventId: "42" })).toBe(true); |
30 | | - expect(transport.calls).toEqual([ |
31 | | - { |
32 | | - chatId: "chat-1", |
33 | | - session: { |
34 | | - publicAccessToken: "pat", |
35 | | - lastEventId: "42", |
36 | | - activeInputSeq: 2, |
37 | | - isStreaming: false, |
38 | | - }, |
| 14 | + it("forwards the transcript's lastOutEventId to the transport", () => { |
| 15 | + const calls: Array<{ chatId: string; lastEventId: string }> = []; |
| 16 | + const transport = { |
| 17 | + seedResumeCursor: (chatId: string, lastEventId: string) => { |
| 18 | + calls.push({ chatId, lastEventId }); |
39 | 19 | }, |
40 | | - ]); |
41 | | - }); |
| 20 | + }; |
42 | 21 |
|
43 | | - it("does nothing when the transport does not know the session yet", () => { |
44 | | - const transport = fakeTransport(); |
45 | | - expect(seedTranscriptCursor(transport, "chat-1", { lastOutEventId: "42" })).toBe(false); |
46 | | - expect(transport.calls).toEqual([]); |
| 22 | + expect(seedTranscriptCursor(transport, "chat-1", { lastOutEventId: "42" })).toBe(true); |
| 23 | + expect(calls).toEqual([{ chatId: "chat-1", lastEventId: "42" }]); |
47 | 24 | }); |
48 | 25 |
|
49 | 26 | it("does nothing when the transcript carries no cursor", () => { |
50 | | - const transport = fakeTransport({ publicAccessToken: "pat", lastEventId: "3" }); |
| 27 | + const calls: string[] = []; |
| 28 | + const transport = { |
| 29 | + seedResumeCursor: (chatId: string) => { |
| 30 | + calls.push(chatId); |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
51 | 34 | expect(seedTranscriptCursor(transport, "chat-1", undefined)).toBe(false); |
52 | 35 | expect(seedTranscriptCursor(transport, "chat-1", { lastOutEventId: "" })).toBe(false); |
53 | | - expect(transport.calls).toEqual([]); |
54 | | - expect(transport.getSession("chat-1")?.lastEventId).toBe("3"); |
| 36 | + expect(calls).toEqual([]); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("TriggerChatTransport resume cursor", () => { |
| 41 | + it("holds a seeded cursor until the session is created, then applies it", async () => { |
| 42 | + const transport = transportWithStart(); |
| 43 | + |
| 44 | + seedTranscriptCursor(transport, "chat-1", { lastOutEventId: "42" }); |
| 45 | + expect(transport.getSession("chat-1")).toBeUndefined(); |
| 46 | + |
| 47 | + await transport.start("chat-1"); |
| 48 | + expect(transport.getSession("chat-1")?.lastEventId).toBe("42"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("applies a seeded cursor immediately when the session already exists", async () => { |
| 52 | + const transport = transportWithStart(); |
| 53 | + |
| 54 | + await transport.start("chat-1"); |
| 55 | + seedTranscriptCursor(transport, "chat-1", { lastOutEventId: "99" }); |
| 56 | + |
| 57 | + expect(transport.getSession("chat-1")?.lastEventId).toBe("99"); |
55 | 58 | }); |
56 | 59 | }); |
0 commit comments