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

main
ts 194 lines 8.36 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AutoScalingConfiguration, Service } from "@alchemy.run/aws/AppRunner";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as apprunner from "@distilled.cloud/aws/apprunner";
6 import * as sts from "@distilled.cloud/aws/sts";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Schedule from "effect/Schedule";
10
11 // App Runner closed to new customers on 2026-04-30; accounts that weren't
12 // already customers get "Account <id> can't create new App Runner services".
13 // Set AWS_TEST_APPRUNNER_SERVICES=1 on an account that can still create them.
14 const appRunnerServices = !!process.env.AWS_TEST_APPRUNNER_SERVICES;
15 import {
16 awaitLogGroups,
17 deleteLogGroups,
18 logGroupNamesFor,
19 observeLogGroups,
20 } from "./logGroups.ts";
21
22 const { test } = Test.make({ providers: AWS.providers() });
23
24 // Ungated typed-error probe: proves the distilled error union carries the
25 // not-found tag the provider's observe/read/delete paths depend on.
26 test.provider(
27 "describeService on a nonexistent ARN fails with ResourceNotFoundException",
28 () =>
29 Effect.gen(function* () {
30 const { Account } = yield* sts.getCallerIdentity({});
31 const error = yield* Effect.flip(
32 apprunner.describeService({
33 ServiceArn: `arn:aws:apprunner:us-west-2:${Account}:service/alchemy-nonexistent-probe/0000000000000000000000000000000000`,
34 }),
35 );
36 expect(error._tag).toBe("ResourceNotFoundException");
37 }),
38 { tags: ["provider:aws", "provider:aws:apprunner", "live"] },
39 );
40
41 // The provider's delete already waits until the service is gone, so this
42 // asserts DELETED-or-gone with a short bounded confirmation retry.
43 const assertServiceGone = (arn: string) =>
44 Effect.gen(function* () {
45 const status = yield* apprunner.describeService({ ServiceArn: arn }).pipe(
46 Effect.map((r) => (r.Service.Status ?? "UNKNOWN").toUpperCase()),
47 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)),
48 );
49 if (status !== "GONE" && status !== "DELETED") {
50 return yield* Effect.fail(new Error(`App Runner service still exists (status: ${status})`));
51 }
52 }).pipe(
53 Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]) }),
54 );
55
56 const assertConfigGone = (name: string) =>
57 Effect.gen(function* () {
58 const page = yield* apprunner.listAutoScalingConfigurations({
59 AutoScalingConfigurationName: name,
60 });
61 const active = (page.AutoScalingConfigurationSummaryList ?? []).filter(
62 (s) => s.AutoScalingConfigurationName === name && s.Status?.toUpperCase() === "ACTIVE",
63 );
64 if (active.length > 0) {
65 return yield* Effect.fail(
66 new Error(`Auto scaling configuration '${name}' still has ACTIVE revisions`),
67 );
68 }
69 }).pipe(
70 Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]) }),
71 );
72
73 // App Runner service provisioning takes 3-5+ minutes and the service bills
74 // while it runs, so the full lifecycle is skipped under `--fast` and
75 // always destroys what it created. The source is a public ECR gallery
76 // image (ECR_PUBLIC), which needs no access role, no local Docker build,
77 // and no private repository.
78 test.provider.skipIf(!!process.env.FAST || !appRunnerServices)(
79 "create a public-ECR service with custom scaling, verify it serves, destroy, verify gone",
80 (stack) =>
81 Effect.gen(function* () {
82 // Clean slate in case a previous run died mid-flight.
83 yield* stack.destroy();
84
85 const { asc, service } = yield* stack.deploy(
86 Effect.gen(function* () {
87 const asc = yield* AutoScalingConfiguration("SvcScaling", {
88 autoScalingConfigurationName: "alchemy-test-svc-asc",
89 maxConcurrency: 100,
90 minSize: 1,
91 maxSize: 1,
92 });
93 const service = yield* Service("HelloService", {
94 serviceName: "alchemy-test-apprunner-svc",
95 imageRepository: {
96 imageIdentifier: "public.ecr.aws/aws-containers/hello-app-runner:latest",
97 imageRepositoryType: "ECR_PUBLIC",
98 port: "8000",
99 },
100 autoDeploymentsEnabled: false,
101 instanceConfiguration: { cpu: "256", memory: "512" },
102 autoScalingConfigurationArn: asc.autoScalingConfigurationArn,
103 });
104 return { asc, service };
105 }),
106 );
107
108 expect(service.serviceName).toBe("alchemy-test-apprunner-svc");
109 expect(service.serviceArn).toContain(":service/alchemy-test-apprunner-svc/");
110 expect(service.status).toBe("RUNNING");
111 expect(service.serviceUrl).toBeDefined();
112
113 // Out-of-band verification via distilled: RUNNING, wired to the
114 // custom auto scaling configuration, right image + instance size.
115 const described = yield* apprunner.describeService({ ServiceArn: service.serviceArn });
116 expect(described.Service.Status).toBe("RUNNING");
117 expect(described.Service.AutoScalingConfigurationSummary?.AutoScalingConfigurationArn).toBe(
118 asc.autoScalingConfigurationArn,
119 );
120 expect(described.Service.SourceConfiguration.ImageRepository?.ImageRepositoryType).toBe(
121 "ECR_PUBLIC",
122 );
123 expect(described.Service.InstanceConfiguration.Cpu).toBe("256");
124 expect(described.Service.InstanceConfiguration.Memory).toBe("512");
125
126 // The public endpoint serves. The URL is live once RUNNING, but ride
127 // out DNS/edge propagation with a bounded retry.
128 const response = yield* HttpClient.get(`https://${service.serviceUrl}`).pipe(
129 Effect.flatMap((res) =>
130 res.status === 200
131 ? Effect.succeed(res)
132 : Effect.fail(new Error(`service returned ${res.status}`)),
133 ),
134 Effect.retry({
135 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
136 }),
137 );
138 expect(response.status).toBe(200);
139
140 // Destroy immediately — App Runner services bill while running —
141 // and verify both the service and its scaling config are gone.
142 yield* stack.destroy();
143 yield* assertServiceGone(service.serviceArn);
144 yield* assertConfigGone("alchemy-test-svc-asc");
145 }),
146 // create (~3-5 min) + delete (~2-3 min), one sequential test.
147 { tags: ["provider:aws", "provider:aws:apprunner", "live"], timeout: 900_000 },
148 );
149
150 // The `retainLogGroups` opt-out: destroying the service leaves the two
151 // auto-created log groups in place. Uses the same public ECR gallery image
152 // as the lifecycle test above (no access role, no Docker build) since all
153 // this needs is a real service that has logged something.
154 test.provider.skipIf(!!process.env.FAST || !appRunnerServices)(
155 "retainLogGroups keeps the auto-created log groups after the service is destroyed",
156 (stack) =>
157 Effect.gen(function* () {
158 // Clean slate in case a previous run died mid-flight.
159 yield* stack.destroy();
160
161 const service = yield* stack.deploy(
162 Service("RetainedLogsService", {
163 serviceName: "alchemy-test-apprunner-retain-logs",
164 imageRepository: {
165 imageIdentifier: "public.ecr.aws/aws-containers/hello-app-runner:latest",
166 imageRepositoryType: "ECR_PUBLIC",
167 port: "8000",
168 },
169 autoDeploymentsEnabled: false,
170 instanceConfiguration: { cpu: "256", memory: "512" },
171 retainLogGroups: true,
172 }),
173 );
174 expect(service.status).toBe("RUNNING");
175
176 const logGroupNames = logGroupNamesFor(service.serviceName, service.serviceId);
177 expect(yield* awaitLogGroups(logGroupNames)).toEqual([true, true]);
178
179 yield* stack.destroy();
180 yield* assertServiceGone(service.serviceArn);
181
182 // The service is gone but its logs are not: the provider honored the
183 // opt-out instead of reaping them.
184 expect(yield* observeLogGroups(logGroupNames)).toEqual([true, true]);
185
186 // Nothing owns them now that the state row is gone, so the test that
187 // asked for them to be retained is what cleans them up — a retained
188 // pair left here would leak into every later run's census.
189 yield* deleteLogGroups(logGroupNames);
190 expect(yield* observeLogGroups(logGroupNames)).toEqual([false, false]);
191 }),
192 // create (~3-5 min) + delete (~2-3 min), one sequential test.
193 { tags: ["provider:aws", "provider:aws:apprunner", "live"], timeout: 900_000 },
194 );