| 1 | import { |
| 2 | NodeId, |
| 3 | type OrchestrationV2ProviderTurn, |
| 4 | ProviderThreadId, |
| 5 | ProviderTurnId, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { assert, describe, it } from "@effect/vitest"; |
| 8 | import * as DateTime from "effect/DateTime"; |
| 9 | |
| 10 | import { upsertProviderTurn } from "./ProjectionStore.ts"; |
| 11 | |
| 12 | describe("provider turn token usage projection", () => { |
| 13 | it("keeps live usage when a terminal update omits it", () => { |
| 14 | const now = DateTime.makeUnsafe("2026-08-29T00:00:00.000Z"); |
| 15 | const running = { |
| 16 | id: ProviderTurnId.make("provider-turn-usage"), |
| 17 | providerThreadId: ProviderThreadId.make("provider-thread-usage"), |
| 18 | nodeId: NodeId.make("node-usage"), |
| 19 | runAttemptId: null, |
| 20 | nativeTurnRef: null, |
| 21 | ordinal: 1, |
| 22 | status: "running", |
| 23 | startedAt: now, |
| 24 | completedAt: null, |
| 25 | tokenUsage: { |
| 26 | usedTokens: 50_000, |
| 27 | maxTokens: 200_000, |
| 28 | updatedAt: "2026-08-29T00:00:00.000Z", |
| 29 | }, |
| 30 | } satisfies OrchestrationV2ProviderTurn; |
| 31 | const completed = { |
| 32 | ...running, |
| 33 | status: "completed", |
| 34 | completedAt: now, |
| 35 | tokenUsage: undefined, |
| 36 | } satisfies OrchestrationV2ProviderTurn; |
| 37 | |
| 38 | const projected = upsertProviderTurn([running], completed); |
| 39 | |
| 40 | assert.deepEqual(projected[0]?.tokenUsage, running.tokenUsage); |
| 41 | assert.equal(projected[0]?.status, "completed"); |
| 42 | }); |
| 43 | }); |