@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 146 lines 6.1 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 batch from "@distilled.cloud/aws/batch";
5 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
6 import * as ecr from "@distilled.cloud/aws/ecr";
7 import * as iam from "@distilled.cloud/aws/iam";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import NightlyJob, { MARKER } from "./fixtures/nightly-job.ts";
11 import { BatchTestNetwork } from "./TestNetwork.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Full end-to-end for the Effect-native `Batch.JobDefinition` form: bundle
16 // the inline run-to-completion Effect, build + push the container image to
17 // the managed ECR repository (Docker build, linux/amd64), provision the
18 // job/execution IAM roles, register the job definition — then submit a job
19 // against it and prove the Effect actually ran inside Batch: the job lands
20 // in SUCCEEDED (container exit 0) and the marker appears in its
21 // `/aws/batch/job` log stream.
22 //
23 // It is heavy (Docker build + ECR push + Fargate provisioning of the job,
24 // ~2.5 minutes end to end), so it is skipped under --fast and always
25 // destroys what it created.
26 test.provider.skipIf(!!process.env.FAST)(
27 "deploys an Effect-native Batch job definition and runs it to SUCCEEDED",
28 (stack) =>
29 Effect.gen(function* () {
30 // Clean slate in case a previous run died mid-flight.
31 yield* stack.destroy();
32
33 const outputs = yield* stack.deploy(
34 Effect.gen(function* () {
35 const network = yield* BatchTestNetwork;
36 const computeEnvironment = yield* AWS.Batch.ComputeEnvironment("PlatformCE", {
37 subnets: network.subnetIds,
38 securityGroupIds: network.securityGroupIds,
39 });
40 const queue = yield* AWS.Batch.JobQueue("PlatformQueue", {
41 computeEnvironments: [computeEnvironment.computeEnvironmentArn],
42 });
43 const job = yield* NightlyJob;
44 return {
45 jobQueueArn: queue.jobQueueArn,
46 jobDefinitionName: job.jobDefinitionName,
47 jobDefinitionArn: job.jobDefinitionArn,
48 imageUri: job.imageUri,
49 repositoryName: job.repositoryName,
50 repositoryUri: job.repositoryUri,
51 codeHash: job.codeHash,
52 jobRoleArn: job.jobRoleArn,
53 jobRoleName: job.jobRoleName,
54 executionRoleName: job.executionRoleName,
55 };
56 }),
57 );
58
59 // Effect-native attributes: managed repo, roles, and image.
60 expect(outputs.jobDefinitionName).toBe("alchemy-test-batch-platform-e2e");
61 expect(outputs.repositoryUri).toBeTruthy();
62 expect(outputs.imageUri).toBe(`${outputs.repositoryUri}:${outputs.codeHash}`);
63 expect(outputs.jobRoleArn).toContain(":role/");
64
65 // Out-of-band verification via distilled: the registered revision runs
66 // the built image with the managed roles.
67 const described = yield* batch.describeJobDefinitions({
68 jobDefinitions: [outputs.jobDefinitionArn!],
69 });
70 const container = described.jobDefinitions?.[0]?.containerProperties;
71 expect(container?.image).toBe(outputs.imageUri);
72 expect(container?.jobRoleArn).toBe(outputs.jobRoleArn);
73
74 // Submit a job against the platform definition and wait for the
75 // run-to-completion Effect to finish (SUCCEEDED = container exit 0).
76 const submitted = yield* batch.submitJob({
77 jobName: "alchemy-e2e-platform-run",
78 jobQueue: outputs.jobQueueArn,
79 jobDefinition: outputs.jobDefinitionArn!,
80 });
81 const job = yield* batch.describeJobs({ jobs: [submitted.jobId] }).pipe(
82 Effect.map((result) => result.jobs?.[0]),
83 Effect.repeat({
84 schedule: Schedule.spaced("10 seconds"),
85 until: (j): boolean => j?.status === "SUCCEEDED" || j?.status === "FAILED",
86 times: 60,
87 }),
88 );
89 // Include the reason so a FAILED run shows why (placement, pull, or a
90 // bootstrap crash all land here with different statusReasons).
91 expect(`${job?.status}: ${job?.statusReason ?? ""}`).toMatch(/^SUCCEEDED:/);
92
93 // The bundled Effect actually executed: its marker is in the job's
94 // log stream (log delivery can lag the state change slightly).
95 const logStreamName = job?.container?.logStreamName;
96 expect(logStreamName).toBeTruthy();
97 const events = yield* logs
98 .getLogEvents({
99 logGroupName: "/aws/batch/job",
100 logStreamName: logStreamName!,
101 })
102 .pipe(
103 Effect.flatMap((result) =>
104 (result.events ?? []).some((e) => e.message?.includes(MARKER))
105 ? Effect.succeed(result.events ?? [])
106 : Effect.fail(new Error("marker not in log stream yet")),
107 ),
108 Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 12 }),
109 );
110 expect(events.some((e) => e.message?.includes(MARKER))).toBe(true);
111
112 // Destroy — and verify zero leftovers: job definition revisions, the
113 // managed repository, and both managed roles.
114 const { jobDefinitionName, repositoryName, jobRoleName, executionRoleName } = outputs;
115 yield* stack.destroy();
116
117 const revisions = yield* batch.describeJobDefinitions({
118 jobDefinitionName,
119 status: "ACTIVE",
120 });
121 expect(revisions.jobDefinitions ?? []).toHaveLength(0);
122
123 const repoError = yield* Effect.flip(
124 ecr.describeRepositories({ repositoryNames: [repositoryName!] }),
125 );
126 expect(repoError._tag).toBe("RepositoryNotFoundException");
127
128 for (const roleName of [jobRoleName!, executionRoleName!]) {
129 const roleError = yield* Effect.flip(iam.getRole({ RoleName: roleName }));
130 expect(roleError._tag).toBe("NoSuchEntityException");
131 }
132 }),
133 // Docker build + push (~2-4 min) + CE/queue (~1-2 min) + Fargate job run
134 // (~2-4 min) + destroy (~2-3 min).
135 {
136 tags: [
137 "provider:aws",
138 "provider:aws:batch",
139 "provider:aws:ec2",
140 "provider:aws:ecr",
141 "provider:aws:iam",
142 "live",
143 ],
144 timeout: 1_200_000,
145 },
146 );