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

main
ts 126 lines 4.7 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 DirectoryServiceTestFunctionLive, { DirectoryServiceTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "DirectoryServiceBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
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 describe.sequential(
52 "DirectoryService Bindings",
53 { tags: ["provider:aws", "provider:aws:directoryservice", "provider:aws:lambda", "live"] },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* Effect.logInfo("DirectoryService test setup: destroying previous resources");
58 yield* sharedStack.destroy();
59
60 yield* Effect.logInfo("DirectoryService test setup: deploying fixture");
61 const attrs = yield* sharedStack.deploy(
62 Effect.gen(function* () {
63 return yield* DirectoryServiceTestFunction;
64 }).pipe(Effect.provide(DirectoryServiceTestFunctionLive)),
65 );
66
67 expect(attrs.functionUrl).toBeTruthy();
68 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
69
70 const readinessUrl = `${baseUrl}/bindings`;
71 yield* Effect.logInfo(`DirectoryService test setup: probing readiness at ${readinessUrl}`);
72 yield* HttpClient.get(readinessUrl).pipe(
73 Effect.flatMap((response) =>
74 response.status === 200
75 ? Effect.succeed(response)
76 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
77 ),
78 Effect.tapError((error) =>
79 Effect.logWarning(
80 `DirectoryService test setup: fixture not ready yet (${String(error)})`,
81 ),
82 ),
83 Effect.retry({ schedule: readinessPolicy }),
84 );
85 }),
86 { timeout: 240_000 },
87 );
88
89 afterAll(sharedStack.destroy(), { timeout: 120_000 });
90
91 describe("binding registration", () => {
92 test.provider("both capabilities initialize in the runtime", (_stack) =>
93 Effect.gen(function* () {
94 const response = (yield* getJson("/bindings")) as { bound: string[] };
95 expect(response.bound).toHaveLength(2);
96 }),
97 );
98 });
99
100 describe("GetDirectoryLimits", () => {
101 test.provider("reads the account's directory limits", (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* getJson("/limits")) as {
104 cloudOnlyLimit: number;
105 cloudOnlyCount: number;
106 };
107 expect(response.cloudOnlyLimit).toBeGreaterThan(0);
108 expect(response.cloudOnlyCount).toBeGreaterThanOrEqual(0);
109 }),
110 );
111 });
112
113 describe("DescribeDirectories", () => {
114 test.provider("enumerates the account's directories", (_stack) =>
115 Effect.gen(function* () {
116 const response = (yield* getJson("/directories")) as { ids: string[] };
117 // The testing account usually has no standing directories; every id
118 // that IS present must be directory-shaped.
119 for (const id of response.ids) {
120 expect(id).toMatch(/^d-/);
121 }
122 }),
123 );
124 });
125 },
126 );