Open History to see the last commit
1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { expect, it } from "@effect/vitest";
3import {
4 ServerConfigStreamEvent,
5 type ServerSettings as ServerSettingsValue,
6 UsageLimitSourceId,
7} from "@t3tools/contracts";
8import * as Duration from "effect/Duration";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as PubSub from "effect/PubSub";
12import * as Ref from "effect/Ref";
13import * as Schema from "effect/Schema";
14import * as Stream from "effect/Stream";
15import * as TestClock from "effect/testing/TestClock";
16import { HttpClient, HttpClientResponse } from "effect/http";
17
18import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
19import * as ServerSettings from "../serverSettings.ts";
20import * as UsageLimitSources from "./UsageLimitSources.ts";
21
22const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent);
23
24it.effect("keeps malformed source URLs encodable for config subscribers", () =>
25 Effect.gen(function* () {
26 const sources = yield* UsageLimitSources.make;
27 yield* sources.refresh;
28 const snapshots = yield* sources.current;
29 expect(snapshots.map((source) => source.label)).toEqual([
30 "local-hub",
31 "opaque-hub",
32 "Named hub",
33 "hub.test:8317",
34 "Usage limit source",
35 ]);
36 expect(() =>
37 encodeConfigEvent({
38 version: 1,
39 type: "usageLimitSourcesUpdated",
40 payload: { sources: snapshots },
41 }),
42 ).not.toThrow();
43 expect(snapshots.every((source) => source.error === "No management key configured.")).toBe(
44 true,
45 );
46 }).pipe(
47 Effect.provide([
48 ServerSettings.layerTest({
49 usageLimitSources: {
50 [UsageLimitSourceId.make("local-hub")]: {
51 kind: "cliproxy",
52 url: "localhost:8317",
53 managementKey: "",
54 enabled: true,
55 },
56 [UsageLimitSourceId.make("opaque-hub")]: {
57 kind: "cliproxy",
58 url: "file:///tmp/hub",
59 managementKey: "",
60 enabled: true,
61 },
62 [UsageLimitSourceId.make("named-hub")]: {
63 kind: "cliproxy",
64 url: "localhost:8317",
65 label: "Named hub",
66 managementKey: "",
67 enabled: true,
68 },
69 [UsageLimitSourceId.make("valid-hub")]: {
70 kind: "cliproxy",
71 url: "https://hub.test:8317",
72 managementKey: "",
73 enabled: true,
74 },
75 [UsageLimitSourceId.make(" ")]: {
76 kind: "cliproxy",
77 url: "localhost:8317",
78 managementKey: "",
79 enabled: true,
80 },
81 },
82 }),
83 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
84 shouldRunScopeWork: () => Effect.succeed(false),
85 }),
86 Layer.succeed(
87 HttpClient.HttpClient,
88 HttpClient.make(() => Effect.die("Unexpected network request")),
89 ),
90 NodeCrypto.layer,
91 ]),
92 Effect.scoped,
93 ),
94);
95
96it.effect.each([
97 { interval: Duration.seconds(0), expectedReads: 1, mode: "disabled" },
98 { interval: Duration.minutes(1), expectedReads: 2, mode: "enabled" },
99])(
100 "honors $mode polling while preserving explicit and settings refreshes",
101 ({ interval, expectedReads }) =>
102 Effect.gen(function* () {
103 const settings = yield* ServerSettings.ServerSettingsService;
104 const settingsChanges = yield* PubSub.unbounded<ServerSettingsValue>();
105 const reads = yield* Ref.make(0);
106 const allowBackgroundWork = yield* Ref.make(true);
107 const sources = yield* UsageLimitSources.make.pipe(
108 Effect.provide([
109 Layer.succeed(ServerSettings.ServerSettingsService, {
110 ...settings,
111 streamChanges: Stream.fromPubSub(settingsChanges),
112 }),
113 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
114 shouldRunScopeWork: (scope) =>
115 scope.type === "provider-status"
116 ? Ref.get(allowBackgroundWork)
117 : Effect.succeed(false),
118 }),
119 Layer.succeed(
120 HttpClient.HttpClient,
121 HttpClient.make((request) =>
122 Ref.update(reads, (count) => count + 1).pipe(
123 Effect.as(HttpClientResponse.fromWeb(request, Response.json({ files: [] }))),
124 ),
125 ),
126 ),
127 NodeCrypto.layer,
128 ]),
129 );
130 yield* sources.streamChanges.pipe(
131 Stream.filter((snapshots) => snapshots.length === 1),
132 Stream.runHead,
133 );
134 expect((yield* sources.current)[0]?.error).toBeUndefined();
135 expect(yield* Ref.get(reads)).toBe(1);
136
137 yield* TestClock.adjust("1 minute");
138 expect(yield* Ref.get(reads)).toBe(expectedReads);
139
140 yield* sources.refresh;
141 expect(yield* Ref.get(reads)).toBe(expectedReads + 1);
142
143 const current = yield* settings.getSettings;
144 const updated = yield* settings.updateSettings({
145 usageLimitSources: {
146 [UsageLimitSourceId.make("hub")]: {
147 ...current.usageLimitSources[UsageLimitSourceId.make("hub")]!,
148 label: "Renamed hub",
149 },
150 },
151 });
152 yield* PubSub.publish(settingsChanges, updated);
153 yield* sources.streamChanges.pipe(
154 Stream.filter((snapshots) => snapshots[0]?.label === "Renamed hub"),
155 Stream.runHead,
156 );
157 expect(yield* Ref.get(reads)).toBe(expectedReads + 2);
158
159 yield* Ref.set(allowBackgroundWork, false);
160 yield* TestClock.adjust("1 minute");
161 expect(yield* Ref.get(reads)).toBe(expectedReads + 2);
162
163 const beforeResuming = (yield* sources.current)[0]?.checkedAt;
164 yield* Ref.set(allowBackgroundWork, true);
165 yield* settings.updateSettings({ providerHealthRefreshInterval: Duration.minutes(1) });
166 yield* TestClock.adjust("2 minutes");
167 expect(yield* Ref.get(reads)).toBeGreaterThan(expectedReads + 2);
168 yield* sources.streamChanges.pipe(
169 Stream.filter((snapshots) => snapshots[0]?.checkedAt !== beforeResuming),
170 Stream.runHead,
171 );
172 yield* TestClock.adjust("30 seconds");
173 const readsBeforeDisabling = yield* Ref.get(reads);
174 yield* settings.updateSettings({ providerHealthRefreshInterval: Duration.seconds(0) });
175 yield* TestClock.adjust("30 seconds");
176 expect(yield* Ref.get(reads)).toBe(readsBeforeDisabling);
177 }).pipe(
178 Effect.provide(
179 ServerSettings.layerTest({
180 providerHealthRefreshInterval: interval,
181 usageLimitSources: {
182 [UsageLimitSourceId.make("hub")]: {
183 kind: "cliproxy",
184 url: "https://hub.test",
185 managementKey: "test-key",
186 enabled: true,
187 },
188 },
189 }),
190 ),
191 Effect.scoped,
192 ),
193);