The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | EventId, |
| 3 | GuestMessageId, |
| 4 | GuestShareId, |
| 5 | MessageId, |
| 6 | ProviderDriverKind, |
| 7 | RunId, |
| 8 | TurnItemId, |
| 9 | type GuestShareView, |
| 10 | type GuestStreamEvent, |
| 11 | type OrchestrationV2TurnItem, |
| 12 | type ThreadGuestMessage, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import * as DateTime from "effect/DateTime"; |
| 15 | import { describe, expect, it } from "vite-plus/test"; |
| 16 | |
| 17 | import { makeThreadProjectionFixture } from "../../test-fixtures"; |
| 18 | import { |
| 19 | applyGuestStreamEvent, |
| 20 | formatGuestExpiry, |
| 21 | guestFilesProblem, |
| 22 | initialGuestStreamState, |
| 23 | inviteShareId, |
| 24 | parseGuestStreamEvent, |
| 25 | unplacedGuestMessages, |
| 26 | type GuestStreamState, |
| 27 | } from "./guestStream.logic"; |
| 28 | |
| 29 | const view: GuestShareView = { |
| 30 | shareId: GuestShareId.make("share-1"), |
| 31 | title: "Fix login", |
| 32 | guestName: "Sam", |
| 33 | expiresAt: "2026-10-24T00:00:00.000Z", |
| 34 | messages: [], |
| 35 | model: { driver: null, name: "gpt-5.4" }, |
| 36 | }; |
| 37 | |
| 38 | const projection = makeThreadProjectionFixture(); |
| 39 | const now = DateTime.makeUnsafe("2026-10-23T10:00:00.000Z"); |
| 40 | |
| 41 | const base = { |
| 42 | threadId: projection.thread.id, |
| 43 | runId: RunId.make("run-1"), |
| 44 | nodeId: null, |
| 45 | providerThreadId: null, |
| 46 | providerTurnId: null, |
| 47 | nativeItemRef: null, |
| 48 | parentItemId: null, |
| 49 | ordinal: 1, |
| 50 | status: "completed", |
| 51 | title: null, |
| 52 | startedAt: now, |
| 53 | completedAt: now, |
| 54 | updatedAt: now, |
| 55 | } as const; |
| 56 | |
| 57 | const reply = (id: string, text: string): OrchestrationV2TurnItem => ({ |
| 58 | ...base, |
| 59 | id: TurnItemId.make(id), |
| 60 | type: "assistant_message", |
| 61 | messageId: MessageId.make(id), |
| 62 | text, |
| 63 | streaming: false, |
| 64 | }); |
| 65 | |
| 66 | const guestMessageRow = (id: string, guestMessageId: GuestMessageId): OrchestrationV2TurnItem => ({ |
| 67 | ...base, |
| 68 | id: TurnItemId.make(id), |
| 69 | type: "user_message", |
| 70 | createdBy: "user", |
| 71 | creationSource: "web", |
| 72 | messageId: MessageId.make(id), |
| 73 | inputIntent: "turn_start", |
| 74 | text: "Why?", |
| 75 | context: { version: 1, records: [], guest: { guestMessageId, name: "Sam", text: "Why?" } }, |
| 76 | attachments: [], |
| 77 | }); |
| 78 | |
| 79 | const snapshot = (snapshotSequence = 1): GuestStreamEvent => ({ |
| 80 | type: "thread", |
| 81 | item: { kind: "snapshot", snapshotSequence, projection }, |
| 82 | }); |
| 83 | |
| 84 | const itemEvent = (sequence: number, item: OrchestrationV2TurnItem): GuestStreamEvent => ({ |
| 85 | type: "thread", |
| 86 | item: { |
| 87 | kind: "event", |
| 88 | sequence, |
| 89 | event: { |
| 90 | id: EventId.make(`event-${sequence}`), |
| 91 | type: "turn-item.updated", |
| 92 | threadId: projection.thread.id, |
| 93 | driver: ProviderDriverKind.make("codex"), |
| 94 | providerInstanceId: projection.thread.providerInstanceId, |
| 95 | occurredAt: now, |
| 96 | payload: item, |
| 97 | }, |
| 98 | }, |
| 99 | }); |
| 100 | |
| 101 | const apply = (events: ReadonlyArray<GuestStreamEvent>, state = initialGuestStreamState) => |
| 102 | events.reduce<GuestStreamState>(applyGuestStreamEvent, state); |
| 103 | |
| 104 | const itemIds = (state: GuestStreamState) => |
| 105 | state.projection?.visibleTurnItems.map((row) => row.item.id) ?? []; |
| 106 | |
| 107 | describe("applyGuestStreamEvent", () => { |
| 108 | it("builds the thread from the snapshot and applies each later event once", () => { |
| 109 | const once = apply([{ type: "view", view }, snapshot(1), itemEvent(2, reply("a1", "Hi"))]); |
| 110 | expect(itemIds(once)).toEqual(["a1"]); |
| 111 | // A replayed or older event changes nothing. |
| 112 | expect(apply([itemEvent(2, reply("a1", "Changed"))], once)).toBe(once); |
| 113 | }); |
| 114 | |
| 115 | it("merges older pages in front, only for the cursor the thread is at", () => { |
| 116 | const windowed: GuestStreamEvent = { |
| 117 | type: "thread", |
| 118 | item: { |
| 119 | kind: "snapshot", |
| 120 | snapshotSequence: 1, |
| 121 | projection, |
| 122 | historyCursor: "cursor-1", |
| 123 | hasMoreHistory: true, |
| 124 | }, |
| 125 | }; |
| 126 | const page = (id: string, nextCursor: string | null) => ({ |
| 127 | snapshotSequence: 1, |
| 128 | items: [ |
| 129 | { |
| 130 | position: 0, |
| 131 | visibility: "local" as const, |
| 132 | sourceThreadId: projection.thread.id, |
| 133 | sourceItemId: TurnItemId.make(id), |
| 134 | item: reply(id, "Earlier"), |
| 135 | }, |
| 136 | ], |
| 137 | nextCursor, |
| 138 | hasMoreHistory: nextCursor !== null, |
| 139 | }); |
| 140 | const live = apply([windowed, itemEvent(2, reply("a1", "Hi"))]); |
| 141 | expect(live.historyCursor).toBe("cursor-1"); |
| 142 | // A page fetched before a reconnect moved the cursor changes nothing. |
| 143 | const stale = { type: "history-page" as const, cursor: "cursor-0", page: page("old", null) }; |
| 144 | expect(applyGuestStreamEvent(live, stale)).toBe(live); |
| 145 | const merged = applyGuestStreamEvent(live, { |
| 146 | type: "history-page", |
| 147 | cursor: "cursor-1", |
| 148 | page: page("o1", null), |
| 149 | }); |
| 150 | expect(itemIds(merged)).toEqual(["o1", "a1"]); |
| 151 | expect(merged.historyCursor).toBeNull(); |
| 152 | }); |
| 153 | |
| 154 | it("replaces the thread on a new snapshot, as after a reconnect", () => { |
| 155 | const before = apply([snapshot(1), itemEvent(2, reply("a1", "Hi")), { type: "ended" }]); |
| 156 | const after = apply([snapshot(1)], { ...before, ended: false }); |
| 157 | expect(itemIds(after)).toEqual([]); |
| 158 | }); |
| 159 | |
| 160 | it("ends when the view names another link than the page's", () => { |
| 161 | const state = apply([ |
| 162 | { type: "view", view }, |
| 163 | { type: "view", view: { ...view, shareId: GuestShareId.make("share-2") } }, |
| 164 | ]); |
| 165 | expect(state.ended).toBe(true); |
| 166 | expect(state.view?.shareId).toBe(view.shareId); |
| 167 | }); |
| 168 | |
| 169 | it("remembers a waiting message across a reconnect until its row arrives", () => { |
| 170 | const waiting = { |
| 171 | id: GuestMessageId.make("m1"), |
| 172 | text: "Why?", |
| 173 | status: "pending" as const, |
| 174 | submittedAt: "2026-10-23T10:00:00.000Z", |
| 175 | }; |
| 176 | const sent = { ...waiting, status: "sent" as const }; |
| 177 | const queued = apply([ |
| 178 | { type: "view", view: { ...view, messages: [waiting] } }, |
| 179 | snapshot(1), |
| 180 | { type: "view", view: { ...view, messages: [sent] } }, |
| 181 | ]); |
| 182 | expect([...queued.awaitingRowIds]).toEqual([waiting.id]); |
| 183 | |
| 184 | // Once placed, a later snapshot whose window no longer holds the row must not resurrect it. |
| 185 | const placed = apply([itemEvent(2, guestMessageRow("u1", waiting.id)), snapshot(3)], queued); |
| 186 | expect(placed.awaitingRowIds.size).toBe(0); |
| 187 | |
| 188 | const windowed = apply( |
| 189 | [ |
| 190 | { |
| 191 | type: "thread", |
| 192 | item: { |
| 193 | kind: "snapshot", |
| 194 | snapshotSequence: 3, |
| 195 | projection, |
| 196 | historyCursor: "cursor-1", |
| 197 | hasMoreHistory: true, |
| 198 | }, |
| 199 | }, |
| 200 | ], |
| 201 | queued, |
| 202 | ); |
| 203 | const historyPlaced = applyGuestStreamEvent(windowed, { |
| 204 | type: "history-page", |
| 205 | cursor: "cursor-1", |
| 206 | page: { |
| 207 | snapshotSequence: 3, |
| 208 | items: [ |
| 209 | { |
| 210 | position: 0, |
| 211 | visibility: "local", |
| 212 | sourceThreadId: projection.thread.id, |
| 213 | sourceItemId: TurnItemId.make("u1"), |
| 214 | item: guestMessageRow("u1", waiting.id), |
| 215 | }, |
| 216 | ], |
| 217 | nextCursor: null, |
| 218 | hasMoreHistory: false, |
| 219 | }, |
| 220 | }); |
| 221 | expect(historyPlaced.awaitingRowIds.size).toBe(0); |
| 222 | // The row is outside the reconnect's window again; the Sent bubble must not come back. |
| 223 | const reconnected = apply( |
| 224 | [{ type: "view", view: { ...view, messages: [sent] } }, snapshot(4)], |
| 225 | historyPlaced, |
| 226 | ); |
| 227 | expect( |
| 228 | unplacedGuestMessages( |
| 229 | reconnected.view!.messages, |
| 230 | reconnected.projection!.visibleTurnItems, |
| 231 | reconnected.awaitingRowIds, |
| 232 | ), |
| 233 | ).toEqual([]); |
| 234 | }); |
| 235 | }); |
| 236 | |
| 237 | describe("parseGuestStreamEvent", () => { |
| 238 | it("decodes a valid frame and rejects malformed ones", () => { |
| 239 | expect(parseGuestStreamEvent(JSON.stringify({ type: "ended" }))).toEqual({ type: "ended" }); |
| 240 | expect(parseGuestStreamEvent("{not json")).toBeNull(); |
| 241 | expect(parseGuestStreamEvent(JSON.stringify({ type: "unknown" }))).toBeNull(); |
| 242 | }); |
| 243 | }); |
| 244 | |
| 245 | describe("unplacedGuestMessages", () => { |
| 246 | const message = (id: string, status: ThreadGuestMessage["status"], submittedAt: string) => ({ |
| 247 | id: GuestMessageId.make(id), |
| 248 | text: id, |
| 249 | status, |
| 250 | submittedAt, |
| 251 | }); |
| 252 | |
| 253 | it("shows pending ones, and resolved ones only if this page saw them waiting", () => { |
| 254 | const messages = [ |
| 255 | message("late", "pending", "2026-10-23T10:05:00.000Z"), |
| 256 | message("placed", "sent", "2026-10-23T10:00:00.000Z"), |
| 257 | message("queued", "sent", "2026-10-23T09:59:00.000Z"), |
| 258 | message("dismissed", "dismissed", "2026-10-23T10:01:00.000Z"), |
| 259 | message("old-history", "sent", "2026-10-23T09:00:00.000Z"), |
| 260 | ]; |
| 261 | const seen = new Set( |
| 262 | ["placed", "queued", "dismissed", "late"].map((id) => GuestMessageId.make(id)), |
| 263 | ); |
| 264 | const items = [ |
| 265 | { |
| 266 | position: 0, |
| 267 | visibility: "local" as const, |
| 268 | sourceThreadId: projection.thread.id, |
| 269 | sourceItemId: TurnItemId.make("u1"), |
| 270 | item: guestMessageRow("u1", GuestMessageId.make("placed")), |
| 271 | }, |
| 272 | ]; |
| 273 | expect(unplacedGuestMessages(messages, items, seen).map((m) => m.id)).toEqual([ |
| 274 | "queued", |
| 275 | "dismissed", |
| 276 | "late", |
| 277 | ]); |
| 278 | }); |
| 279 | }); |
| 280 | |
| 281 | describe("formatGuestExpiry", () => { |
| 282 | const now = Date.parse("2026-10-23T00:00:00.000Z"); |
| 283 | it("shows whole hours, then minutes, then expired", () => { |
| 284 | expect(formatGuestExpiry("2026-10-23T23:59:00.000Z", now)).toBe("23h left"); |
| 285 | expect(formatGuestExpiry("2026-10-23T00:45:30.000Z", now)).toBe("45m left"); |
| 286 | expect(formatGuestExpiry("2026-10-23T00:00:00.000Z", now)).toBe("Expired"); |
| 287 | }); |
| 288 | }); |
| 289 | |
| 290 | describe("inviteShareId", () => { |
| 291 | it("reads the share id before the signature and rejects anything else", () => { |
| 292 | expect(inviteShareId("k3J9xQ2mT8vL5nR1pW7yZa.sig")).toBe("k3J9xQ2mT8vL5nR1pW7yZa"); |
| 293 | expect(inviteShareId("no-signature")).toBeNull(); |
| 294 | expect(inviteShareId(".sig")).toBeNull(); |
| 295 | expect(inviteShareId("")).toBeNull(); |
| 296 | }); |
| 297 | }); |
| 298 | |
| 299 | describe("guestFilesProblem", () => { |
| 300 | const file = (type: string, size = 1) => ({ type, size }); |
| 301 | it("takes up to four files and 10 MB, with images in the types the owner's composer takes", () => { |
| 302 | expect(guestFilesProblem([file("image/png"), file("application/pdf"), file("")])).toBeNull(); |
| 303 | expect(guestFilesProblem([1, 2, 3, 4, 5].map(() => file("image/png")))).toMatch(/up to 4/); |
| 304 | expect(guestFilesProblem([file("image/png", 10 * 1024 * 1024 + 1)])).toMatch(/10 MB/); |
| 305 | expect(guestFilesProblem([file("image/svg+xml")])).toMatch(/PNG, JPEG, GIF, or WebP/); |
| 306 | expect(guestFilesProblem([file("text/plain", 0)])).toMatch(/cannot be attached/); |
| 307 | }); |
| 308 | }); |