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

threadSnapshotHttp.test.ts

82 lines2.6 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6​
7import { PrimaryConnectionTarget, type PreparedConnection } from "../connection/model.ts";
8import { remoteHttpClientLayer } from "../rpc/http.ts";
9import { ThreadSnapshotLoader, threadSnapshotLoaderLayer } from "./threadSnapshotHttp.ts";
10​
11const TARGET = new PrimaryConnectionTarget({
12 environmentId: EnvironmentId.make("environment-1"),
13 label: "Test environment",
14 httpBaseUrl: "https://environment.example.test",
15 wsBaseUrl: "wss://environment.example.test",
16});
17const PREPARED: PreparedConnection = {
18 environmentId: TARGET.environmentId,
19 label: TARGET.label,
20 httpBaseUrl: TARGET.httpBaseUrl,
21 socketUrl: TARGET.wsBaseUrl,
22 httpAuthorization: null,
23 target: TARGET,
24};
25const THREAD_ID = ThreadId.make("thread-1");
26​
27function provideLoader(fetchFn: typeof fetch) {
28 return Effect.provide(
29 threadSnapshotLoaderLayer.pipe(Layer.provide(remoteHttpClientLayer(fetchFn))),
30 );
32​
33describe("ThreadSnapshotLoader", () => {
34 it.effect("preserves the decoded thread_not_found response", () => {
35 const fetchFn = (() =>
36 Promise.resolve(
37 Response.json(
38 {
39 _tag: "EnvironmentResourceNotFoundError",
40 code: "not_found",
41 reason: "thread_not_found",
42 traceId: "trace-thread-not-found",
43 },
44 { status: 404 },
45 ),
46 )) satisfies typeof fetch;
47​
48 return Effect.gen(function* () {
49 const loader = yield* ThreadSnapshotLoader;
50 const error = yield* Effect.flip(loader.load(PREPARED, THREAD_ID));
51​
52 expect(error).toMatchObject({
53 _tag: "EnvironmentResourceNotFoundError",
54 code: "not_found",
55 reason: "thread_not_found",
56 traceId: "trace-thread-not-found",
57 });
58 }).pipe(provideLoader(fetchFn));
59 });
60​
61 it.effect("maps a transient HTTP failure to the socket fallback", () => {
62 const fetchFn = (() =>
63 Promise.resolve(
64 Response.json(
65 {
66 _tag: "EnvironmentInternalError",
67 code: "internal_error",
68 reason: "orchestration_thread_snapshot_failed",
69 traceId: "trace-thread-snapshot-failed",
70 },
71 { status: 500 },
72 ),
73 )) satisfies typeof fetch;
74​
75 return Effect.gen(function* () {
76 const loader = yield* ThreadSnapshotLoader;
77 const snapshot = yield* loader.load(PREPARED, THREAD_ID);
78​
79 expect(Option.isNone(snapshot)).toBe(true);
80 }).pipe(provideLoader(fetchFn));
81 });
82});