|
1 | 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
2 | 2 | import { expect, it } from "@effect/vitest"; |
3 | | -import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"; |
| 3 | +import { |
| 4 | + ServerConfigStreamEvent, |
| 5 | + type ServerSettings as ServerSettingsValue, |
| 6 | + UsageLimitSourceId, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import * as Duration from "effect/Duration"; |
4 | 9 | import * as Effect from "effect/Effect"; |
5 | 10 | import * as Layer from "effect/Layer"; |
| 11 | +import * as PubSub from "effect/PubSub"; |
| 12 | +import * as Ref from "effect/Ref"; |
6 | 13 | import * as Schema from "effect/Schema"; |
7 | | -import { HttpClient } from "effect/http"; |
| 14 | +import * as Stream from "effect/Stream"; |
| 15 | +import * as TestClock from "effect/testing/TestClock"; |
| 16 | +import { HttpClient, HttpClientResponse } from "effect/http"; |
8 | 17 |
|
9 | 18 | import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
10 | 19 | import * as ServerSettings from "../serverSettings.ts"; |
@@ -83,3 +92,102 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () => |
83 | 92 | Effect.scoped, |
84 | 93 | ), |
85 | 94 | ); |
| 95 | + |
| 96 | +it.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 | +); |
0 commit comments