@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 154 lines 5.41 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Contact, ContactList } from "@alchemy.run/aws/SES";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as sesv2 from "@distilled.cloud/aws/sesv2";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 class ContactStillExists extends Data.TaggedError("ContactStillExists")<{
13 readonly contactListName: string;
14 readonly emailAddress: string;
15 }> {}
16
17 const assertContactDeleted = (contactListName: string, emailAddress: string) =>
18 sesv2
19 .getContact({
20 ContactListName: contactListName,
21 EmailAddress: emailAddress,
22 })
23 .pipe(
24 Effect.flatMap(() => Effect.fail(new ContactStillExists({ contactListName, emailAddress }))),
25 // A deleted contact list takes its contacts with it.
26 Effect.catchTag("NotFoundException", () => Effect.void),
27 Effect.retry({
28 while: (e) => e._tag === "ContactStillExists",
29 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
30 }),
31 );
32
33 test.provider(
34 "contact lifecycle: create with preferences, update, delete",
35 (stack) =>
36 Effect.gen(function* () {
37 yield* stack.destroy();
38
39 const { list, contact } = yield* stack.deploy(
40 Effect.gen(function* () {
41 const list = yield* ContactList("ContactHostList", {
42 topics: [
43 {
44 TopicName: "product-updates",
45 DisplayName: "Product Updates",
46 DefaultSubscriptionStatus: "OPT_OUT",
47 },
48 ],
49 });
50 const contact = yield* Contact("Subscriber", {
51 contactListName: list.contactListName,
52 emailAddress: "reader@alchemy-test.example.com",
53 topicPreferences: [{ TopicName: "product-updates", SubscriptionStatus: "OPT_IN" }],
54 attributes: { plan: "free" },
55 });
56 return { list, contact };
57 }),
58 );
59
60 expect(contact.contactListName).toBe(list.contactListName);
61 expect(contact.emailAddress).toBe("reader@alchemy-test.example.com");
62
63 // out-of-band verification via distilled
64 const observed = yield* sesv2.getContact({
65 ContactListName: list.contactListName,
66 EmailAddress: "reader@alchemy-test.example.com",
67 });
68 expect(observed.TopicPreferences?.[0]?.SubscriptionStatus).toBe("OPT_IN");
69 expect(observed.AttributesData).toContain("free");
70
71 // update preferences and unsubscribe-all in place
72 yield* stack.deploy(
73 Effect.gen(function* () {
74 const list = yield* ContactList("ContactHostList", {
75 topics: [
76 {
77 TopicName: "product-updates",
78 DisplayName: "Product Updates",
79 DefaultSubscriptionStatus: "OPT_OUT",
80 },
81 ],
82 });
83 yield* Contact("Subscriber", {
84 contactListName: list.contactListName,
85 emailAddress: "reader@alchemy-test.example.com",
86 topicPreferences: [{ TopicName: "product-updates", SubscriptionStatus: "OPT_OUT" }],
87 unsubscribeAll: true,
88 });
89 }),
90 );
91 const updated = yield* sesv2.getContact({
92 ContactListName: list.contactListName,
93 EmailAddress: "reader@alchemy-test.example.com",
94 });
95 expect(updated.UnsubscribeAll).toBe(true);
96
97 yield* stack.destroy();
98 yield* assertContactDeleted(list.contactListName, "reader@alchemy-test.example.com");
99 }),
100 {
101 tags: ["provider:aws", "provider:aws:ses", "live"],
102 timeout: 120_000,
103 exclusive: "aws:ses-account",
104 },
105 );
106
107 test.provider(
108 "changing the email address replaces the contact within the same list",
109 (stack) =>
110 Effect.gen(function* () {
111 yield* stack.destroy();
112
113 // The list stays deployed across the replacement so the engine never
114 // removes a dependency of the resource being replaced.
115 const first = yield* stack.deploy(
116 Effect.gen(function* () {
117 const list = yield* ContactList("EmailChangeList", {});
118 const contact = yield* Contact("Subscriber", {
119 contactListName: list.contactListName,
120 emailAddress: "first@alchemy-test.example.com",
121 });
122 return {
123 contactListName: list.contactListName,
124 emailAddress: contact.emailAddress,
125 };
126 }),
127 );
128 expect(first.emailAddress).toBe("first@alchemy-test.example.com");
129
130 const second = yield* stack.deploy(
131 Effect.gen(function* () {
132 const list = yield* ContactList("EmailChangeList", {});
133 const contact = yield* Contact("Subscriber", {
134 contactListName: list.contactListName,
135 emailAddress: "second@alchemy-test.example.com",
136 });
137 return {
138 contactListName: list.contactListName,
139 emailAddress: contact.emailAddress,
140 };
141 }),
142 );
143 expect(second.emailAddress).toBe("second@alchemy-test.example.com");
144 yield* assertContactDeleted(first.contactListName, "first@alchemy-test.example.com");
145
146 yield* stack.destroy();
147 yield* assertContactDeleted(second.contactListName, "second@alchemy-test.example.com");
148 }),
149 {
150 tags: ["provider:aws", "provider:aws:ses", "live"],
151 timeout: 120_000,
152 exclusive: "aws:ses-account",
153 },
154 );