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

UsageLimitSources.test.ts

159 lines5.2 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { expect, it } from "@effect/vitest";
3import { SecretProviderId, ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
4import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Schema from "effect/Schema";
8import * as Stream from "effect/Stream";
9import { HttpClient, HttpClientResponse } from "effect/http";
10​
11import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
12import * as ServerSettings from "../serverSettings.ts";
13import * as UsageLimitSources from "./UsageLimitSources.ts";
14​
15const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent);
16​
17it.effect("keeps malformed source URLs encodable for config subscribers", () =>
18 Effect.gen(function* () {
19 const sources = yield* UsageLimitSources.make;
20 yield* sources.refresh;
21 const snapshots = yield* sources.current;
22 expect(snapshots.map((source) => source.label)).toEqual([
23 "local-hub",
24 "opaque-hub",
25 "Named hub",
26 "hub.test:8317",
27 "Usage limit source",
28 ]);
29 expect(() =>
30 encodeConfigEvent({
31 version: 1,
32 type: "usageLimitSourcesUpdated",
33 payload: { sources: snapshots },
34 }),
35 ).not.toThrow();
36 expect(snapshots.every((source) => source.error === "No management key configured.")).toBe(
37 true,
38 );
39 }).pipe(
40 Effect.provide([
41 Layer.succeed(SecretResolver.SecretResolver, {
42 label: (provider) => Effect.succeed(provider),
43 read: (references) => Effect.succeed(references.map(() => undefined)),
44 invalidate: () => Effect.void,
45 }),
46 ServerSettings.layerTest({
47 usageLimitSources: {
48 [UsageLimitSourceId.make("local-hub")]: {
49 kind: "cliproxy",
50 url: "localhost:8317",
51 managementKey: "",
52 enabled: true,
53 },
54 [UsageLimitSourceId.make("opaque-hub")]: {
55 kind: "cliproxy",
56 url: "file:///tmp/hub",
57 managementKey: "",
58 enabled: true,
59 },
60 [UsageLimitSourceId.make("named-hub")]: {
61 kind: "cliproxy",
62 url: "localhost:8317",
63 label: "Named hub",
64 managementKey: "",
65 enabled: true,
66 },
67 [UsageLimitSourceId.make("valid-hub")]: {
68 kind: "cliproxy",
69 url: "https://hub.test:8317",
70 managementKey: "",
71 enabled: true,
72 },
73 [UsageLimitSourceId.make(" ")]: {
74 kind: "cliproxy",
75 url: "localhost:8317",
76 managementKey: "",
77 enabled: true,
78 },
79 },
80 }),
81 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
82 shouldRunScopeWork: () => Effect.succeed(false),
83 }),
84 Layer.succeed(
85 HttpClient.HttpClient,
86 HttpClient.make(() => Effect.die("Unexpected network request")),
87 ),
88 NodeCrypto.layer,
89 ]),
90 Effect.scoped,
91 ),
92);
93​
94it.effect("reads a management key from its secret provider", () => {
95 const authorizations: Array<string | undefined> = [];
96 const keySource = (reference: string) => ({
97 provider: SecretProviderId.make("homelab"),
98 reference,
99 });
100 return Effect.gen(function* () {
101 const sources = yield* UsageLimitSources.make;
102 const snapshots = yield* sources.streamChanges.pipe(
103 Stream.filter((next) => next.length > 0),
104 Stream.runHead,
105 Effect.flatMap(Effect.fromOption),
106 );
107 expect(snapshots.map(({ id, error }) => ({ id, error }))).toEqual([
108 { id: "resolved-hub", error: undefined },
109 {
110 id: "unresolved-hub",
111 error: "Could not read the management key from its secret provider.",
112 },
113 ]);
114 expect(authorizations).toEqual(["Bearer resolved-key"]);
115 }).pipe(
116 Effect.provide([
117 ServerSettings.layerTest({
118 usageLimitSources: {
119 [UsageLimitSourceId.make("resolved-hub")]: {
120 kind: "cliproxy",
121 url: "https://resolved.test",
122 managementKey: keySource("cliproxy#key"),
123 enabled: true,
124 },
125 [UsageLimitSourceId.make("unresolved-hub")]: {
126 kind: "cliproxy",
127 url: "https://unresolved.test",
128 managementKey: keySource("missing#key"),
129 enabled: true,
130 },
131 },
132 }),
133 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
134 shouldRunScopeWork: () => Effect.succeed(false),
135 }),
136 Layer.succeed(SecretResolver.SecretResolver, {
137 label: (provider) => Effect.succeed(provider),
138 read: (references) =>
139 Effect.succeed(
140 references.map((reference) =>
141 reference.reference === "cliproxy#key" ? "resolved-key" : undefined,
142 ),
143 ),
144 invalidate: () => Effect.void,
145 }),
146 Layer.succeed(
147 HttpClient.HttpClient,
148 HttpClient.make((request) =>
149 Effect.sync(() => {
150 authorizations.push(request.headers.authorization);
151 return HttpClientResponse.fromWeb(request, Response.json({ files: [] }));
152 }),
153 ),
154 ),
155 NodeCrypto.layer,
156 ]),
157 Effect.scoped,
158 );
159});