1import * as AWS from "@/AWS";
2import * as Core from "@/Test/Core";
3import * as Test from "@/Test/Alchemy";
4import { describe, expect } from "alchemy-test";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import * as HttpClientRequest from "effect/http/HttpClientRequest";
10import EcrPublicTestFunctionLive, { EcrPublicTestFunction } from "./handler";
11
12const testOptions = { providers: AWS.providers() };
13const { test, beforeAll, afterAll } = Test.make(testOptions);
14const 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.
18const readinessPolicy = Schedule.max([
19 Schedule.fixed("2 seconds"),
20 Schedule.recurs(75),
21]);
22
23let baseUrl: string;
24
25class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28}> {}
29
30// The shared Lambda fixture occasionally answers a transient 5xx under load
31// (cold re-init, IAM propagation on the freshly attached policy that the
32// handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
33// 4xx/assertion failure surfaces immediately.
34const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(
41 new TransientUpstream({ status: response.status, body }),
42 ),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([
50 Schedule.exponential("500 millis"),
51 Schedule.recurs(6),
52 ]),
53 }),
54 );
55
56const getJson = (path: string) =>
57 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
58 Effect.flatMap((r) => r.json),
59 );
60
61describe.sequential(
62 "ECRPublic Bindings",
63 {
64 tags: [
65 "provider:aws",
66 "provider:aws:batch",
67 "provider:aws:ecrpublic",
68 "provider:aws:lambda",
69 "live",
70 ],
71 },
72 () => {
73 beforeAll(
74 Effect.gen(function* () {
75 yield* Effect.logInfo(
76 "ECRPublic test setup: destroying previous resources",
77 );
78 yield* sharedStack.destroy();
79
80 yield* Effect.logInfo("ECRPublic test setup: deploying fixture");
81 const { functionUrl } = yield* sharedStack.deploy(
82 Effect.gen(function* () {
83 return yield* EcrPublicTestFunction;
84 }).pipe(Effect.provide(EcrPublicTestFunctionLive)),
85 );
86
87 expect(functionUrl).toBeTruthy();
88 baseUrl = functionUrl!.replace(/\/+$/, "");
89
90 const readinessUrl = `${baseUrl}/bindings`;
91 yield* Effect.logInfo(
92 `ECRPublic test setup: probing readiness at ${readinessUrl}`,
93 );
94 yield* HttpClient.get(readinessUrl).pipe(
95 Effect.flatMap((response) =>
96 response.status === 200
97 ? Effect.succeed(response)
98 : Effect.fail(
99 new Error(`Function not ready: ${response.status}`),
100 ),
101 ),
102 Effect.tapError((error) =>
103 Effect.logWarning(
104 `ECRPublic test setup: fixture not ready yet (${String(error)})`,
105 ),
106 ),
107 Effect.retry({ schedule: readinessPolicy }),
108 );
109 }),
110 { timeout: 240_000 },
111 );
112
113 afterAll(sharedStack.destroy(), { timeout: 120_000 });
114
115 describe("binding registration", () => {
116 test.provider("all 12 capabilities initialize in the runtime", (_stack) =>
117 Effect.gen(function* () {
118 const response = yield* getJson("/bindings");
119 expect((response as any).bound).toHaveLength(12);
120 }),
121 );
122 });
123
124 describe("DescribeRegistries", () => {
125 test.provider(
126 "reads the account's public registry and alias",
127 (_stack) =>
128 Effect.gen(function* () {
129 const response = (yield* getJson("/registries")) as any;
130 expect(response.count).toBeGreaterThanOrEqual(1);
131 expect(typeof response.alias).toBe("string");
132 }),
133 { timeout: 60_000 },
134 );
135 });
136
137 describe("GetRegistryCatalogData", () => {
138 test.provider(
139 "reads the registry catalog data",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* getJson("/registry-catalog")) as any;
143 expect(response.tag).toBe("Ok");
144 }),
145 { timeout: 60_000 },
146 );
147 });
148
149 describe("GetAuthorizationToken", () => {
150 test.provider(
151 "mints a registry auth token (proving the sts:GetServiceBearerToken grant)",
152 (_stack) =>
153 Effect.gen(function* () {
154 const response = (yield* getJson("/auth-token")) as any;
155 expect(response.hasToken).toBe(true);
156 }),
157 { timeout: 60_000 },
158 );
159 });
160
161 describe("GetRepositoryCatalogData", () => {
162 test.provider(
163 "reads the fixture repository's gallery metadata (proving repositoryName injection)",
164 (_stack) =>
165 Effect.gen(function* () {
166 const response = (yield* getJson("/repo-catalog")) as any;
167 expect(response.description).toBe(
168 "alchemy ECRPublic bindings fixture",
169 );
170 }),
171 { timeout: 60_000 },
172 );
173 });
174
175 describe("BatchCheckLayerAvailability", () => {
176 test.provider(
177 "reports a nonexistent layer digest as failed or unavailable",
178 (_stack) =>
179 Effect.gen(function* () {
180 const response = (yield* getJson("/check-layers")) as any;
181 // ECR Public reports a nonexistent digest either as a per-layer
182 // failure or as an UNAVAILABLE layer (observed live: UNAVAILABLE).
183 expect(
184 response.failures + response.unavailable,
185 ).toBeGreaterThanOrEqual(1);
186 }),
187 { timeout: 60_000 },
188 );
189 });
190
191 describe("BatchDeleteImage", () => {
192 test.provider(
193 "reports ImageNotFound for a nonexistent tag (proving the grant)",
194 (_stack) =>
195 Effect.gen(function* () {
196 const response = (yield* getJson("/delete-missing")) as any;
197 expect(response.failureCode).toBe("ImageNotFound");
198 }),
199 { timeout: 60_000 },
200 );
201 });
202
203 describe("InitiateLayerUpload + UploadLayerPart + CompleteLayerUpload + PutImage", () => {
204 test.provider(
205 "pushes a minimal image end-to-end",
206 (_stack) =>
207 Effect.gen(function* () {
208 const response = (yield* getJson("/push-flow")) as any;
209 expect(response.tag).toBe("Ok");
210 expect(response.imageDigest).toContain("sha256:");
211 }),
212 { timeout: 120_000 },
213 );
214 });
215
216 describe("DescribeImages", () => {
217 test.provider(
218 "lists the pushed image",
219 (_stack) =>
220 Effect.gen(function* () {
221 const response = (yield* getJson("/images")) as any;
222 expect(response.count).toBeGreaterThanOrEqual(1);
223 }),
224 { timeout: 60_000 },
225 );
226 });
227
228 describe("DescribeImageTags", () => {
229 test.provider(
230 "lists the pushed image's tag",
231 (_stack) =>
232 Effect.gen(function* () {
233 const response = (yield* getJson("/image-tags")) as any;
234 expect(response.count).toBeGreaterThanOrEqual(1);
235 }),
236 { timeout: 60_000 },
237 );
238 });
239 },
240);