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

orchestrationTiming.test.ts

118 lines3.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { deriveActiveWorkStartedAt, deriveSubagentElapsedMs } from "./orchestrationTiming.ts";
4​
5describe("deriveActiveWorkStartedAt", () => {
6 it.each([null, "2026-09-06T23:34:00.000Z"])(
7 "does not time a superseded turn when the active turn differs",
8 (sendStartedAt) => {
9 expect(
10 deriveActiveWorkStartedAt(
11 {
12 runId: "old",
13 requestedAt: "2026-09-06T23:33:00.000Z",
14 startedAt: null,
15 completedAt: null,
16 },
17 { orchestrationStatus: "running", activeRunId: "new" },
18 sendStartedAt,
19 ),
20 ).toBe(sendStartedAt);
21 },
22 );
23​
24 it("stops timing a turn that failed before its provider started", () => {
25 expect(
26 deriveActiveWorkStartedAt(
27 {
28 runId: "turn-1",
29 requestedAt: "2026-09-06T23:33:00.000Z",
30 startedAt: null,
31 completedAt: "2026-09-06T23:33:05.000Z",
32 },
33 { orchestrationStatus: "error", activeRunId: null },
34 null,
35 ),
36 ).toBeNull();
37 });
38 it("counts from requestedAt while the provider is still starting", () => {
39 expect(
40 deriveActiveWorkStartedAt(
41 {
42 runId: "turn-1",
43 requestedAt: "2026-09-06T23:33:00.000Z",
44 startedAt: null,
45 completedAt: null,
46 },
47 { orchestrationStatus: "starting", activeRunId: null },
48 null,
49 ),
50 ).toBe("2026-09-06T23:33:00.000Z");
51 });
52​
53 it("prefers the turn's own startedAt once the provider reports it", () => {
54 expect(
55 deriveActiveWorkStartedAt(
56 {
57 runId: "turn-1",
58 requestedAt: "2026-09-06T23:33:00.000Z",
59 startedAt: "2026-09-06T23:33:05.000Z",
60 completedAt: null,
61 },
62 { orchestrationStatus: "running", activeRunId: "turn-1" },
63 null,
64 ),
65 ).toBe("2026-09-06T23:33:05.000Z");
66 });
67​
68 it("stops counting once the turn has settled", () => {
69 expect(
70 deriveActiveWorkStartedAt(
71 {
72 runId: "turn-1",
73 requestedAt: "2026-09-06T23:33:00.000Z",
74 startedAt: "2026-09-06T23:33:05.000Z",
75 completedAt: "2026-09-06T23:33:09.000Z",
76 },
77 { orchestrationStatus: "idle", activeRunId: null },
78 null,
79 ),
80 ).toBeNull();
81 });
82​
83 it("does not count a settled turn while a session is starting again", () => {
84 expect(
85 deriveActiveWorkStartedAt(
86 {
87 runId: "turn-1",
88 requestedAt: "2026-09-06T23:33:00.000Z",
89 startedAt: "2026-09-06T23:33:05.000Z",
90 completedAt: "2026-09-06T23:33:09.000Z",
91 },
92 { orchestrationStatus: "starting", activeRunId: null },
93 null,
94 ),
95 ).toBeNull();
96 });
97});
98​
99describe("deriveSubagentElapsedMs", () => {
100 const startedAt = "2026-09-21T12:00:00.000Z";
101 const completedAt = "2026-09-21T12:00:10.000Z";
102 const now = Date.parse("2026-09-21T13:00:00.000Z");
103 it.each(["idle", "completed", "failed"] as const)(
104 "does not count the age of %s work with unknown completion timing",
105 (status) => {
106 expect(deriveSubagentElapsedMs({ status, startedAt, completedAt: null }, now)).toBeNull();
107 expect(deriveSubagentElapsedMs({ status, startedAt, completedAt }, now)).toBe(10_000);
108 },
109 );
110 it("counts a resumed activation despite a stale previous completion timestamp", () => {
111 expect(deriveSubagentElapsedMs({ status: "running", startedAt, completedAt }, now)).toBe(
112 3_600_000,
113 );
114 expect(
115 deriveSubagentElapsedMs({ status: "running", startedAt: null, completedAt: null }, now),
116 ).toBeNull();
117 });
118});