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