@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 263 lines 10.8 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { Contact } from "@alchemy.run/aws/SSMContacts/Contact";
4 import { ContactChannel } from "@alchemy.run/aws/SSMContacts/ContactChannel";
5 import { Plan } from "@alchemy.run/aws/SSMContacts/Plan";
6 import { Rotation } from "@alchemy.run/aws/SSMContacts/Rotation";
7 import { expect } from "@alchemy.run/test";
8 import * as Test from "@alchemy.run/test/Alchemy";
9 import * as contacts from "@distilled.cloud/aws/ssm-contacts";
10 import * as incidents from "@distilled.cloud/aws/ssm-incidents";
11 import * as Effect from "effect/Effect";
12 import * as Result from "effect/Result";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 // Ungated typed-error probe: prove the distilled error union carries the
18 // tags this provider's read/delete paths depend on. On an account that has
19 // not onboarded Incident Manager, every ssm-contacts call fails with the
20 // synthetic `IncidentManagerNotOnboarded` tag (patched from the overloaded
21 // ValidationException "Account not found for the request"); once onboarded,
22 // a missing contact is a plain `ResourceNotFoundException`.
23 test.provider(
24 "getContact on a nonexistent contact fails with a typed tag",
25 () =>
26 Effect.gen(function* () {
27 const { accountId, region } = yield* AWSEnvironment.current;
28 const error = yield* Effect.flip(
29 contacts.getContact({
30 ContactId: `arn:aws:ssm-contacts:${region}:${accountId}:contact/alchemy-nonexistent-probe`,
31 }),
32 );
33 expect(["ResourceNotFoundException", "IncidentManagerNotOnboarded"]).toContain(error._tag);
34 }),
35 { tags: ["provider:aws", "provider:aws:ssmcontacts", "live"] },
36 );
37
38 // Contacts require the Incident Manager replication set (the account
39 // singleton that AWS.SSMIncidents.ReplicationSet manages). Ensure it exists
40 // and is ACTIVE; if we create it here we intentionally LEAVE it in place —
41 // offboarding Incident Manager account-wide is only exercised by the gated
42 // ReplicationSet lifecycle test. Returns the replication set ARN, or
43 // `undefined` when the account cannot be onboarded: AWS deprecated
44 // CreateReplicationSet on Nov 7, 2025, so only accounts that were already
45 // onboarded can exercise the contacts lifecycle.
46 const ensureReplicationSet = Effect.gen(function* () {
47 const listed = yield* incidents.listReplicationSets({});
48 let arn = listed.replicationSetArns[0];
49 if (arn === undefined) {
50 const { region } = yield* AWSEnvironment.current;
51 const created = yield* Effect.result(
52 incidents.createReplicationSet({ regions: { [region]: {} } }),
53 );
54 if (Result.isFailure(created)) {
55 expect(created.failure._tag).toBe("UnsupportedOperationException");
56 return undefined;
57 }
58 arn = created.success.arn;
59 }
60 const status = yield* incidents.getReplicationSet({ arn }).pipe(
61 Effect.map((r) => r.replicationSet.status),
62 Effect.repeat({
63 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(60)]),
64 until: (status) => status !== "CREATING" && status !== "UPDATING",
65 }),
66 );
67 expect(status).toBe("ACTIVE");
68 return arn;
69 });
70
71 // Contact / ContactChannel / Plan / Rotation are cheap once the replication
72 // set exists, but they depend on that account-wide singleton — gate the whole
73 // lifecycle behind AWS_TEST_INCIDENT_MANAGER=1.
74 test.provider.skipIf(!process.env.AWS_TEST_INCIDENT_MANAGER)(
75 "lifecycle: contact + channel + engagement plan + rotation",
76 (stack) =>
77 Effect.gen(function* () {
78 const replicationSetArn = yield* ensureReplicationSet;
79 if (replicationSetArn === undefined) {
80 yield* Effect.logInfo(
81 "CreateReplicationSet is deprecated (Nov 7, 2025) and this account is not onboarded to Incident Manager — skipping contacts lifecycle",
82 );
83 return;
84 }
85 yield* stack.destroy();
86
87 // Create — a personal contact with a deferred-activation email channel,
88 // a staged engagement plan targeting that channel, and a daily rotation.
89 const deployed = yield* stack.deploy(
90 Effect.gen(function* () {
91 const oncall = yield* Contact("Oncall", {
92 type: "PERSONAL",
93 displayName: "Primary On-Call",
94 tags: { fixture: "ssm-contacts" },
95 });
96 const email = yield* ContactChannel("Email", {
97 contactId: oncall.contactArn,
98 type: "EMAIL",
99 deliveryAddress: { SimpleAddress: "oncall@example.com" },
100 deferActivation: true,
101 });
102 const plan = yield* Plan("OncallPlan", {
103 contactId: oncall.contactArn,
104 stages: [
105 {
106 DurationInMinutes: 5,
107 Targets: [
108 {
109 ChannelTargetInfo: {
110 ContactChannelId: email.contactChannelArn,
111 RetryIntervalInMinutes: 1,
112 },
113 },
114 ],
115 },
116 ],
117 });
118 const rotation = yield* Rotation("Primary", {
119 contactIds: [oncall.contactArn],
120 timeZoneId: "America/Los_Angeles",
121 startTime: "2030-01-01T00:00:00Z",
122 recurrence: {
123 NumberOfOnCalls: 1,
124 RecurrenceMultiplier: 1,
125 DailySettings: [{ HourOfDay: 9, MinuteOfHour: 0 }],
126 },
127 tags: { fixture: "ssm-contacts" },
128 });
129 return { oncall, email, plan, rotation };
130 }),
131 );
132
133 expect(deployed.oncall.contactArn).toContain(":contact/");
134 expect(deployed.oncall.type).toBe("PERSONAL");
135 expect(deployed.email.contactChannelArn).toContain(":contact-channel/");
136 expect(deployed.email.activationStatus).toBe("NOT_ACTIVATED");
137 expect(deployed.plan.stageCount).toBe(1);
138 expect(deployed.rotation.rotationArn).toContain(":rotation/");
139
140 // Out-of-band verification via distilled.
141 const liveContact = yield* contacts.getContact({ ContactId: deployed.oncall.contactArn });
142 expect(liveContact.Alias).toBe(deployed.oncall.alias);
143 expect(liveContact.DisplayName).toBe("Primary On-Call");
144 expect(liveContact.Plan.Stages).toHaveLength(1);
145 const contactTags = yield* contacts.listTagsForResource({
146 ResourceARN: deployed.oncall.contactArn,
147 });
148 expect(contactTags.Tags?.some((t) => t.Key === "alchemy::id" && t.Value === "Oncall")).toBe(
149 true,
150 );
151
152 const liveChannel = yield* contacts.getContactChannel({
153 ContactChannelId: deployed.email.contactChannelArn,
154 });
155 expect(liveChannel.Type).toBe("EMAIL");
156 expect(liveChannel.DeliveryAddress.SimpleAddress).toBe("oncall@example.com");
157
158 const liveRotation = yield* contacts.getRotation({
159 RotationId: deployed.rotation.rotationArn,
160 });
161 expect(liveRotation.TimeZoneId).toBe("America/Los_Angeles");
162 expect(liveRotation.Recurrence.DailySettings?.[0]?.HourOfDay).toBe(9);
163
164 // Update — display name, channel address, plan stage duration, and
165 // rotation hand-off time all update in place; a resource policy is
166 // attached (put/getContactPolicy sync).
167 const { accountId } = yield* AWSEnvironment.current;
168 const sharePolicy = {
169 Version: "2012-10-17",
170 Statement: [
171 {
172 Effect: "Allow",
173 Principal: { AWS: accountId },
174 Action: ["ssm-contacts:GetContact"],
175 Resource: [
176 deployed.oncall.contactArn,
177 `${deployed.oncall.contactArn.replace(":contact/", ":engagement/")}/*`,
178 ],
179 },
180 ],
181 };
182 yield* stack.deploy(
183 Effect.gen(function* () {
184 const oncall = yield* Contact("Oncall", {
185 type: "PERSONAL",
186 displayName: "Secondary On-Call",
187 policy: sharePolicy,
188 tags: { fixture: "ssm-contacts", env: "test" },
189 });
190 const email = yield* ContactChannel("Email", {
191 contactId: oncall.contactArn,
192 type: "EMAIL",
193 deliveryAddress: { SimpleAddress: "standby@example.com" },
194 deferActivation: true,
195 });
196 const plan = yield* Plan("OncallPlan", {
197 contactId: oncall.contactArn,
198 stages: [
199 {
200 DurationInMinutes: 10,
201 Targets: [
202 {
203 ChannelTargetInfo: {
204 ContactChannelId: email.contactChannelArn,
205 RetryIntervalInMinutes: 2,
206 },
207 },
208 ],
209 },
210 ],
211 });
212 const rotation = yield* Rotation("Primary", {
213 contactIds: [oncall.contactArn],
214 timeZoneId: "America/Los_Angeles",
215 startTime: "2030-01-01T00:00:00Z",
216 recurrence: {
217 NumberOfOnCalls: 1,
218 RecurrenceMultiplier: 1,
219 DailySettings: [{ HourOfDay: 10, MinuteOfHour: 30 }],
220 },
221 tags: { fixture: "ssm-contacts" },
222 });
223 return { oncall, email, plan, rotation };
224 }),
225 );
226
227 const updatedContact = yield* contacts.getContact({ ContactId: deployed.oncall.contactArn });
228 expect(updatedContact.DisplayName).toBe("Secondary On-Call");
229 expect(updatedContact.Plan.Stages?.[0]?.DurationInMinutes).toBe(10);
230 const updatedChannel = yield* contacts.getContactChannel({
231 ContactChannelId: deployed.email.contactChannelArn,
232 });
233 expect(updatedChannel.DeliveryAddress.SimpleAddress).toBe("standby@example.com");
234 const updatedRotation = yield* contacts.getRotation({
235 RotationId: deployed.rotation.rotationArn,
236 });
237 expect(updatedRotation.Recurrence.DailySettings?.[0]?.HourOfDay).toBe(10);
238 const updatedTags = yield* contacts.listTagsForResource({
239 ResourceARN: deployed.oncall.contactArn,
240 });
241 expect(updatedTags.Tags?.some((t) => t.Key === "env" && t.Value === "test")).toBe(true);
242 const updatedPolicy = yield* contacts.getContactPolicy({
243 ContactArn: deployed.oncall.contactArn,
244 });
245 expect(updatedPolicy.Policy).toContain("ssm-contacts:GetContact");
246
247 // Destroy — everything but the account-singleton replication set.
248 yield* stack.destroy();
249 const contactError = yield* Effect.flip(
250 contacts.getContact({ ContactId: deployed.oncall.contactArn }),
251 );
252 expect(contactError._tag).toBe("ResourceNotFoundException");
253 const rotationError = yield* Effect.flip(
254 contacts.getRotation({ RotationId: deployed.rotation.rotationArn }),
255 );
256 expect(rotationError._tag).toBe("ResourceNotFoundException");
257 }),
258 // ensureReplicationSet may onboard Incident Manager (~1-2 min) on first run.
259 {
260 tags: ["provider:aws", "provider:aws:ssmcontacts", "provider:aws:ssmincidents", "live"],
261 timeout: 600_000,
262 },
263 );