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

main
ts 218 lines 7.35 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 EcrPublicTestFunctionLive, { EcrPublicTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "ECRPublicBindings");
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 "ECRPublic Bindings",
53 {
54 tags: [
55 "provider:aws",
56 "provider:aws:batch",
57 "provider:aws:ecrpublic",
58 "provider:aws:lambda",
59 "live",
60 ],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* Effect.logInfo("ECRPublic test setup: destroying previous resources");
66 yield* sharedStack.destroy();
67
68 yield* Effect.logInfo("ECRPublic test setup: deploying fixture");
69 const { functionUrl } = yield* sharedStack.deploy(
70 Effect.gen(function* () {
71 return yield* EcrPublicTestFunction;
72 }).pipe(Effect.provide(EcrPublicTestFunctionLive)),
73 );
74
75 expect(functionUrl).toBeTruthy();
76 baseUrl = functionUrl!.replace(/\/+$/, "");
77
78 const readinessUrl = `${baseUrl}/bindings`;
79 yield* Effect.logInfo(`ECRPublic test setup: probing readiness at ${readinessUrl}`);
80 yield* HttpClient.get(readinessUrl).pipe(
81 Effect.flatMap((response) =>
82 response.status === 200
83 ? Effect.succeed(response)
84 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
85 ),
86 Effect.tapError((error) =>
87 Effect.logWarning(`ECRPublic test setup: fixture not ready yet (${String(error)})`),
88 ),
89 Effect.retry({ schedule: readinessPolicy }),
90 );
91 }),
92 { timeout: 240_000 },
93 );
94
95 afterAll(sharedStack.destroy(), { timeout: 120_000 });
96
97 describe("binding registration", () => {
98 test.provider("all 12 capabilities initialize in the runtime", (_stack) =>
99 Effect.gen(function* () {
100 const response = yield* getJson("/bindings");
101 expect((response as any).bound).toHaveLength(12);
102 }),
103 );
104 });
105
106 describe("DescribeRegistries", () => {
107 test.provider(
108 "reads the account's public registry and alias",
109 (_stack) =>
110 Effect.gen(function* () {
111 const response = (yield* getJson("/registries")) as any;
112 expect(response.count).toBeGreaterThanOrEqual(1);
113 expect(typeof response.alias).toBe("string");
114 }),
115 { timeout: 60_000 },
116 );
117 });
118
119 describe("GetRegistryCatalogData", () => {
120 test.provider(
121 "reads the registry catalog data",
122 (_stack) =>
123 Effect.gen(function* () {
124 const response = (yield* getJson("/registry-catalog")) as any;
125 expect(response.tag).toBe("Ok");
126 }),
127 { timeout: 60_000 },
128 );
129 });
130
131 describe("GetAuthorizationToken", () => {
132 test.provider(
133 "mints a registry auth token (proving the sts:GetServiceBearerToken grant)",
134 (_stack) =>
135 Effect.gen(function* () {
136 const response = (yield* getJson("/auth-token")) as any;
137 expect(response.hasToken).toBe(true);
138 }),
139 { timeout: 60_000 },
140 );
141 });
142
143 describe("GetRepositoryCatalogData", () => {
144 test.provider(
145 "reads the fixture repository's gallery metadata (proving repositoryName injection)",
146 (_stack) =>
147 Effect.gen(function* () {
148 const response = (yield* getJson("/repo-catalog")) as any;
149 expect(response.description).toBe("alchemy ECRPublic bindings fixture");
150 }),
151 { timeout: 60_000 },
152 );
153 });
154
155 describe("BatchCheckLayerAvailability", () => {
156 test.provider(
157 "reports a nonexistent layer digest as failed or unavailable",
158 (_stack) =>
159 Effect.gen(function* () {
160 const response = (yield* getJson("/check-layers")) as any;
161 // ECR Public reports a nonexistent digest either as a per-layer
162 // failure or as an UNAVAILABLE layer (observed live: UNAVAILABLE).
163 expect(response.failures + response.unavailable).toBeGreaterThanOrEqual(1);
164 }),
165 { timeout: 60_000 },
166 );
167 });
168
169 describe("BatchDeleteImage", () => {
170 test.provider(
171 "reports ImageNotFound for a nonexistent tag (proving the grant)",
172 (_stack) =>
173 Effect.gen(function* () {
174 const response = (yield* getJson("/delete-missing")) as any;
175 expect(response.failureCode).toBe("ImageNotFound");
176 }),
177 { timeout: 60_000 },
178 );
179 });
180
181 describe("InitiateLayerUpload + UploadLayerPart + CompleteLayerUpload + PutImage", () => {
182 test.provider(
183 "pushes a minimal image end-to-end",
184 (_stack) =>
185 Effect.gen(function* () {
186 const response = (yield* getJson("/push-flow")) as any;
187 expect(response.tag).toBe("Ok");
188 expect(response.imageDigest).toContain("sha256:");
189 }),
190 { timeout: 120_000 },
191 );
192 });
193
194 describe("DescribeImages", () => {
195 test.provider(
196 "lists the pushed image",
197 (_stack) =>
198 Effect.gen(function* () {
199 const response = (yield* getJson("/images")) as any;
200 expect(response.count).toBeGreaterThanOrEqual(1);
201 }),
202 { timeout: 60_000 },
203 );
204 });
205
206 describe("DescribeImageTags", () => {
207 test.provider(
208 "lists the pushed image's tag",
209 (_stack) =>
210 Effect.gen(function* () {
211 const response = (yield* getJson("/image-tags")) as any;
212 expect(response.count).toBeGreaterThanOrEqual(1);
213 }),
214 { timeout: 60_000 },
215 );
216 });
217 },
218 );