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

main
ts 216 lines 8.35 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { LicenseConfiguration } from "@alchemy.run/aws/LicenseManager";
3 import type { LicenseConfigurationProps } from "@alchemy.run/aws/LicenseManager/LicenseConfiguration";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as iam from "@distilled.cloud/aws/iam";
7 import * as licensemanager from "@distilled.cloud/aws/license-manager";
8 import * as sts from "@distilled.cloud/aws/sts";
9 import * as Provider from "alchemy/Provider";
10 import * as Effect from "effect/Effect";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // License Manager requires one-time account onboarding: without its
16 // service-linked role EVERY operation fails with AccessDeniedException
17 // ("Service role not found..."). Creating the SLR is idempotent — an
18 // already-onboarded account returns the typed InvalidInputException
19 // ("has been taken in this account"). A freshly created SLR takes a few
20 // seconds of IAM propagation before License Manager sees it, so probe
21 // with a bounded typed retry on AccessDeniedException.
22 const ensureOnboarded = Effect.gen(function* () {
23 yield* iam
24 .createServiceLinkedRole({
25 AWSServiceName: "license-manager.amazonaws.com",
26 })
27 .pipe(Effect.catchTag("InvalidInputException", () => Effect.void));
28 yield* licensemanager.listLicenseConfigurations({ MaxResults: 1 }).pipe(
29 Effect.retry({
30 while: (e) => e._tag === "AccessDeniedException",
31 schedule: Schedule.exponential("1 second"),
32 times: 8,
33 }),
34 );
35 });
36
37 // Ungated typed-error probe: prove the distilled error union carries the
38 // not-found tag this provider's read/delete paths depend on.
39 test.provider(
40 "getLicenseConfiguration on a bogus ARN fails with LicenseConfigurationNotFound",
41 () =>
42 Effect.gen(function* () {
43 yield* ensureOnboarded;
44 const region = yield* yield* AWS.Region;
45 const { Account } = yield* sts.getCallerIdentity({});
46 const error = yield* Effect.flip(
47 licensemanager.getLicenseConfiguration({
48 LicenseConfigurationArn: `arn:aws:license-manager:${region}:${Account}:license-configuration:lic-00000000000000000000000000000000`,
49 }),
50 );
51 expect(error._tag).toBe("LicenseConfigurationNotFound");
52 }),
53 {
54 tags: ["provider:aws", "provider:aws:iam", "provider:aws:licensemanager", "live"],
55 timeout: 60_000,
56 },
57 );
58
59 // CreateLicenseConfiguration has a small DAILY account quota (~10 creates;
60 // deletes do not refund it). Keep create-based lifecycle coverage explicit so
61 // aggregate sweeps never exhaust the shared account for the rest of the day.
62 // The typed not-found probe and provider-diff assertion remain unconditional.
63 const RUN_CREATE_LIFECYCLE = process.env.AWS_TEST_LICENSE_MANAGER_CREATE === "1";
64
65 const callDiff = (olds: LicenseConfigurationProps, news: LicenseConfigurationProps) =>
66 Effect.gen(function* () {
67 const provider = yield* Provider.findProvider(LicenseConfiguration);
68 return yield* provider.diff!({
69 id: "Licenses",
70 fqn: "Licenses",
71 instanceId: "licenses",
72 olds,
73 news,
74 oldBindings: undefined as never,
75 newBindings: undefined as never,
76 output: undefined,
77 });
78 });
79 const isLive = (status: string | undefined) => status !== "DELETED";
80
81 // Out-of-band read via distilled; DELETED (soft-deleted) counts as gone.
82 const getLive = (arn: string) =>
83 licensemanager.getLicenseConfiguration({ LicenseConfigurationArn: arn }).pipe(
84 Effect.map((r) => (isLive(r.Status) ? r : undefined)),
85 Effect.catchTag("LicenseConfigurationNotFound", () => Effect.succeed(undefined)),
86 );
87
88 test.provider.skipIf(!RUN_CREATE_LIFECYCLE)(
89 "create, update, and delete a license configuration",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* ensureOnboarded;
93 yield* stack.destroy();
94
95 // Create — vCPU counting, soft count.
96 const { licenses } = yield* stack.deploy(
97 Effect.gen(function* () {
98 const licenses = yield* LicenseConfiguration("Licenses", {
99 licenseCountingType: "vCPU",
100 licenseCount: 10,
101 description: "alchemy license-manager test",
102 tags: { fixture: "license-configuration" },
103 });
104 return { licenses };
105 }),
106 );
107
108 expect(licenses.licenseConfigurationId).toMatch(/^lic-/);
109 expect(licenses.licenseConfigurationArn).toContain(":license-configuration:");
110 expect(licenses.licenseCountingType).toBe("vCPU");
111
112 // Out-of-band verification via distilled.
113 const observed = yield* licensemanager.getLicenseConfiguration({
114 LicenseConfigurationArn: licenses.licenseConfigurationArn,
115 });
116 expect(observed.Name).toBe(licenses.name);
117 expect(observed.LicenseCountingType).toBe("vCPU");
118 expect(observed.LicenseCount).toBe(10);
119 expect(observed.LicenseCountHardLimit).toBe(false);
120 const tags = Object.fromEntries((observed.Tags ?? []).map((t) => [t.Key, t.Value]));
121 expect(tags.fixture).toBe("license-configuration");
122 expect(tags["alchemy::id"]).toBe("Licenses");
123
124 // Update in place — count, hard limit, description, tags.
125 const { licenses: updated } = yield* stack.deploy(
126 Effect.gen(function* () {
127 const licenses = yield* LicenseConfiguration("Licenses", {
128 licenseCountingType: "vCPU",
129 licenseCount: 20,
130 licenseCountHardLimit: true,
131 description: "alchemy license-manager test (updated)",
132 tags: { fixture: "license-configuration", phase: "two" },
133 });
134 return { licenses };
135 }),
136 );
137
138 // Same physical resource — update, not replace.
139 expect(updated.licenseConfigurationArn).toBe(licenses.licenseConfigurationArn);
140 const afterUpdate = yield* licensemanager.getLicenseConfiguration({
141 LicenseConfigurationArn: licenses.licenseConfigurationArn,
142 });
143 expect(afterUpdate.LicenseCount).toBe(20);
144 expect(afterUpdate.LicenseCountHardLimit).toBe(true);
145 expect(afterUpdate.Description).toBe("alchemy license-manager test (updated)");
146 const updatedTags = Object.fromEntries((afterUpdate.Tags ?? []).map((t) => [t.Key, t.Value]));
147 expect(updatedTags.phase).toBe("two");
148
149 // Destroy and verify deletion out-of-band (soft-delete counts).
150 yield* stack.destroy();
151 const gone = yield* getLive(licenses.licenseConfigurationArn);
152 expect(gone).toBeUndefined();
153 }),
154 {
155 tags: ["provider:aws", "provider:aws:iam", "provider:aws:licensemanager", "live"],
156 timeout: 120_000,
157 },
158 );
159
160 test.provider(
161 "diff: changing licenseCountingType forces replacement",
162 () =>
163 Effect.gen(function* () {
164 const result = yield* callDiff(
165 { licenseCountingType: "Instance", licenseCount: 2 },
166 { licenseCountingType: "Core", licenseCount: 2 },
167 );
168 expect(result).toEqual({ action: "replace" });
169 }),
170 { tags: ["provider:aws", "provider:aws:licensemanager", "live"] },
171 );
172
173 test.provider.skipIf(!RUN_CREATE_LIFECYCLE)(
174 "changing licenseCountingType replaces the license configuration",
175 (stack) =>
176 Effect.gen(function* () {
177 yield* ensureOnboarded;
178 yield* stack.destroy();
179
180 const { licenses: first } = yield* stack.deploy(
181 Effect.gen(function* () {
182 const licenses = yield* LicenseConfiguration("Replaceable", {
183 licenseCountingType: "Instance",
184 licenseCount: 2,
185 });
186 return { licenses };
187 }),
188 );
189 expect(first.licenseCountingType).toBe("Instance");
190
191 const { licenses: second } = yield* stack.deploy(
192 Effect.gen(function* () {
193 const licenses = yield* LicenseConfiguration("Replaceable", {
194 licenseCountingType: "Core",
195 licenseCount: 2,
196 });
197 return { licenses };
198 }),
199 );
200
201 expect(second.licenseCountingType).toBe("Core");
202 expect(second.licenseConfigurationArn).not.toBe(first.licenseConfigurationArn);
203
204 // The replaced (old) configuration is deleted.
205 const old = yield* getLive(first.licenseConfigurationArn);
206 expect(old).toBeUndefined();
207
208 yield* stack.destroy();
209 const gone = yield* getLive(second.licenseConfigurationArn);
210 expect(gone).toBeUndefined();
211 }),
212 {
213 tags: ["provider:aws", "provider:aws:iam", "provider:aws:licensemanager", "live"],
214 timeout: 120_000,
215 },
216 );