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

main
ts 138 lines 5.95 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as apprunner from "@distilled.cloud/aws/apprunner";
5 import * as ecr from "@distilled.cloud/aws/ecr";
6 import * as iam from "@distilled.cloud/aws/iam";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Schedule from "effect/Schedule";
10 import TestService from "./fixtures/service.ts";
11 import { awaitLogGroups, logGroupNamesFor, observeLogGroups } from "./logGroups.ts";
12
13 // App Runner closed to new customers on 2026-04-30; accounts that weren't
14 // already customers get "Account <id> can't create new App Runner services".
15 // Set AWS_TEST_APPRUNNER_SERVICES=1 on an account that can still create them.
16 const appRunnerServices = !!process.env.AWS_TEST_APPRUNNER_SERVICES;
17
18 const { test } = Test.make({ providers: AWS.providers() });
19
20 // Full end-to-end for the Effect-native `AppRunner.Service` form: bundle the
21 // inline Effect program, build + push the container image to the managed ECR
22 // repository (Docker build, linux/amd64), provision the instance/access IAM
23 // roles, and deploy the App Runner service — then prove over HTTPS that (a)
24 // the `{ fetch }` handler is served and (b) the `ServerHost.run` background
25 // loop is actually executing inside the deployed container (`/ticks` climbs).
26 //
27 // It is heavy (Docker build + ECR push + 3-5 min App Runner provisioning,
28 // bills while running), so it is skipped under `--fast` and always
29 // destroys what it created.
30 test.provider.skipIf(!!process.env.FAST || !appRunnerServices)(
31 "deploys an Effect-native App Runner service and serves HTTP",
32 (stack) =>
33 Effect.gen(function* () {
34 // Clean slate in case a previous run died mid-flight.
35 yield* stack.destroy();
36
37 const service = yield* stack.deploy(
38 Effect.gen(function* () {
39 return yield* TestService;
40 }),
41 );
42
43 expect(service.serviceName).toBe("alchemy-test-apprunner-e2e");
44 expect(service.status).toBe("RUNNING");
45 expect(service.serviceUrl).toBeTruthy();
46 // Effect-native attributes: managed repo, roles, and image.
47 expect(service.repositoryUri).toBeTruthy();
48 expect(service.imageUri).toBe(`${service.repositoryUri}:${service.codeHash}`);
49 expect(service.instanceRoleArn).toContain(":role/");
50 expect(service.accessRoleArn).toContain(":role/");
51
52 // Out-of-band verification via distilled: private ECR source wired to
53 // the managed access role and instance role.
54 const described = yield* apprunner.describeService({
55 ServiceArn: service.serviceArn,
56 });
57 expect(described.Service.SourceConfiguration.ImageRepository?.ImageRepositoryType).toBe(
58 "ECR",
59 );
60 expect(described.Service.SourceConfiguration.AuthenticationConfiguration?.AccessRoleArn).toBe(
61 service.accessRoleArn,
62 );
63 expect(described.Service.InstanceConfiguration.InstanceRoleArn).toBe(service.instanceRoleArn);
64
65 // The deployed program serves HTTP (ride out DNS/edge propagation).
66 const health = yield* HttpClient.get(`https://${service.serviceUrl}/health`).pipe(
67 Effect.flatMap((res) =>
68 res.status === 200
69 ? Effect.succeed(res)
70 : Effect.fail(new Error(`/health returned ${res.status}`)),
71 ),
72 Effect.retry({
73 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
74 }),
75 );
76 expect(health.status).toBe(200);
77
78 // The `host.run` background loop is executing: /ticks climbs.
79 const readTicks = HttpClient.get(`https://${service.serviceUrl}/ticks`).pipe(
80 Effect.flatMap((res) => res.json),
81 Effect.map((json) => (json as { ticks: number }).ticks),
82 );
83 const first = yield* readTicks;
84 const later = yield* readTicks.pipe(
85 Effect.flatMap((ticks) =>
86 ticks > first
87 ? Effect.succeed(ticks)
88 : Effect.fail(new Error(`ticks not climbing (still ${ticks})`)),
89 ),
90 Effect.retry({
91 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
92 }),
93 );
94 expect(later).toBeGreaterThan(first);
95
96 // App Runner auto-creates these two log groups for the service; they
97 // survive `deleteService`, so the provider reaps them explicitly.
98 // Asserting they exist FIRST keeps the post-destroy check below from
99 // passing vacuously on a wrong name.
100 const logGroupNames = logGroupNamesFor(service.serviceName, service.serviceId);
101 expect(yield* awaitLogGroups(logGroupNames)).toEqual([true, true]);
102
103 // Destroy immediately — App Runner services bill while running — and
104 // verify zero leftovers: service, managed repository, both roles, and
105 // both log groups.
106 const { repositoryName, instanceRoleName, accessRoleName, serviceArn } = service;
107 yield* stack.destroy();
108
109 const serviceAfter = yield* apprunner.describeService({ ServiceArn: serviceArn }).pipe(
110 Effect.map((r) => (r.Service.Status ?? "UNKNOWN").toUpperCase()),
111 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)),
112 );
113 expect(["GONE", "DELETED"]).toContain(serviceAfter);
114
115 const repoError = yield* Effect.flip(
116 ecr.describeRepositories({ repositoryNames: [repositoryName!] }),
117 );
118 expect(repoError._tag).toBe("RepositoryNotFoundException");
119
120 for (const roleName of [instanceRoleName!, accessRoleName!]) {
121 const roleError = yield* Effect.flip(iam.getRole({ RoleName: roleName }));
122 expect(roleError._tag).toBe("NoSuchEntityException");
123 }
124
125 expect(yield* observeLogGroups(logGroupNames)).toEqual([false, false]);
126 }),
127 // Docker build + push (~2-4 min) + create (~3-5 min) + delete (~2-3 min).
128 {
129 tags: [
130 "provider:aws",
131 "provider:aws:apprunner",
132 "provider:aws:ecr",
133 "provider:aws:iam",
134 "live",
135 ],
136 timeout: 1_200_000,
137 },
138 );