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

main
ts 256 lines 9.29 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 identitystore from "@distilled.cloud/aws/identitystore";
6 import * as ssoAdmin from "@distilled.cloud/aws/sso-admin";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Schedule from "effect/Schedule";
10 import IdentityCenterBindingsFunctionLive, { IdentityCenterBindingsFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14
15 // The testing account is an organization management account with no enabled
16 // Identity Center instance, and `CreateInstance` there fails with a typed
17 // ValidationException ("Organization management account is not allowed to
18 // perform the operation."), so the Lambda E2E — which binds a live instance —
19 // is gated behind ALCHEMY_TEST_IDENTITY_CENTER=1 for entitled accounts (same
20 // gate as the resource lifecycle tests in this suite).
21 const RUN_LIVE = !!process.env.ALCHEMY_TEST_IDENTITY_CENTER;
22
23 // Ungated typed-error probes: prove the distilled error unions the bindings
24 // depend on are typed on every account, entitled or not, at near-zero cost.
25 test.provider(
26 "describeUser on a nonexistent identity store fails with a typed tag",
27 () =>
28 Effect.gen(function* () {
29 const error = yield* Effect.flip(
30 identitystore.describeUser({
31 IdentityStoreId: "d-9067000000",
32 UserId: "00000000-0000-0000-0000-000000000000",
33 }),
34 );
35 expect([
36 "ResourceNotFoundException",
37 "ValidationException",
38 "AccessDeniedException",
39 ]).toContain(error._tag);
40 }),
41 { tags: ["provider:aws", "provider:aws:identitycenter", "live"] },
42 );
43
44 test.provider(
45 "isMemberInGroups on a nonexistent identity store fails with a typed tag",
46 () =>
47 Effect.gen(function* () {
48 const error = yield* Effect.flip(
49 identitystore.isMemberInGroups({
50 IdentityStoreId: "d-9067000000",
51 MemberId: { UserId: "00000000-0000-0000-0000-000000000000" },
52 GroupIds: ["00000000-0000-0000-0000-000000000000"],
53 }),
54 );
55 expect([
56 "ResourceNotFoundException",
57 "ValidationException",
58 "AccessDeniedException",
59 ]).toContain(error._tag);
60 }),
61 { tags: ["provider:aws", "provider:aws:identitycenter", "live"] },
62 );
63
64 test.provider(
65 "listPermissionSets on a nonexistent instance fails with a typed tag",
66 () =>
67 Effect.gen(function* () {
68 const error = yield* Effect.flip(
69 ssoAdmin.listPermissionSets({
70 InstanceArn: "arn:aws:sso:::instance/ssoins-0000000000000000",
71 }),
72 );
73 expect([
74 "ResourceNotFoundException",
75 "ValidationException",
76 "AccessDeniedException",
77 ]).toContain(error._tag);
78 }),
79 { tags: ["provider:aws", "provider:aws:identitycenter", "live"] },
80 );
81
82 const sharedStack = Core.scratchStack(testOptions, "IdentityCenterBindings");
83
84 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
85 // over 60s on a fresh deploy.
86 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
87
88 let baseUrl: string;
89
90 const getJson = (path: string) =>
91 HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
92 const postJson = (path: string) =>
93 HttpClient.post(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json));
94
95 describe.sequential(
96 "IdentityCenter Bindings (E2E)",
97 { tags: ["provider:aws", "provider:aws:identitycenter", "provider:aws:lambda", "live"] },
98 () => {
99 beforeAll(
100 Effect.gen(function* () {
101 if (!RUN_LIVE) return;
102 yield* Effect.logInfo("IdentityCenter E2E setup: destroying previous resources");
103 yield* sharedStack.destroy();
104
105 yield* Effect.logInfo("IdentityCenter E2E setup: deploying fixture");
106 const attrs = yield* sharedStack.deploy(
107 Effect.gen(function* () {
108 return yield* IdentityCenterBindingsFunction;
109 }).pipe(Effect.provide(IdentityCenterBindingsFunctionLive)),
110 );
111
112 expect(attrs.functionUrl).toBeTruthy();
113 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
114
115 yield* HttpClient.get(`${baseUrl}/bindings`).pipe(
116 Effect.flatMap((response) =>
117 response.status === 200
118 ? Effect.succeed(response)
119 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
120 ),
121 Effect.retry({ schedule: readinessPolicy }),
122 );
123 }),
124 { timeout: 240_000 },
125 );
126
127 afterAll(
128 Effect.gen(function* () {
129 if (!RUN_LIVE) return;
130 yield* sharedStack.destroy();
131 }),
132 { timeout: 180_000 },
133 );
134
135 describe("binding registration", () => {
136 test.provider.skipIf(!RUN_LIVE)("all twenty capabilities initialize in the runtime", () =>
137 Effect.gen(function* () {
138 const response = (yield* getJson("/bindings")) as { bound: string[] };
139 expect(response.bound).toHaveLength(20);
140 }),
141 );
142 });
143
144 describe("group + user lookups", () => {
145 test.provider.skipIf(!RUN_LIVE)(
146 "listGroups sees the fixture group and getGroupId resolves it",
147 () =>
148 Effect.gen(function* () {
149 const groups = (yield* getJson("/groups")) as { displayNames: string[] };
150 expect(groups.displayNames).toContain("alchemy-idc-bindings-group");
151
152 const groupId = (yield* getJson("/group-id")) as {
153 groupId: string;
154 matchesBoundGroup: boolean;
155 };
156 expect(groupId.matchesBoundGroup).toBe(true);
157 }),
158 { timeout: 120_000 },
159 );
160
161 test.provider.skipIf(!RUN_LIVE)(
162 "listUsers enumerates the identity store",
163 () =>
164 Effect.gen(function* () {
165 const users = (yield* getJson("/users")) as { userNames: string[] };
166 expect(Array.isArray(users.userNames)).toBe(true);
167 }),
168 { timeout: 120_000 },
169 );
170 });
171
172 describe("user lifecycle", () => {
173 test.provider.skipIf(!RUN_LIVE)(
174 "create/getUserId/describe/update/delete roundtrip",
175 () =>
176 Effect.gen(function* () {
177 const result = (yield* postJson("/user-roundtrip")) as {
178 userId: string;
179 resolvedMatches: boolean;
180 userName: string;
181 displayNameBefore: string;
182 displayNameAfter: string;
183 };
184 expect(result.userId).toBeTruthy();
185 expect(result.resolvedMatches).toBe(true);
186 expect(result.userName).toBe("alchemy-idc-bindings-user");
187 expect(result.displayNameAfter).toBe("Alchemy Bindings Test User (updated)");
188 }),
189 { timeout: 120_000 },
190 );
191 });
192
193 describe("membership lifecycle", () => {
194 test.provider.skipIf(!RUN_LIVE)(
195 "create/resolve/describe/list/check/delete roundtrip",
196 () =>
197 Effect.gen(function* () {
198 const result = (yield* postJson("/membership-roundtrip")) as {
199 membershipId: string;
200 resolvedMatches: boolean;
201 describedGroupMatches: boolean;
202 memberCount: number;
203 memberOfCount: number;
204 isMember: boolean;
205 isMemberAfterDelete: boolean;
206 };
207 expect(result.membershipId).toBeTruthy();
208 expect(result.resolvedMatches).toBe(true);
209 expect(result.describedGroupMatches).toBe(true);
210 expect(result.memberCount).toBeGreaterThanOrEqual(1);
211 expect(result.memberOfCount).toBeGreaterThanOrEqual(1);
212 expect(result.isMember).toBe(true);
213 expect(result.isMemberAfterDelete).toBe(false);
214 }),
215 { timeout: 120_000 },
216 );
217 });
218
219 describe("permission set + assignment audit", () => {
220 test.provider.skipIf(!RUN_LIVE)(
221 "listPermissionSets/describePermissionSet see the fixture permission set",
222 () =>
223 Effect.gen(function* () {
224 const result = (yield* getJson("/permission-sets")) as {
225 containsFixturePermissionSet: boolean;
226 name: string;
227 sessionDuration: string;
228 provisionedAccounts: string[];
229 };
230 expect(result.containsFixturePermissionSet).toBe(true);
231 expect(result.name).toBe("AlchemyIdcBindings");
232 expect(result.sessionDuration).toBe("PT1H");
233 }),
234 { timeout: 120_000 },
235 );
236
237 test.provider.skipIf(!RUN_LIVE)(
238 "assignment audit reads answer for the fixture group",
239 () =>
240 Effect.gen(function* () {
241 const result = (yield* getJson("/assignments")) as {
242 forPrincipal: { ok: true; count: number } | { ok: false; errorTag: string };
243 };
244 // Organization instances answer with a count; account instances
245 // reject ListAccountAssignmentsForPrincipal with a typed tag.
246 if (result.forPrincipal.ok) {
247 expect(result.forPrincipal.count).toBeGreaterThanOrEqual(0);
248 } else {
249 expect(result.forPrincipal.errorTag).toBeTruthy();
250 }
251 }),
252 { timeout: 120_000 },
253 );
254 });
255 },
256 );