Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AnalyticsService.test.ts

119 lines4.2 KB

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