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

orchestrationTiming.test.ts

138 lines4.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { formatDuration, deriveActiveWorkStartedAt } from "./orchestrationTiming.ts";
4​
5describe("formatDuration", () => {
6 it.each([
7 [0, "1ms"],
8 [250, "250ms"],
9 [1_500, "1.5s"],
10 [9_950, "10s"],
11 [22_000, "22s"],
12 [60_000, "1m"],
13 [65_000, "1m 5s"],
14 [119_500, "2m"],
15 [3_599_499, "59m 59s"],
16 [3_599_500, "1h"],
17 [3_600_000, "1h"],
18 [3_601_000, "1h 1s"],
19 [3_660_000, "1h 1m"],
20 [3_661_000, "1h 1m 1s"],
21 [7_199_500, "2h"],
22 [25_190_000, "6h 59m 50s"],
23 [90_061_000, "25h 1m 1s"],
24 ])("formats %d ms as %s", (durationMs, expected) => {
25 expect(formatDuration(durationMs)).toBe(expected);
26 });
27​
28 it.each([-1, NaN, Infinity, -Infinity])("handles invalid durations: %s", (durationMs) => {
29 expect(formatDuration(durationMs)).toBe("0ms");
30 });
31});
32​
33describe("deriveActiveWorkStartedAt", () => {
34 it.each([null, "2026-09-06T23:34:00.000Z"])(
35 "does not time a superseded turn when the active turn differs",
36 (sendStartedAt) => {
37 expect(
38 deriveActiveWorkStartedAt(
39 {
40 turnId: "old",
41 requestedAt: "2026-09-06T23:33:00.000Z",
42 startedAt: null,
43 completedAt: null,
44 },
45 { orchestrationStatus: "running", activeTurnId: "new" },
46 sendStartedAt,
47 ),
48 ).toBe(sendStartedAt);
49 },
50 );
51​
52 it("stops timing a turn that failed before its provider started", () => {
53 expect(
54 deriveActiveWorkStartedAt(
55 {
56 turnId: "turn-1",
57 requestedAt: "2026-09-06T23:33:00.000Z",
58 startedAt: null,
59 completedAt: "2026-09-06T23:33:05.000Z",
60 },
61 { orchestrationStatus: "error", activeTurnId: null },
62 null,
63 ),
64 ).toBeNull();
65 });
66 // The gap this closes. The projector stamps startedAt in the same update
67 // that moves the session to "running", so during provider spin-up the turn
68 // is requested with no startedAt and the session is "starting". Returning
69 // null there blinks the working indicator out between "Setting up
70 // worktree..." and "Working for 0s".
71 it("counts from requestedAt while the provider is still starting", () => {
72 expect(
73 deriveActiveWorkStartedAt(
74 {
75 turnId: "turn-1",
76 requestedAt: "2026-09-06T23:33:00.000Z",
77 startedAt: null,
78 completedAt: null,
79 },
80 { orchestrationStatus: "starting", activeTurnId: null },
81 null,
82 ),
83 ).toBe("2026-09-06T23:33:00.000Z");
84 });
85​
86 it("prefers the turn's own startedAt once the provider reports it", () => {
87 expect(
88 deriveActiveWorkStartedAt(
89 {
90 turnId: "turn-1",
91 requestedAt: "2026-09-06T23:33:00.000Z",
92 startedAt: "2026-09-06T23:33:05.000Z",
93 completedAt: null,
94 },
95 { orchestrationStatus: "running", activeTurnId: "turn-1" },
96 null,
97 ),
98 ).toBe("2026-09-06T23:33:05.000Z");
99 });
100​
101 // requestedAt must not leak past the end of the work.
102 it("stops counting once the turn has settled", () => {
103 expect(
104 deriveActiveWorkStartedAt(
105 {
106 turnId: "turn-1",
107 requestedAt: "2026-09-06T23:33:00.000Z",
108 startedAt: "2026-09-06T23:33:05.000Z",
109 completedAt: "2026-09-06T23:33:09.000Z",
110 },
111 { orchestrationStatus: "idle", activeTurnId: null },
112 null,
113 ),
114 ).toBeNull();
115 });
116​
117 // A session restarting with no new turn must not resurrect the old one.
118 it("does not count a settled turn while a session is starting again", () => {
119 expect(
120 deriveActiveWorkStartedAt(
121 {
122 turnId: "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", activeTurnId: 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});