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