|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import { UsageLimitSourceId } from "@t3tools/contracts"; |
| 3 | +import * as Duration from "effect/Duration"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as Ref from "effect/Ref"; |
| 7 | +import * as Stream from "effect/Stream"; |
| 8 | +import { TestClock } from "effect/testing"; |
| 9 | +import { HttpClient, HttpClientResponse } from "effect/unstable/http"; |
| 10 | + |
| 11 | +import { BackgroundPolicy } from "../background/BackgroundPolicy.ts"; |
| 12 | +import { layerTest } from "../serverSettings.ts"; |
| 13 | +import * as UsageLimitSources from "./UsageLimitSources.ts"; |
| 14 | + |
| 15 | +const backgroundPolicy = Layer.succeed(BackgroundPolicy, { |
| 16 | + reportClientActivity: () => Effect.void, |
| 17 | + removeRpcClient: () => Effect.void, |
| 18 | + reportHostPowerState: () => Effect.void, |
| 19 | + snapshot: Effect.die("unused"), |
| 20 | + streamChanges: Stream.empty, |
| 21 | + subscribe: Effect.die("unused"), |
| 22 | + hasDemand: () => Effect.succeed(true), |
| 23 | + shouldRunScopeWork: () => Effect.succeed(true), |
| 24 | + shouldRunOpportunisticWork: Effect.succeed(true), |
| 25 | +}); |
| 26 | + |
| 27 | +describe("UsageLimitSources refresh interval", () => { |
| 28 | + it.effect.each([ |
| 29 | + { interval: Duration.seconds(0), expectedReads: 1 }, |
| 30 | + { interval: Duration.minutes(1), expectedReads: 2 }, |
| 31 | + ])("polls only when enabled: $expectedReads reads", ({ interval, expectedReads }) => |
| 32 | + Effect.scoped( |
| 33 | + Effect.gen(function* () { |
| 34 | + const reads = yield* Ref.make(0); |
| 35 | + const httpClient = Layer.succeed( |
| 36 | + HttpClient.HttpClient, |
| 37 | + HttpClient.make((request) => |
| 38 | + Ref.update(reads, (count) => count + 1).pipe( |
| 39 | + Effect.as(HttpClientResponse.fromWeb(request, Response.json({ accounts: {} }))), |
| 40 | + ), |
| 41 | + ), |
| 42 | + ); |
| 43 | + const sources = yield* UsageLimitSources.make.pipe( |
| 44 | + Effect.provide( |
| 45 | + Layer.mergeAll( |
| 46 | + backgroundPolicy, |
| 47 | + httpClient, |
| 48 | + layerTest({ |
| 49 | + providerHealthRefreshInterval: interval, |
| 50 | + usageLimitSources: { |
| 51 | + [UsageLimitSourceId.make("hub")]: { |
| 52 | + kind: "cliproxy", |
| 53 | + url: "https://hub.example.com", |
| 54 | + managementKey: "test-key", |
| 55 | + enabled: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }), |
| 59 | + ), |
| 60 | + ), |
| 61 | + ); |
| 62 | + yield* sources.streamChanges.pipe( |
| 63 | + Stream.filter((snapshots) => snapshots.length === 1), |
| 64 | + Stream.runHead, |
| 65 | + ); |
| 66 | + assert.strictEqual(yield* Ref.get(reads), 1); |
| 67 | + |
| 68 | + yield* TestClock.adjust("61 seconds"); |
| 69 | + assert.strictEqual(yield* Ref.get(reads), expectedReads); |
| 70 | + |
| 71 | + yield* sources.refresh; |
| 72 | + assert.strictEqual(yield* Ref.get(reads), expectedReads + 1); |
| 73 | + }), |
| 74 | + ).pipe(Effect.provide(TestClock.layer())), |
| 75 | + ); |
| 76 | +}); |
0 commit comments