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";
10import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
11
12import * as ServerConfig from "../config.ts";
13import { getTelemetryIdentifier } from "./Identify.ts";
14import * as AnalyticsService 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 readonly serverOs?: string;
25 readonly serverArch?: string;
26 readonly serverAppVersion?: string;
27 readonly serverMode?: string;
28 readonly t3CodeVersion?: string;
29 };
30 }>;
31 } | null;
32}
33
34interface RecordedBatchBody {
35 readonly batch: ReadonlyArray<{
36 readonly event?: string;
37 readonly properties?: {
38 readonly index?: number;
39 readonly clientType?: string;
40 readonly serverOs?: string;
41 readonly serverArch?: string;
42 readonly serverAppVersion?: string;
43 readonly serverMode?: string;
44 readonly t3CodeVersion?: string;
45 };
46 }>;
47}
48
49it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
50 it.effect("flush drains all buffered events across multiple batches", () =>
51 Effect.gen(function* () {
52 const capturedRequests: Array<RecordedBatchRequest> = [];
53 const serverConfigLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
54 prefix: "t3-telemetry-base-",
55 });
56
57 const telemetryLayer = AnalyticsService.layer.pipe(Layer.provideMerge(serverConfigLayer));
58 const configLayer = ConfigProvider.layer(
59 ConfigProvider.fromUnknown({
60 T3CODE_TELEMETRY_ENABLED: true,
61 T3CODE_POSTHOG_KEY: "phc_test_key",
62 T3CODE_POSTHOG_HOST: "http://localhost",
63 T3CODE_TELEMETRY_FLUSH_BATCH_SIZE: 20,
64 }),
65 );
66 const batchServerLayer = HttpServer.serve(
67 Effect.gen(function* () {
68 const request = yield* HttpServerRequest.HttpServerRequest;
69 if (request.method !== "POST") {
70 return HttpServerResponse.empty({ status: 404 });
71 }
72
73 const payload = yield* request.json.pipe(
74 Effect.map((body) => body as RecordedBatchRequest["body"]),
75 Effect.orElseSucceed(() => null),
76 );
77
78 capturedRequests.push({ path: request.url, body: payload });
79
80 return HttpServerResponse.jsonUnsafe({});
81 }),
82 );
83 const runtimeLayer = telemetryLayer.pipe(
84 Layer.provide(configLayer),
85 Layer.provide(
86 Layer.mergeAll(
87 Layer.succeed(HostProcessPlatform, "linux"),
88 Layer.succeed(HostProcessArchitecture, "arm64"),
89 ),
90 ),
91 Layer.provideMerge(NodeHttpServer.layerTest),
92 );
93
94 yield* Effect.gen(function* () {
95 yield* Layer.launch(batchServerLayer).pipe(Effect.forkScoped);
96 const telemetryIdentifier = yield* getTelemetryIdentifier;
97 assert.equal(telemetryIdentifier !== null, true);
98 const analytics = yield* AnalyticsService.AnalyticsService;
99
100 for (let index = 0; index < 45; index += 1) {
101 yield* analytics.record("test.flush.drain", { index });
102 }
103
104 yield* analytics.flush;
105 }).pipe(Effect.provide(runtimeLayer));
106
107 const batchRequests = capturedRequests.filter(
108 (request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } =>
109 Array.isArray(request.body?.batch),
110 );
111 assert.equal(batchRequests.length, 3);
112 assert.equal(
113 batchRequests.every(
114 (request) => request.path.endsWith("/batch/") || request.path.endsWith("/batch"),
115 ),
116 true,
117 );
118 const deliveredIndexes = batchRequests.flatMap((request) =>
119 request.body.batch
120 .filter((event) => event.event === "test.flush.drain")
121 .map((event) => event.properties?.index)
122 .filter((index): index is number => typeof index === "number"),
123 );
124
125 const sorted = deliveredIndexes.toSorted((a, b) => a - b);
126 assert.equal(sorted.length, 45);
127 assert.deepEqual(
128 sorted,
129 Array.from({ length: 45 }, (_, index) => index),
130 );
131 assert.equal(
132 batchRequests.every((request) =>
133 request.body.batch.every((event) => event.properties?.clientType === "cli-web-client"),
134 ),
135 true,
136 );
137 assert.equal(
138 batchRequests.every((request) =>
139 request.body.batch.every(
140 (event) =>
141 event.properties?.serverOs === "Linux" &&
142 event.properties.serverArch === "arm64" &&
143 event.properties.serverAppVersion === event.properties.t3CodeVersion &&
144 event.properties.serverMode === "web",
145 ),
146 ),
147 true,
148 );
149 }),
150 );
151});