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

main
ts 121 lines 4.82 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { CredentialLocker } from "@alchemy.run/aws/IoTManagedIntegrations";
3 import { Region } from "@alchemy.run/aws/Region";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as mi from "@distilled.cloud/aws/iot-managed-integrations";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // IoT Managed Integrations is only deployed in a few regions (eu-west-1,
14 // ca-central-1, ...). The testing profile's default region (us-west-2) has no
15 // endpoint, so the ungated probe pins a supported region explicitly. The gated
16 // lifecycle below requires an AWS profile whose region IS a supported one
17 // (SSO profiles resolve region from ~/.aws/config; the AWS_REGION env var is
18 // not consulted), e.g. a profile with region = eu-west-1.
19 const MI_REGION = "eu-west-1";
20 const pin = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
21 effect.pipe(Effect.provideService(Region, Effect.succeed(MI_REGION)));
22
23 // Ungated typed-error probe: prove the distilled error union carries the
24 // not-found tag this provider's read/delete paths depend on. Runs in every
25 // CI pass at near-zero cost, unlike the gated lifecycle below.
26 test.provider(
27 "getCredentialLocker on a nonexistent locker fails with ResourceNotFoundException",
28 () =>
29 Effect.gen(function* () {
30 const error = yield* Effect.flip(
31 pin(
32 mi.getCredentialLocker({
33 Identifier: "alchemynonexistentlockerprobe",
34 }),
35 ),
36 );
37 expect(error._tag).toBe("ResourceNotFoundException");
38 }),
39 { tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"] },
40 );
41
42 class LockerStillExists extends Data.TaggedError("LockerStillExists")<{
43 readonly credentialLockerId: string;
44 }> {}
45
46 const assertLockerGone = (credentialLockerId: string) =>
47 mi.getCredentialLocker({ Identifier: credentialLockerId }).pipe(
48 Effect.flatMap(() => Effect.fail(new LockerStillExists({ credentialLockerId }))),
49 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
50 Effect.retry({
51 while: (e) => e._tag === "LockerStillExists",
52 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
53 }),
54 );
55
56 // Full lifecycle is gated: the service only exists in a few regions. Run with
57 // AWS_TEST_IOT_MI=1 and an AWS profile configured in a supported region.
58 test.provider.skipIf(!process.env.AWS_TEST_IOT_MI)(
59 "create credential locker, verify, update tags, destroy",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const { locker } = yield* stack.deploy(
65 Effect.gen(function* () {
66 const locker = yield* CredentialLocker("Locker", {
67 tags: { fixture: "iot-mi-credential-locker" },
68 });
69 return { locker };
70 }),
71 );
72
73 expect(locker.credentialLockerId).toBeDefined();
74 expect(locker.credentialLockerArn).toContain(":credential-locker/");
75 expect(locker.credentialLockerName).toBeDefined();
76 expect(locker.tags.fixture).toBe("iot-mi-credential-locker");
77
78 // Out-of-band verification via distilled.
79 const observed = yield* mi.getCredentialLocker({
80 Identifier: locker.credentialLockerId,
81 });
82 expect(observed.Arn).toBe(locker.credentialLockerArn);
83 expect(observed.Tags?.fixture).toBe("iot-mi-credential-locker");
84
85 // Update tags in place (no replacement — same name).
86 const { locker: updated } = yield* stack.deploy(
87 Effect.gen(function* () {
88 const locker = yield* CredentialLocker("Locker", {
89 tags: { fixture: "iot-mi-credential-locker", phase: "two" },
90 });
91 return { locker };
92 }),
93 );
94 expect(updated.credentialLockerId).toBe(locker.credentialLockerId);
95 const retagged = yield* mi.getCredentialLocker({
96 Identifier: locker.credentialLockerId,
97 });
98 expect(retagged.Tags?.phase).toBe("two");
99
100 // Replacement: an explicit name change replaces the locker.
101 const { locker: replaced } = yield* stack.deploy(
102 Effect.gen(function* () {
103 const locker = yield* CredentialLocker("Locker", {
104 name: "alchemy-iot-mi-locker-replacement",
105 tags: { fixture: "iot-mi-credential-locker" },
106 });
107 return { locker };
108 }),
109 );
110 expect(replaced.credentialLockerId).not.toBe(locker.credentialLockerId);
111 expect(replaced.credentialLockerName).toBe("alchemy-iot-mi-locker-replacement");
112 yield* assertLockerGone(locker.credentialLockerId);
113
114 yield* stack.destroy();
115 yield* assertLockerGone(replaced.credentialLockerId);
116 }),
117 {
118 tags: ["provider:aws", "provider:aws:iotmanagedintegrations", "live"],
119 timeout: 120_000,
120 },
121 );