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

main
ts 116 lines 4.85 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ContactInformation } from "@alchemy.run/aws/Account";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as account from "@distilled.cloud/aws/account";
6 import * as Effect from "effect/Effect";
7 import * as Redacted from "effect/Redacted";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const unwrap = (value: string | Redacted.Redacted<string> | undefined) =>
13 value === undefined ? undefined : typeof value === "string" ? value : Redacted.value(value);
14
15 const captureContact = () =>
16 account.getContactInformation({}).pipe(
17 Effect.map((r) => r.ContactInformation),
18 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
19 );
20
21 // getContactInformation is eventually consistent after putContactInformation,
22 // so out-of-band reads poll (bounded) until the expected company name shows.
23 const captureContactUntil = (expectedCompany: string | undefined) =>
24 captureContact().pipe(
25 Effect.repeat({
26 schedule: Schedule.spaced("3 seconds"),
27 until: (contact): boolean => unwrap(contact?.CompanyName) === expectedCompany,
28 times: 10,
29 }),
30 );
31
32 // AWS validates the phone number for pattern validity (e.g. NANP numbers
33 // with an invalid area code like 555, or an exchange starting with 1, are
34 // rejected with `ValidationException: The specified PhoneNumber is not
35 // valid`). Reuse the account's current primary-contact phone when one exists
36 // — it is guaranteed to pass — and fall back to a pattern-valid number.
37 const FALLBACK_PHONE = "+12025550100";
38
39 // The primary contact is an account-global singleton that cannot be deleted.
40 // This test captures whatever primary contact the account currently has, sets
41 // its own, updates it, and restores the original in `ensuring` so the account
42 // is left exactly as it was found.
43 describe.sequential(
44 "Account ContactInformation",
45 { tags: ["provider:aws", "provider:aws:account", "live"] },
46 () => {
47 test.provider(
48 "set, update, and restore the primary contact",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const original = yield* captureContact();
54 const testPhone = unwrap(original?.PhoneNumber) ?? FALLBACK_PHONE;
55
56 const restore = Effect.gen(function* () {
57 if (original) {
58 yield* account.putContactInformation({ ContactInformation: original });
59 }
60 });
61
62 yield* Effect.gen(function* () {
63 const deployContact = (companyName: string) =>
64 stack.deploy(
65 Effect.gen(function* () {
66 return yield* ContactInformation("PrimaryContact", {
67 fullName: "Alchemy Test",
68 addressLine1: "123 Any Street",
69 city: "Seattle",
70 stateOrRegion: "WA",
71 postalCode: "98101",
72 countryCode: "US",
73 phoneNumber: testPhone,
74 companyName,
75 });
76 }),
77 );
78
79 const contact = yield* deployContact("Alchemy");
80 expect(contact.fullName).toBe("Alchemy Test");
81 expect(contact.companyName).toBe("Alchemy");
82
83 // Out-of-band verification (bounded poll through put→get eventual
84 // consistency).
85 const live = yield* captureContactUntil("Alchemy");
86 expect(unwrap(live?.FullName)).toBe("Alchemy Test");
87 expect(unwrap(live?.CompanyName)).toBe("Alchemy");
88
89 // Update: the singleton is upserted in place.
90 const updated = yield* deployContact("Alchemy Updated");
91 expect(updated.companyName).toBe("Alchemy Updated");
92 const liveUpdated = yield* captureContactUntil("Alchemy Updated");
93 expect(unwrap(liveUpdated?.CompanyName)).toBe("Alchemy Updated");
94
95 // Destroy is a no-op — AWS does not allow deleting the primary
96 // contact, so the last value stays in place.
97 yield* stack.destroy();
98 const after = yield* captureContactUntil("Alchemy Updated");
99 expect(unwrap(after?.FullName)).toBe("Alchemy Test");
100 }).pipe(Effect.ensuring(restore.pipe(Effect.orDie)));
101
102 // Confirm restoration matches the captured original (bounded poll
103 // through put→get eventual consistency).
104 const restored = yield* captureContact().pipe(
105 Effect.repeat({
106 schedule: Schedule.spaced("3 seconds"),
107 until: (contact): boolean => unwrap(contact?.FullName) === unwrap(original?.FullName),
108 times: 10,
109 }),
110 );
111 expect(unwrap(restored?.FullName)).toBe(unwrap(original?.FullName));
112 }),
113 { timeout: 120_000 },
114 );
115 },
116 );