@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 171 lines 6.53 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Studio } from "@alchemy.run/aws/EMR/Studio";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as EC2 from "@distilled.cloud/aws/ec2";
6 import * as emr from "@distilled.cloud/aws/emr";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import { getDefaultVpc } from "../DefaultVpc.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Ungated typed-error probe: proves the distilled emr error union carries the
14 // StudioNotFound tag this provider's read/delete paths depend on (EMR
15 // overloads InvalidRequestException with "Studio does not exist.").
16 test.provider(
17 "describeStudio on a nonexistent id fails with StudioNotFound",
18 () =>
19 Effect.gen(function* () {
20 const error = yield* Effect.flip(
21 emr.describeStudio({ StudioId: "es-AAAAAAAAAAAAAAAAAAAAAAAAA" }),
22 );
23 expect(error._tag).toBe("StudioNotFound");
24 }),
25 { tags: ["provider:aws", "provider:aws:emr", "live"] },
26 );
27
28 // Resolve a subnet of the account's default VPC.
29 const resolveSubnets = Effect.gen(function* () {
30 const vpc = yield* getDefaultVpc;
31 const subnets = yield* EC2.describeSubnets({
32 Filters: [
33 { Name: "vpc-id", Values: [vpc.vpcId] },
34 { Name: "default-for-az", Values: ["true"] },
35 ],
36 });
37 const subnetIds = (subnets.Subnets ?? [])
38 .flatMap((s) => (s.SubnetId ? [s.SubnetId] : []))
39 .slice(0, 2);
40 return { vpcId: vpc.vpcId, subnetIds };
41 });
42
43 test.provider(
44 "lifecycle: create IAM studio, update in place, destroy",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 const { vpcId, subnetIds } = yield* resolveSubnets;
50 expect(subnetIds.length).toBeGreaterThan(0);
51
52 const deploy = (description: string, s3Suffix: string) =>
53 stack.deploy(
54 Effect.gen(function* () {
55 const bucket = yield* AWS.S3.Bucket("StudioBackup", { forceDestroy: true });
56 const role = yield* AWS.IAM.Role("StudioServiceRole", {
57 assumeRolePolicyDocument: {
58 Version: "2012-10-17",
59 Statement: [
60 {
61 Effect: "Allow",
62 Principal: { Service: ["elasticmapreduce.amazonaws.com"] },
63 Action: ["sts:AssumeRole"],
64 },
65 ],
66 },
67 inlinePolicies: {
68 "studio-service": {
69 Version: "2012-10-17",
70 Statement: [
71 {
72 // CreateStudio validates the service role can access
73 // the default S3 location (incl. GetEncryptionConfiguration).
74 Effect: "Allow",
75 Action: ["s3:*"],
76 Resource: [bucket.bucketArn, Output.interpolate`${bucket.bucketArn}/*`],
77 },
78 {
79 Effect: "Allow",
80 Action: [
81 "ec2:CreateNetworkInterface",
82 "ec2:CreateNetworkInterfacePermission",
83 "ec2:DeleteNetworkInterface",
84 "ec2:DeleteNetworkInterfacePermission",
85 "ec2:DescribeNetworkInterfaces",
86 "ec2:ModifyNetworkInterfaceAttribute",
87 "ec2:DescribeSecurityGroups",
88 "ec2:DescribeSubnets",
89 "ec2:DescribeVpcs",
90 "ec2:DescribeTags",
91 "ec2:CreateTags",
92 ],
93 Resource: ["*"],
94 },
95 ],
96 },
97 },
98 });
99 const workspaceSg = yield* AWS.EC2.SecurityGroup("WorkspaceSg", {
100 vpcId: vpcId,
101 description: "EMR Studio workspace",
102 });
103 const engineSg = yield* AWS.EC2.SecurityGroup("EngineSg", {
104 vpcId: vpcId,
105 description: "EMR Studio engine",
106 });
107 // Engine must accept TCP 18888 from the workspace security group.
108 yield* AWS.EC2.SecurityGroupRule("EngineIngress", {
109 groupId: engineSg.groupId,
110 type: "ingress",
111 ipProtocol: "tcp",
112 fromPort: 18888,
113 toPort: 18888,
114 referencedGroupId: workspaceSg.groupId,
115 });
116 const studio = yield* Studio("Studio", {
117 authMode: "IAM",
118 vpcId: vpcId,
119 subnetIds,
120 serviceRole: role.roleArn,
121 workspaceSecurityGroupId: workspaceSg.groupId,
122 engineSecurityGroupId: engineSg.groupId,
123 defaultS3Location: Output.interpolate`s3://${bucket.bucketName}/${s3Suffix}/`,
124 description,
125 tags: { fixture: "emr-studio" },
126 });
127 return { studio };
128 }),
129 );
130
131 // Create.
132 const { studio } = yield* deploy("alchemy test studio", "studio");
133 expect(studio.studioId).toMatch(/^es-/);
134 expect(studio.studioArn).toContain(":studio/");
135 expect(studio.url).toBeDefined();
136
137 // Out-of-band verification via distilled.
138 const created = yield* emr.describeStudio({ StudioId: studio.studioId });
139 expect(created.Studio?.AuthMode).toBe("IAM");
140 expect(created.Studio?.Description).toBe("alchemy test studio");
141 expect(created.Studio?.DefaultS3Location).toContain("/studio/");
142 const createdTags = Object.fromEntries(
143 (created.Studio?.Tags ?? []).map((t) => [t.Key, t.Value]),
144 );
145 expect(createdTags.fixture).toBe("emr-studio");
146
147 // Update — description and default S3 location sync in place (same
148 // StudioId, no replacement).
149 const { studio: updated } = yield* deploy("alchemy test studio v2", "studio-v2");
150 expect(updated.studioId).toBe(studio.studioId);
151 const afterUpdate = yield* emr.describeStudio({ StudioId: studio.studioId });
152 expect(afterUpdate.Studio?.Description).toBe("alchemy test studio v2");
153 expect(afterUpdate.Studio?.DefaultS3Location).toContain("/studio-v2/");
154
155 // Destroy — verify gone out of band.
156 yield* stack.destroy();
157 const afterDestroy = yield* Effect.flip(emr.describeStudio({ StudioId: studio.studioId }));
158 expect(afterDestroy._tag).toBe("StudioNotFound");
159 }),
160 {
161 tags: [
162 "provider:aws",
163 "provider:aws:ec2",
164 "provider:aws:emr",
165 "provider:aws:iam",
166 "provider:aws:s3",
167 "live",
168 ],
169 timeout: 300_000,
170 },
171 );