Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 formatDuration,
5 deriveActiveWorkStartedAt,
6 deriveSubagentElapsedMs,
7} from "./orchestrationTiming.ts";
8
9describe("formatDuration", () => {
10 it.each([
11 [0, "1ms"],
12 [250, "250ms"],
13 [1_500, "1.5s"],
14 [9_950, "10s"],
15 [22_000, "22s"],
16 [60_000, "1m"],
17 [65_000, "1m 5s"],
18 [119_500, "2m"],
19 [3_599_499, "59m 59s"],
20 [3_599_500, "1h"],
21 [3_600_000, "1h"],
22 [3_601_000, "1h 1s"],
23 [3_660_000, "1h 1m"],
24 [3_661_000, "1h 1m 1s"],
25 [7_199_500, "2h"],
26 [25_190_000, "6h 59m 50s"],
27 [90_061_000, "25h 1m 1s"],
28 ])("formats %d ms as %s", (durationMs, expected) => {
29 expect(formatDuration(durationMs)).toBe(expected);
30 });
31
32 it.each([-1, NaN, Infinity, -Infinity])("handles invalid durations: %s", (durationMs) => {
33 expect(formatDuration(durationMs)).toBe("0ms");
34 });
35});
36
37describe("deriveActiveWorkStartedAt", () => {
38 it.each([null, "2026-09-06T23:34:00.000Z"])(
39 "does not time a superseded turn when the active turn differs",
40 (sendStartedAt) => {
41 expect(
42 deriveActiveWorkStartedAt(
43 {
44 runId: "old",
45 requestedAt: "2026-09-06T23:33:00.000Z",
46 startedAt: null,
47 completedAt: null,
48 },
49 { orchestrationStatus: "running", activeRunId: "new" },
50 sendStartedAt,
51 ),
52 ).toBe(sendStartedAt);
53 },
54 );
55
56 it("stops timing a turn that failed before its provider started", () => {
57 expect(
58 deriveActiveWorkStartedAt(
59 {
60 runId: "turn-1",
61 requestedAt: "2026-09-06T23:33:00.000Z",
62 startedAt: null,
63 completedAt: "2026-09-06T23:33:05.000Z",
64 },
65 { orchestrationStatus: "error", activeRunId: null },
66 null,
67 ),
68 ).toBeNull();
69 });
70 it("counts from requestedAt while the provider is still starting", () => {
71 expect(
72 deriveActiveWorkStartedAt(
73 {
74 runId: "turn-1",
75 requestedAt: "2026-09-06T23:33:00.000Z",
76 startedAt: null,
77 completedAt: null,
78 },
79 { orchestrationStatus: "starting", activeRunId: null },
80 null,
81 ),
82 ).toBe("2026-09-06T23:33:00.000Z");
83 });
84
85 it("prefers the turn's own startedAt once the provider reports it", () => {
86 expect(
87 deriveActiveWorkStartedAt(
88 {
89 runId: "turn-1",
90 requestedAt: "2026-09-06T23:33:00.000Z",
91 startedAt: "2026-09-06T23:33:05.000Z",
92 completedAt: null,
93 },
94 { orchestrationStatus: "running", activeRunId: "turn-1" },
95 null,
96 ),
97 ).toBe("2026-09-06T23:33:05.000Z");
98 });
99
100 it("stops counting once the turn has settled", () => {
101 expect(
102 deriveActiveWorkStartedAt(
103 {
104 runId: "turn-1",
105 requestedAt: "2026-09-06T23:33:00.000Z",
106 startedAt: "2026-09-06T23:33:05.000Z",
107 completedAt: "2026-09-06T23:33:09.000Z",
108 },
109 { orchestrationStatus: "idle", activeRunId: null },
110 null,
111 ),
112 ).toBeNull();
113 });
114
115 it("does not count a settled turn while a session is starting again", () => {
116 expect(
117 deriveActiveWorkStartedAt(
118 {
119 runId: "turn-1",
120 requestedAt: "2026-09-06T23:33:00.000Z",
121 startedAt: "2026-09-06T23:33:05.000Z",
122 completedAt: "2026-09-06T23:33:09.000Z",
123 },
124 { orchestrationStatus: "starting", activeRunId: null },
125 null,
126 ),
127 ).toBeNull();
128 });
129
130 it("falls back to the caller's send timestamp when there is no turn yet", () => {
131 expect(deriveActiveWorkStartedAt(null, null, "2026-09-06T23:33:00.000Z")).toBe(
132 "2026-09-06T23:33:00.000Z",
133 );
134 });
135});
136
137describe("deriveSubagentElapsedMs", () => {
138 const startedAt = "2026-09-21T12:00:00.000Z";
139 const completedAt = "2026-09-21T12:00:10.000Z";
140 const now = Date.parse("2026-09-21T13:00:00.000Z");
141 it.each(["idle", "completed", "failed", "cancelled", "interrupted"] as const)(
142 "does not count the age of %s work with unknown completion timing",
143 (status) => {
144 expect(deriveSubagentElapsedMs({ status, startedAt, completedAt: null }, now)).toBeNull();
145 expect(deriveSubagentElapsedMs({ status, startedAt, completedAt }, now)).toBe(10_000);
146 },
147 );
148 it("counts a resumed activation despite a stale previous completion timestamp", () => {
149 expect(deriveSubagentElapsedMs({ status: "running", startedAt, completedAt }, now)).toBe(
150 3_600_000,
151 );
152 expect(
153 deriveSubagentElapsedMs({ status: "running", startedAt: null, completedAt: null }, now),
154 ).toBeNull();
155 });
156});