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

http.test.ts

119 lines4.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import * as PlatformError from "effect/PlatformError";
6import { HttpClient } from "effect/unstable/http";
7​
8import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
9import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
10import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts";
11import * as CliTokenManager from "./CliTokenManager.ts";
12import { consumeCloudReplayGuards, reconcileDesiredCloudLink } from "./http.ts";
13import {
14 CloudManagedEndpointRuntime,
15 type CloudManagedEndpointRuntimeShape,
16} from "./ManagedEndpointRuntime.ts";
17​
18const storeFailure = (tag: "AlreadyExists" | "PermissionDenied") =>
19 new ServerSecretStore.SecretStoreError({
20 message: "Failed to persist cloud replay guard.",
21 cause: PlatformError.systemError({
22 _tag: tag,
23 module: "FileSystem",
24 method: "open",
25 pathOrDescriptor: "cloud-replay-guard.bin",
26 }),
27 });
28​
29const unusedSecretStoreOperation = () => Effect.die("unused secret-store operation");
30​
31function makeSecretStore(
32 create: ServerSecretStore.ServerSecretStoreShape["create"],
33): ServerSecretStore.ServerSecretStoreShape {
34 return {
35 get: unusedSecretStoreOperation,
36 set: unusedSecretStoreOperation,
37 create,
38 getOrCreateRandom: unusedSecretStoreOperation,
39 remove: unusedSecretStoreOperation,
40 };
42​
43describe("consumeCloudReplayGuards", () => {
44 it.effect("reports already-created guards as replay conflicts", () =>
45 Effect.gen(function* () {
46 const consumed = yield* consumeCloudReplayGuards({
47 secrets: makeSecretStore(() => Effect.fail(storeFailure("AlreadyExists"))),
48 names: ["cloud-jti", "cloud-nonce"],
49 value: new Uint8Array(),
50 });
51​
52 expect(consumed).toBe(false);
53 }),
54 );
55​
56 it.effect("preserves replay-store availability failures", () =>
57 Effect.gen(function* () {
58 const failure = storeFailure("PermissionDenied");
59 const error = yield* Effect.flip(
60 consumeCloudReplayGuards({
61 secrets: makeSecretStore(() => Effect.fail(failure)),
62 names: ["cloud-jti", "cloud-nonce"],
63 value: new Uint8Array(),
64 }),
65 );
66​
67 expect(error).toBe(failure);
68 }),
69 );
70});
71​
72describe("reconcileDesiredCloudLink", () => {
73 it.effect("requires stored CLI authorization without exposing an HTTP endpoint", () =>
74 Effect.gen(function* () {
75 const error = yield* Effect.flip(reconcileDesiredCloudLink("http://127.0.0.1:3774"));
76​
77 expect(error).toMatchObject({
78 _tag: "EnvironmentHttpUnauthorizedError",
79 message: "Run `t3 connect link` to authorize this environment.",
80 });
81 }).pipe(
82 Effect.provideService(
83 ServerSecretStore.ServerSecretStore,
84 makeSecretStore(unusedSecretStoreOperation),
85 ),
86 Effect.provideService(
87 ServerEnvironment,
88 ServerEnvironment.of({
89 getEnvironmentId: unusedSecretStoreOperation(),
90 getDescriptor: unusedSecretStoreOperation(),
91 }),
92 ),
93 Effect.provideService(
94 CloudManagedEndpointRuntime,
95 CloudManagedEndpointRuntime.of({
96 applyConfig: unusedSecretStoreOperation,
97 } satisfies CloudManagedEndpointRuntimeShape),
98 ),
99 Effect.provideService(
100 EnvironmentAuth.EnvironmentAuth,
101 EnvironmentAuth.EnvironmentAuth.of({} as EnvironmentAuth.EnvironmentAuthShape),
102 ),
103 Effect.provideService(
104 CliTokenManager.CloudCliTokenManager,
105 CliTokenManager.CloudCliTokenManager.of({
106 get: unusedSecretStoreOperation(),
107 getExisting: Effect.succeed(Option.none()),
108 hasCredential: unusedSecretStoreOperation(),
109 clear: unusedSecretStoreOperation(),
110 }),
111 ),
112 Effect.provideService(
113 HttpClient.HttpClient,
114 HttpClient.make(() => unusedSecretStoreOperation()),
115 ),
116 Effect.provide(NodeServices.layer),
117 ),
118 );
119});