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

main
ts 117 lines 4.51 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 Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import * as Schedule from "effect/Schedule";
9 import NotificationsContactsTestFunctionLive, {
10 NotificationsContactsTestFunction,
11 } from "./handler.ts";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "NotificationsContactsBindings");
16
17 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
18
19 let baseUrl: string;
20
21 describe(
22 "NotificationsContacts Bindings",
23 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:notificationscontacts", "live"] },
24 () => {
25 beforeAll(
26 Effect.gen(function* () {
27 yield* Effect.logInfo("NotificationsContacts test setup: destroying previous resources");
28 yield* sharedStack.destroy();
29
30 yield* Effect.logInfo("NotificationsContacts test setup: deploying fixture");
31 const { functionUrl } = yield* sharedStack.deploy(
32 Effect.gen(function* () {
33 return yield* NotificationsContactsTestFunction;
34 }).pipe(Effect.provide(NotificationsContactsTestFunctionLive)),
35 );
36
37 expect(functionUrl).toBeTruthy();
38 baseUrl = functionUrl!.replace(/\/+$/, "");
39 const readinessUrl = `${baseUrl}/ping`;
40
41 yield* Effect.logInfo(
42 `NotificationsContacts test setup: probing readiness at ${readinessUrl}`,
43 );
44 yield* HttpClient.get(readinessUrl).pipe(
45 Effect.flatMap((response) =>
46 response.status === 200
47 ? Effect.succeed(response)
48 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
49 ),
50 Effect.tapError((error) =>
51 Effect.logWarning(
52 `NotificationsContacts test setup: fixture not ready yet (${String(error)})`,
53 ),
54 ),
55 Effect.retry({ schedule: readinessPolicy }),
56 );
57 }),
58 { timeout: 240_000 },
59 );
60
61 afterAll(sharedStack.destroy(), { timeout: 120_000 });
62
63 describe("NotificationsContacts.GetEmailContact", () => {
64 test.provider(
65 "reads the bound contact's activation status",
66 (_stack) =>
67 Effect.gen(function* () {
68 const response = (yield* HttpClient.get(`${baseUrl}/contact`).pipe(
69 Effect.flatMap((r) => r.json),
70 )) as { arn: string; status: string };
71
72 expect(response.arn).toContain(":emailcontact/");
73 // Activation is a human email-confirmation loop; a fresh fixture
74 // contact stays `inactive`.
75 expect(["inactive", "active"]).toContain(response.status);
76 }),
77 { timeout: 120_000 },
78 );
79 });
80
81 describe("NotificationsContacts.SendActivationCode", () => {
82 test.provider(
83 "sends the activation email to the bound contact",
84 (_stack) =>
85 Effect.gen(function* () {
86 const response = (yield* HttpClient.execute(
87 HttpClientRequest.post(`${baseUrl}/send-code`),
88 ).pipe(Effect.flatMap((r) => r.json))) as { sent: boolean; conflict: boolean };
89
90 // A successful send, or the typed ConflictException for an
91 // already-active contact — either outcome proves the binding.
92 expect(response.sent || response.conflict).toBe(true);
93 }),
94 { timeout: 120_000 },
95 );
96 });
97
98 describe("NotificationsContacts.ActivateEmailContact", () => {
99 test.provider(
100 "rejects a bogus activation code with a typed error",
101 (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* HttpClient.execute(
104 HttpClientRequest.post(`${baseUrl}/activate-bogus`),
105 ).pipe(Effect.flatMap((r) => r.json))) as { activated: boolean; errorTag?: string };
106
107 // The code from the activation email is a human loop — a bogus
108 // code must surface one of the operation's TYPED error tags,
109 // proving the IAM grant and request wiring end-to-end.
110 expect(response.activated).toBe(false);
111 expect(["ValidationException", "ConflictException"]).toContain(response.errorTag);
112 }),
113 { timeout: 120_000 },
114 );
115 });
116 },
117 );