The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Branch } from "@alchemy.run/neon/Branch"; |
| 2 | import type { PostgresOrigin } from "@alchemy.run/neon/PostgresOrigin"; |
| 3 | import { Project, waitForOperations } from "@alchemy.run/neon/Project"; |
| 4 | import { providers } from "@alchemy.run/neon/Providers"; |
| 5 | import { Role } from "@alchemy.run/neon/Role"; |
| 6 | import { describe, expect } from "@alchemy.run/test"; |
| 7 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 8 | import { createProjectBranchRole, getProjectBranchRole } from "@distilled.cloud/neon"; |
| 9 | import * as PgClient from "@effect/sql-pg/PgClient"; |
| 10 | import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | import * as Result from "effect/Result"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: providers() }); |
| 16 | |
| 17 | const tags = ["provider:neon", "provider:neon:role", "live"]; |
| 18 | |
| 19 | /** Log in as the role and return `current_user`. */ |
| 20 | const currentUser = Effect.fn( |
| 21 | function* (_origin: PostgresOrigin, _user: string, _password: Redacted.Redacted<string>) { |
| 22 | const sql = yield* PgClient.PgClient; |
| 23 | const rows = yield* sql<{ user: string }>`select current_user as user`; |
| 24 | return rows[0]?.user; |
| 25 | }, |
| 26 | (effect, origin, user, password) => |
| 27 | effect.pipe( |
| 28 | Effect.provide( |
| 29 | PgClient.layer({ |
| 30 | host: origin.host, |
| 31 | port: origin.port, |
| 32 | database: origin.database, |
| 33 | username: user, |
| 34 | password, |
| 35 | ssl: true, |
| 36 | }), |
| 37 | ), |
| 38 | ), |
| 39 | ); |
| 40 | |
| 41 | const roleExists = (scope: { projectId: string; branchId: string }, roleName: string) => |
| 42 | getProjectBranchRole({ |
| 43 | project_id: scope.projectId, |
| 44 | branch_id: scope.branchId, |
| 45 | role_name: roleName, |
| 46 | }).pipe( |
| 47 | Effect.as(true), |
| 48 | Effect.catchTag("NotFound", () => Effect.succeed(false)), |
| 49 | ); |
| 50 | |
| 51 | describe.concurrent("Neon.Role", { tags }, () => { |
| 52 | test.provider( |
| 53 | "creates a login role, keeps its password, replaces on noLogin, and deletes", |
| 54 | (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | yield* stack.destroy(); |
| 57 | const program = (noLogin?: boolean) => |
| 58 | Effect.gen(function* () { |
| 59 | const project = yield* Project("RoleProject"); |
| 60 | const branch = yield* Branch("RoleBranch", { project }); |
| 61 | const role = yield* Role("AppRole", { branch, noLogin }); |
| 62 | return { branch, role }; |
| 63 | }); |
| 64 | |
| 65 | const { branch, role } = yield* stack.deploy(program()); |
| 66 | expect(role.roleName).toMatch(/^[a-z0-9_]+$/); |
| 67 | expect(role.noLogin).toBe(false); |
| 68 | expect(role.password).toBeDefined(); |
| 69 | expect(yield* currentUser(branch.origin, role.roleName, role.password!)).toBe( |
| 70 | role.roleName, |
| 71 | ); |
| 72 | |
| 73 | // An unchanged redeploy keeps the same password. |
| 74 | expect((yield* stack.plan(program())).resources.AppRole.action).toBe("noop"); |
| 75 | const redeployed = yield* stack.deploy(program()); |
| 76 | expect(Redacted.value(redeployed.role.password!)).toBe(Redacted.value(role.password!)); |
| 77 | |
| 78 | // noLogin is creation-only; a generated name gets a new role, created first. |
| 79 | const groupPlan = yield* stack.plan(program(true)); |
| 80 | expect(groupPlan.resources.AppRole).toMatchObject({ |
| 81 | action: "replace", |
| 82 | deleteFirst: false, |
| 83 | }); |
| 84 | const group = yield* stack.deploy(program(true)); |
| 85 | expect(group.role.roleName).not.toBe(role.roleName); |
| 86 | expect(group.role.noLogin).toBe(true); |
| 87 | expect(group.role.password).toBeUndefined(); |
| 88 | expect(yield* roleExists(role, role.roleName)).toBe(false); |
| 89 | |
| 90 | yield* stack.destroy(); |
| 91 | expect(yield* roleExists(group.role, group.role.roleName)).toBe(false); |
| 92 | }), |
| 93 | { timeout: 180_000 }, |
| 94 | ); |
| 95 | |
| 96 | test.provider( |
| 97 | "renames an explicit role on the project's default branch and replaces it delete-first", |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | yield* stack.destroy(); |
| 101 | const program = (name: string, noLogin?: boolean) => |
| 102 | Effect.gen(function* () { |
| 103 | const project = yield* Project("RoleDefaultProject"); |
| 104 | const role = yield* Role("Migrator", { project, name, noLogin }); |
| 105 | return { project, role }; |
| 106 | }); |
| 107 | const first = yield* stack.deploy(program("migrator")); |
| 108 | expect(first.role.branchId).toBe(first.project.defaultBranchId); |
| 109 | expect(first.role.roleName).toBe("migrator"); |
| 110 | |
| 111 | const renamed = yield* stack.deploy(program("migrator_v2")); |
| 112 | expect(renamed.role.roleName).toBe("migrator_v2"); |
| 113 | expect(yield* roleExists(first.role, "migrator")).toBe(false); |
| 114 | expect( |
| 115 | yield* currentUser(first.project.origin, "migrator_v2", renamed.role.password!), |
| 116 | ).toBe("migrator_v2"); |
| 117 | |
| 118 | // The same explicit name must be freed before the replacement is created. |
| 119 | expect((yield* stack.plan(program("migrator_v2", true))).resources.Migrator).toMatchObject({ |
| 120 | action: "replace", |
| 121 | deleteFirst: true, |
| 122 | }); |
| 123 | const group = yield* stack.deploy(program("migrator_v2", true)); |
| 124 | expect(group.role.roleName).toBe("migrator_v2"); |
| 125 | expect(group.role.noLogin).toBe(true); |
| 126 | |
| 127 | yield* stack.destroy(); |
| 128 | expect(yield* roleExists(first.role, "migrator_v2")).toBe(false); |
| 129 | }), |
| 130 | { timeout: 180_000 }, |
| 131 | ); |
| 132 | |
| 133 | test.provider( |
| 134 | "refuses a foreign role until adopted, then reveals its existing password", |
| 135 | (stack) => |
| 136 | Effect.gen(function* () { |
| 137 | yield* stack.destroy(); |
| 138 | const base = Project("RoleAdoptProject"); |
| 139 | const project = yield* stack.deploy(base); |
| 140 | const foreign = yield* createProjectBranchRole({ |
| 141 | project_id: project.projectId, |
| 142 | branch_id: project.defaultBranchId, |
| 143 | role: { name: "foreign_role" }, |
| 144 | }); |
| 145 | yield* waitForOperations(foreign.operations); |
| 146 | const program = (allow: boolean) => |
| 147 | Effect.gen(function* () { |
| 148 | const project = yield* base; |
| 149 | return yield* Role("ForeignRole", { project, name: "foreign_role" }).pipe(adopt(allow)); |
| 150 | }); |
| 151 | |
| 152 | const refused = yield* stack.deploy(program(false)).pipe(Effect.result); |
| 153 | expect(Result.isFailure(refused)).toBe(true); |
| 154 | if (Result.isFailure(refused)) expect(refused.failure).toBeInstanceOf(OwnedBySomeoneElse); |
| 155 | |
| 156 | const adopted = yield* stack.deploy(program(true)); |
| 157 | const foreignPassword = foreign.role.password!; |
| 158 | expect(Redacted.value(adopted.password!)).toBe( |
| 159 | Redacted.isRedacted(foreignPassword) ? Redacted.value(foreignPassword) : foreignPassword, |
| 160 | ); |
| 161 | expect(yield* currentUser(project.origin, "foreign_role", adopted.password!)).toBe( |
| 162 | "foreign_role", |
| 163 | ); |
| 164 | |
| 165 | yield* stack.destroy(); |
| 166 | }), |
| 167 | { timeout: 180_000 }, |
| 168 | ); |
| 169 | }); |