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

UsageLimitSources.test.ts

157 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 ),
91);
92​
93it.effect("reads a management key from its secret provider", () => {
94 const authorizations: Array<string | undefined> = [];
95 const keySource = (reference: string) => ({
96 provider: SecretProviderId.make("homelab"),
97 reference,
98 });
99 return Effect.gen(function* () {
100 const sources = yield* UsageLimitSources.make;
101 const snapshots = yield* sources.streamChanges.pipe(
102 Stream.filter((next) => next.length > 0),
103 Stream.runHead,
104 Effect.flatMap(Effect.fromOption),
105 );
106 expect(snapshots.map(({ id, error }) => ({ id, error }))).toEqual([
107 { id: "resolved-hub", error: undefined },
108 {
109 id: "unresolved-hub",
110 error: "Could not read the management key from its secret provider.",
111 },
112 ]);
113 expect(authorizations).toEqual(["Bearer resolved-key"]);
114 }).pipe(
115 Effect.provide([
116 ServerSettings.layerTest({
117 usageLimitSources: {
118 [UsageLimitSourceId.make("resolved-hub")]: {
119 kind: "cliproxy",
120 url: "https://resolved.test",
121 managementKey: keySource("cliproxy#key"),
122 enabled: true,
123 },
124 [UsageLimitSourceId.make("unresolved-hub")]: {
125 kind: "cliproxy",
126 url: "https://unresolved.test",
127 managementKey: keySource("missing#key"),
128 enabled: true,
129 },
130 },
131 }),
132 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
133 shouldRunScopeWork: () => Effect.succeed(false),
134 }),
135 Layer.succeed(SecretResolver.SecretResolver, {
136 label: (provider) => Effect.succeed(provider),
137 read: (references) =>
138 Effect.succeed(
139 references.map((reference) =>
140 reference.reference === "cliproxy#key" ? "resolved-key" : undefined,
141 ),
142 ),
143 invalidate: () => Effect.void,
144 }),
145 Layer.succeed(
146 HttpClient.HttpClient,
147 HttpClient.make((request) =>
148 Effect.sync(() => {
149 authorizations.push(request.headers.authorization);
150 return HttpClientResponse.fromWeb(request, Response.json({ files: [] }));
151 }),
152 ),
153 ),
154 NodeCrypto.layer,
155 ]),
156 );
157});