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

main
ts 105 lines 3.98 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { App, Branch } from "@alchemy.run/aws/Amplify";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as amplify from "@distilled.cloud/aws/amplify";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
9 import * as Schedule from "effect/Schedule";
10 import { makeAmplifyTestLease } from "./TestLease.ts";
11
12 const { test, beforeAll, afterAll } = Test.make({ providers: AWS.providers() });
13 const testLease = makeAmplifyTestLease();
14
15 beforeAll(testLease.acquire, { timeout: 240_000 });
16 afterAll(testLease.release);
17
18 // base64 of "user:passw0rd" — generated once and checked in as a constant.
19 const BASIC_AUTH_CREDENTIALS = "dXNlcjpwYXNzdzByZA==";
20
21 const findBranch = (appId: string, branchName: string) =>
22 amplify.getBranch({ appId, branchName }).pipe(
23 Effect.map((r) => r.branch),
24 Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)),
25 );
26
27 class BranchStillExists extends Data.TaggedError("BranchStillExists")<{
28 readonly branchName: string;
29 }> {}
30
31 const assertBranchDeleted = (appId: string, branchName: string) =>
32 findBranch(appId, branchName).pipe(
33 Effect.flatMap((branch) =>
34 branch === undefined ? Effect.void : Effect.fail(new BranchStillExists({ branchName })),
35 ),
36 Effect.retry({
37 while: (e) => e._tag === "BranchStillExists",
38 schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(15)]),
39 }),
40 );
41
42 test.provider(
43 "create, update, and replace an Amplify branch",
44 (stack) =>
45 Effect.gen(function* () {
46 yield* stack.destroy();
47
48 const program = (branchName: string, description: string) =>
49 Effect.gen(function* () {
50 const app = yield* App("BranchTestApp", {
51 description: "Amplify Branch resource test app",
52 platform: "WEB",
53 });
54 const branch = yield* Branch("TestBranch", {
55 appId: app.appId,
56 branchName,
57 description,
58 stage: "DEVELOPMENT",
59 enableAutoBuild: false,
60 ttl: "5 minutes",
61 enableBasicAuth: true,
62 basicAuthCredentials: Redacted.make(BASIC_AUTH_CREDENTIALS),
63 environmentVariables: { STAGE: "test" },
64 tags: { Environment: "test" },
65 });
66 return {
67 appId: app.appId,
68 branchName: branch.branchName,
69 branchArn: branch.branchArn,
70 };
71 });
72
73 const created = yield* stack.deploy(program("main", "initial"));
74 expect(created.branchName).toBe("main");
75 expect(created.branchArn).toContain("/branches/main");
76
77 const live = yield* findBranch(created.appId, "main");
78 expect(live?.description).toBe("initial");
79 expect(live?.stage).toBe("DEVELOPMENT");
80 expect(live?.enableAutoBuild).toBe(false);
81 expect(live?.enableBasicAuth).toBe(true);
82 // ttl is converted from the Duration input to wire seconds.
83 expect(live?.ttl).toBe("300");
84 expect(live?.environmentVariables?.STAGE).toBe("test");
85 expect(live?.tags?.["alchemy::id"]).toBe("TestBranch");
86 expect(live?.tags?.Environment).toBe("test");
87
88 // Update in place — same branchName, changed mutable settings.
89 const updated = yield* stack.deploy(program("main", "updated"));
90 expect(updated.branchArn).toBe(created.branchArn);
91
92 const live2 = yield* findBranch(created.appId, "main");
93 expect(live2?.description).toBe("updated");
94
95 // Replacement — changing branchName replaces the branch.
96 const replaced = yield* stack.deploy(program("release", "initial"));
97 expect(replaced.branchName).toBe("release");
98 expect(replaced.branchArn).not.toBe(created.branchArn);
99 yield* assertBranchDeleted(created.appId, "main");
100
101 yield* stack.destroy();
102 yield* assertBranchDeleted(created.appId, "release");
103 }),
104 { tags: ["provider:aws", "provider:aws:amplify", "live"], timeout: 240_000 },
105 );