@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 144 lines 5.18 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Cluster } from "@alchemy.run/aws/ECS/Cluster";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as ecs from "@distilled.cloud/aws/ecs";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { getDefaultVpcNetwork } from "../DefaultVpc.ts";
9 import BuildSecretsTask from "./fixtures/buildsecrets-task.ts";
10 import InlineDockerfileTaskLive, {
11 InlineDockerfileTask,
12 } from "./fixtures/inline-dockerfile-task.ts";
13
14 const { test } = Test.make({ providers: AWS.providers() });
15
16 /**
17 * Run a one-shot task once, out-of-band, on the standing default VPC (a
18 * public subnet + public IP lets the task ENI pull from ECR), and wait for
19 * it to stop.
20 */
21 const runToCompletion = Effect.fn(function* (clusterArn: string, taskDefinitionArn: string) {
22 const { subnetIds } = yield* getDefaultVpcNetwork;
23 const subnetId = subnetIds[0];
24 expect(subnetId).toBeTruthy();
25
26 // The task/execution roles were created seconds ago: ECS rejects `runTask`
27 // with "unable to assume the role" until IAM propagates, so retry that one
28 // typed error (bounded).
29 const started = yield* ecs
30 .runTask({
31 cluster: clusterArn,
32 taskDefinition: taskDefinitionArn,
33 launchType: "FARGATE",
34 count: 1,
35 startedBy: "alchemy-inline-dockerfile-test",
36 networkConfiguration: {
37 awsvpcConfiguration: {
38 subnets: [subnetId!],
39 assignPublicIp: "ENABLED",
40 },
41 },
42 })
43 .pipe(
44 Effect.retry({
45 while: (e) =>
46 e._tag === "ClientException" && (e.message ?? "").includes("unable to assume the role"),
47 schedule: Schedule.spaced("5 seconds"),
48 times: 12,
49 }),
50 );
51 expect(started.failures ?? []).toEqual([]);
52 const taskArn = started.tasks?.[0]?.taskArn;
53 expect(taskArn).toBeTruthy();
54
55 // Wait for the task to stop: image pull + container boot + the one-shot
56 // program running to completion (~1-3 minutes cold).
57 const stopped = yield* ecs.describeTasks({ cluster: clusterArn, tasks: [taskArn!] }).pipe(
58 Effect.flatMap((result) => {
59 const task = result.tasks?.[0];
60 return task?.lastStatus === "STOPPED"
61 ? Effect.succeed(task)
62 : Effect.fail(new Error(`task not stopped yet: ${task?.lastStatus}`));
63 }),
64 Effect.tapError((error) => Effect.logInfo(String(error))),
65 Effect.retry({ schedule: Schedule.spaced("6 seconds"), times: 50 }),
66 );
67 expect(stopped.stoppedReason ?? "").not.toContain("CannotPullContainer");
68 return stopped;
69 });
70
71 const tags = [
72 "provider:aws",
73 "provider:aws:ec2",
74 "provider:aws:ecs",
75 "provider:docker",
76 "provider:docker:dockerfile",
77 "live",
78 ];
79
80 // Live proof of the `main` + `Dockerfile.inline` environment composition: the
81 // fixture's inline content carries a `RUN` that bakes a marker file into an
82 // image layer, and its bundled `{ run }` program reads the marker back at
83 // container runtime, exiting non-zero if absent. Exit code 0 proves the
84 // inline preamble replaced the generated `FROM` (the RUN executed at build
85 // time) AND the bundle was layered on top of that environment.
86 //
87 // Docker + ECR + Fargate placement is ~1 minute of wall clock, so it is
88 // skipped under --fast like the Task e2e smoke test.
89 test.provider.skipIf(!!process.env.FAST)(
90 "inline-dockerfile environment bakes RUN artifact into the bundled image",
91 (stack) =>
92 Effect.gen(function* () {
93 yield* stack.destroy();
94
95 const { clusterArn, taskDefinitionArn } = yield* stack.deploy(
96 Effect.gen(function* () {
97 const cluster = yield* Cluster("InlineDockerfileCluster", {
98 clusterName: "alchemy-test-inline-dockerfile",
99 });
100 const task = yield* InlineDockerfileTask;
101 return {
102 clusterArn: cluster.clusterArn,
103 taskDefinitionArn: task.taskDefinitionArn,
104 };
105 }).pipe(Effect.provide(InlineDockerfileTaskLive)),
106 );
107 expect(taskDefinitionArn).toBeTruthy();
108
109 // Exit 0 requires the artifact to have been baked by the inline RUN.
110 const stopped = yield* runToCompletion(clusterArn, taskDefinitionArn!);
111 expect(stopped.containers?.[0]?.exitCode).toBe(0);
112
113 yield* stack.destroy();
114 }),
115 { tags, timeout: 900_000 },
116 );
117
118 // A `context` Task built with a build arg and a BuildKit secret. Its command
119 // exits 0 only when the build saw the arg and the mounted secret.
120 test.provider.skipIf(!!process.env.FAST)(
121 "a Dockerfile task builds with build args and BuildKit secrets",
122 (stack) =>
123 Effect.gen(function* () {
124 yield* stack.destroy();
125
126 const { clusterArn, taskDefinitionArn } = yield* stack.deploy(
127 Effect.gen(function* () {
128 const cluster = yield* Cluster("BuildSecretsCluster");
129 const task = yield* BuildSecretsTask;
130 return {
131 clusterArn: cluster.clusterArn,
132 taskDefinitionArn: task.taskDefinitionArn,
133 };
134 }),
135 );
136 expect(taskDefinitionArn).toBeTruthy();
137
138 const stopped = yield* runToCompletion(clusterArn, taskDefinitionArn!);
139 expect(stopped.containers?.[0]?.exitCode).toBe(0);
140
141 yield* stack.destroy();
142 }),
143 { tags, timeout: 900_000 },
144 );