| ... | ... | @@ -1,10 +1,7 @@ |
| 1 | | import fs from "node:fs"; |
| 2 | | import os from "node:os"; |
| 3 | | import path from "node:path"; |
| 4 | | |
| 5 | 1 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | 3 | import { assert, it } from "@effect/vitest"; |
| 7 | | import { ConfigProvider, Effect, Layer } from "effect"; |
| 4 | import { ConfigProvider, Effect, FileSystem, Layer } from "effect"; |
| 8 | 5 | import * as HttpServer from "effect/unstable/http/HttpServer"; |
| 9 | 6 | import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest"; |
| 10 | 7 | import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"; |
| ... | ... | @@ -16,16 +13,14 @@ import { AnalyticsServiceLayerLive } from "./AnalyticsService.ts"; |
| 16 | 13 | |
| 17 | 14 | interface RecordedBatchRequest { |
| 18 | 15 | readonly path: string; |
| 19 | | readonly body: |
| 20 | | | { |
| 21 | | readonly batch?: ReadonlyArray<{ |
| 22 | | readonly event?: string; |
| 23 | | readonly properties?: { |
| 24 | | readonly index?: number; |
| 25 | | }; |
| 26 | | }>; |
| 27 | | } |
| 28 | | | null; |
| 16 | readonly body: { |
| 17 | readonly batch?: ReadonlyArray<{ |
| 18 | readonly event?: string; |
| 19 | readonly properties?: { |
| 20 | readonly index?: number; |
| 21 | }; |
| 22 | }>; |
| 23 | } | null; |
| 29 | 24 | } |
| 30 | 25 | |
| 31 | 26 | 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", () => |
| 41 | | Effect.gen(function* () { |
| 42 | | const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-telemetry-flush-")); |
| 43 | | const capturedRequests: Array<RecordedBatchRequest> = []; |
| 44 | | const telemetryLayer = AnalyticsServiceLayerLive.pipe( |
| 45 | | Layer.provideMerge(ServerConfig.layerTest(process.cwd(), tempDir)), |
| 46 | | ); |
| 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 | | }); |
| 35 | it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
| 36 | it.effect("flush drains all buffered events across multiple batches", () => |
| 37 | Effect.gen(function* () { |
| 38 | const fileSystem = yield* FileSystem.FileSystem; |
| 39 | const stateDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 40 | prefix: "t3-telemetry-flush-", |
| 41 | }); |
| 69 | 42 | |
| 70 | | return HttpServerResponse.jsonUnsafe({}); |
| 71 | | }), |
| 72 | | ); |
| 73 | | const runtimeLayer = telemetryLayer.pipe( |
| 74 | | Layer.provide(configLayer), |
| 75 | | Layer.provideMerge(NodeHttpServer.layerTest), |
| 76 | | ); |
| 43 | const capturedRequests: Array<RecordedBatchRequest> = []; |
| 44 | const serverConfigLayer = ServerConfig.layerTest(process.cwd(), stateDir); |
| 77 | 45 | |
| 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; |
| 46 | const telemetryLayer = AnalyticsServiceLayerLive.pipe(Layer.provideMerge(serverConfigLayer)); |
| 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 | } |
| 83 | 61 | |
| 84 | | for (let index = 0; index < 45; index += 1) { |
| 85 | | yield* analytics.record("test.flush.drain", { index }); |
| 86 | | } |
| 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 | }); |
| 87 | 69 | |
| 88 | | yield* analytics.flush; |
| 89 | | }).pipe( |
| 90 | | Effect.provide(runtimeLayer), |
| 91 | | Effect.ensuring( |
| 92 | | Effect.sync(() => { |
| 93 | | fs.rmSync(tempDir, { recursive: true, force: true }); |
| 70 | return HttpServerResponse.jsonUnsafe({}); |
| 94 | 71 | }), |
| 95 | | ), |
| 96 | | ); |
| 72 | ); |
| 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( |
| 99 | | (request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } => |
| 100 | | Array.isArray(request.body?.batch), |
| 101 | | ); |
| 102 | | assert.equal(batchRequests.length, 3); |
| 103 | | assert.equal( |
| 104 | | batchRequests.every((request) => request.path === "/batch/" || request.path === "/batch"), |
| 105 | | true, |
| 106 | | ); |
| 107 | | const deliveredIndexes = batchRequests.flatMap((request) => |
| 108 | | request.body.batch |
| 109 | | .filter((event) => event.event === "test.flush.drain") |
| 110 | | .map((event) => event.properties?.index) |
| 111 | | .filter((index): index is number => typeof index === "number"), |
| 112 | | ); |
| 91 | const batchRequests = capturedRequests.filter( |
| 92 | (request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } => |
| 93 | Array.isArray(request.body?.batch), |
| 94 | ); |
| 95 | assert.equal(batchRequests.length, 3); |
| 96 | assert.equal( |
| 97 | batchRequests.every((request) => request.path === "/batch/" || request.path === "/batch"), |
| 98 | true, |
| 99 | ); |
| 100 | const deliveredIndexes = batchRequests.flatMap((request) => |
| 101 | request.body.batch |
| 102 | .filter((event) => event.event === "test.flush.drain") |
| 103 | .map((event) => event.properties?.index) |
| 104 | .filter((index): index is number => typeof index === "number"), |
| 105 | ); |
| 113 | 106 | |
| 114 | | const sorted = deliveredIndexes.toSorted((a, b) => a - b); |
| 115 | | assert.equal(sorted.length, 45); |
| 116 | | assert.deepEqual( |
| 117 | | sorted, |
| 118 | | Array.from({ length: 45 }, (_, index) => index), |
| 119 | | ); |
| 120 | | }), |
| 121 | | ); |
| 107 | const sorted = deliveredIndexes.toSorted((a, b) => a - b); |
| 108 | assert.equal(sorted.length, 45); |
| 109 | assert.deepEqual( |
| 110 | sorted, |
| 111 | Array.from({ length: 45 }, (_, index) => index), |
| 112 | ); |
| 113 | }), |
| 114 | ); |
| 115 | }); |