@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 123 lines 4.95 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 { materializeIsolatedProject, removeIsolatedProject } from "../../IsolatedProject.ts";
9 import { getDefaultVpcNetwork } from "../DefaultVpc.ts";
10 import IsolatedProjectTaskLive, {
11 IsolatedProjectTask,
12 project,
13 } from "./fixtures/isolated-project-task.ts";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 // Live proof that the generated bun bootstrap boots when the task's `main`
18 // lives in a project that cannot resolve alchemy's dependencies itself — a
19 // consumer installed with bun's isolated linker (or pnpm), where
20 // `@distilled.cloud/aws`, `@effect/platform-bun`, … sit beside `alchemy`
21 // rather than in the project's own `node_modules`.
22 //
23 // The fixture's `main` points at an isolated project OUTSIDE the repository
24 // (see test/IsolatedProject.ts), so the bundle `cwd` resolves nothing bare.
25 // We deploy it (Docker build + ECR push), launch it once on Fargate, and
26 // assert the task runs to completion with container exit code 0. With the
27 // bootstrap's imports left external the container dies at boot with
28 // `Cannot find module …` (exit code 1).
29 //
30 // Docker + ECR + Fargate placement is ~1 minute of wall clock, so it is
31 // skipped under --fast like the Task e2e smoke test.
32 test.provider.skipIf(!!process.env.FAST)(
33 "task bundled from an isolated project boots and exits 0",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* materializeIsolatedProject(project);
37 yield* stack.destroy();
38
39 try {
40 // Reuse the standing default VPC: a public subnet + public IP is
41 // required for the task ENI to pull the image from ECR.
42 const { subnetIds } = yield* getDefaultVpcNetwork;
43 const subnetId = subnetIds[0];
44 expect(subnetId).toBeTruthy();
45
46 const { clusterArn, taskDefinitionArn } = yield* stack.deploy(
47 Effect.gen(function* () {
48 const cluster = yield* Cluster("IsolatedProjectCluster", {
49 clusterName: "alchemy-test-isolated-project",
50 });
51 const task = yield* IsolatedProjectTask;
52 return {
53 clusterArn: cluster.clusterArn,
54 taskDefinitionArn: task.taskDefinitionArn,
55 };
56 }).pipe(Effect.provide(IsolatedProjectTaskLive)),
57 );
58
59 expect(taskDefinitionArn).toBeTruthy();
60
61 // Launch the one-shot task once, out-of-band. The task/execution
62 // roles were created seconds ago: ECS rejects `runTask` with
63 // "unable to assume the role" until IAM propagates, so retry that
64 // one typed error (bounded).
65 const started = yield* ecs
66 .runTask({
67 cluster: clusterArn,
68 taskDefinition: taskDefinitionArn!,
69 launchType: "FARGATE",
70 count: 1,
71 startedBy: "alchemy-isolated-project-test",
72 networkConfiguration: {
73 awsvpcConfiguration: {
74 subnets: [subnetId!],
75 assignPublicIp: "ENABLED",
76 },
77 },
78 })
79 .pipe(
80 Effect.retry({
81 while: (e) =>
82 e._tag === "ClientException" &&
83 (e.message ?? "").includes("unable to assume the role"),
84 schedule: Schedule.spaced("5 seconds"),
85 times: 12,
86 }),
87 );
88 expect(started.failures ?? []).toEqual([]);
89 const taskArn = started.tasks?.[0]?.taskArn;
90 expect(taskArn).toBeTruthy();
91
92 // Wait for the task to stop: image pull + container boot + the
93 // one-shot program running to completion (~1-3 minutes cold).
94 const stopped = yield* ecs.describeTasks({ cluster: clusterArn, tasks: [taskArn!] }).pipe(
95 Effect.flatMap((result) => {
96 const task = result.tasks?.[0];
97 return task?.lastStatus === "STOPPED"
98 ? Effect.succeed(task)
99 : Effect.fail(new Error(`task not stopped yet: ${task?.lastStatus}`));
100 }),
101 Effect.tapError((error) => Effect.logInfo(String(error))),
102 Effect.retry({
103 schedule: Schedule.spaced("6 seconds"),
104 times: 50,
105 }),
106 );
107
108 // Exit code 0 proves the bootstrap's own imports were bundled and the
109 // program ran `{ run }` to completion; externalised imports crash bun
110 // at module load and the container exits 1.
111 expect(stopped.stoppedReason ?? "").not.toContain("CannotPullContainer");
112 expect(stopped.containers?.[0]?.exitCode).toBe(0);
113
114 yield* stack.destroy();
115 } finally {
116 yield* removeIsolatedProject(project);
117 }
118 }),
119 {
120 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:ecs", "live"],
121 timeout: 900_000,
122 },
123 );