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

tracing.test.ts

97 lines3.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 * as Tracing from "./tracing";
11​
12vi.mock("expo-constants", () => ({
13 default: {
14 expoConfig: {
15 extra: {},
16 },
17 },
18}));
19​
20it.effect("exports spans through the scoped mobile OTLP layer", () => {
21 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
22 const layerTracing = 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 serviceVersion: "1.2.3",
31 },
32 ).pipe(Layer.provide(layerRemoteHttpClient(fetchFn)));
33 const layerTracedApplication = Layer.effectDiscard(
34 Effect.void.pipe(Effect.withSpan("mobile.test.span"), withRelayClientTracing),
35 ).pipe(Layer.provide(layerTracing));
36​
37 return Effect.gen(function* () {
38 yield* Layer.build(layerTracedApplication);
39​
40 expect(fetchFn).not.toHaveBeenCalled();
41 }).pipe(
42 Effect.scoped,
43 Effect.andThen(
44 Effect.sync(() => {
45 expect(fetchFn).toHaveBeenCalledOnce();
46 const [url, init] = fetchFn.mock.calls[0]!;
47 expect(String(url)).toBe("https://api.axiom.test/v1/traces");
48 expect(new Headers(init?.headers).get("authorization")).toBe("Bearer public-ingest-token");
49 expect(new Headers(init?.headers).get("x-axiom-dataset")).toBe("mobile-traces");
50 expect(new TextDecoder().decode(init?.body as Uint8Array)).toContain("mobile.test.span");
51 }),
52 ),
53 );
54});
55​
56it.effect("does not let OTLP serialization failures alter application effects", () => {
57 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
58 const layerTracing = Tracing.layerFromConfig(
59 {
60 tracesUrl: "https://api.axiom.test/v1/traces",
61 tracesDataset: "mobile-traces",
62 tracesToken: "public-ingest-token",
63 },
64 {
65 appVariant: "test",
66 serviceVersion: "1.2.3",
67 },
68 ).pipe(Layer.provide(layerRemoteHttpClient(fetchFn)));
69 const failure = { durationNanos: 1n };
70 const layerTracedApplication = Layer.effectDiscard(
71 Effect.fail(failure).pipe(
72 Effect.withSpan("mobile.test.failed-span"),
73 withRelayClientTracing,
74 Effect.exit,
75 Effect.flatMap((exit) => {
76 const reason = exit._tag === "Failure" ? exit.cause.reasons[0] : undefined;
77 return reason && Cause.isFailReason(reason)
78 ? Effect.sync(() => {
79 expect(reason.error).toBe(failure);
80 })
81 : Effect.die(new Error("Expected the original typed failure."));
82 }),
83 ),
84 ).pipe(Layer.provide(layerTracing));
85​
86 return Layer.build(layerTracedApplication).pipe(
87 Effect.scoped,
88 Effect.andThen(
89 Effect.sync(() => {
90 expect(fetchFn).toHaveBeenCalledOnce();
91 expect(new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array)).toContain(
92 "mobile.test.failed-span",
93 );
94 }),
95 ),
96 );
97});