@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 246 lines 9.14 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as lambda from "@distilled.cloud/aws/lambda";
6 import * as socialmessaging from "@distilled.cloud/aws/socialmessaging";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as Schedule from "effect/Schedule";
11 import SocialMessagingBindingsFunctionLive, {
12 SocialMessagingBindingsFunction,
13 } from "./bindings-handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17
18 // Every SocialMessaging binding operates on a WhatsApp Business Account
19 // that can only be linked through the Meta embedded-signup flow in the AWS
20 // console, so the live Lambda E2E is gated behind AWS_TEST_SOCIALMESSAGING=1
21 // + AWS_TEST_SOCIALMESSAGING_WABA_ID. The ungated probes below prove the
22 // distilled error unions the bindings surface are typed on every account.
23 const RUN_LIVE =
24 !!process.env.AWS_TEST_SOCIALMESSAGING && !!process.env.AWS_TEST_SOCIALMESSAGING_WABA_ID;
25
26 // Well-formed but nonexistent identifiers.
27 const BOGUS_WABA_ID = "waba-0123456789abcdef0123456789abcdef";
28 const BOGUS_PHONE_ID = "phone-number-id-0123456789abcdef0123456789abcdef";
29
30 test.provider(
31 "listWhatsAppMessageTemplates on a nonexistent WABA fails with a typed tag",
32 () =>
33 Effect.gen(function* () {
34 const error = yield* Effect.flip(
35 socialmessaging.listWhatsAppMessageTemplates({ id: BOGUS_WABA_ID }),
36 );
37 expect([
38 "ResourceNotFoundException",
39 "InvalidParametersException",
40 "AccessDeniedByMetaException",
41 "DependencyException",
42 ]).toContain(error._tag);
43 }),
44 { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] },
45 );
46
47 test.provider(
48 "listWhatsAppFlows on a nonexistent WABA fails with a typed tag",
49 () =>
50 Effect.gen(function* () {
51 const error = yield* Effect.flip(socialmessaging.listWhatsAppFlows({ id: BOGUS_WABA_ID }));
52 expect([
53 "ResourceNotFoundException",
54 "InvalidParametersException",
55 "AccessDeniedByMetaException",
56 "DependencyException",
57 ]).toContain(error._tag);
58 }),
59 { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] },
60 );
61
62 test.provider(
63 "getLinkedWhatsAppBusinessAccountPhoneNumber on a nonexistent phone number fails with a typed tag",
64 () =>
65 Effect.gen(function* () {
66 const error = yield* Effect.flip(
67 socialmessaging.getLinkedWhatsAppBusinessAccountPhoneNumber({ id: BOGUS_PHONE_ID }),
68 );
69 expect(["ResourceNotFoundException", "InvalidParametersException"]).toContain(error._tag);
70 }),
71 { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] },
72 );
73
74 test.provider(
75 "sendWhatsAppMessage from a nonexistent phone number fails with a typed tag",
76 () =>
77 Effect.gen(function* () {
78 const error = yield* Effect.flip(
79 socialmessaging.sendWhatsAppMessage({
80 originationPhoneNumberId: BOGUS_PHONE_ID,
81 metaApiVersion: "v20.0",
82 message: new TextEncoder().encode(
83 JSON.stringify({
84 messaging_product: "whatsapp",
85 to: "+10000000000",
86 type: "text",
87 text: { body: "alchemy-socialmessaging-probe" },
88 }),
89 ),
90 }),
91 );
92 expect([
93 "ResourceNotFoundException",
94 "InvalidParametersException",
95 "AccessDeniedByMetaException",
96 "DependencyException",
97 ]).toContain(error._tag);
98 }),
99 { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] },
100 );
101
102 test.provider(
103 "getWhatsAppMessageMedia on a nonexistent phone number fails with a typed tag",
104 () =>
105 Effect.gen(function* () {
106 const error = yield* Effect.flip(
107 socialmessaging.getWhatsAppMessageMedia({
108 mediaId: "alchemy-nonexistent-media-id",
109 originationPhoneNumberId: BOGUS_PHONE_ID,
110 metadataOnly: true,
111 }),
112 );
113 expect([
114 "ResourceNotFoundException",
115 "InvalidParametersException",
116 "AccessDeniedByMetaException",
117 "DependencyException",
118 // media ids are validated before the phone-number lookup
119 "ValidationException",
120 ]).toContain(error._tag);
121 }),
122 { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] },
123 );
124
125 const sharedStack = Core.scratchStack(testOptions, "SocialMessagingBindings");
126
127 let baseUrl: string;
128 let functionArn: string | undefined;
129
130 class FunctionStillExists extends Data.TaggedError("FunctionStillExists") {}
131
132 const get = (path: string) =>
133 HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
134 const post = (path: string) =>
135 HttpClient.post(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
136
137 // Live Lambda E2E — requires a WhatsApp Business Account already linked via
138 // the AWS End User Messaging Social console (Meta embedded signup). NOTE:
139 // the final stack.destroy() disassociates the WABA from the AWS account;
140 // re-running afterwards requires redoing the console signup.
141 describe(
142 "SocialMessaging Bindings (E2E)",
143 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:socialmessaging", "live"] },
144 () => {
145 beforeAll(
146 Effect.gen(function* () {
147 if (!RUN_LIVE) return;
148 yield* Effect.logInfo("SocialMessaging E2E setup: destroying previous");
149 yield* sharedStack.destroy();
150
151 yield* Effect.logInfo("SocialMessaging E2E setup: deploying Lambda");
152 const deployed = yield* sharedStack.deploy(
153 Effect.gen(function* () {
154 return yield* SocialMessagingBindingsFunction;
155 }).pipe(Effect.provide(SocialMessagingBindingsFunctionLive)),
156 );
157 functionArn = deployed.functionArn;
158
159 expect(deployed.functionUrl).toBeTruthy();
160 baseUrl = deployed.functionUrl!.replace(/\/+$/, "");
161
162 // Readiness probe — fresh function URLs take seconds to serve 200s.
163 yield* HttpClient.get(`${baseUrl}/bindings`).pipe(
164 Effect.flatMap((response) =>
165 response.status === 200
166 ? Effect.succeed(response)
167 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
168 ),
169 Effect.retry({
170 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]),
171 }),
172 );
173 }),
174 { timeout: 300_000 },
175 );
176 afterAll(
177 Effect.gen(function* () {
178 if (!RUN_LIVE) return;
179 yield* sharedStack.destroy();
180 // Assert gone (skipped when beforeAll never got far enough to deploy):
181 // the fixture Lambda answers with the typed not-found tag out-of-band.
182 // afterAll runs outside `test.provider`'s layer, so raw distilled calls
183 // need the provider layer (credentials, region) supplied explicitly.
184 if (functionArn) {
185 yield* Core.withProviders(
186 lambda.getFunction({ FunctionName: functionArn }).pipe(
187 Effect.flatMap(() => Effect.fail(new FunctionStillExists())),
188 Effect.retry({
189 while: (error) => error._tag === "FunctionStillExists",
190 schedule: Schedule.exponential("500 millis"),
191 times: 8,
192 }),
193 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
194 ),
195 testOptions,
196 sharedStack.name,
197 );
198 }
199 }),
200 { timeout: 300_000 },
201 );
202
203 test.provider.skipIf(!RUN_LIVE)("all 23 capabilities initialize in the runtime", () =>
204 Effect.gen(function* () {
205 const response = (yield* get("/bindings")) as any;
206 expect(response.bound).toHaveLength(23);
207 }),
208 );
209
210 test.provider.skipIf(!RUN_LIVE)(
211 "template + flow list bindings round-trip against the linked WABA",
212 () =>
213 Effect.gen(function* () {
214 const templates = (yield* get("/templates")) as any;
215 expect(typeof templates.count).toBe("number");
216 const flows = (yield* get("/flows")) as any;
217 expect(typeof flows.count).toBe("number");
218 const library = (yield* get("/library")) as any;
219 expect(typeof library.count).toBe("number");
220 }),
221 );
222
223 test.provider.skipIf(!RUN_LIVE)("read bindings surface typed errors on bogus identifiers", () =>
224 Effect.gen(function* () {
225 const phone = (yield* get("/phone/typed-not-found")) as any;
226 expect(phone.typed).toBe(true);
227 const template = (yield* get("/template/typed-not-found")) as any;
228 expect(template.typed).toBe(true);
229 const flow = (yield* get("/flow/typed-not-found")) as any;
230 expect(flow.typed).toBe(true);
231 }),
232 );
233
234 test.provider.skipIf(!RUN_LIVE)(
235 "message + media mutation bindings surface typed errors without side effects",
236 () =>
237 Effect.gen(function* () {
238 const media = (yield* post("/media/typed-not-found")) as any;
239 expect(media.get).toBe(true);
240 expect(media.del).toBe(true);
241 const send = (yield* post("/send/typed-invalid")) as any;
242 expect(send.typed).toBe(true);
243 }),
244 );
245 },
246 );