| 1 | import * as AWS from "@/AWS"; |
| 2 | import { App, Branch } from "@/AWS/Amplify"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as amplify from "@distilled.cloud/aws/amplify"; |
| 5 | import { expect } from "alchemy-test"; |
| 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 |
| 35 | ? Effect.void |
| 36 | : Effect.fail(new BranchStillExists({ branchName })), |
| 37 | ), |
| 38 | Effect.retry({ |
| 39 | while: (e) => e._tag === "BranchStillExists", |
| 40 | schedule: Schedule.max([ |
| 41 | Schedule.spaced("2 seconds"), |
| 42 | Schedule.recurs(15), |
| 43 | ]), |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "create, update, and replace an Amplify branch", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const program = (branchName: string, description: string) => |
| 54 | Effect.gen(function* () { |
| 55 | const app = yield* App("BranchTestApp", { |
| 56 | description: "Amplify Branch resource test app", |
| 57 | platform: "WEB", |
| 58 | }); |
| 59 | const branch = yield* Branch("TestBranch", { |
| 60 | appId: app.appId, |
| 61 | branchName, |
| 62 | description, |
| 63 | stage: "DEVELOPMENT", |
| 64 | enableAutoBuild: false, |
| 65 | ttl: "5 minutes", |
| 66 | enableBasicAuth: true, |
| 67 | basicAuthCredentials: Redacted.make(BASIC_AUTH_CREDENTIALS), |
| 68 | environmentVariables: { STAGE: "test" }, |
| 69 | tags: { Environment: "test" }, |
| 70 | }); |
| 71 | return { |
| 72 | appId: app.appId, |
| 73 | branchName: branch.branchName, |
| 74 | branchArn: branch.branchArn, |
| 75 | }; |
| 76 | }); |
| 77 | |
| 78 | const created = yield* stack.deploy(program("main", "initial")); |
| 79 | expect(created.branchName).toBe("main"); |
| 80 | expect(created.branchArn).toContain("/branches/main"); |
| 81 | |
| 82 | const live = yield* findBranch(created.appId, "main"); |
| 83 | expect(live?.description).toBe("initial"); |
| 84 | expect(live?.stage).toBe("DEVELOPMENT"); |
| 85 | expect(live?.enableAutoBuild).toBe(false); |
| 86 | expect(live?.enableBasicAuth).toBe(true); |
| 87 | // ttl is converted from the Duration input to wire seconds. |
| 88 | expect(live?.ttl).toBe("300"); |
| 89 | expect(live?.environmentVariables?.STAGE).toBe("test"); |
| 90 | expect(live?.tags?.["alchemy::id"]).toBe("TestBranch"); |
| 91 | expect(live?.tags?.Environment).toBe("test"); |
| 92 | |
| 93 | // Update in place — same branchName, changed mutable settings. |
| 94 | const updated = yield* stack.deploy(program("main", "updated")); |
| 95 | expect(updated.branchArn).toBe(created.branchArn); |
| 96 | |
| 97 | const live2 = yield* findBranch(created.appId, "main"); |
| 98 | expect(live2?.description).toBe("updated"); |
| 99 | |
| 100 | // Replacement — changing branchName replaces the branch. |
| 101 | const replaced = yield* stack.deploy(program("release", "initial")); |
| 102 | expect(replaced.branchName).toBe("release"); |
| 103 | expect(replaced.branchArn).not.toBe(created.branchArn); |
| 104 | yield* assertBranchDeleted(created.appId, "main"); |
| 105 | |
| 106 | yield* stack.destroy(); |
| 107 | yield* assertBranchDeleted(created.appId, "release"); |
| 108 | }), |
| 109 | { tags: ["provider:aws", "provider:aws:amplify", "live"], timeout: 240_000 }, |
| 110 | ); |