| ... | @@ -1,10 +1,7 @@ | ... | @@ -1,10 +1,7 @@ |
| 1 | import fs from "node:fs"; | | |
| 2 | import os from "node:os"; | | |
| 3 | import path from "node:path"; | | |
| 4 | | | |
| 5 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; | 1 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
| | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | import { assert, it } from "@effect/vitest"; | 3 | import { assert, it } from "@effect/vitest"; |
| 7 | import { ConfigProvider, Effect, Layer } from "effect"; | 4 | import { ConfigProvider, Effect, FileSystem, Layer } from "effect"; |
| 8 | import * as HttpServer from "effect/unstable/http/HttpServer"; | 5 | import * as HttpServer from "effect/unstable/http/HttpServer"; |
| 9 | import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest"; | 6 | import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest"; |
| 10 | import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"; | 7 | import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"; |
| ... | @@ -16,16 +13,14 @@ import { AnalyticsServiceLayerLive } from "./AnalyticsService.ts"; | ... | @@ -16,16 +13,14 @@ import { AnalyticsServiceLayerLive } from "./AnalyticsService.ts"; |
| 16 | | 13 | |
| 17 | interface RecordedBatchRequest { | 14 | interface RecordedBatchRequest { |
| 18 | readonly path: string; | 15 | readonly path: string; |
| 19 | readonly body: | 16 | readonly body: { |
| 20 | | { | 17 | readonly batch?: ReadonlyArray<{ |
| 21 | readonly batch?: ReadonlyArray<{ | 18 | readonly event?: string; |
| 22 | readonly event?: string; | 19 | readonly properties?: { |
| 23 | readonly properties?: { | 20 | readonly index?: number; |
| 24 | readonly index?: number; | 21 | }; |
| 25 | }; | 22 | }>; |
| 26 | }>; | 23 | } | null; |
| 27 | } | | |
| 28 | | null; | | |
| 29 | } | 24 | } |
| 30 | | 25 | |
| 31 | interface RecordedBatchBody { | 26 | interface RecordedBatchBody { |
| ... | @@ -37,85 +32,84 @@ interface RecordedBatchBody { | ... | @@ -37,85 +32,84 @@ interface RecordedBatchBody { |
| 37 | }>; | 32 | }>; |
| 38 | } | 33 | } |
| 39 | | 34 | |
| 40 | it.effect("flush drains all buffered events across multiple batches", () => | 35 | it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
| 41 | Effect.gen(function* () { | 36 | it.effect("flush drains all buffered events across multiple batches", () => |
| 42 | const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-telemetry-flush-")); | 37 | Effect.gen(function* () { |
| 43 | const capturedRequests: Array<RecordedBatchRequest> = []; | 38 | const fileSystem = yield* FileSystem.FileSystem; |
| 44 | const telemetryLayer = AnalyticsServiceLayerLive.pipe( | 39 | const stateDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 45 | Layer.provideMerge(ServerConfig.layerTest(process.cwd(), tempDir)), | 40 | prefix: "t3-telemetry-flush-", |
| 46 | ); | 41 | }); |
| 47 | const configLayer = ConfigProvider.layer( | | |
| 48 | ConfigProvider.fromUnknown({ | | |
| 49 | T3CODE_TELEMETRY_ENABLED: true, | | |
| 50 | T3CODE_POSTHOG_KEY: "phc_test_key", | | |
| 51 | T3CODE_POSTHOG_HOST: "", | | |
| 52 | T3CODE_TELEMETRY_FLUSH_BATCH_SIZE: 20, | | |
| 53 | }), | | |
| 54 | ); | | |
| 55 | const batchServerLayer = HttpServer.serve( | | |
| 56 | Effect.gen(function* () { | | |
| 57 | const request = yield* HttpServerRequest.HttpServerRequest; | | |
| 58 | if (request.method !== "POST") { | | |
| 59 | return HttpServerResponse.empty({ status: 404 }); | | |
| 60 | } | | |
| 61 | | | |
| 62 | const payload = yield* request.json.pipe( | | |
| 63 | Effect.map((body) => body as RecordedBatchRequest["body"]), | | |
| 64 | Effect.catch(() => Effect.succeed(null)), | | |
| 65 | ); | | |
| 66 | yield* Effect.sync(() => { | | |
| 67 | capturedRequests.push({ path: request.url, body: payload }); | | |
| 68 | }); | | |
| 69 | | 42 | |
| 70 | return HttpServerResponse.jsonUnsafe({}); | 43 | const capturedRequests: Array<RecordedBatchRequest> = []; |
| 71 | }), | 44 | const serverConfigLayer = ServerConfig.layerTest(process.cwd(), stateDir); |
| 72 | ); | | |
| 73 | const runtimeLayer = telemetryLayer.pipe( | | |
| 74 | Layer.provide(configLayer), | | |
| 75 | Layer.provideMerge(NodeHttpServer.layerTest), | | |
| 76 | ); | | |
| 77 | | 45 | |
| 78 | yield* Effect.gen(function* () { | 46 | const telemetryLayer = AnalyticsServiceLayerLive.pipe(Layer.provideMerge(serverConfigLayer)); |
| 79 | yield* Layer.launch(batchServerLayer).pipe(Effect.forkScoped); | 47 | const configLayer = ConfigProvider.layer( |
| 80 | const telemetryIdentifier = yield* getTelemetryIdentifier; | 48 | ConfigProvider.fromUnknown({ |
| 81 | assert.equal(telemetryIdentifier !== null, true); | 49 | T3CODE_TELEMETRY_ENABLED: true, |
| 82 | const analytics = yield* AnalyticsService; | 50 | T3CODE_POSTHOG_KEY: "phc_test_key", |
| | 51 | T3CODE_POSTHOG_HOST: "", |
| | 52 | T3CODE_TELEMETRY_FLUSH_BATCH_SIZE: 20, |
| | 53 | }), |
| | 54 | ); |
| | 55 | const batchServerLayer = HttpServer.serve( |
| | 56 | Effect.gen(function* () { |
| | 57 | const request = yield* HttpServerRequest.HttpServerRequest; |
| | 58 | if (request.method !== "POST") { |
| | 59 | return HttpServerResponse.empty({ status: 404 }); |
| | 60 | } |
| 83 | | 61 | |
| 84 | for (let index = 0; index < 45; index += 1) { | 62 | const payload = yield* request.json.pipe( |
| 85 | yield* analytics.record("test.flush.drain", { index }); | 63 | Effect.map((body) => body as RecordedBatchRequest["body"]), |
| 86 | } | 64 | Effect.catch(() => Effect.succeed(null)), |
| | 65 | ); |
| | 66 | yield* Effect.sync(() => { |
| | 67 | capturedRequests.push({ path: request.url, body: payload }); |
| | 68 | }); |
| 87 | | 69 | |
| 88 | yield* analytics.flush; | 70 | return HttpServerResponse.jsonUnsafe({}); |
| 89 | }).pipe( | | |
| 90 | Effect.provide(runtimeLayer), | | |
| 91 | Effect.ensuring( | | |
| 92 | Effect.sync(() => { | | |
| 93 | fs.rmSync(tempDir, { recursive: true, force: true }); | | |
| 94 | }), | 71 | }), |
| 95 | ), | 72 | ); |
| 96 | ); | 73 | const runtimeLayer = telemetryLayer.pipe( |
| | 74 | Layer.provide(configLayer), |
| | 75 | Layer.provideMerge(NodeHttpServer.layerTest), |
| | 76 | ); |
| | 77 | |
| | 78 | yield* Effect.gen(function* () { |
| | 79 | yield* Layer.launch(batchServerLayer).pipe(Effect.forkScoped); |
| | 80 | const telemetryIdentifier = yield* getTelemetryIdentifier; |
| | 81 | assert.equal(telemetryIdentifier !== null, true); |
| | 82 | const analytics = yield* AnalyticsService; |
| | 83 | |
| | 84 | for (let index = 0; index < 45; index += 1) { |
| | 85 | yield* analytics.record("test.flush.drain", { index }); |
| | 86 | } |
| | 87 | |
| | 88 | yield* analytics.flush; |
| | 89 | }).pipe(Effect.provide(runtimeLayer)); |
| 97 | | 90 | |
| 98 | const batchRequests = capturedRequests.filter( | 91 | const batchRequests = capturedRequests.filter( |
| 99 | (request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } => | 92 | (request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } => |
| 100 | Array.isArray(request.body?.batch), | 93 | Array.isArray(request.body?.batch), |
| 101 | ); | 94 | ); |
| 102 | assert.equal(batchRequests.length, 3); | 95 | assert.equal(batchRequests.length, 3); |
| 103 | assert.equal( | 96 | assert.equal( |
| 104 | batchRequests.every((request) => request.path === "/batch/" || request.path === "/batch"), | 97 | batchRequests.every((request) => request.path === "/batch/" || request.path === "/batch"), |
| 105 | true, | 98 | true, |
| 106 | ); | 99 | ); |
| 107 | const deliveredIndexes = batchRequests.flatMap((request) => | 100 | const deliveredIndexes = batchRequests.flatMap((request) => |
| 108 | request.body.batch | 101 | request.body.batch |
| 109 | .filter((event) => event.event === "test.flush.drain") | 102 | .filter((event) => event.event === "test.flush.drain") |
| 110 | .map((event) => event.properties?.index) | 103 | .map((event) => event.properties?.index) |
| 111 | .filter((index): index is number => typeof index === "number"), | 104 | .filter((index): index is number => typeof index === "number"), |
| 112 | ); | 105 | ); |
| 113 | | 106 | |
| 114 | const sorted = deliveredIndexes.toSorted((a, b) => a - b); | 107 | const sorted = deliveredIndexes.toSorted((a, b) => a - b); |
| 115 | assert.equal(sorted.length, 45); | 108 | assert.equal(sorted.length, 45); |
| 116 | assert.deepEqual( | 109 | assert.deepEqual( |
| 117 | sorted, | 110 | sorted, |
| 118 | Array.from({ length: 45 }, (_, index) => index), | 111 | Array.from({ length: 45 }, (_, index) => index), |
| 119 | ); | 112 | ); |
| 120 | }), | 113 | }), |
| 121 | ); | 114 | ); |
| | 115 | }); |