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

main
ts 134 lines 5.39 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 ManagedLoginBranding,
4 UserPool,
5 UserPoolClient,
6 UserPoolDomain,
7 } from "@alchemy.run/aws/Cognito";
8 import { expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as cip from "@distilled.cloud/aws/cognito-identity-provider";
11 import * as Data from "effect/Data";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: AWS.providers() });
16
17 class BrandingStillExists extends Data.TaggedError("BrandingStillExists")<{
18 readonly managedLoginBrandingId: string;
19 }> {}
20
21 const assertBrandingDeleted = (userPoolId: string, managedLoginBrandingId: string) =>
22 cip
23 .describeManagedLoginBranding({
24 UserPoolId: userPoolId,
25 ManagedLoginBrandingId: managedLoginBrandingId,
26 })
27 .pipe(
28 Effect.flatMap(() => Effect.fail(new BrandingStillExists({ managedLoginBrandingId }))),
29 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
30 Effect.retry({
31 while: (e) => e._tag === "BrandingStillExists",
32 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
33 }),
34 );
35
36 test.provider(
37 "default branding activates managed login; update to custom settings; replace on client change",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const infra = (useSecondClient: boolean, settings?: Record<string, unknown>) =>
43 Effect.gen(function* () {
44 const pool = yield* UserPool("BrandingPool", {});
45 const clientA = yield* UserPoolClient("WebA", {
46 userPoolId: pool.userPoolId,
47 });
48 const clientB = yield* UserPoolClient("WebB", {
49 userPoolId: pool.userPoolId,
50 });
51 const domain = yield* UserPoolDomain("AuthDomain", {
52 userPoolId: pool.userPoolId,
53 managedLoginVersion: 2,
54 });
55 const branding = yield* ManagedLoginBranding("Branding", {
56 userPoolId: pool.userPoolId,
57 clientId: useSecondClient ? clientB.clientId : clientA.clientId,
58 ...(settings === undefined ? {} : { settings }),
59 });
60 return { pool, clientA, clientB, domain, branding };
61 });
62
63 const created = yield* stack.deploy(infra(false));
64
65 expect(created.branding.managedLoginBrandingId).toBeDefined();
66 expect(created.branding.userPoolId).toBe(created.pool.userPoolId);
67 expect(created.branding.clientId).toBe(created.clientA.clientId);
68 expect(created.domain.domain).toBeDefined();
69
70 // out-of-band: the client has the style assigned with Cognito's
71 // provided default values (no console step required)
72 const byClient = yield* cip.describeManagedLoginBrandingByClient({
73 UserPoolId: created.pool.userPoolId,
74 ClientId: created.clientA.clientId,
75 });
76 expect(byClient.ManagedLoginBranding?.ManagedLoginBrandingId).toBe(
77 created.branding.managedLoginBrandingId,
78 );
79 expect(byClient.ManagedLoginBranding?.UseCognitoProvidedValues).toBe(true);
80
81 // update in place: switch from provided values to custom settings
82 // (use the merged provided-values document, the exact designer shape)
83 const merged = yield* cip.describeManagedLoginBranding({
84 UserPoolId: created.pool.userPoolId,
85 ManagedLoginBrandingId: created.branding.managedLoginBrandingId,
86 ReturnMergedResources: true,
87 });
88 const settings = merged.ManagedLoginBranding?.Settings as Record<string, unknown>;
89 expect(settings).toBeDefined();
90
91 const updated = yield* stack.deploy(infra(false, settings));
92 expect(updated.branding.managedLoginBrandingId).toBe(created.branding.managedLoginBrandingId);
93 const afterUpdate = yield* cip.describeManagedLoginBranding({
94 UserPoolId: created.pool.userPoolId,
95 ManagedLoginBrandingId: created.branding.managedLoginBrandingId,
96 });
97 expect(afterUpdate.ManagedLoginBranding?.UseCognitoProvidedValues).toBe(false);
98
99 // changing the app client replaces the branding style
100 const replaced = yield* stack.deploy(infra(true, settings));
101 expect(replaced.branding.managedLoginBrandingId).not.toBe(
102 created.branding.managedLoginBrandingId,
103 );
104 expect(replaced.branding.clientId).toBe(created.clientB.clientId);
105 yield* assertBrandingDeleted(
106 created.pool.userPoolId,
107 created.branding.managedLoginBrandingId,
108 );
109 const replacedByClient = yield* cip.describeManagedLoginBrandingByClient({
110 UserPoolId: created.pool.userPoolId,
111 ClientId: created.clientB.clientId,
112 });
113 expect(replacedByClient.ManagedLoginBranding?.ManagedLoginBrandingId).toBe(
114 replaced.branding.managedLoginBrandingId,
115 );
116
117 yield* stack.destroy();
118 yield* cip.describeUserPool({ UserPoolId: created.pool.userPoolId }).pipe(
119 Effect.flatMap(() =>
120 Effect.fail(
121 new BrandingStillExists({
122 managedLoginBrandingId: created.pool.userPoolId,
123 }),
124 ),
125 ),
126 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
127 Effect.retry({
128 while: (e) => e._tag === "BrandingStillExists",
129 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
130 }),
131 );
132 }),
133 { tags: ["provider:aws", "provider:aws:cognito", "live"], timeout: 180_000 },
134 );