|
| 1 | +import { expect, it } from "@effect/vitest"; |
| 2 | +import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Schema from "effect/Schema"; |
| 6 | +import { HttpClient } from "effect/unstable/http"; |
| 7 | + |
| 8 | +import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
| 9 | +import * as ServerSettings from "../serverSettings.ts"; |
| 10 | +import * as UsageLimitSources from "./UsageLimitSources.ts"; |
| 11 | + |
| 12 | +const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent); |
| 13 | + |
| 14 | +it.effect("keeps malformed source URLs encodable for config subscribers", () => |
| 15 | + Effect.gen(function* () { |
| 16 | + const sources = yield* UsageLimitSources.make; |
| 17 | + yield* sources.refresh; |
| 18 | + const snapshots = yield* sources.current; |
| 19 | + expect(snapshots.map((source) => source.label)).toEqual([ |
| 20 | + "local-hub", |
| 21 | + "opaque-hub", |
| 22 | + "Named hub", |
| 23 | + "hub.test:8317", |
| 24 | + "Usage limit source", |
| 25 | + ]); |
| 26 | + expect(() => |
| 27 | + encodeConfigEvent({ |
| 28 | + version: 1, |
| 29 | + type: "usageLimitSourcesUpdated", |
| 30 | + payload: { sources: snapshots }, |
| 31 | + }), |
| 32 | + ).not.toThrow(); |
| 33 | + expect(snapshots.every((source) => source.error === "No management key configured.")).toBe( |
| 34 | + true, |
| 35 | + ); |
| 36 | + }).pipe( |
| 37 | + Effect.provide([ |
| 38 | + ServerSettings.layerTest({ |
| 39 | + usageLimitSources: { |
| 40 | + [UsageLimitSourceId.make("local-hub")]: { |
| 41 | + kind: "cliproxy", |
| 42 | + url: "localhost:8317", |
| 43 | + managementKey: "", |
| 44 | + enabled: true, |
| 45 | + }, |
| 46 | + [UsageLimitSourceId.make("opaque-hub")]: { |
| 47 | + kind: "cliproxy", |
| 48 | + url: "file:///tmp/hub", |
| 49 | + managementKey: "", |
| 50 | + enabled: true, |
| 51 | + }, |
| 52 | + [UsageLimitSourceId.make("named-hub")]: { |
| 53 | + kind: "cliproxy", |
| 54 | + url: "localhost:8317", |
| 55 | + label: "Named hub", |
| 56 | + managementKey: "", |
| 57 | + enabled: true, |
| 58 | + }, |
| 59 | + [UsageLimitSourceId.make("valid-hub")]: { |
| 60 | + kind: "cliproxy", |
| 61 | + url: "https://hub.test:8317", |
| 62 | + managementKey: "", |
| 63 | + enabled: true, |
| 64 | + }, |
| 65 | + [UsageLimitSourceId.make(" ")]: { |
| 66 | + kind: "cliproxy", |
| 67 | + url: "localhost:8317", |
| 68 | + managementKey: "", |
| 69 | + enabled: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }), |
| 73 | + Layer.mock(BackgroundPolicy.BackgroundPolicy)({ |
| 74 | + shouldRunScopeWork: () => Effect.succeed(false), |
| 75 | + }), |
| 76 | + Layer.succeed( |
| 77 | + HttpClient.HttpClient, |
| 78 | + HttpClient.make(() => Effect.die("Unexpected network request")), |
| 79 | + ), |
| 80 | + ]), |
| 81 | + Effect.scoped, |
| 82 | + ), |
| 83 | +); |
0 commit comments