Open History to see the last commit
1import {
2 EventId,
3 MessageId,
4 ThreadId,
5 TurnId,
6 type OrchestrationThreadActivity,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vitest";
9
10import {
11 deriveActiveWorkStartedAt,
12 deriveActivePlanState,
13 PROVIDER_OPTIONS,
14 derivePendingApprovals,
15 derivePendingUserInputs,
16 deriveTimelineEntries,
17 deriveWorkLogEntries,
18 findLatestProposedPlan,
19 findSidebarProposedPlan,
20 hasActionableProposedPlan,
21 hasToolActivityForTurn,
22 isLatestTurnSettled,
23} from "./session-logic";
24
25function makeActivity(overrides: {
26 id?: string;
27 createdAt?: string;
28 kind?: string;
29 summary?: string;
30 tone?: OrchestrationThreadActivity["tone"];
31 payload?: Record<string, unknown>;
32 turnId?: string;
33 sequence?: number;
34}): OrchestrationThreadActivity {
35 const payload = overrides.payload ?? {};
36 return {
37 id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()),
38 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
39 kind: overrides.kind ?? "tool.started",
40 summary: overrides.summary ?? "Tool call",
41 tone: overrides.tone ?? "tool",
42 payload,
43 turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null,
44 workUnitId: null,
45 ...(overrides.sequence !== undefined ? { sequence: overrides.sequence } : {}),
46 };
47}
48
49describe("derivePendingApprovals", () => {
50 it("tracks open approvals and removes resolved ones", () => {
51 const activities: OrchestrationThreadActivity[] = [
52 makeActivity({
53 id: "approval-open",
54 createdAt: "2026-02-23T00:00:01.000Z",
55 kind: "approval.requested",
56 summary: "Command approval requested",
57 tone: "approval",
58 payload: {
59 requestId: "req-1",
60 requestKind: "command",
61 detail: "bun run lint",
62 },
63 }),
64 makeActivity({
65 id: "approval-close",
66 createdAt: "2026-02-23T00:00:02.000Z",
67 kind: "approval.resolved",
68 summary: "Approval resolved",
69 tone: "info",
70 payload: { requestId: "req-2" },
71 }),
72 makeActivity({
73 id: "approval-closed-request",
74 createdAt: "2026-02-23T00:00:01.500Z",
75 kind: "approval.requested",
76 summary: "File-change approval requested",
77 tone: "approval",
78 payload: { requestId: "req-2", requestKind: "file-change" },
79 }),
80 ];
81
82 expect(derivePendingApprovals(activities)).toEqual([
83 {
84 requestId: "req-1",
85 requestKind: "command",
86 createdAt: "2026-02-23T00:00:01.000Z",
87 detail: "bun run lint",
88 },
89 ]);
90 });
91
92 it("maps canonical requestType payloads into pending approvals", () => {
93 const activities: OrchestrationThreadActivity[] = [
94 makeActivity({
95 id: "approval-open-request-type",
96 createdAt: "2026-02-23T00:00:01.000Z",
97 kind: "approval.requested",
98 summary: "Command approval requested",
99 tone: "approval",
100 payload: {
101 requestId: "req-request-type",
102 requestType: "command_execution_approval",
103 detail: "pwd",
104 },
105 }),
106 ];
107
108 expect(derivePendingApprovals(activities)).toEqual([
109 {
110 requestId: "req-request-type",
111 requestKind: "command",
112 createdAt: "2026-02-23T00:00:01.000Z",
113 detail: "pwd",
114 },
115 ]);
116 });
117
118 it("clears stale pending approvals when provider reports unknown pending request", () => {
119 const activities: OrchestrationThreadActivity[] = [
120 makeActivity({
121 id: "approval-open-stale",
122 createdAt: "2026-02-23T00:00:01.000Z",
123 kind: "approval.requested",
124 summary: "Command approval requested",
125 tone: "approval",
126 payload: {
127 requestId: "req-stale-1",
128 requestKind: "command",
129 },
130 }),
131 makeActivity({
132 id: "approval-failed-stale",
133 createdAt: "2026-02-23T00:00:02.000Z",
134 kind: "provider.approval.respond.failed",
135 summary: "Provider approval response failed",
136 tone: "error",
137 payload: {
138 requestId: "req-stale-1",
139 detail: "Unknown pending permission request: req-stale-1",
140 },
141 }),
142 ];
143
144 expect(derivePendingApprovals(activities)).toEqual([]);
145 });
146});
147
148describe("derivePendingUserInputs", () => {
149 it("tracks open structured prompts and removes resolved ones", () => {
150 const activities: OrchestrationThreadActivity[] = [
151 makeActivity({
152 id: "user-input-open",
153 createdAt: "2026-02-23T00:00:01.000Z",
154 kind: "user-input.requested",
155 summary: "User input requested",
156 tone: "info",
157 payload: {
158 requestId: "req-user-input-1",
159 questions: [
160 {
161 id: "sandbox_mode",
162 header: "Sandbox",
163 question: "Which mode should be used?",
164 options: [
165 {
166 label: "workspace-write",
167 description: "Allow workspace writes only",
168 },
169 ],
170 },
171 ],
172 },
173 }),
174 makeActivity({
175 id: "user-input-resolved",
176 createdAt: "2026-02-23T00:00:02.000Z",
177 kind: "user-input.resolved",
178 summary: "User input submitted",
179 tone: "info",
180 payload: {
181 requestId: "req-user-input-2",
182 answers: {
183 sandbox_mode: "workspace-write",
184 },
185 },
186 }),
187 makeActivity({
188 id: "user-input-open-2",
189 createdAt: "2026-02-23T00:00:01.500Z",
190 kind: "user-input.requested",
191 summary: "User input requested",
192 tone: "info",
193 payload: {
194 requestId: "req-user-input-2",
195 questions: [
196 {
197 id: "approval",
198 header: "Approval",
199 question: "Continue?",
200 options: [
201 {
202 label: "yes",
203 description: "Continue execution",
204 },
205 ],
206 },
207 ],
208 },
209 }),
210 ];
211
212 expect(derivePendingUserInputs(activities)).toEqual([
213 {
214 requestId: "req-user-input-1",
215 createdAt: "2026-02-23T00:00:01.000Z",
216 questions: [
217 {
218 id: "sandbox_mode",
219 header: "Sandbox",
220 question: "Which mode should be used?",
221 options: [
222 {
223 label: "workspace-write",
224 description: "Allow workspace writes only",
225 },
226 ],
227 },
228 ],
229 },
230 ]);
231 });
232});
233
234describe("deriveActivePlanState", () => {
235 it("returns the latest plan update for the active turn", () => {
236 const activities: OrchestrationThreadActivity[] = [
237 makeActivity({
238 id: "plan-old",
239 createdAt: "2026-02-23T00:00:01.000Z",
240 kind: "turn.plan.updated",
241 summary: "Plan updated",
242 tone: "info",
243 turnId: "turn-1",
244 payload: {
245 explanation: "Initial plan",
246 plan: [{ step: "Inspect code", status: "pending" }],
247 },
248 }),
249 makeActivity({
250 id: "plan-latest",
251 createdAt: "2026-02-23T00:00:02.000Z",
252 kind: "turn.plan.updated",
253 summary: "Plan updated",
254 tone: "info",
255 turnId: "turn-1",
256 payload: {
257 explanation: "Refined plan",
258 plan: [{ step: "Implement Codex user input", status: "inProgress" }],
259 },
260 }),
261 ];
262
263 expect(deriveActivePlanState(activities, TurnId.makeUnsafe("turn-1"))).toEqual({
264 createdAt: "2026-02-23T00:00:02.000Z",
265 turnId: "turn-1",
266 explanation: "Refined plan",
267 steps: [{ step: "Implement Codex user input", status: "inProgress" }],
268 });
269 });
270});
271
272describe("findLatestProposedPlan", () => {
273 it("prefers the latest proposed plan for the active turn", () => {
274 expect(
275 findLatestProposedPlan(
276 [
277 {
278 id: "plan:thread-1:turn:turn-1",
279 turnId: TurnId.makeUnsafe("turn-1"),
280 planMarkdown: "# Older",
281 implementedAt: null,
282 implementationThreadId: null,
283 createdAt: "2026-02-23T00:00:01.000Z",
284 updatedAt: "2026-02-23T00:00:01.000Z",
285 },
286 {
287 id: "plan:thread-1:turn:turn-1",
288 turnId: TurnId.makeUnsafe("turn-1"),
289 planMarkdown: "# Latest",
290 implementedAt: null,
291 implementationThreadId: null,
292 createdAt: "2026-02-23T00:00:01.000Z",
293 updatedAt: "2026-02-23T00:00:02.000Z",
294 },
295 {
296 id: "plan:thread-1:turn:turn-2",
297 turnId: TurnId.makeUnsafe("turn-2"),
298 planMarkdown: "# Different turn",
299 implementedAt: null,
300 implementationThreadId: null,
301 createdAt: "2026-02-23T00:00:03.000Z",
302 updatedAt: "2026-02-23T00:00:03.000Z",
303 },
304 ],
305 TurnId.makeUnsafe("turn-1"),
306 ),
307 ).toEqual({
308 id: "plan:thread-1:turn:turn-1",
309 turnId: "turn-1",
310 planMarkdown: "# Latest",
311 implementedAt: null,
312 implementationThreadId: null,
313 createdAt: "2026-02-23T00:00:01.000Z",
314 updatedAt: "2026-02-23T00:00:02.000Z",
315 });
316 });
317
318 it("falls back to the most recently updated proposed plan", () => {
319 const latestPlan = findLatestProposedPlan(
320 [
321 {
322 id: "plan:thread-1:turn:turn-1",
323 turnId: TurnId.makeUnsafe("turn-1"),
324 planMarkdown: "# First",
325 implementedAt: null,
326 implementationThreadId: null,
327 createdAt: "2026-02-23T00:00:01.000Z",
328 updatedAt: "2026-02-23T00:00:01.000Z",
329 },
330 {
331 id: "plan:thread-1:turn:turn-2",
332 turnId: TurnId.makeUnsafe("turn-2"),
333 planMarkdown: "# Latest",
334 implementedAt: null,
335 implementationThreadId: null,
336 createdAt: "2026-02-23T00:00:02.000Z",
337 updatedAt: "2026-02-23T00:00:03.000Z",
338 },
339 ],
340 null,
341 );
342
343 expect(latestPlan?.planMarkdown).toBe("# Latest");
344 });
345});
346
347describe("hasActionableProposedPlan", () => {
348 it("returns true for an unimplemented proposed plan", () => {
349 expect(
350 hasActionableProposedPlan({
351 id: "plan-1",
352 turnId: TurnId.makeUnsafe("turn-1"),
353 planMarkdown: "# Plan",
354 implementedAt: null,
355 implementationThreadId: null,
356 createdAt: "2026-02-23T00:00:00.000Z",
357 updatedAt: "2026-02-23T00:00:01.000Z",
358 }),
359 ).toBe(true);
360 });
361
362 it("returns false for a proposed plan already implemented elsewhere", () => {
363 expect(
364 hasActionableProposedPlan({
365 id: "plan-1",
366 turnId: TurnId.makeUnsafe("turn-1"),
367 planMarkdown: "# Plan",
368 implementedAt: "2026-02-23T00:00:02.000Z",
369 implementationThreadId: ThreadId.makeUnsafe("thread-implement"),
370 createdAt: "2026-02-23T00:00:00.000Z",
371 updatedAt: "2026-02-23T00:00:02.000Z",
372 }),
373 ).toBe(false);
374 });
375});
376
377describe("findSidebarProposedPlan", () => {
378 it("prefers the running turn source proposed plan when available on the same thread", () => {
379 expect(
380 findSidebarProposedPlan({
381 threads: [
382 {
383 id: ThreadId.makeUnsafe("thread-1"),
384 proposedPlans: [
385 {
386 id: "plan-1",
387 turnId: TurnId.makeUnsafe("turn-plan"),
388 planMarkdown: "# Source plan",
389 implementedAt: "2026-02-23T00:00:03.000Z",
390 implementationThreadId: ThreadId.makeUnsafe("thread-2"),
391 createdAt: "2026-02-23T00:00:01.000Z",
392 updatedAt: "2026-02-23T00:00:02.000Z",
393 },
394 ],
395 },
396 {
397 id: ThreadId.makeUnsafe("thread-2"),
398 proposedPlans: [
399 {
400 id: "plan-2",
401 turnId: TurnId.makeUnsafe("turn-other"),
402 planMarkdown: "# Latest elsewhere",
403 implementedAt: null,
404 implementationThreadId: null,
405 createdAt: "2026-02-23T00:00:04.000Z",
406 updatedAt: "2026-02-23T00:00:05.000Z",
407 },
408 ],
409 },
410 ],
411 latestTurn: {
412 turnId: TurnId.makeUnsafe("turn-implementation"),
413 sourceProposedPlan: {
414 threadId: ThreadId.makeUnsafe("thread-1"),
415 planId: "plan-1",
416 },
417 },
418 latestTurnSettled: false,
419 threadId: ThreadId.makeUnsafe("thread-1"),
420 }),
421 ).toEqual({
422 id: "plan-1",
423 turnId: "turn-plan",
424 planMarkdown: "# Source plan",
425 implementedAt: "2026-02-23T00:00:03.000Z",
426 implementationThreadId: "thread-2",
427 createdAt: "2026-02-23T00:00:01.000Z",
428 updatedAt: "2026-02-23T00:00:02.000Z",
429 });
430 });
431
432 it("falls back to the latest proposed plan once the turn is settled", () => {
433 expect(
434 findSidebarProposedPlan({
435 threads: [
436 {
437 id: ThreadId.makeUnsafe("thread-1"),
438 proposedPlans: [
439 {
440 id: "plan-1",
441 turnId: TurnId.makeUnsafe("turn-plan"),
442 planMarkdown: "# Older",
443 implementedAt: null,
444 implementationThreadId: null,
445 createdAt: "2026-02-23T00:00:01.000Z",
446 updatedAt: "2026-02-23T00:00:02.000Z",
447 },
448 {
449 id: "plan-2",
450 turnId: TurnId.makeUnsafe("turn-latest"),
451 planMarkdown: "# Latest",
452 implementedAt: null,
453 implementationThreadId: null,
454 createdAt: "2026-02-23T00:00:03.000Z",
455 updatedAt: "2026-02-23T00:00:04.000Z",
456 },
457 ],
458 },
459 ],
460 latestTurn: {
461 turnId: TurnId.makeUnsafe("turn-implementation"),
462 sourceProposedPlan: {
463 threadId: ThreadId.makeUnsafe("thread-1"),
464 planId: "plan-1",
465 },
466 },
467 latestTurnSettled: true,
468 threadId: ThreadId.makeUnsafe("thread-1"),
469 })?.planMarkdown,
470 ).toBe("# Latest");
471 });
472});
473
474describe("deriveWorkLogEntries", () => {
475 it("omits tool started entries and keeps completed entries", () => {
476 const activities: OrchestrationThreadActivity[] = [
477 makeActivity({
478 id: "tool-complete",
479 createdAt: "2026-02-23T00:00:03.000Z",
480 summary: "Tool call complete",
481 kind: "tool.completed",
482 }),
483 makeActivity({
484 id: "tool-start",
485 createdAt: "2026-02-23T00:00:02.000Z",
486 summary: "Tool call",
487 kind: "tool.started",
488 }),
489 ];
490
491 const entries = deriveWorkLogEntries(activities, undefined);
492 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
493 });
494
495 it("omits task start and completion lifecycle entries", () => {
496 const activities: OrchestrationThreadActivity[] = [
497 makeActivity({
498 id: "task-start",
499 createdAt: "2026-02-23T00:00:01.000Z",
500 kind: "task.started",
501 summary: "default task started",
502 tone: "info",
503 }),
504 makeActivity({
505 id: "task-progress",
506 createdAt: "2026-02-23T00:00:02.000Z",
507 kind: "task.progress",
508 summary: "Updating files",
509 tone: "info",
510 }),
511 makeActivity({
512 id: "task-complete",
513 createdAt: "2026-02-23T00:00:03.000Z",
514 kind: "task.completed",
515 summary: "Task completed",
516 tone: "info",
517 }),
518 ];
519
520 const entries = deriveWorkLogEntries(activities, undefined);
521 expect(entries.map((entry) => entry.id)).toEqual(["task-progress"]);
522 });
523
524 it("filters by turn id when provided", () => {
525 const activities: OrchestrationThreadActivity[] = [
526 makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }),
527 makeActivity({
528 id: "turn-2",
529 turnId: "turn-2",
530 summary: "Tool call complete",
531 kind: "tool.completed",
532 }),
533 makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }),
534 ];
535
536 const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2"));
537 expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]);
538 });
539
540 it("omits checkpoint captured info entries", () => {
541 const activities: OrchestrationThreadActivity[] = [
542 makeActivity({
543 id: "checkpoint",
544 createdAt: "2026-02-23T00:00:01.000Z",
545 summary: "Checkpoint captured",
546 tone: "info",
547 }),
548 makeActivity({
549 id: "tool-complete",
550 createdAt: "2026-02-23T00:00:02.000Z",
551 summary: "Ran command",
552 tone: "tool",
553 kind: "tool.completed",
554 }),
555 ];
556
557 const entries = deriveWorkLogEntries(activities, undefined);
558 expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]);
559 });
560
561 it("omits ExitPlanMode lifecycle entries once the plan card is shown", () => {
562 const activities: OrchestrationThreadActivity[] = [
563 makeActivity({
564 id: "exit-plan-updated",
565 createdAt: "2026-02-23T00:00:01.000Z",
566 kind: "tool.updated",
567 summary: "Tool call",
568 payload: {
569 detail: 'ExitPlanMode: {"allowedPrompts":[{"tool":"Bash","prompt":"run tests"}]}',
570 },
571 }),
572 makeActivity({
573 id: "exit-plan-completed",
574 createdAt: "2026-02-23T00:00:02.000Z",
575 kind: "tool.completed",
576 summary: "Tool call",
577 payload: {
578 detail: "ExitPlanMode: {}",
579 },
580 }),
581 makeActivity({
582 id: "real-work-log",
583 createdAt: "2026-02-23T00:00:03.000Z",
584 kind: "tool.completed",
585 summary: "Ran command",
586 payload: {
587 itemType: "command_execution",
588 detail: "Bash: bun test",
589 },
590 }),
591 ];
592
593 const entries = deriveWorkLogEntries(activities, undefined);
594 expect(entries.map((entry) => entry.id)).toEqual(["real-work-log"]);
595 });
596
597 it("orders work log by activity sequence when present", () => {
598 const activities: OrchestrationThreadActivity[] = [
599 makeActivity({
600 id: "second",
601 createdAt: "2026-02-23T00:00:03.000Z",
602 sequence: 2,
603 summary: "Tool call complete",
604 kind: "tool.completed",
605 }),
606 makeActivity({
607 id: "first",
608 createdAt: "2026-02-23T00:00:04.000Z",
609 sequence: 1,
610 summary: "Tool call complete",
611 kind: "tool.completed",
612 }),
613 ];
614
615 const entries = deriveWorkLogEntries(activities, undefined);
616 expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]);
617 });
618
619 it("extracts command text for command tool activities", () => {
620 const activities: OrchestrationThreadActivity[] = [
621 makeActivity({
622 id: "command-tool",
623 kind: "tool.completed",
624 summary: "Ran command",
625 payload: {
626 itemType: "command_execution",
627 data: {
628 item: {
629 command: ["bun", "run", "lint"],
630 },
631 },
632 },
633 }),
634 ];
635
636 const [entry] = deriveWorkLogEntries(activities, undefined);
637 expect(entry?.command).toBe("bun run lint");
638 });
639
640 it("keeps compact Codex tool metadata used for icons and labels", () => {
641 const activities: OrchestrationThreadActivity[] = [
642 makeActivity({
643 id: "tool-with-metadata",
644 kind: "tool.completed",
645 summary: "bash",
646 payload: {
647 itemType: "command_execution",
648 title: "bash",
649 status: "completed",
650 detail: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
651 data: {
652 item: {
653 command: ["bun", "run", "dev"],
654 result: {
655 content: '{ "dev": "vite dev --port 3000" } <exited with exit code 0>',
656 exitCode: 0,
657 },
658 },
659 },
660 },
661 }),
662 ];
663
664 const [entry] = deriveWorkLogEntries(activities, undefined);
665 expect(entry).toMatchObject({
666 command: "bun run dev",
667 detail: '{ "dev": "vite dev --port 3000" }',
668 itemType: "command_execution",
669 toolTitle: "bash",
670 });
671 });
672
673 it("extracts changed file paths for file-change tool activities", () => {
674 const activities: OrchestrationThreadActivity[] = [
675 makeActivity({
676 id: "file-tool",
677 kind: "tool.completed",
678 summary: "File change",
679 payload: {
680 itemType: "file_change",
681 data: {
682 item: {
683 changes: [
684 { path: "apps/web/src/components/ChatView.tsx" },
685 { filename: "apps/web/src/session-logic.ts" },
686 ],
687 },
688 },
689 },
690 }),
691 ];
692
693 const [entry] = deriveWorkLogEntries(activities, undefined);
694 expect(entry?.changedFiles).toEqual([
695 "apps/web/src/components/ChatView.tsx",
696 "apps/web/src/session-logic.ts",
697 ]);
698 });
699});
700
701describe("deriveTimelineEntries", () => {
702 it("includes proposed plans alongside messages and work entries in chronological order", () => {
703 const entries = deriveTimelineEntries(
704 [
705 {
706 id: MessageId.makeUnsafe("message-1"),
707 role: "assistant",
708 text: "hello",
709 createdAt: "2026-02-23T00:00:01.000Z",
710 streaming: false,
711 },
712 ],
713 [
714 {
715 id: "plan:thread-1:turn:turn-1",
716 turnId: TurnId.makeUnsafe("turn-1"),
717 planMarkdown: "# Ship it",
718 implementedAt: null,
719 implementationThreadId: null,
720 createdAt: "2026-02-23T00:00:02.000Z",
721 updatedAt: "2026-02-23T00:00:02.000Z",
722 },
723 ],
724 [
725 {
726 id: "work-1",
727 createdAt: "2026-02-23T00:00:03.000Z",
728 label: "Ran tests",
729 tone: "tool",
730 },
731 ],
732 );
733
734 expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]);
735 expect(entries[1]).toMatchObject({
736 kind: "proposed-plan",
737 proposedPlan: {
738 planMarkdown: "# Ship it",
739 implementedAt: null,
740 implementationThreadId: null,
741 },
742 });
743 });
744});
745
746describe("hasToolActivityForTurn", () => {
747 it("returns false when turn id is missing", () => {
748 const activities: OrchestrationThreadActivity[] = [
749 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
750 ];
751
752 expect(hasToolActivityForTurn(activities, undefined)).toBe(false);
753 expect(hasToolActivityForTurn(activities, null)).toBe(false);
754 });
755
756 it("returns true only for matching tool activity in the target turn", () => {
757 const activities: OrchestrationThreadActivity[] = [
758 makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }),
759 makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }),
760 ];
761
762 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true);
763 expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false);
764 });
765});
766
767describe("isLatestTurnSettled", () => {
768 const latestTurn = {
769 turnId: TurnId.makeUnsafe("turn-1"),
770 startedAt: "2026-02-27T21:10:00.000Z",
771 completedAt: "2026-02-27T21:10:06.000Z",
772 } as const;
773
774 it("returns false while the same turn is still active in a running session", () => {
775 expect(
776 isLatestTurnSettled(latestTurn, {
777 orchestrationStatus: "running",
778 activeTurnId: TurnId.makeUnsafe("turn-1"),
779 }),
780 ).toBe(false);
781 });
782
783 it("returns false while any turn is running to avoid stale latest-turn banners", () => {
784 expect(
785 isLatestTurnSettled(latestTurn, {
786 orchestrationStatus: "running",
787 activeTurnId: TurnId.makeUnsafe("turn-2"),
788 }),
789 ).toBe(false);
790 });
791
792 it("returns true once the session is no longer running that turn", () => {
793 expect(
794 isLatestTurnSettled(latestTurn, {
795 orchestrationStatus: "ready",
796 activeTurnId: undefined,
797 }),
798 ).toBe(true);
799 });
800
801 it("returns false when turn timestamps are incomplete", () => {
802 expect(
803 isLatestTurnSettled(
804 {
805 turnId: TurnId.makeUnsafe("turn-1"),
806 startedAt: null,
807 completedAt: "2026-02-27T21:10:06.000Z",
808 },
809 null,
810 ),
811 ).toBe(false);
812 });
813});
814
815describe("deriveActiveWorkStartedAt", () => {
816 const latestTurn = {
817 turnId: TurnId.makeUnsafe("turn-1"),
818 startedAt: "2026-02-27T21:10:00.000Z",
819 completedAt: "2026-02-27T21:10:06.000Z",
820 } as const;
821
822 it("prefers the in-flight turn start when the latest turn is not settled", () => {
823 expect(
824 deriveActiveWorkStartedAt(
825 latestTurn,
826 {
827 orchestrationStatus: "running",
828 activeTurnId: TurnId.makeUnsafe("turn-1"),
829 },
830 "2026-02-27T21:11:00.000Z",
831 ),
832 ).toBe("2026-02-27T21:10:00.000Z");
833 });
834
835 it("falls back to sendStartedAt once the latest turn is settled", () => {
836 expect(
837 deriveActiveWorkStartedAt(
838 latestTurn,
839 {
840 orchestrationStatus: "ready",
841 activeTurnId: undefined,
842 },
843 "2026-02-27T21:11:00.000Z",
844 ),
845 ).toBe("2026-02-27T21:11:00.000Z");
846 });
847
848 it("uses sendStartedAt for a fresh send after the prior turn completed", () => {
849 expect(
850 deriveActiveWorkStartedAt(
851 {
852 turnId: TurnId.makeUnsafe("turn-1"),
853 startedAt: "2026-02-27T21:10:00.000Z",
854 completedAt: "2026-02-27T21:10:06.000Z",
855 },
856 null,
857 "2026-02-27T21:11:00.000Z",
858 ),
859 ).toBe("2026-02-27T21:11:00.000Z");
860 });
861});
862
863describe("PROVIDER_OPTIONS", () => {
864 it("advertises Claude as available while keeping Cursor as a placeholder", () => {
865 const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeAgent");
866 const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
867 expect(PROVIDER_OPTIONS).toEqual([
868 { value: "codex", label: "Codex", available: true },
869 { value: "claudeAgent", label: "Claude", available: true },
870 { value: "cursor", label: "Cursor", available: false },
871 ]);
872 expect(claude).toEqual({
873 value: "claudeAgent",
874 label: "Claude",
875 available: true,
876 });
877 expect(cursor).toEqual({
878 value: "cursor",
879 label: "Cursor",
880 available: false,
881 });
882 });
883});