@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 183 lines 6.41 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 Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import SmsVoicePhoneTestFunctionLive, {
11 SmsVoicePhoneTestFunction,
12 } from "./fixtures/phone-handler.ts";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "SmsVoicePhoneBindings");
17
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 const post = (path: string) =>
28 HttpClient.execute(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(
29 Effect.flatMap((response) =>
30 response.status >= 500
31 ? response.text.pipe(
32 Effect.flatMap((body) =>
33 Effect.fail(new TransientUpstream({ status: response.status, body })),
34 ),
35 )
36 : Effect.succeed(response),
37 ),
38 Effect.retry({
39 while: (e) => e._tag === "TransientUpstream",
40 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
41 }),
42 );
43
44 // Leasing even a SIMULATOR number incurs a (small) monthly fee and
45 // consumes account quota, so this fixture is gated behind
46 // AWS_TEST_PINPOINT_SMS=1 like the PhoneNumber lifecycle test. The suite
47 // skips clean without the flag.
48 describe.skipIf(!process.env.AWS_TEST_PINPOINT_SMS)(
49 "PinpointSMSVoiceV2 PhoneNumber Bindings",
50 {
51 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:pinpointsmsvoicev2", "live"],
52 },
53 () => {
54 beforeAll(
55 Effect.gen(function* () {
56 yield* Effect.logInfo("SmsVoice phone bindings setup: destroying previous resources");
57 yield* sharedStack.destroy();
58
59 yield* Effect.logInfo("SmsVoice phone bindings setup: deploying fixture");
60 const { functionUrl } = yield* sharedStack.deploy(
61 Effect.gen(function* () {
62 return yield* SmsVoicePhoneTestFunction;
63 }).pipe(Effect.provide(SmsVoicePhoneTestFunctionLive)),
64 );
65
66 expect(functionUrl).toBeTruthy();
67 baseUrl = functionUrl!.replace(/\/+$/, "");
68 const readinessUrl = `${baseUrl}/ping`;
69
70 yield* Effect.logInfo(
71 `SmsVoice phone bindings setup: probing readiness at ${readinessUrl}`,
72 );
73 yield* HttpClient.get(readinessUrl).pipe(
74 Effect.flatMap((response) =>
75 response.status === 200
76 ? Effect.succeed(response)
77 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
78 ),
79 Effect.tapError((error) =>
80 Effect.logWarning(
81 `SmsVoice phone bindings setup: fixture not ready yet (${String(error)})`,
82 ),
83 ),
84 Effect.retry({ schedule: readinessPolicy }),
85 );
86 }),
87 { timeout: 240_000 },
88 );
89
90 afterAll(sharedStack.destroy(), { timeout: 240_000 });
91
92 describe("PinpointSMSVoiceV2.SendTextMessage", () => {
93 test.provider(
94 "sends an SMS to the simulator destination",
95 (_stack) =>
96 Effect.gen(function* () {
97 const response = (yield* post("/send-text").pipe(Effect.flatMap((r) => r.json))) as {
98 messageId?: string;
99 };
100
101 expect(typeof response.messageId).toBe("string");
102 expect(response.messageId!.length).toBeGreaterThan(0);
103 }),
104 { timeout: 120_000 },
105 );
106 });
107
108 describe("PinpointSMSVoiceV2.SendVoiceMessage", () => {
109 test.provider(
110 "the grant allows the send (typed capability verdict otherwise)",
111 (_stack) =>
112 Effect.gen(function* () {
113 const response = (yield* post("/send-voice").pipe(Effect.flatMap((r) => r.json))) as {
114 ok: boolean;
115 messageId?: string;
116 tag?: string;
117 message?: string;
118 };
119
120 // The SIMULATOR number is SMS-only; a typed capability
121 // rejection still proves the binding + IAM grant.
122 expect(response.tag, JSON.stringify(response)).not.toBe("AccessDeniedException");
123 if (response.ok) {
124 expect(response.messageId).toBeTruthy();
125 }
126 }),
127 { timeout: 120_000 },
128 );
129 });
130
131 describe("PinpointSMSVoiceV2.SendMediaMessage", () => {
132 test.provider(
133 "the grant allows the send (typed capability verdict otherwise)",
134 (_stack) =>
135 Effect.gen(function* () {
136 const response = (yield* post("/send-media").pipe(Effect.flatMap((r) => r.json))) as {
137 ok: boolean;
138 messageId?: string;
139 tag?: string;
140 message?: string;
141 };
142
143 expect(response.tag, JSON.stringify(response)).not.toBe("AccessDeniedException");
144 if (response.ok) {
145 expect(response.messageId).toBeTruthy();
146 }
147 }),
148 { timeout: 120_000 },
149 );
150 });
151
152 describe("PinpointSMSVoiceV2 Keywords", () => {
153 test.provider(
154 "put, list, and delete a keyword on the number",
155 (_stack) =>
156 Effect.gen(function* () {
157 const put = (yield* post("/keyword-put").pipe(Effect.flatMap((r) => r.json))) as {
158 keyword?: string;
159 message?: string;
160 };
161 expect(put.keyword).toBe("INFO");
162 expect(put.message).toContain("alchemy.run");
163
164 const list = (yield* post("/keyword-list").pipe(Effect.flatMap((r) => r.json))) as {
165 keywords: string[];
166 };
167 expect(list.keywords).toContain("INFO");
168
169 const deleted = (yield* post("/keyword-delete").pipe(
170 Effect.flatMap((r) => r.json),
171 )) as { deleted?: string };
172 expect(deleted.deleted).toBe("INFO");
173
174 const after = (yield* post("/keyword-list").pipe(Effect.flatMap((r) => r.json))) as {
175 keywords: string[];
176 };
177 expect(after.keywords).not.toContain("INFO");
178 }),
179 { timeout: 120_000 },
180 );
181 });
182 },
183 );