Files
t3code/packages/shared/src/orchestrationTiming.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

164 lines
5.0 KiB
TypeScript

import { describe, expect, it } from "vite-plus/test";
import {
formatDuration,
deriveActiveWorkStartedAt,
deriveSubagentElapsedMs,
} from "./orchestrationTiming.ts";
describe("formatDuration", () => {
it.each([
[0, "1ms"],
[250, "250ms"],
[1_500, "1.5s"],
[9_950, "10s"],
[22_000, "22s"],
[60_000, "1m"],
[65_000, "1m 5s"],
[119_500, "2m"],
[3_599_499, "59m 59s"],
[3_599_500, "1h"],
[3_600_000, "1h"],
[3_601_000, "1h 1s"],
[3_660_000, "1h 1m"],
[3_661_000, "1h 1m 1s"],
[7_199_500, "2h"],
[25_190_000, "6h 59m 50s"],
[90_061_000, "25h 1m 1s"],
])("formats %d ms as %s", (durationMs, expected) => {
expect(formatDuration(durationMs)).toBe(expected);
});
it.each([-1, NaN, Infinity, -Infinity])("handles invalid durations: %s", (durationMs) => {
expect(formatDuration(durationMs)).toBe("0ms");
});
});
describe("deriveActiveWorkStartedAt", () => {
it.each([null, "2026-09-06T23:34:00.000Z"])(
"does not time a superseded turn when the active turn differs",
(sendStartedAt) => {
expect(
deriveActiveWorkStartedAt(
{
runId: "old",
requestedAt: "2026-09-06T23:33:00.000Z",
startedAt: null,
completedAt: null,
},
{ orchestrationStatus: "running", activeRunId: "new" },
sendStartedAt,
),
).toBe(sendStartedAt);
},
);
it("stops timing a turn that failed before its provider started", () => {
expect(
deriveActiveWorkStartedAt(
{
runId: "turn-1",
requestedAt: "2026-09-06T23:33:00.000Z",
startedAt: null,
completedAt: "2026-09-06T23:33:05.000Z",
},
{ orchestrationStatus: "error", activeRunId: null },
null,
),
).toBeNull();
});
// The gap this closes. The projector stamps startedAt in the same update
// that moves the session to "running", so during provider spin-up the turn
// is requested with no startedAt and the session is "starting". Returning
// null there blinks the working indicator out between "Setting up
// worktree..." and "Working for 0s".
it("counts from requestedAt while the provider is still starting", () => {
expect(
deriveActiveWorkStartedAt(
{
runId: "turn-1",
requestedAt: "2026-09-06T23:33:00.000Z",
startedAt: null,
completedAt: null,
},
{ orchestrationStatus: "starting", activeRunId: null },
null,
),
).toBe("2026-09-06T23:33:00.000Z");
});
it("prefers the turn's own startedAt once the provider reports it", () => {
expect(
deriveActiveWorkStartedAt(
{
runId: "turn-1",
requestedAt: "2026-09-06T23:33:00.000Z",
startedAt: "2026-09-06T23:33:05.000Z",
completedAt: null,
},
{ orchestrationStatus: "running", activeRunId: "turn-1" },
null,
),
).toBe("2026-09-06T23:33:05.000Z");
});
// requestedAt must not leak past the end of the work.
it("stops counting once the turn has settled", () => {
expect(
deriveActiveWorkStartedAt(
{
runId: "turn-1",
requestedAt: "2026-09-06T23:33:00.000Z",
startedAt: "2026-09-06T23:33:05.000Z",
completedAt: "2026-09-06T23:33:09.000Z",
},
{ orchestrationStatus: "idle", activeRunId: null },
null,
),
).toBeNull();
});
// A session restarting with no new turn must not resurrect the old one.
it("does not count a settled turn while a session is starting again", () => {
expect(
deriveActiveWorkStartedAt(
{
runId: "turn-1",
requestedAt: "2026-09-06T23:33:00.000Z",
startedAt: "2026-09-06T23:33:05.000Z",
completedAt: "2026-09-06T23:33:09.000Z",
},
{ orchestrationStatus: "starting", activeRunId: null },
null,
),
).toBeNull();
});
it("falls back to the caller's send timestamp when there is no turn yet", () => {
expect(deriveActiveWorkStartedAt(null, null, "2026-09-06T23:33:00.000Z")).toBe(
"2026-09-06T23:33:00.000Z",
);
});
});
describe("deriveSubagentElapsedMs", () => {
const startedAt = "2026-09-21T12:00:00.000Z";
const completedAt = "2026-09-21T12:00:10.000Z";
const now = Date.parse("2026-09-21T13:00:00.000Z");
it.each(["idle", "completed", "failed", "cancelled", "interrupted"] as const)(
"does not count the age of %s work with unknown completion timing",
(status) => {
expect(deriveSubagentElapsedMs({ status, startedAt, completedAt: null }, now)).toBeNull();
expect(deriveSubagentElapsedMs({ status, startedAt, completedAt }, now)).toBe(10_000);
},
);
it("counts a resumed activation despite a stale previous completion timestamp", () => {
expect(deriveSubagentElapsedMs({ status: "running", startedAt, completedAt }, now)).toBe(
3_600_000,
);
expect(
deriveSubagentElapsedMs({ status: "running", startedAt: null, completedAt: null }, now),
).toBeNull();
});
});