|
7 | 7 | type ServerConfig, |
8 | 8 | type ServerConfigStreamEvent, |
9 | 9 | type ServerSettings, |
| 10 | + type UsageThreadBreakdown, |
10 | 11 | type UsageSummary, |
11 | 12 | type UsageSummaryInput, |
12 | 13 | } from "@t3tools/contracts"; |
@@ -57,7 +58,21 @@ const makeHarness = Effect.fn("ServerUsageTest.makeHarness")(function* ( |
57 | 58 | const events = yield* Queue.unbounded<ServerConfigStreamEvent>(); |
58 | 59 | let settings = DEFAULT_SERVER_SETTINGS; |
59 | 60 | let requests = 0; |
| 61 | + let threadRequests = 0; |
60 | 62 | const client = { |
| 63 | + [WS_METHODS.serverGetUsageThreadBreakdown]: () => |
| 64 | + Effect.sync(() => { |
| 65 | + threadRequests += 1; |
| 66 | + return { |
| 67 | + ...INPUT, |
| 68 | + contractVersion: USAGE_CONTRACT_VERSION, |
| 69 | + readAt: "2026-09-04T12:00:00Z", |
| 70 | + rows: [], |
| 71 | + truncatedRows: 0, |
| 72 | + scanDurationMs: |
| 73 | + settings.usagePriceOverrides["custom-model"]?.inputCostPerMillionTokens ?? 0, |
| 74 | + } satisfies UsageThreadBreakdown; |
| 75 | + }), |
61 | 76 | [WS_METHODS.subscribeServerConfig]: () => |
62 | 77 | Stream.concat( |
63 | 78 | Stream.make({ version: 1 as const, type: "snapshot" as const, config: CONFIG }), |
@@ -168,6 +183,8 @@ const makeHarness = Effect.fn("ServerUsageTest.makeHarness")(function* ( |
168 | 183 | return { |
169 | 184 | registry, |
170 | 185 | requests: () => requests, |
| 186 | + threadRequests: () => threadRequests, |
| 187 | + threads: atoms.usageThreadBreakdown({ environmentId: TARGET.environmentId, input: INPUT }), |
171 | 188 | updateSettings, |
172 | 189 | summary: (input = INPUT) => atoms.usageSummary({ environmentId: TARGET.environmentId, input }), |
173 | 190 | }; |
@@ -261,3 +278,36 @@ it.effect("restarts a pending usage read after a price change", () => |
261 | 278 | }), |
262 | 279 | ), |
263 | 280 | ); |
| 281 | + |
| 282 | +it.effect("refreshes mounted thread breakdowns when override prices change", () => |
| 283 | + Effect.scoped( |
| 284 | + Effect.gen(function* () { |
| 285 | + const harness = yield* makeHarness(); |
| 286 | + const unmount = harness.registry.mount(harness.threads); |
| 287 | + const waitForPrice = (price: number) => |
| 288 | + AtomRegistry.toStream(harness.registry, harness.threads).pipe( |
| 289 | + Stream.filter( |
| 290 | + (result) => |
| 291 | + AsyncResult.isSuccess(result) && |
| 292 | + !result.waiting && |
| 293 | + result.value.scanDurationMs === price, |
| 294 | + ), |
| 295 | + Stream.runHead, |
| 296 | + ); |
| 297 | + yield* waitForPrice(0); |
| 298 | + expect(harness.threadRequests()).toBe(1); |
| 299 | + yield* harness.updateSettings({ |
| 300 | + ...DEFAULT_SERVER_SETTINGS, |
| 301 | + usagePriceOverrides: { |
| 302 | + "custom-model": { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 9 }, |
| 303 | + }, |
| 304 | + }); |
| 305 | + yield* waitForPrice(3); |
| 306 | + expect(harness.threadRequests()).toBe(2); |
| 307 | + yield* harness.updateSettings(DEFAULT_SERVER_SETTINGS); |
| 308 | + yield* waitForPrice(0); |
| 309 | + expect(harness.threadRequests()).toBe(3); |
| 310 | + unmount(); |
| 311 | + }), |
| 312 | + ), |
| 313 | +); |
0 commit comments