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

tracing.test.ts

68 lines2.2 KB

1import { expect, it } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import { vi } from "vite-plus/test";
6​
7import { layerRemoteHttpClient } from "@t3tools/client-runtime/rpc";
8import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
9​
10import { setConnectionTracesEnabled } from "./diagnosticsSetting";
11import * as Tracing from "./tracing";
12​
13vi.mock("expo-constants", () => ({
14 default: {
15 expoConfig: {
16 extra: {},
17 },
18 },
19}));
20​
21const layerTracingTo = (fetchFn: typeof fetch) =>
22 Tracing.layerFromConfig(
23 {
24 tracesUrl: "https://api.axiom.test/v1/traces",
25 tracesDataset: "mobile-traces",
26 tracesToken: "public-ingest-token",
27 },
28 {
29 appVariant: "test",
30 serviceInstanceId: "test-instance",
31 serviceVersion: "1.2.3",
32 },
33 ).pipe(Layer.provide(layerRemoteHttpClient(fetchFn)));
34​
35it.effect.each([false, true])("gates relay sends and preserves typed failures (%s)", (enabled) => {
36 setConnectionTracesEnabled(enabled);
37 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
38 const layerTracing = layerTracingTo(fetchFn);
39 const failure = { durationNanos: 1n };
40 const layerTracedApplication = Layer.effectDiscard(
41 Effect.fail(failure).pipe(
42 Effect.withSpan("mobile.test.failed-span"),
43 withRelayClientTracing,
44 Effect.exit,
45 Effect.flatMap((exit) => {
46 const reason = exit._tag === "Failure" ? exit.cause.reasons[0] : undefined;
47 return reason && Cause.isFailReason(reason)
48 ? Effect.sync(() => {
49 expect(reason.error).toBe(failure);
50 })
51 : Effect.die(new Error("Expected the original typed failure."));
52 }),
53 ),
54 ).pipe(Layer.provide(layerTracing));
55​
56 return Layer.build(layerTracedApplication).pipe(
57 Effect.scoped,
58 Effect.andThen(
59 Effect.sync(() => {
60 expect(fetchFn).toHaveBeenCalledTimes(Number(enabled));
61 if (!enabled) return;
62 expect(new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array)).toContain(
63 "mobile.test.failed-span",
64 );
65 }),
66 ),
67 );
68});