@@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import * as Layer from "effect/Layer"; |
4 | 4 | import { assert, describe, it } from "@effect/vitest"; |
5 | 5 | import * as Deferred from "effect/Deferred"; |
| 6 | +import * as Queue from "effect/Queue"; |
6 | 7 | import * as Context from "effect/Context"; |
7 | 8 | import * as Effect from "effect/Effect"; |
8 | 9 | import * as Fiber from "effect/Fiber"; |
@@ -510,37 +511,76 @@ describe("readCursorAccountUsage", () => { |
510 | 511 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
511 | 512 | ); |
512 | 513 |
|
513 | | - it.effect("reads the pages behind the first together and keeps them in page order", () => |
514 | | - Effect.gen(function* () { |
515 | | - const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`; |
516 | | - let inFlight = 0; |
517 | | - let mostInFlight = 0; |
518 | | - const result = yield* readAccountUsage( |
519 | | - { kind: "apiKey", apiKey: accessToken }, |
520 | | - 0, |
521 | | - 1781000000000, |
522 | | - async ({ page }) => { |
523 | | - inFlight++; |
524 | | - mostInFlight = Math.max(mostInFlight, inFlight); |
525 | | - for (let turn = page; turn < 9; turn++) await Promise.resolve(); |
526 | | - inFlight--; |
527 | | - return Response.json({ |
528 | | - totalUsageEventsCount: 8001, |
529 | | - usageEventsDisplay: Array.from({ length: page === 9 ? 1 : 1000 }, (_, index) => ({ |
530 | | - timestamp: String(1780000000000 + (page - 1) * 1000 + index), |
531 | | - model: "gpt-5", |
532 | | - tokenUsage: { inputTokens: 1 }, |
533 | | - })), |
534 | | - }); |
535 | | - }, |
536 | | - ); |
537 | | - assert.isNull(result.error); |
538 | | - assert.strictEqual(mostInFlight, 6); |
539 | | - assert.deepStrictEqual( |
540 | | - result.records.map((record) => record.timestampMs), |
541 | | - Array.from({ length: 8001 }, (_, index) => 1780000000000 + index), |
542 | | - ); |
543 | | - }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 514 | + it.effect.each([{ queued: false }, { queued: true }])( |
| 515 | + "reads concurrent pages in order with a full request deadline after queuing: $queued", |
| 516 | + ({ queued }) => |
| 517 | + Effect.gen(function* () { |
| 518 | + const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`; |
| 519 | + const started = yield* Queue.unbounded<void>(); |
| 520 | + const firstBatch = yield* Deferred.make<void>(); |
| 521 | + const secondBatch = yield* Deferred.make<void>(); |
| 522 | + let delayedRequests = 0; |
| 523 | + let inFlight = 0; |
| 524 | + let mostInFlight = 0; |
| 525 | + const client = dashboard( |
| 526 | + async ({ page }) => { |
| 527 | + inFlight++; |
| 528 | + mostInFlight = Math.max(mostInFlight, inFlight); |
| 529 | + for (let turn = page; turn < 9; turn++) await Promise.resolve(); |
| 530 | + inFlight--; |
| 531 | + return Response.json({ |
| 532 | + totalUsageEventsCount: 8001, |
| 533 | + usageEventsDisplay: Array.from({ length: page === 9 ? 1 : 1000 }, (_, index) => ({ |
| 534 | + timestamp: String(1780000000000 + (page - 1) * 1000 + index), |
| 535 | + model: "gpt-5", |
| 536 | + tokenUsage: { inputTokens: 1 }, |
| 537 | + })), |
| 538 | + }); |
| 539 | + }, |
| 540 | + { userId: 42, teamIds: queued ? [7] : [] }, |
| 541 | + ).pipe( |
| 542 | + HttpClient.transform((response, request) => { |
| 543 | + if (!queued || !request.url.endsWith("/GetFilteredUsageEvents")) return response; |
| 544 | + const body = |
| 545 | + request.body._tag === "Uint8Array" |
| 546 | + ? JSON.parse(new TextDecoder().decode(request.body.body)) |
| 547 | + : {}; |
| 548 | + if (body.page === 1) return response; |
| 549 | + return Effect.gen(function* () { |
| 550 | + const batch = delayedRequests++ < 6 ? firstBatch : secondBatch; |
| 551 | + yield* Queue.offer(started, undefined); |
| 552 | + yield* Deferred.await(batch); |
| 553 | + return yield* response; |
| 554 | + }); |
| 555 | + }), |
| 556 | + ); |
| 557 | + const read = yield* Effect.forkChild( |
| 558 | + makeCursorAccountUsageReader.pipe( |
| 559 | + Effect.flatMap((reader) => |
| 560 | + reader({ kind: "apiKey", apiKey: accessToken }, 0, 1781000000000), |
| 561 | + ), |
| 562 | + Effect.provideService(HttpClient.HttpClient, client), |
| 563 | + ), |
| 564 | + ); |
| 565 | + if (queued) { |
| 566 | + yield* Effect.all(Array.from({ length: 6 }, () => Queue.take(started))); |
| 567 | + yield* TestClock.adjust("6 seconds"); |
| 568 | + yield* Deferred.succeed(firstBatch, undefined); |
| 569 | + yield* Effect.all(Array.from({ length: 6 }, () => Queue.take(started))); |
| 570 | + yield* TestClock.adjust("6 seconds"); |
| 571 | + yield* Deferred.succeed(secondBatch, undefined); |
| 572 | + } |
| 573 | + const result = yield* Fiber.join(read); |
| 574 | + assert.isNull(result.error); |
| 575 | + assert.strictEqual(mostInFlight, 6); |
| 576 | + assert.deepStrictEqual( |
| 577 | + result.records.map((record) => record.timestampMs), |
| 578 | + Array.from( |
| 579 | + { length: queued ? 16002 : 8001 }, |
| 580 | + (_, index) => 1780000000000 + (index % 8001), |
| 581 | + ), |
| 582 | + ); |
| 583 | + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
544 | 584 | ); |
545 | 585 |
|
546 | 586 | it.effect("ends like a page-by-page read when a page fails, leaving no request open", () => |
|