Open History to see the last commit
1import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import * as ConfigProvider from "effect/ConfigProvider";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as HttpServer from "effect/unstable/http/HttpServer";
8import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
9import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
10
11import { ServerConfig } from "../../config.ts";
12import { getTelemetryIdentifier } from "../Identify.ts";
13import { AnalyticsService } from "../Services/AnalyticsService.ts";
14import { AnalyticsServiceLayerLive } from "./AnalyticsService.ts";
15
16interface RecordedBatchRequest {
17 readonly path: string;
18 readonly body: {
19 readonly batch?: ReadonlyArray<{
20 readonly event?: string;
21 readonly properties?: {
22 readonly index?: number;
23 readonly clientType?: string;
24 };
25 }>;
26 } | null;
27}
28
29interface RecordedBatchBody {
30 readonly batch: ReadonlyArray<{
31 readonly event?: string;
32 readonly properties?: {
33 readonly index?: number;
34 readonly clientType?: string;
35 };
36 }>;
37}
38
39it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
40 it.effect("flush drains all buffered events across multiple batches", () =>
41 Effect.gen(function* () {
42 const capturedRequests: Array<RecordedBatchRequest> = [];
43 const serverConfigLayer = ServerConfig.layerTest(process.cwd(), {
44 prefix: "t3-telemetry-base-",
45 });
46
47 const telemetryLayer = AnalyticsServiceLayerLive.pipe(Layer.provideMerge(serverConfigLayer));
48 const configLayer = ConfigProvider.layer(
49 ConfigProvider.fromUnknown({
50 T3CODE_TELEMETRY_ENABLED: true,
51 T3CODE_POSTHOG_KEY: "phc_test_key",
52 T3CODE_POSTHOG_HOST: "",
53 T3CODE_TELEMETRY_FLUSH_BATCH_SIZE: 20,
54 }),
55 );
56 const batchServerLayer = HttpServer.serve(
57 Effect.gen(function* () {
58 const request = yield* HttpServerRequest.HttpServerRequest;
59 if (request.method !== "POST") {
60 return HttpServerResponse.empty({ status: 404 });
61 }
62
63 const payload = yield* request.json.pipe(
64 Effect.map((body) => body as RecordedBatchRequest["body"]),
65 Effect.catch(() => Effect.succeed(null)),
66 );
67
68 capturedRequests.push({ path: request.url, body: payload });
69
70 return HttpServerResponse.jsonUnsafe({});
71 }),
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));
90
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 );
106
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 assert.equal(
114 batchRequests.every((request) =>
115 request.body.batch.every((event) => event.properties?.clientType === "cli-web-client"),
116 ),
117 true,
118 );
119 }),
120 );
121});