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

main
ts 135 lines 5.28 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { JobTemplate } from "@alchemy.run/aws/EMRContainers";
3 import { Role } from "@alchemy.run/aws/IAM";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as emrc from "@distilled.cloud/aws/emr-containers";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 // Ungated typed-error probe: proves the tags the observe/read/delete paths
13 // depend on. describeJobTemplate on a well-formed nonexistent id must
14 // surface the typed ResourceNotFoundException; deleteJobTemplate on the
15 // same id must surface the typed ValidationException the idempotent delete
16 // path swallows (its typed union has no not-found tag).
17 test.provider(
18 "typed error semantics on a nonexistent job template",
19 () =>
20 Effect.gen(function* () {
21 const id = "abcdefabcdefabcdefabcdef01";
22
23 const describeError = yield* Effect.flip(emrc.describeJobTemplate({ id }));
24 expect(describeError._tag).toBe("ResourceNotFoundException");
25
26 const deleteError = yield* Effect.flip(emrc.deleteJobTemplate({ id }));
27 expect(deleteError._tag).toBe("ValidationException");
28 }),
29 { tags: ["provider:aws", "provider:aws:emrcontainers", "live"] },
30 );
31
32 // Ungated list() probe: proves the pagination + attribute mapping wiring.
33 test.provider(
34 "list returns a well-formed array of job templates",
35 () =>
36 Effect.gen(function* () {
37 const provider = yield* Provider.findProvider(JobTemplate);
38 const all = yield* provider.list();
39
40 expect(Array.isArray(all)).toBe(true);
41 for (const jt of all) {
42 expect(typeof jt.jobTemplateId).toBe("string");
43 expect(typeof jt.jobTemplateName).toBe("string");
44 expect(jt.jobTemplateArn).toContain(":/jobtemplates/");
45 }
46 }),
47 { tags: ["provider:aws", "provider:aws:emrcontainers", "live"] },
48 );
49
50 // Full lifecycle — job templates are account-level, free, and provision
51 // synchronously (no EKS cluster required), so this runs ungated.
52 test.provider(
53 "job template lifecycle: create, tag replace, data replace, destroy",
54 (stack) =>
55 Effect.gen(function* () {
56 // Clean slate in case a previous run died mid-flight.
57 yield* stack.destroy();
58
59 const deployTemplate = (releaseLabel: string, tags: Record<string, string>) =>
60 stack.deploy(
61 Effect.gen(function* () {
62 const role = yield* Role("JobRole", {
63 assumeRolePolicyDocument: {
64 Version: "2012-10-17",
65 Statement: [
66 {
67 Effect: "Allow",
68 Principal: { Service: "pods.eks.amazonaws.com" },
69 Action: ["sts:AssumeRole", "sts:TagSession"],
70 },
71 ],
72 },
73 });
74 return yield* JobTemplate("Template", {
75 jobTemplateData: {
76 executionRoleArn: role.roleArn,
77 releaseLabel,
78 jobDriver: {
79 sparkSubmitJobDriver: {
80 entryPoint: "s3://alchemy-test-emrc/scripts/etl.py",
81 sparkSubmitParameters: "--conf spark.executor.instances=1",
82 },
83 },
84 jobTags: { Origin: "alchemy-test" },
85 },
86 tags,
87 });
88 }),
89 );
90
91 // 1. CREATE
92 const first = yield* deployTemplate("emr-7.5.0-latest", {
93 Purpose: "alchemy-emrc-test",
94 });
95 expect(first.jobTemplateArn).toContain(":/jobtemplates/");
96
97 // Out-of-band verification via distilled.
98 const observed = yield* emrc.describeJobTemplate({
99 id: first.jobTemplateId,
100 });
101 expect(observed.jobTemplate?.jobTemplateData.releaseLabel).toBe("emr-7.5.0-latest");
102 expect(observed.jobTemplate?.tags?.Purpose).toBe("alchemy-emrc-test");
103
104 // 2. TAG CHANGE — replaces (EMR containers' TagResource rejects job
105 // template ARNs with the typed InvalidResourceArn, so tags are
106 // create-only and any tag change replaces the template)
107 const retagged = yield* deployTemplate("emr-7.5.0-latest", {
108 Purpose: "alchemy-emrc-test-updated",
109 });
110 expect(retagged.jobTemplateId).not.toBe(first.jobTemplateId);
111 const retaggedObserved = yield* emrc.describeJobTemplate({
112 id: retagged.jobTemplateId,
113 });
114 expect(retaggedObserved.jobTemplate?.tags?.Purpose).toBe("alchemy-emrc-test-updated");
115
116 // 3. REPLACE — jobTemplateData is immutable, changing it replaces
117 const replaced = yield* deployTemplate("emr-7.2.0-latest", {
118 Purpose: "alchemy-emrc-test-updated",
119 });
120 expect(replaced.jobTemplateId).not.toBe(retagged.jobTemplateId);
121
122 // The replaced (old) template is deleted by the engine.
123 const oldGone = yield* Effect.flip(emrc.describeJobTemplate({ id: retagged.jobTemplateId }));
124 expect(oldGone._tag).toBe("ResourceNotFoundException");
125
126 // 4. DESTROY
127 yield* stack.destroy();
128 const gone = yield* Effect.flip(emrc.describeJobTemplate({ id: replaced.jobTemplateId }));
129 expect(gone._tag).toBe("ResourceNotFoundException");
130 }),
131 {
132 tags: ["provider:aws", "provider:aws:emrcontainers", "provider:aws:iam", "live"],
133 timeout: 240_000,
134 },
135 );