|
1 | 1 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import { assert, it, vi } from "@effect/vitest"; |
| 4 | +import * as Clock from "effect/Clock"; |
4 | 5 | import * as ConfigProvider from "effect/ConfigProvider"; |
5 | 6 | import * as Effect from "effect/Effect"; |
| 7 | +import * as FileSystem from "effect/FileSystem"; |
6 | 8 | import * as Layer from "effect/Layer"; |
7 | 9 | import * as Schema from "effect/Schema"; |
8 | 10 | import * as TestClock from "effect/testing/TestClock"; |
@@ -140,7 +142,14 @@ it("retryDelayMs doubles from 2s and stays under the 5 minute cap", () => { |
140 | 142 | assert.equal(AnalyticsService.retryDelayMs(30, 0.999_999), 300_000); |
141 | 143 | }); |
142 | 144 |
|
143 | | -it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
| 145 | +const layerTestServices = Layer.effect( |
| 146 | + HostProcess.HomeDirectory, |
| 147 | + Effect.flatMap(FileSystem.FileSystem, (fs) => |
| 148 | + fs.makeTempDirectoryScoped({ prefix: "t3-telemetry-home-" }), |
| 149 | + ), |
| 150 | +).pipe(Layer.provideMerge(NodeServices.layer)); |
| 151 | + |
| 152 | +it.layer(layerTestServices)("AnalyticsService test", (it) => { |
144 | 153 | it.effect("a batch that keeps failing is retried with the same uuids, then dropped", () => |
145 | 154 | Effect.gen(function* () { |
146 | 155 | const failures = { |
@@ -283,46 +292,82 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
283 | 292 | }), |
284 | 293 | ); |
285 | 294 |
|
286 | | - it.effect("does not send batch requests when telemetry is disabled", () => |
287 | | - Effect.gen(function* () { |
288 | | - const capturedPaths: Array<string> = []; |
289 | | - const layerServerConfig = ServerConfig.ServerConfig.layerTest(process.cwd(), { |
290 | | - prefix: "t3-telemetry-disabled-", |
291 | | - }); |
292 | | - const layerTelemetry = AnalyticsService.layer.pipe(Layer.provideMerge(layerServerConfig)); |
293 | | - const layerConfig = ConfigProvider.layer( |
294 | | - ConfigProvider.fromUnknown({ |
295 | | - T3CODE_TELEMETRY_ENABLED: false, |
296 | | - T3CODE_POSTHOG_KEY: "phc_test_key", |
297 | | - T3CODE_POSTHOG_HOST: "http://localhost", |
298 | | - }), |
299 | | - ); |
300 | | - const layerBatchServer = HttpServer.serve( |
301 | | - Effect.gen(function* () { |
302 | | - const request = yield* HttpServerRequest.HttpServerRequest; |
303 | | - capturedPaths.push(request.url); |
304 | | - return HttpServerResponse.jsonUnsafe({}); |
305 | | - }), |
306 | | - ); |
307 | | - const layerRuntime = layerTelemetry.pipe( |
308 | | - Layer.provide(layerConfig), |
309 | | - Layer.provide( |
310 | | - Layer.mergeAll( |
311 | | - Layer.succeed(HostProcess.Platform, "linux"), |
312 | | - Layer.succeed(HostProcess.Architecture, "arm64"), |
| 295 | + it.effect.each([ |
| 296 | + { state: "disabled", enabled: false, identityAvailable: true }, |
| 297 | + { state: "identity is unavailable", enabled: true, identityAvailable: false }, |
| 298 | + { state: "enabled with an identity", enabled: true, identityAvailable: true }, |
| 299 | + ])( |
| 300 | + "background delivery, manual flush and shutdown when $state", |
| 301 | + ({ enabled, identityAvailable }) => |
| 302 | + Effect.gen(function* () { |
| 303 | + const config = yield* ServerConfig.ServerConfig; |
| 304 | + const fs = yield* FileSystem.FileSystem; |
| 305 | + if (!identityAvailable) { |
| 306 | + yield* fs.makeDirectory(config.anonymousIdPath); |
| 307 | + } |
| 308 | + assert.equal((yield* getTelemetryIdentifier) !== null, identityAvailable); |
| 309 | + |
| 310 | + const capturedPaths: Array<string> = []; |
| 311 | + const layerRuntime = AnalyticsService.layer.pipe( |
| 312 | + Layer.provide( |
| 313 | + ConfigProvider.layer( |
| 314 | + ConfigProvider.fromUnknown({ |
| 315 | + T3CODE_TELEMETRY_ENABLED: enabled, |
| 316 | + T3CODE_POSTHOG_KEY: "phc_test_key", |
| 317 | + T3CODE_POSTHOG_HOST: "http://localhost", |
| 318 | + }), |
| 319 | + ), |
313 | 320 | ), |
314 | | - ), |
315 | | - Layer.provideMerge(NodeHttpServer.layerTest), |
316 | | - ); |
| 321 | + Layer.provide( |
| 322 | + Layer.succeed( |
| 323 | + HttpClient.HttpClient, |
| 324 | + HttpClient.make((request) => { |
| 325 | + capturedPaths.push(request.url); |
| 326 | + return respondWithStatus(204)(request); |
| 327 | + }), |
| 328 | + ), |
| 329 | + ), |
| 330 | + ); |
| 331 | + const clock = yield* Clock.Clock; |
| 332 | + let sleepRequests = 0; |
| 333 | + const canSend = enabled && identityAvailable; |
| 334 | + const idleSleepRequests = yield* Effect.gen(function* () { |
| 335 | + const analytics = yield* AnalyticsService.AnalyticsService; |
| 336 | + yield* TestClock.adjust(0); |
| 337 | + const idleSleepRequests = sleepRequests; |
317 | 338 |
|
318 | | - yield* Effect.gen(function* () { |
319 | | - yield* Layer.launch(layerBatchServer).pipe(Effect.forkScoped); |
320 | | - const analytics = yield* AnalyticsService.AnalyticsService; |
321 | | - yield* analytics.record("test.disabled", { index: 1 }); |
322 | | - yield* analytics.flush; |
323 | | - }).pipe(Effect.provide(layerRuntime)); |
| 339 | + yield* analytics.record("test.background", { index: 1 }); |
| 340 | + yield* TestClock.adjust("10 seconds"); |
| 341 | + assert.equal(capturedPaths.length, canSend ? 1 : 0); |
324 | 342 |
|
325 | | - assert.deepEqual(capturedPaths, []); |
326 | | - }), |
| 343 | + yield* analytics.record("test.manual", { index: 2 }); |
| 344 | + yield* analytics.flush; |
| 345 | + assert.equal(capturedPaths.length, canSend ? 2 : 0); |
| 346 | + |
| 347 | + yield* analytics.record("test.shutdown", { index: 3 }); |
| 348 | + return idleSleepRequests; |
| 349 | + }).pipe( |
| 350 | + Effect.provide(layerRuntime), |
| 351 | + Effect.provideService(Clock.Clock, { |
| 352 | + ...clock, |
| 353 | + sleep: (duration) => |
| 354 | + Effect.suspend(() => { |
| 355 | + sleepRequests += 1; |
| 356 | + return clock.sleep(duration); |
| 357 | + }), |
| 358 | + }), |
| 359 | + ); |
| 360 | + |
| 361 | + assert.equal(capturedPaths.length, canSend ? 3 : 0); |
| 362 | + assert.equal( |
| 363 | + idleSleepRequests > 0, |
| 364 | + canSend, |
| 365 | + "only deliverable telemetry schedules idle sleeps", |
| 366 | + ); |
| 367 | + }).pipe( |
| 368 | + Effect.provide( |
| 369 | + ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "t3-telemetry-idle-" }), |
| 370 | + ), |
| 371 | + ), |
327 | 372 | ); |
328 | 373 | }); |
0 commit comments