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

orchestrationTiming.test.ts

159 lines4.6 KB

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