Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadReducer.test.ts

773 lines25.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 CheckpointRef,
5 EventId,
6 MessageId,
7 ProjectId,
8 ProviderInstanceId,
9 ThreadId,
10 TurnId,
11} from "@t3tools/contracts";
12import type { OrchestrationThread, OrchestrationThreadActivity } from "@t3tools/contracts";
13​
14import {
15 applyThreadDetailEvent,
16 liveWindowOldestActivityId,
17 oldestActivityByChronology,
18} from "./threadReducer.ts";
19​
20const activity = (id: string, createdAt: string, sequence?: number): OrchestrationThreadActivity =>
21 ({
22 id,
23 tone: "tool",
24 kind: "command",
25 summary: id,
26 payload: {},
27 turnId: TurnId.make("turn-1"),
28 createdAt,
29 ...(sequence !== undefined ? { sequence } : {}),
30 }) as unknown as OrchestrationThreadActivity;
31​
32const baseEventFields = {
33 eventId: EventId.make("event-1"),
34 commandId: null,
35 causationEventId: null,
36 correlationId: null,
37 metadata: {},
38} as const;
39​
40const baseThread: OrchestrationThread = {
41 id: ThreadId.make("thread-1"),
42 projectId: ProjectId.make("project-1"),
43 title: "Test Thread",
44 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
45 runtimeMode: "full-access",
46 interactionMode: "default",
47 branch: null,
48 worktreePath: null,
49 latestTurn: null,
50 createdAt: "2026-04-01T00:00:00.000Z",
51 updatedAt: "2026-04-01T00:00:00.000Z",
52 archivedAt: null,
53 deletedAt: null,
54 messages: [],
55 proposedPlans: [],
56 activities: [],
57 checkpoints: [],
58 session: null,
59};
60​
61describe("applyThreadDetailEvent", () => {
62 describe("project events", () => {
63 it("returns unchanged for project.created", () => {
64 const result = applyThreadDetailEvent(baseThread, {
65 ...baseEventFields,
66 sequence: 1,
67 occurredAt: "2026-04-01T01:00:00.000Z",
68 aggregateKind: "project",
69 aggregateId: ProjectId.make("project-1"),
70 type: "project.created",
71 payload: {
72 projectId: ProjectId.make("project-1"),
73 title: "T3 Code",
74 workspaceRoot: "/repo",
75 repositoryIdentity: null,
76 defaultModelSelection: null,
77 scripts: [],
78 createdAt: "2026-04-01T01:00:00.000Z",
79 updatedAt: "2026-04-01T01:00:00.000Z",
80 deletedAt: null,
81 },
82 } as any);
83 expect(result.kind).toBe("unchanged");
84 });
85 });
86​
87 describe("thread.created", () => {
88 it("creates a fresh thread", () => {
89 const result = applyThreadDetailEvent(baseThread, {
90 ...baseEventFields,
91 sequence: 1,
92 occurredAt: "2026-04-01T01:00:00.000Z",
93 aggregateKind: "thread",
94 aggregateId: ThreadId.make("thread-2"),
95 type: "thread.created",
96 payload: {
97 threadId: ThreadId.make("thread-2"),
98 projectId: ProjectId.make("project-1"),
99 title: "New Thread",
100 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
101 runtimeMode: "full-access",
102 interactionMode: "default",
103 branch: "main",
104 worktreePath: null,
105 createdAt: "2026-04-01T01:00:00.000Z",
106 updatedAt: "2026-04-01T01:00:00.000Z",
107 },
108 });
109​
110 expect(result.kind).toBe("updated");
111 if (result.kind === "updated") {
112 expect(result.thread.id).toBe("thread-2");
113 expect(result.thread.title).toBe("New Thread");
114 expect(result.thread.branch).toBe("main");
115 expect(result.thread.messages).toEqual([]);
116 expect(result.thread.session).toBeNull();
117 }
118 });
119 });
120​
121 describe("thread.deleted", () => {
122 it("returns deleted signal", () => {
123 const result = applyThreadDetailEvent(baseThread, {
124 ...baseEventFields,
125 sequence: 2,
126 occurredAt: "2026-04-01T02:00:00.000Z",
127 aggregateKind: "thread",
128 aggregateId: ThreadId.make("thread-1"),
129 type: "thread.deleted",
130 payload: {
131 threadId: ThreadId.make("thread-1"),
132 deletedAt: "2026-04-01T02:00:00.000Z",
133 },
134 });
135 expect(result.kind).toBe("deleted");
136 });
137 });
138​
139 describe("thread.archived / thread.unarchived", () => {
140 it("sets archivedAt", () => {
141 const result = applyThreadDetailEvent(baseThread, {
142 ...baseEventFields,
143 sequence: 3,
144 occurredAt: "2026-04-01T03:00:00.000Z",
145 aggregateKind: "thread",
146 aggregateId: ThreadId.make("thread-1"),
147 type: "thread.archived",
148 payload: {
149 threadId: ThreadId.make("thread-1"),
150 archivedAt: "2026-04-01T03:00:00.000Z",
151 updatedAt: "2026-04-01T03:00:00.000Z",
152 },
153 });
154​
155 expect(result.kind).toBe("updated");
156 if (result.kind === "updated") {
157 expect(result.thread.archivedAt).toBe("2026-04-01T03:00:00.000Z");
158 }
159 });
160​
161 it("clears archivedAt", () => {
162 const archivedThread = { ...baseThread, archivedAt: "2026-04-01T03:00:00.000Z" };
163 const result = applyThreadDetailEvent(archivedThread, {
164 ...baseEventFields,
165 sequence: 4,
166 occurredAt: "2026-04-01T04:00:00.000Z",
167 aggregateKind: "thread",
168 aggregateId: ThreadId.make("thread-1"),
169 type: "thread.unarchived",
170 payload: {
171 threadId: ThreadId.make("thread-1"),
172 updatedAt: "2026-04-01T04:00:00.000Z",
173 },
174 });
175​
176 expect(result.kind).toBe("updated");
177 if (result.kind === "updated") {
178 expect(result.thread.archivedAt).toBeNull();
179 }
180 });
181 });
182​
183 describe("thread.meta-updated", () => {
184 it("patches title and branch", () => {
185 const result = applyThreadDetailEvent(baseThread, {
186 ...baseEventFields,
187 sequence: 5,
188 occurredAt: "2026-04-01T05:00:00.000Z",
189 aggregateKind: "thread",
190 aggregateId: ThreadId.make("thread-1"),
191 type: "thread.meta-updated",
192 payload: {
193 threadId: ThreadId.make("thread-1"),
194 title: "Updated Title",
195 branch: "feature/demo",
196 updatedAt: "2026-04-01T05:00:00.000Z",
197 },
198 });
199​
200 expect(result.kind).toBe("updated");
201 if (result.kind === "updated") {
202 expect(result.thread.title).toBe("Updated Title");
203 expect(result.thread.branch).toBe("feature/demo");
204 // Model selection should be unchanged since it wasn't in the payload
205 expect(result.thread.modelSelection).toEqual(baseThread.modelSelection);
206 }
207 });
208 });
209​
210 describe("thread.message-sent", () => {
211 it("appends a new message", () => {
212 const result = applyThreadDetailEvent(baseThread, {
213 ...baseEventFields,
214 sequence: 6,
215 occurredAt: "2026-04-01T06:00:00.000Z",
216 aggregateKind: "thread",
217 aggregateId: ThreadId.make("thread-1"),
218 type: "thread.message-sent",
219 payload: {
220 threadId: ThreadId.make("thread-1"),
221 messageId: MessageId.make("msg-1"),
222 role: "user",
223 text: "Hello, world!",
224 turnId: null,
225 streaming: false,
226 createdAt: "2026-04-01T06:00:00.000Z",
227 updatedAt: "2026-04-01T06:00:00.000Z",
228 },
229 });
230​
231 expect(result.kind).toBe("updated");
232 if (result.kind === "updated") {
233 expect(result.thread.messages).toHaveLength(1);
234 expect(result.thread.messages[0]?.text).toBe("Hello, world!");
235 }
236 });
237​
238 it("appends text for streaming messages", () => {
239 const threadWithMessage: OrchestrationThread = {
240 ...baseThread,
241 messages: [
242 {
243 id: MessageId.make("msg-2"),
244 role: "assistant",
245 text: "Hello",
246 turnId: TurnId.make("turn-1"),
247 streaming: true,
248 createdAt: "2026-04-01T06:00:00.000Z",
249 updatedAt: "2026-04-01T06:00:00.000Z",
250 },
251 ],
252 };
253​
254 const result = applyThreadDetailEvent(threadWithMessage, {
255 ...baseEventFields,
256 sequence: 7,
257 occurredAt: "2026-04-01T06:01:00.000Z",
258 aggregateKind: "thread",
259 aggregateId: ThreadId.make("thread-1"),
260 type: "thread.message-sent",
261 payload: {
262 threadId: ThreadId.make("thread-1"),
263 messageId: MessageId.make("msg-2"),
264 role: "assistant",
265 text: ", world!",
266 turnId: TurnId.make("turn-1"),
267 streaming: true,
268 createdAt: "2026-04-01T06:00:00.000Z",
269 updatedAt: "2026-04-01T06:01:00.000Z",
270 },
271 });
272​
273 expect(result.kind).toBe("updated");
274 if (result.kind === "updated") {
275 expect(result.thread.messages).toHaveLength(1);
276 expect(result.thread.messages[0]?.text).toBe("Hello, world!");
277 }
278 });
279​
280 it("updates latestTurn for assistant messages with a turn", () => {
281 const result = applyThreadDetailEvent(baseThread, {
282 ...baseEventFields,
283 sequence: 8,
284 occurredAt: "2026-04-01T07:00:00.000Z",
285 aggregateKind: "thread",
286 aggregateId: ThreadId.make("thread-1"),
287 type: "thread.message-sent",
288 payload: {
289 threadId: ThreadId.make("thread-1"),
290 messageId: MessageId.make("msg-3"),
291 role: "assistant",
292 text: "Done.",
293 turnId: TurnId.make("turn-1"),
294 streaming: false,
295 createdAt: "2026-04-01T07:00:00.000Z",
296 updatedAt: "2026-04-01T07:00:00.000Z",
297 },
298 });
299​
300 expect(result.kind).toBe("updated");
301 if (result.kind === "updated") {
302 expect(result.thread.latestTurn?.turnId).toBe("turn-1");
303 expect(result.thread.latestTurn?.state).toBe("completed");
304 expect(result.thread.latestTurn?.assistantMessageId).toBe("msg-3");
305 }
306 });
307​
308 it("keeps latestTurn running for interim assistant messages while the session runs the turn", () => {
309 const threadWithRunningSession: OrchestrationThread = {
310 ...baseThread,
311 session: {
312 threadId: ThreadId.make("thread-1"),
313 status: "running",
314 providerName: "claude",
315 runtimeMode: "full-access",
316 activeTurnId: TurnId.make("turn-1"),
317 lastError: null,
318 updatedAt: "2026-04-01T06:59:00.000Z",
319 },
320 latestTurn: {
321 turnId: TurnId.make("turn-1"),
322 state: "running",
323 requestedAt: "2026-04-01T06:59:00.000Z",
324 startedAt: "2026-04-01T06:59:00.000Z",
325 completedAt: null,
326 assistantMessageId: null,
327 },
328 };
329​
330 const result = applyThreadDetailEvent(threadWithRunningSession, {
331 ...baseEventFields,
332 sequence: 8,
333 occurredAt: "2026-04-01T07:00:00.000Z",
334 aggregateKind: "thread",
335 aggregateId: ThreadId.make("thread-1"),
336 type: "thread.message-sent",
337 payload: {
338 threadId: ThreadId.make("thread-1"),
339 messageId: MessageId.make("msg-3"),
340 role: "assistant",
341 text: "Interim commentary between tool calls.",
342 turnId: TurnId.make("turn-1"),
343 streaming: false,
344 createdAt: "2026-04-01T07:00:00.000Z",
345 updatedAt: "2026-04-01T07:00:00.000Z",
346 },
347 });
348​
349 expect(result.kind).toBe("updated");
350 if (result.kind === "updated") {
351 expect(result.thread.latestTurn?.state).toBe("running");
352 expect(result.thread.latestTurn?.completedAt).toBeNull();
353 }
354 });
355 });
356​
357 describe("thread.session-set", () => {
358 it("settles a running latestTurn when the session leaves the running status", () => {
359 const threadWithRunningTurn: OrchestrationThread = {
360 ...baseThread,
361 latestTurn: {
362 turnId: TurnId.make("turn-1"),
363 state: "running",
364 requestedAt: "2026-04-01T07:00:00.000Z",
365 startedAt: "2026-04-01T07:00:00.000Z",
366 completedAt: null,
367 assistantMessageId: MessageId.make("msg-3"),
368 },
369 };
370​
371 const result = applyThreadDetailEvent(threadWithRunningTurn, {
372 ...baseEventFields,
373 sequence: 9,
374 occurredAt: "2026-04-01T08:00:00.000Z",
375 aggregateKind: "thread",
376 aggregateId: ThreadId.make("thread-1"),
377 type: "thread.session-set",
378 payload: {
379 threadId: ThreadId.make("thread-1"),
380 session: {
381 threadId: ThreadId.make("thread-1"),
382 status: "ready",
383 providerName: "claude",
384 runtimeMode: "full-access",
385 activeTurnId: null,
386 lastError: null,
387 updatedAt: "2026-04-01T08:00:00.000Z",
388 },
389 },
390 });
391​
392 expect(result.kind).toBe("updated");
393 if (result.kind === "updated") {
394 expect(result.thread.latestTurn?.state).toBe("completed");
395 expect(result.thread.latestTurn?.completedAt).toBe("2026-04-01T08:00:00.000Z");
396 }
397 });
398​
399 it("updates session and latestTurn for a running session", () => {
400 const result = applyThreadDetailEvent(baseThread, {
401 ...baseEventFields,
402 sequence: 9,
403 occurredAt: "2026-04-01T08:00:00.000Z",
404 aggregateKind: "thread",
405 aggregateId: ThreadId.make("thread-1"),
406 type: "thread.session-set",
407 payload: {
408 threadId: ThreadId.make("thread-1"),
409 session: {
410 threadId: ThreadId.make("thread-1"),
411 status: "running",
412 providerName: "codex",
413 runtimeMode: "full-access",
414 activeTurnId: TurnId.make("turn-1"),
415 lastError: null,
416 updatedAt: "2026-04-01T08:00:00.000Z",
417 },
418 },
419 });
420​
421 expect(result.kind).toBe("updated");
422 if (result.kind === "updated") {
423 expect(result.thread.session?.status).toBe("running");
424 expect(result.thread.latestTurn?.turnId).toBe("turn-1");
425 expect(result.thread.latestTurn?.state).toBe("running");
426 }
427 });
428 });
429​
430 describe("thread.session-stop-requested", () => {
431 it("marks session as stopped", () => {
432 const threadWithSession: OrchestrationThread = {
433 ...baseThread,
434 session: {
435 threadId: ThreadId.make("thread-1"),
436 status: "running",
437 providerName: "codex",
438 runtimeMode: "full-access",
439 activeTurnId: TurnId.make("turn-1"),
440 lastError: null,
441 updatedAt: "2026-04-01T08:00:00.000Z",
442 },
443 };
444​
445 const result = applyThreadDetailEvent(threadWithSession, {
446 ...baseEventFields,
447 sequence: 10,
448 occurredAt: "2026-04-01T09:00:00.000Z",
449 aggregateKind: "thread",
450 aggregateId: ThreadId.make("thread-1"),
451 type: "thread.session-stop-requested",
452 payload: {
453 threadId: ThreadId.make("thread-1"),
454 createdAt: "2026-04-01T09:00:00.000Z",
455 },
456 });
457​
458 expect(result.kind).toBe("updated");
459 if (result.kind === "updated") {
460 expect(result.thread.session?.status).toBe("stopped");
461 expect(result.thread.session?.activeTurnId).toBeNull();
462 }
463 });
464​
465 it("returns unchanged when no session exists", () => {
466 const result = applyThreadDetailEvent(baseThread, {
467 ...baseEventFields,
468 sequence: 10,
469 occurredAt: "2026-04-01T09:00:00.000Z",
470 aggregateKind: "thread",
471 aggregateId: ThreadId.make("thread-1"),
472 type: "thread.session-stop-requested",
473 payload: {
474 threadId: ThreadId.make("thread-1"),
475 createdAt: "2026-04-01T09:00:00.000Z",
476 },
477 });
478 expect(result.kind).toBe("unchanged");
479 });
480 });
481​
482 describe("thread.proposed-plan-upserted", () => {
483 it("adds a proposed plan", () => {
484 const result = applyThreadDetailEvent(baseThread, {
485 ...baseEventFields,
486 sequence: 11,
487 occurredAt: "2026-04-01T10:00:00.000Z",
488 aggregateKind: "thread",
489 aggregateId: ThreadId.make("thread-1"),
490 type: "thread.proposed-plan-upserted",
491 payload: {
492 threadId: ThreadId.make("thread-1"),
493 proposedPlan: {
494 id: "plan-1",
495 turnId: TurnId.make("turn-1"),
496 planMarkdown: "## Plan\n- Do stuff",
497 implementedAt: null,
498 implementationThreadId: null,
499 createdAt: "2026-04-01T10:00:00.000Z",
500 updatedAt: "2026-04-01T10:00:00.000Z",
501 },
502 },
503 });
504​
505 expect(result.kind).toBe("updated");
506 if (result.kind === "updated") {
507 expect(result.thread.proposedPlans).toHaveLength(1);
508 expect(result.thread.proposedPlans[0]?.id).toBe("plan-1");
509 }
510 });
511 });
512​
513 describe("thread.activity-appended", () => {
514 it("adds an activity", () => {
515 const result = applyThreadDetailEvent(baseThread, {
516 ...baseEventFields,
517 sequence: 12,
518 occurredAt: "2026-04-01T11:00:00.000Z",
519 aggregateKind: "thread",
520 aggregateId: ThreadId.make("thread-1"),
521 type: "thread.activity-appended",
522 payload: {
523 threadId: ThreadId.make("thread-1"),
524 activity: {
525 id: EventId.make("activity-1"),
526 tone: "tool",
527 kind: "file-edit",
528 summary: "Edited src/index.ts",
529 payload: {},
530 turnId: TurnId.make("turn-1"),
531 createdAt: "2026-04-01T11:00:00.000Z",
532 },
533 },
534 });
535​
536 expect(result.kind).toBe("updated");
537 if (result.kind === "updated") {
538 expect(result.thread.activities).toHaveLength(1);
539 expect(result.thread.activities[0]?.kind).toBe("file-edit");
540 }
541 });
542​
543 it("preserves the complete activity history when live events arrive", () => {
544 const existingActivities = Array.from({ length: 129 }, (_, index) => ({
545 id: EventId.make(`activity-${index}`),
546 tone: "tool" as const,
547 kind: "command",
548 summary: `Ran command ${index}`,
549 payload: {},
550 turnId: TurnId.make("turn-1"),
551 sequence: index,
552 createdAt: "2026-04-01T11:00:00.000Z",
553 }));
554 const result = applyThreadDetailEvent(
555 { ...baseThread, activities: existingActivities },
556 {
557 ...baseEventFields,
558 sequence: 130,
559 occurredAt: "2026-04-01T11:01:00.000Z",
560 aggregateKind: "thread",
561 aggregateId: ThreadId.make("thread-1"),
562 type: "thread.activity-appended",
563 payload: {
564 threadId: ThreadId.make("thread-1"),
565 activity: {
566 id: EventId.make("activity-129"),
567 tone: "tool",
568 kind: "command",
569 summary: "Ran command 129",
570 payload: {},
571 turnId: TurnId.make("turn-1"),
572 sequence: 129,
573 createdAt: "2026-04-01T11:01:00.000Z",
574 },
575 },
576 },
577 );
578​
579 expect(result.kind).toBe("updated");
580 if (result.kind === "updated") {
581 expect(result.thread.activities).toHaveLength(130);
582 expect(result.thread.activities[0]?.id).toBe("activity-0");
583 }
584 });
585 });
586​
587 describe("thread.turn-diff-completed", () => {
588 it("adds a checkpoint and updates latestTurn", () => {
589 const result = applyThreadDetailEvent(baseThread, {
590 ...baseEventFields,
591 sequence: 13,
592 occurredAt: "2026-04-01T12:00:00.000Z",
593 aggregateKind: "thread",
594 aggregateId: ThreadId.make("thread-1"),
595 type: "thread.turn-diff-completed",
596 payload: {
597 threadId: ThreadId.make("thread-1"),
598 turnId: TurnId.make("turn-1"),
599 checkpointTurnCount: 1,
600 checkpointRef: CheckpointRef.make("ref-1"),
601 status: "ready",
602 files: [],
603 assistantMessageId: MessageId.make("msg-3"),
604 completedAt: "2026-04-01T12:00:00.000Z",
605 },
606 });
607​
608 expect(result.kind).toBe("updated");
609 if (result.kind === "updated") {
610 expect(result.thread.checkpoints).toHaveLength(1);
611 expect(result.thread.latestTurn?.turnId).toBe("turn-1");
612 expect(result.thread.latestTurn?.state).toBe("completed");
613 }
614 });
615 });
616​
617 describe("thread.reverted", () => {
618 it("filters entities to retained turns", () => {
619 const threadWithData: OrchestrationThread = {
620 ...baseThread,
621 messages: [
622 {
623 id: MessageId.make("msg-1"),
624 role: "user",
625 text: "First",
626 turnId: null,
627 streaming: false,
628 createdAt: "2026-04-01T01:00:00.000Z",
629 updatedAt: "2026-04-01T01:00:00.000Z",
630 },
631 {
632 id: MessageId.make("msg-2"),
633 role: "assistant",
634 text: "Response 1",
635 turnId: TurnId.make("turn-1"),
636 streaming: false,
637 createdAt: "2026-04-01T02:00:00.000Z",
638 updatedAt: "2026-04-01T02:00:00.000Z",
639 },
640 {
641 id: MessageId.make("msg-3"),
642 role: "assistant",
643 text: "Response 2",
644 turnId: TurnId.make("turn-2"),
645 streaming: false,
646 createdAt: "2026-04-01T03:00:00.000Z",
647 updatedAt: "2026-04-01T03:00:00.000Z",
648 },
649 ],
650 checkpoints: [
651 {
652 turnId: TurnId.make("turn-1"),
653 checkpointTurnCount: 1,
654 checkpointRef: CheckpointRef.make("ref-1"),
655 status: "ready",
656 files: [],
657 assistantMessageId: MessageId.make("msg-2"),
658 completedAt: "2026-04-01T02:00:00.000Z",
659 },
660 {
661 turnId: TurnId.make("turn-2"),
662 checkpointTurnCount: 2,
663 checkpointRef: CheckpointRef.make("ref-2"),
664 status: "ready",
665 files: [],
666 assistantMessageId: MessageId.make("msg-3"),
667 completedAt: "2026-04-01T03:00:00.000Z",
668 },
669 ],
670 };
671​
672 const result = applyThreadDetailEvent(threadWithData, {
673 ...baseEventFields,
674 sequence: 14,
675 occurredAt: "2026-04-01T04:00:00.000Z",
676 aggregateKind: "thread",
677 aggregateId: ThreadId.make("thread-1"),
678 type: "thread.reverted",
679 payload: {
680 threadId: ThreadId.make("thread-1"),
681 turnCount: 1,
682 },
683 });
684​
685 expect(result.kind).toBe("updated");
686 if (result.kind === "updated") {
687 // turn-2 checkpoint is filtered out (turnCount 2 > revert target 1)
688 expect(result.thread.checkpoints).toHaveLength(1);
689 expect(result.thread.checkpoints[0]?.turnId).toBe("turn-1");
690 // msg-3 (turn-2) is filtered, msg-1 (no turn) and msg-2 (turn-1) remain
691 expect(result.thread.messages).toHaveLength(2);
692 expect(result.thread.latestTurn?.turnId).toBe("turn-1");
693 }
694 });
695 });
696​
697 describe("no-op events", () => {
698 it("returns unchanged for approval-response-requested", () => {
699 const result = applyThreadDetailEvent(baseThread, {
700 ...baseEventFields,
701 sequence: 15,
702 occurredAt: "2026-04-01T13:00:00.000Z",
703 aggregateKind: "thread",
704 aggregateId: ThreadId.make("thread-1"),
705 type: "thread.approval-response-requested",
706 payload: {
707 threadId: ThreadId.make("thread-1"),
708 requestId: "req-1",
709 decision: "approve",
710 createdAt: "2026-04-01T13:00:00.000Z",
711 },
712 } as any);
713 expect(result.kind).toBe("unchanged");
714 });
715 });
716​
717 describe("liveWindowOldestActivityId", () => {
718 it("returns null for an empty window", () => {
719 expect(liveWindowOldestActivityId([])).toBeNull();
720 });
721​
722 it("returns the chronologically-oldest id regardless of array position", () => {
723 // Reducer order places the unsequenced legacy row (oldest) LAST while the
724 // server snapshot would list it first; the helper picks it either way.
725 const window = [
726 activity("seq-1", "2026-04-01T10:00:01.000Z", 1),
727 activity("seq-2", "2026-04-01T10:00:02.000Z", 2),
728 activity("legacy", "2026-04-01T09:00:00.000Z"),
729 ];
730 expect(liveWindowOldestActivityId(window)).toBe("legacy");
731 });
732​
733 it("breaks createdAt ties by id", () => {
734 const window = [
735 activity("b", "2026-04-01T10:00:00.000Z", 2),
736 activity("a", "2026-04-01T10:00:00.000Z", 1),
737 ];
738 expect(liveWindowOldestActivityId(window)).toBe("a");
739 });
740​
741 it("is stable when a newer activity is appended (no false reshape)", () => {
742 const before = [
743 activity("legacy", "2026-04-01T09:00:00.000Z"),
744 activity("seq-1", "2026-04-01T10:00:01.000Z", 1),
745 ];
746 // A live append is the newest activity and is unsequenced in the payload;
747 // it must not change the detected oldest boundary.
748 const after = [...before, activity("appended", "2026-04-01T11:00:00.000Z")];
749 expect(liveWindowOldestActivityId(after)).toBe(liveWindowOldestActivityId(before));
750 expect(liveWindowOldestActivityId(after)).toBe("legacy");
751 });
752 });
753​
754 describe("oldestActivityByChronology", () => {
755 it("returns null for an empty set", () => {
756 expect(oldestActivityByChronology([])).toBeNull();
757 });
758​
759 it("returns the unsequenced legacy row so the pagination cursor agrees with the sentinel", () => {
760 // The reducer placed the legacy (unsequenced, oldest) row at the END; paging
761 // must cursor from it (createdAt cursor), not from index 0's sequenced row.
762 const merged = [
763 activity("seq-5", "2026-04-01T10:00:05.000Z", 5),
764 activity("seq-6", "2026-04-01T10:00:06.000Z", 6),
765 activity("legacy", "2026-04-01T09:00:00.000Z"),
766 ];
767 const oldest = oldestActivityByChronology(merged);
768 expect(oldest?.id).toBe("legacy");
769 expect(oldest?.sequence).toBeUndefined(); // → drives the unsequenced cursor
770 expect(liveWindowOldestActivityId(merged)).toBe(oldest?.id); // sentinel agrees
771 });
772 });
773});