Open History to see the last commit
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 read: (references) => Effect.succeed(references.map(() => undefined)),
43 invalidate: () => Effect.void,
44 }),
45 ServerSettings.layerTest({
46 usageLimitSources: {
47 [UsageLimitSourceId.make("local-hub")]: {
48 kind: "cliproxy",
49 url: "localhost:8317",
50 managementKey: "",
51 enabled: true,
52 },
53 [UsageLimitSourceId.make("opaque-hub")]: {
54 kind: "cliproxy",
55 url: "file:///tmp/hub",
56 managementKey: "",
57 enabled: true,
58 },
59 [UsageLimitSourceId.make("named-hub")]: {
60 kind: "cliproxy",
61 url: "localhost:8317",
62 label: "Named hub",
63 managementKey: "",
64 enabled: true,
65 },
66 [UsageLimitSourceId.make("valid-hub")]: {
67 kind: "cliproxy",
68 url: "https://hub.test:8317",
69 managementKey: "",
70 enabled: true,
71 },
72 [UsageLimitSourceId.make(" ")]: {
73 kind: "cliproxy",
74 url: "localhost:8317",
75 managementKey: "",
76 enabled: true,
77 },
78 },
79 }),
80 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
81 shouldRunScopeWork: () => Effect.succeed(false),
82 }),
83 Layer.succeed(
84 HttpClient.HttpClient,
85 HttpClient.make(() => Effect.die("Unexpected network request")),
86 ),
87 NodeCrypto.layer,
88 ]),
89 Effect.scoped,
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 read: (references) =>
137 Effect.succeed(
138 references.map((reference) =>
139 reference.reference === "cliproxy#key" ? "resolved-key" : undefined,
140 ),
141 ),
142 invalidate: () => Effect.void,
143 }),
144 Layer.succeed(
145 HttpClient.HttpClient,
146 HttpClient.make((request) =>
147 Effect.sync(() => {
148 authorizations.push(request.headers.authorization);
149 return HttpClientResponse.fromWeb(request, Response.json({ files: [] }));
150 }),
151 ),
152 ),
153 NodeCrypto.layer,
154 ]),
155 Effect.scoped,
156 );
157});