Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { EnvironmentHttpApi } from "@t3tools/contracts";
3import { describe, expect, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Etag from "effect/http/Etag";
7import * as HttpPlatform from "effect/http/HttpPlatform";
8import * as HttpRouter from "effect/http/HttpRouter";
9import * as HttpApi from "effect/http-api/HttpApi";
10import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
11
12import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
13import * as AuthHttp from "../auth/http.ts";
14import * as CloudLink from "./CloudLink.ts";
15import * as ConnectHttp from "./http.ts";
16
17class ConnectTestApi extends HttpApi.make("environment").add(EnvironmentHttpApi.groups.connect) {}
18
19type HealthFailure = Effect.Error<
20 ReturnType<CloudLink.CloudLink["Service"]["answerHealthRequest"]>
21>;
22
23const answerHealthWith = async (failure: HealthFailure) => {
24 const layerRoutes = HttpApiBuilder.layer(ConnectTestApi).pipe(
25 Layer.provide(ConnectHttp.layer),
26 Layer.provide(
27 Layer.mock(CloudLink.CloudLink)({
28 answerHealthRequest: () => Effect.fail(failure),
29 }),
30 ),
31 Layer.provide(AuthHttp.layerAuthenticatedAuth),
32 Layer.provide(Layer.mock(EnvironmentAuth.EnvironmentAuth)({})),
33 Layer.provideMerge(
34 HttpPlatform.layer.pipe(
35 Layer.provideMerge(NodeServices.layer),
36 Layer.provideMerge(Etag.layerWeak),
37 ),
38 ),
39 );
40 const { handler, dispose } = HttpRouter.toWebHandler(layerRoutes, {
41 disableLogger: true,
42 });
43 try {
44 const response = await handler(
45 new Request("http://127.0.0.1/api/t3-connect/health", {
46 method: "POST",
47 headers: { "content-type": "application/json" },
48 body: JSON.stringify({ proof: "proof" }),
49 }),
50 );
51 return { status: response.status, body: (await response.json()) as unknown };
52 } finally {
53 await dispose();
54 }
55};
56
57describe("connect routes", () => {
58 it.each([
59 {
60 failure: new CloudLink.CloudLinkProofRejectedError({ request: "health" }),
61 status: 401,
62 body: { _tag: "EnvironmentHttpUnauthorizedError", message: "Invalid cloud health request." },
63 },
64 {
65 failure: new CloudLink.CloudLinkProofReplayedError({ request: "health" }),
66 status: 409,
67 body: {
68 _tag: "EnvironmentHttpConflictError",
69 message: "Cloud health request was already consumed.",
70 },
71 },
72 {
73 failure: new CloudLink.CloudLinkInternalError({
74 operation: "answer-health",
75 cause: new Error("disk full"),
76 }),
77 status: 500,
78 body: {
79 _tag: "EnvironmentHttpInternalServerError",
80 message: "Could not answer cloud health request.",
81 },
82 },
83 {
84 failure: new EnvironmentAuth.ServerAuthCloudMintPublicKeyMissingError({}),
85 status: 500,
86 body: {
87 _tag: "EnvironmentHttpInternalServerError",
88 message: "Cloud mint public key is not installed for this environment.",
89 },
90 },
91 ])("answers $failure._tag with HTTP $status", async ({ failure, status, body }) => {
92 const response = await answerHealthWith(failure);
93 expect(response).toEqual({ status, body });
94 });
95});