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

serverUsage.test.ts

408 lines14.1 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 EnvironmentId,
4 UsageDay,
5 UsageReadError,
6 USAGE_CONTRACT_VERSION,
7 WS_METHODS,
8 type ServerConfig,
9 type ServerConfigStreamEvent,
10 type ServerSettings,
11 type UsageSource,
12 type UsageSummary,
13 type UsageSummaryInput,
14} from "@t3tools/contracts";
15import { expect, it } from "@effect/vitest";
16import * as Deferred from "effect/Deferred";
17import * as Effect from "effect/Effect";
18import * as Layer from "effect/Layer";
19import * as Option from "effect/Option";
20import * as Queue from "effect/Queue";
21import * as Result from "effect/Result";
22import * as Stream from "effect/Stream";
23import * as SubscriptionRef from "effect/SubscriptionRef";
24import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
25​
26import {
27 AVAILABLE_CONNECTION_STATE,
28 PrimaryConnectionTarget,
29 type PreparedConnection,
30 type SupervisorConnectionState,
31} from "../connection/model.ts";
32import type { EnvironmentPresentation } from "../connection/presentation.ts";
33import * as EnvironmentRegistry from "../connection/registry.ts";
34import * as EnvironmentSupervisor from "../connection/supervisor.ts";
35import * as Persistence from "../platform/persistence.ts";
36import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
37import type { RpcSession } from "../rpc/session.ts";
38import { createServerEnvironmentAtoms } from "./server.ts";
39import { refreshUsage } from "./usage.ts";
40​
41const TARGET = new PrimaryConnectionTarget({
42 environmentId: EnvironmentId.make("usage-environment"),
43 label: "Usage environment",
44 httpBaseUrl: "https://environment.example.test",
45 wsBaseUrl: "wss://environment.example.test",
46});
47​
48const INPUT: UsageSummaryInput = {
49 sinceDay: UsageDay.make("2026-09-01"),
50 untilDay: UsageDay.make("2026-09-04"),
51 timeZone: "UTC",
52};
53​
54const CONFIG = {
55 settings: DEFAULT_SERVER_SETTINGS,
56 environment: { serverVersion: "0.0.1", capabilities: {} },
57} as ServerConfig;
58​
59const PRICING = { status: "unavailable" as const, source: "test", fetchedAt: null, knownModels: 0 };
60​
61const cursorSource = (refreshing: boolean): UsageSource => ({
62 fingerprint: {
63 hostId: "cursor.com",
64 provider: "cursor",
65 resolvedHomePath: "cursor-account:abc",
66 volumeId: "abc",
67 },
68 status: "ok",
69 scannedFiles: 1,
70 skippedFiles: 0,
71 malformedRecords: 0,
72 distinctSessions: 1,
73 message: null,
74 ...(refreshing ? { refreshing: true as const } : {}),
75});
76​
77const makeHarness = Effect.fn("ServerUsageTest.makeHarness")(function* (
78 beforeRead: (
79 request: number,
80 input: UsageSummaryInput,
81 ) => Effect.Effect<void, UsageReadError> = () => Effect.void,
82 sources: (input: UsageSummaryInput) => readonly UsageSource[] = () => [],
83) {
84 const events = yield* Queue.unbounded<ServerConfigStreamEvent>();
85 let settings = DEFAULT_SERVER_SETTINGS;
86 let requests = 0;
87 const inputs: UsageSummaryInput[] = [];
88 const client = {
89 [WS_METHODS.serverRefreshUsageRates]: () => Effect.succeed(PRICING),
90 [WS_METHODS.subscribeServerConfig]: () =>
91 Stream.concat(
92 Stream.make({ version: 1 as const, type: "snapshot" as const, config: CONFIG }),
93 Stream.fromQueue(events),
94 ),
95 [WS_METHODS.serverGetUsageSummary]: (input: UsageSummaryInput) =>
96 Effect.gen(function* () {
97 requests += 1;
98 inputs.push(input);
99 const price = settings.usagePriceOverrides["custom-model"]?.inputCostPerMillionTokens ?? 0;
100 yield* beforeRead(requests, input);
101 return {
102 contractVersion: USAGE_CONTRACT_VERSION,
103 readAt: "2026-09-04T12:00:00Z",
104 ...input,
105 buckets: [
106 {
107 day: input.sinceDay,
108 provider: "codex",
109 model: "custom-model",
110 totals: {
111 uncachedInputTokens: 1_000_000,
112 cachedInputTokens: 0,
113 cacheCreationTokens: 0,
114 outputTokens: 0,
115 reasoningTokens: 0,
116 },
117 costUsd: price,
118 cacheSavingsUsd: 0,
119 costSource: price === 0 ? "unpriced" : "modelPriced",
120 records: 1,
121 unpricedRecords: price === 0 ? 1 : 0,
122 sessions: 1,
123 },
124 ],
125 sources: sources(input),
126 pricing: PRICING,
127 scanDurationMs: 0,
128 } satisfies UsageSummary;
129 }),
130 } as unknown as WsRpcProtocolClient;
131 const session: RpcSession = {
132 client,
133 initialConfig: Effect.succeed(CONFIG),
134 subscribeServerConfig: (input) => client.subscribeServerConfig(input),
135 ready: Effect.void,
136 probe: Effect.void,
137 closed: Effect.never,
138 };
139 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
140 target: TARGET,
141 state: yield* SubscriptionRef.make<SupervisorConnectionState>({
142 ...AVAILABLE_CONNECTION_STATE,
143 phase: "connected",
144 }),
145 session: yield* SubscriptionRef.make(Option.some(session)),
146 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
147 connect: Effect.void,
148 disconnect: Effect.void,
149 retryNow: Effect.void,
150 });
151 const environments = EnvironmentRegistry.EnvironmentRegistry.of({
152 run: (_environmentId, effect) =>
153 Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
154 followStream: (_environmentId, stream) =>
155 Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
156 } as EnvironmentRegistry.EnvironmentRegistry["Service"]);
157 const cache = Persistence.EnvironmentCacheStore.of({
158 loadShell: () => Effect.succeedNone,
159 saveShell: () => Effect.void,
160 loadThread: () => Effect.succeedNone,
161 saveThread: () => Effect.void,
162 removeThread: () => Effect.void,
163 loadServerConfig: () => Effect.succeedNone,
164 saveServerConfig: () => Effect.void,
165 loadVcsRefs: () => Effect.succeedNone,
166 saveVcsRefs: () => Effect.void,
167 removeVcsRefs: () => Effect.void,
168 clearVcsRefs: () => Effect.void,
169 clear: () => Effect.void,
170 });
171 const runtime = Atom.runtime(
172 Layer.merge(
173 Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environments),
174 Layer.succeed(Persistence.EnvironmentCacheStore, cache),
175 ),
176 );
177 const initialConfigValueAtom = Atom.make(CONFIG);
178 const atoms = createServerEnvironmentAtoms(runtime, {
179 initialConfigValueAtom: () => initialConfigValueAtom,
180 });
181 const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
182 Effect.sync(() => registry.dispose()),
183 );
184 const updateSettings = Effect.fn("ServerUsageTest.updateSettings")(function* (
185 next: ServerSettings,
186 ) {
187 settings = next;
188 yield* Queue.offer(events, {
189 version: 1,
190 type: "settingsUpdated",
191 payload: { settings },
192 });
193 yield* AtomRegistry.toStream(registry, atoms.settingsValueAtom(TARGET.environmentId)).pipe(
194 Stream.filter((current) => current === next),
195 Stream.runHead,
196 );
197 });
198 return {
199 registry,
200 atoms,
201 requests: () => requests,
202 inputs: () => inputs,
203 updateSettings,
204 summary: (input = INPUT) => atoms.usageSummary({ environmentId: TARGET.environmentId, input }),
205 };
206});
207​
208function settledSummary<E>(
209 registry: AtomRegistry.AtomRegistry,
210 atom: Atom.Atom<AsyncResult.AsyncResult<UsageSummary, E>>,
211) {
212 return AtomRegistry.toStream(registry, atom).pipe(
213 Stream.filterMap((result) =>
214 AsyncResult.isSuccess(result) && !result.waiting
215 ? Result.succeed(result.value)
216 : Result.failVoid,
217 ),
218 Stream.runHead,
219 Effect.map(Option.getOrThrow),
220 );
222​
223const makeRefreshingHarness = Effect.fn("ServerUsageTest.makeRefreshingHarness")(function* (
224 followUp: Effect.Effect<void, UsageReadError>,
225) {
226 const followUpStarted = yield* Deferred.make<void>();
227 const harness = yield* makeHarness(
228 (_request, input) =>
229 input.awaitRefresh === true
230 ? Deferred.succeed(followUpStarted, undefined).pipe(Effect.andThen(followUp))
231 : Effect.void,
232 (input) => [cursorSource(input.awaitRefresh !== true)],
233 );
234 return { ...harness, followUpStarted };
235});
236​
237function waitForCost<E>(
238 registry: AtomRegistry.AtomRegistry,
239 atom: Atom.Atom<AsyncResult.AsyncResult<UsageSummary, E>>,
240 cost: number,
241) {
242 return AtomRegistry.toStream(registry, atom).pipe(
243 Stream.filter(
244 (result) =>
245 AsyncResult.isSuccess(result) &&
246 !result.waiting &&
247 result.value.buckets[0]?.costUsd === cost,
248 ),
249 Stream.runHead,
250 );
252​
253it.effect("refreshes cached usage windows only when override prices change", () =>
254 Effect.scoped(
255 Effect.gen(function* () {
256 const harness = yield* makeHarness();
257 const current = harness.summary();
258 const previous = harness.summary({ ...INPUT, sinceDay: UsageDay.make("2026-08-01") });
259 const unmountCurrent = harness.registry.mount(current);
260 const unmountPrevious = harness.registry.mount(previous);
261 yield* waitForCost(harness.registry, current, 0);
262 yield* waitForCost(harness.registry, previous, 0);
263 unmountPrevious();
264 expect(harness.requests()).toBe(2);
265​
266 const prices = {
267 "custom-model": { inputCostPerMillionTokens: 3, outputCostPerMillionTokens: 9 },
268 "other-model": { inputCostPerMillionTokens: 1, outputCostPerMillionTokens: 2 },
269 };
270 yield* harness.updateSettings({ ...DEFAULT_SERVER_SETTINGS, usagePriceOverrides: prices });
271 yield* waitForCost(harness.registry, current, 3);
272 yield* waitForCost(harness.registry, previous, 3);
273 expect(harness.requests()).toBe(4);
274​
275 yield* harness.updateSettings({
276 ...DEFAULT_SERVER_SETTINGS,
277 usagePriceOverrides: {
278 "other-model": { outputCostPerMillionTokens: 2, inputCostPerMillionTokens: 1 },
279 "custom-model": { outputCostPerMillionTokens: 9, inputCostPerMillionTokens: 3 },
280 },
281 });
282 yield* harness.updateSettings({
283 ...DEFAULT_SERVER_SETTINGS,
284 usagePriceOverrides: prices,
285 defaultTheme: "custom-theme",
286 });
287 yield* harness.updateSettings(DEFAULT_SERVER_SETTINGS);
288 yield* waitForCost(harness.registry, current, 0);
289 yield* waitForCost(harness.registry, previous, 0);
290 expect(harness.requests()).toBe(6);
291 unmountCurrent();
292 }),
293 ),
294);
295​
296it.effect("restarts a pending usage read after a price change", () =>
297 Effect.scoped(
298 Effect.gen(function* () {
299 const started = yield* Deferred.make<void>();
300 const interrupted = yield* Deferred.make<void>();
301 const harness = yield* makeHarness((request) =>
302 request === 1
303 ? Deferred.succeed(started, undefined).pipe(
304 Effect.andThen(Effect.never),
305 Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined)),
306 )
307 : Effect.void,
308 );
309 const summary = harness.summary();
310 const unmount = harness.registry.mount(summary);
311 yield* Deferred.await(started);
312 yield* harness.updateSettings({
313 ...DEFAULT_SERVER_SETTINGS,
314 usagePriceOverrides: {
315 "custom-model": { inputCostPerMillionTokens: 5, outputCostPerMillionTokens: 10 },
316 },
317 });
318 yield* waitForCost(harness.registry, summary, 5);
319 yield* Deferred.await(interrupted);
320 expect(harness.requests()).toBe(2);
321 unmount();
322 }),
323 ),
324);
325​
326it.effect("settles after one usage read when no source is refreshing", () =>
327 Effect.scoped(
328 Effect.gen(function* () {
329 const harness = yield* makeHarness();
330 const summary = harness.summary();
331 const unmount = harness.registry.mount(summary);
332 yield* settledSummary(harness.registry, summary);
333 expect(harness.inputs()).toEqual([INPUT]);
334 unmount();
335 }),
336 ),
337);
338​
339it.effect("shows the cached usage summary until one awaited refresh replaces it", () =>
340 Effect.scoped(
341 Effect.gen(function* () {
342 const refreshed = yield* Deferred.make<void>();
343 const harness = yield* makeRefreshingHarness(Deferred.await(refreshed));
344 const summary = harness.summary();
345 const unmount = harness.registry.mount(summary);
346 yield* Deferred.await(harness.followUpStarted);
347​
348 const provisional = harness.registry.get(summary);
349 expect(AsyncResult.isSuccess(provisional) && provisional.waiting).toBe(true);
350 expect(Option.getOrNull(AsyncResult.value(provisional))?.sources).toEqual([
351 cursorSource(true),
352 ]);
353 expect(harness.inputs()).toEqual([INPUT, { ...INPUT, awaitRefresh: true }]);
354​
355 yield* Deferred.succeed(refreshed, undefined);
356 expect((yield* settledSummary(harness.registry, summary)).sources).toEqual([
357 cursorSource(false),
358 ]);
359 expect(harness.requests()).toBe(2);
360 unmount();
361 }),
362 ),
363);
364​
365it.effect("keeps the cached usage summary when the awaited refresh fails", () =>
366 Effect.scoped(
367 Effect.gen(function* () {
368 const harness = yield* makeRefreshingHarness(
369 Effect.fail(new UsageReadError({ reason: "scanFailed", detail: "Cursor is down" })),
370 );
371 const summary = harness.summary();
372 const unmount = harness.registry.mount(summary);
373 expect((yield* settledSummary(harness.registry, summary)).sources).toEqual([
374 cursorSource(true),
375 ]);
376 expect(AsyncResult.isSuccess(harness.registry.get(summary))).toBe(true);
377 expect(harness.requests()).toBe(2);
378 unmount();
379 }),
380 ),
381);
382​
383it.effect("finishes a manual usage refresh only after the awaited refresh", () =>
384 Effect.scoped(
385 Effect.gen(function* () {
386 const refreshed = yield* Deferred.make<void>();
387 const harness = yield* makeRefreshingHarness(Deferred.await(refreshed));
388 const presentation = Atom.make({
389 connection: { phase: "connected" },
390 } as EnvironmentPresentation | null);
391 let finished = false;
392 const refreshing = refreshUsage({
393 registry: harness.registry,
394 server: harness.atoms,
395 presentations: { presentationAtom: () => presentation },
396 environmentIds: [TARGET.environmentId],
397 input: INPUT,
398 }).then(() => {
399 finished = true;
400 });
401 yield* Deferred.await(harness.followUpStarted);
402 expect(finished).toBe(false);
403 yield* Deferred.succeed(refreshed, undefined);
404 yield* Effect.promise(() => refreshing);
405 expect(harness.requests()).toBe(2);
406 }),
407 ),
408);