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 { Credential, validateCredential, type CredentialScope } from "@alchemy.run/neon/Credential"; |
| 3 | import { Project } from "@alchemy.run/neon/Project"; |
| 4 | import { providers } from "@alchemy.run/neon/Providers"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as SDK from "@distilled.cloud/neon"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: providers() }); |
| 13 | |
| 14 | test.provider( |
| 15 | "credential reveals without rotation and replaces scopes", |
| 16 | (stack) => |
| 17 | Effect.gen(function* () { |
| 18 | yield* stack.destroy(); |
| 19 | const deploy = (scopes: CredentialScope[]) => |
| 20 | stack.deploy( |
| 21 | Effect.gen(function* () { |
| 22 | const project = yield* Project("CredentialProject", { |
| 23 | region: "aws-us-east-2", |
| 24 | }); |
| 25 | const credential = yield* Credential("Reader", { project, scopes }); |
| 26 | const named = yield* Credential("Named", { |
| 27 | project, |
| 28 | scopes, |
| 29 | name: "named-storage-credential", |
| 30 | }); |
| 31 | return { project, credential, named }; |
| 32 | }), |
| 33 | ); |
| 34 | const first = yield* deploy(["storage:read"]); |
| 35 | const second = yield* deploy(["storage:read"]); |
| 36 | expect(second.credential.tokenId).toBe(first.credential.tokenId); |
| 37 | const scope = { |
| 38 | project_id: first.credential.projectId, |
| 39 | branch_id: first.credential.branchId, |
| 40 | }; |
| 41 | const listed = yield* SDK.listCredentials(scope); |
| 42 | expect( |
| 43 | listed.credentials.filter((credential) => credential.token_id === first.credential.tokenId), |
| 44 | ).toHaveLength(1); |
| 45 | const revealed = yield* SDK.revealCredential({ |
| 46 | ...scope, |
| 47 | token_id: first.credential.tokenId, |
| 48 | }); |
| 49 | expect( |
| 50 | Redacted.value(first.credential.apiToken) === |
| 51 | (Redacted.isRedacted(revealed.api_token) |
| 52 | ? Redacted.value(revealed.api_token) |
| 53 | : revealed.api_token), |
| 54 | ).toBe(true); |
| 55 | expect(Redacted.isRedacted(second.credential.s3SecretAccessKey)).toBe(true); |
| 56 | const changed = yield* deploy(["storage:write"]); |
| 57 | expect(changed.credential.tokenId).not.toBe(first.credential.tokenId); |
| 58 | expect(changed.named.tokenId).not.toBe(first.named.tokenId); |
| 59 | expect(changed.named.name).toBe(first.named.name); |
| 60 | expect( |
| 61 | (yield* SDK.listCredentials(scope)).credentials.some( |
| 62 | (credential) => credential.token_id === first.named.tokenId && !credential.revoked_at, |
| 63 | ), |
| 64 | ).toBe(false); |
| 65 | const old = yield* SDK.revealCredential({ |
| 66 | ...scope, |
| 67 | token_id: first.credential.tokenId, |
| 68 | }).pipe( |
| 69 | Effect.as(false), |
| 70 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 71 | ); |
| 72 | expect(old).toBe(true); |
| 73 | yield* stack.destroy(); |
| 74 | yield* stack.destroy(); |
| 75 | }), |
| 76 | { |
| 77 | tags: ["provider:neon", "provider:neon:credential", "provider:neon:project", "live"], |
| 78 | timeout: 120_000, |
| 79 | }, |
| 80 | ); |
| 81 | |
| 82 | test.provider( |
| 83 | "credential override validates ancestry scopes and out-of-band revocation", |
| 84 | (stack) => |
| 85 | Effect.gen(function* () { |
| 86 | yield* stack.destroy(); |
| 87 | const program = (scopes: CredentialScope[] = ["storage:read", "storage:write"]) => |
| 88 | Effect.gen(function* () { |
| 89 | const project = yield* Project("LineageProject", { |
| 90 | region: "aws-us-east-2", |
| 91 | }); |
| 92 | const left = yield* Branch("Left", { project }); |
| 93 | const child = yield* Branch("Child", { project, parentBranch: left }); |
| 94 | const sibling = yield* Branch("Sibling", { project }); |
| 95 | const credential = yield* Credential("Writer", { |
| 96 | branch: left, |
| 97 | scopes, |
| 98 | }); |
| 99 | return { project, left, child, sibling, credential }; |
| 100 | }); |
| 101 | const first = yield* stack.deploy(program()); |
| 102 | yield* validateCredential(first.credential, first.child, "storage:read"); |
| 103 | yield* validateCredential(first.credential, first.left, "storage:write"); |
| 104 | const sibling = yield* validateCredential( |
| 105 | first.credential, |
| 106 | first.sibling, |
| 107 | "storage:read", |
| 108 | ).pipe(Effect.result); |
| 109 | expect(Result.isFailure(sibling)).toBe(true); |
| 110 | if (Result.isFailure(sibling)) expect(sibling.failure._tag).toBe("CredentialRecoveryError"); |
| 111 | const scope = yield* validateCredential( |
| 112 | first.credential, |
| 113 | first.left, |
| 114 | "ai_gateway:invoke", |
| 115 | ).pipe(Effect.result); |
| 116 | expect(Result.isFailure(scope)).toBe(true); |
| 117 | yield* SDK.revokeCredential({ |
| 118 | project_id: first.credential.projectId, |
| 119 | branch_id: first.credential.branchId, |
| 120 | token_id: first.credential.tokenId, |
| 121 | }); |
| 122 | // Reordering equal scopes schedules reconciliation without changing permissions. |
| 123 | const recovered = yield* stack.deploy(program(["storage:write", "storage:read"])); |
| 124 | expect(recovered.credential.tokenId).not.toBe(first.credential.tokenId); |
| 125 | expect(recovered.credential.name).toBe(first.credential.name); |
| 126 | yield* stack.destroy(); |
| 127 | yield* stack.destroy(); |
| 128 | }), |
| 129 | { |
| 130 | tags: [ |
| 131 | "provider:neon", |
| 132 | "provider:neon:branch", |
| 133 | "provider:neon:credential", |
| 134 | "provider:neon:project", |
| 135 | "live", |
| 136 | ], |
| 137 | timeout: 120_000, |
| 138 | }, |
| 139 | ); |