@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 242 lines 9.13 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import FSxBindingsFunctionLive, { FSxBindingsFunction } from "./bindings-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "FSxBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy under parallel-suite load.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation on the freshly attached policy that the
29 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
30 // 4xx/assertion failure surfaces immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 const getJson = (path: string) =>
49 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 const postJson = (path: string) =>
52 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
53
54 describe.sequential(
55 "FSx Bindings",
56 { tags: ["provider:aws", "provider:aws:fsx", "provider:aws:lambda", "live"] },
57 () => {
58 beforeAll(
59 Effect.gen(function* () {
60 yield* Effect.logInfo("FSx test setup: destroying previous resources");
61 yield* sharedStack.destroy();
62
63 yield* Effect.logInfo("FSx test setup: deploying fixture");
64 const { functionUrl } = yield* sharedStack.deploy(
65 Effect.gen(function* () {
66 return yield* FSxBindingsFunction;
67 }).pipe(Effect.provide(FSxBindingsFunctionLive)),
68 );
69
70 expect(functionUrl).toBeTruthy();
71 baseUrl = functionUrl!.replace(/\/+$/, "");
72
73 const readinessUrl = `${baseUrl}/bindings`;
74 yield* Effect.logInfo(`FSx test setup: probing readiness at ${readinessUrl}`);
75 yield* HttpClient.get(readinessUrl).pipe(
76 Effect.flatMap((response) =>
77 response.status === 200
78 ? Effect.succeed(response)
79 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
80 ),
81 Effect.tapError((error) =>
82 Effect.logWarning(`FSx test setup: fixture not ready yet (${String(error)})`),
83 ),
84 Effect.retry({ schedule: readinessPolicy }),
85 );
86 }),
87 { timeout: 240_000 },
88 );
89
90 afterAll(sharedStack.destroy(), { timeout: 240_000 });
91
92 describe("binding registration", () => {
93 test.provider("all 14 capabilities initialize in the runtime", (_stack) =>
94 Effect.gen(function* () {
95 const response = (yield* getJson("/bindings")) as any;
96 expect(response.bound).toHaveLength(14);
97 }),
98 );
99 });
100
101 describe("DescribeBackups", () => {
102 test.provider("lists the account's FSx backups", (_stack) =>
103 Effect.gen(function* () {
104 const response = (yield* getJson("/backups")) as any;
105 expect(typeof response.count).toBe("number");
106 }),
107 );
108 });
109
110 describe("DescribeSnapshots", () => {
111 test.provider("lists the account's OpenZFS snapshots", (_stack) =>
112 Effect.gen(function* () {
113 const response = (yield* getJson("/snapshots")) as any;
114 expect(typeof response.count).toBe("number");
115 }),
116 );
117 });
118
119 describe("DescribeVolumes", () => {
120 test.provider("lists the account's ONTAP/OpenZFS volumes", (_stack) =>
121 Effect.gen(function* () {
122 const response = (yield* getJson("/volumes")) as any;
123 expect(typeof response.count).toBe("number");
124 }),
125 );
126 });
127
128 describe("DescribeStorageVirtualMachines", () => {
129 test.provider("lists the account's ONTAP SVMs", (_stack) =>
130 Effect.gen(function* () {
131 const response = (yield* getJson("/svms")) as any;
132 expect(typeof response.count).toBe("number");
133 }),
134 );
135 });
136
137 describe("DescribeDataRepositoryTasks", () => {
138 test.provider("lists the account's data repository tasks", (_stack) =>
139 Effect.gen(function* () {
140 const response = (yield* getJson("/dr-tasks")) as any;
141 expect(typeof response.count).toBe("number");
142 }),
143 );
144 });
145
146 describe("DescribeDataRepositoryAssociations", () => {
147 test.provider("lists the account's data repository associations", (_stack) =>
148 Effect.gen(function* () {
149 const response = (yield* getJson("/dr-associations")) as any;
150 expect(typeof response.count).toBe("number");
151 }),
152 );
153 });
154
155 describe("DeleteBackup", () => {
156 test.provider("returns the typed BackupNotFound for a missing backup", (_stack) =>
157 Effect.gen(function* () {
158 const response = (yield* postJson("/backup/delete-missing")) as any;
159 expect(response.tag).toBe("BackupNotFound");
160 }),
161 );
162 });
163
164 describe("CopyBackup", () => {
165 test.provider("returns the typed BackupNotFound for a missing source backup", (_stack) =>
166 Effect.gen(function* () {
167 const response = (yield* postJson("/backup/copy-missing")) as any;
168 expect(response.tag).toBe("BackupNotFound");
169 }),
170 );
171 });
172
173 describe("UpdateSnapshot", () => {
174 // FSx misclassifies this not-found as a wire `BadRequest`; the distilled
175 // patch (patches/fsx.json) carves the typed UpdateSnapshotNotFound out
176 // of it by message predicate.
177 test.provider("returns the typed UpdateSnapshotNotFound for a missing snapshot", (_stack) =>
178 Effect.gen(function* () {
179 const response = (yield* postJson("/snapshot/update-missing")) as any;
180 expect(response.tag).toBe("UpdateSnapshotNotFound");
181 }),
182 );
183 });
184
185 describe("DeleteSnapshot", () => {
186 test.provider("returns the typed SnapshotNotFound for a missing snapshot", (_stack) =>
187 Effect.gen(function* () {
188 const response = (yield* postJson("/snapshot/delete-missing")) as any;
189 expect(response.tag).toBe("SnapshotNotFound");
190 }),
191 );
192 });
193
194 describe("CreateSnapshot", () => {
195 // FSx misclassifies this not-found as a wire `BadRequest`; the distilled
196 // patch (patches/fsx.json) carves the typed SnapshotVolumeNotFound out
197 // of it by message predicate.
198 test.provider("returns the typed SnapshotVolumeNotFound for a missing volume", (_stack) =>
199 Effect.gen(function* () {
200 const response = (yield* postJson("/snapshot/create-missing-volume")) as any;
201 expect(response.tag).toBe("SnapshotVolumeNotFound");
202 }),
203 );
204 });
205
206 describe("RestoreVolumeFromSnapshot", () => {
207 // FSx misclassifies this not-found as a wire `BadRequest`; the distilled
208 // patch (patches/fsx.json) carves the typed RestoreSnapshotNotFound out
209 // of it by message predicate.
210 test.provider("returns the typed RestoreSnapshotNotFound for a missing snapshot", (_stack) =>
211 Effect.gen(function* () {
212 const response = (yield* postJson("/volume/restore-missing")) as any;
213 expect(response.tag).toBe("RestoreSnapshotNotFound");
214 }),
215 );
216 });
217
218 describe("CopySnapshotAndUpdateVolume", () => {
219 // FSx reports a nonexistent source snapshot as a wire `BadRequest` with
220 // "SourceSnapshotARN provided is not a valid ARN"; the distilled patch
221 // (patches/fsx.json) carves the typed SourceSnapshotNotFound out of it
222 // by message predicate.
223 test.provider(
224 "returns the typed SourceSnapshotNotFound for a missing source snapshot",
225 (_stack) =>
226 Effect.gen(function* () {
227 const response = (yield* postJson("/volume/copy-snapshot-missing")) as any;
228 expect(response.tag).toBe("SourceSnapshotNotFound");
229 }),
230 );
231 });
232
233 describe("CancelDataRepositoryTask", () => {
234 test.provider("returns the typed DataRepositoryTaskNotFound for a missing task", (_stack) =>
235 Effect.gen(function* () {
236 const response = (yield* postJson("/dr-task/cancel-missing")) as any;
237 expect(response.tag).toBe("DataRepositoryTaskNotFound");
238 }),
239 );
240 });
241 },
242 );