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

tracing.test.ts

61 lines1.9 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("does not let OTLP serialization failures alter application effects", () => {
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 failure = { durationNanos: 1n };
34 const layerTracedApplication = Layer.effectDiscard(
35 Effect.fail(failure).pipe(
36 Effect.withSpan("mobile.test.failed-span"),
37 withRelayClientTracing,
38 Effect.exit,
39 Effect.flatMap((exit) => {
40 const reason = exit._tag === "Failure" ? exit.cause.reasons[0] : undefined;
41 return reason && Cause.isFailReason(reason)
42 ? Effect.sync(() => {
43 expect(reason.error).toBe(failure);
44 })
45 : Effect.die(new Error("Expected the original typed failure."));
46 }),
47 ),
48 ).pipe(Layer.provide(layerTracing));
49​
50 return Layer.build(layerTracedApplication).pipe(
51 Effect.scoped,
52 Effect.andThen(
53 Effect.sync(() => {
54 expect(fetchFn).toHaveBeenCalledOnce();
55 expect(new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array)).toContain(
56 "mobile.test.failed-span",
57 );
58 }),
59 ),
60 );
61});