The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { Auth } from "@alchemy.run/neon/Auth"; |
| 2 | import { Branch } from "@alchemy.run/neon/Branch"; |
| 3 | import { DataApi } from "@alchemy.run/neon/DataApi"; |
| 4 | import { Project, waitForOperations } from "@alchemy.run/neon/Project"; |
| 5 | import { providers } from "@alchemy.run/neon/Providers"; |
| 6 | import { expect } from "@alchemy.run/test"; |
| 7 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 8 | import * as SDK from "@distilled.cloud/neon"; |
| 9 | import { adopt } from "alchemy/AdoptPolicy"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as HttpClient from "effect/http/HttpClient"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: providers() }); |
| 14 | |
| 15 | test.provider( |
| 16 | "Data API recovery treats incomplete uncreated identities as absent", |
| 17 | () => |
| 18 | Effect.gen(function* () { |
| 19 | const provider = yield* DataApi.Provider; |
| 20 | const missingScope = { |
| 21 | branch: { projectId: "uncreated-project", branchId: "uncreated-branch" }, |
| 22 | }; |
| 23 | const partialScope = { |
| 24 | branch: { projectId: "uncreated-project", branchId: "uncreated-branch" }, |
| 25 | }; |
| 26 | yield* Effect.sync(() => { |
| 27 | Reflect.deleteProperty(missingScope, "branch"); |
| 28 | Reflect.deleteProperty(partialScope.branch, "branchId"); |
| 29 | }); |
| 30 | for (const olds of [ |
| 31 | missingScope, |
| 32 | partialScope, |
| 33 | { branch: { projectId: "", branchId: "uncreated-branch" } }, |
| 34 | { project: { projectId: "" } }, |
| 35 | ]) { |
| 36 | expect( |
| 37 | yield* provider.read!({ |
| 38 | id: "UncreatedDataApi", |
| 39 | fqn: "UncreatedDataApi", |
| 40 | instanceId: "uncreated-data-api", |
| 41 | olds, |
| 42 | output: undefined, |
| 43 | }), |
| 44 | ).toBeUndefined(); |
| 45 | } |
| 46 | }), |
| 47 | { tags: ["provider:neon", "provider:neon:dataapi", "live"] }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "Data API infers custom databases and rejects ambiguous selection before mutation", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | const program = (selection: "infer" | "selected" | "explicit", rows = 10) => |
| 56 | Effect.gen(function* () { |
| 57 | const project = yield* Project("SelectedDatabaseProject", { |
| 58 | region: "aws-us-east-2", |
| 59 | databaseName: "selected_app", |
| 60 | }); |
| 61 | const auth = yield* Auth("SelectedDatabaseAuth", { project, database: "selected_app" }); |
| 62 | const api = yield* DataApi("SelectedDatabaseApi", { |
| 63 | branch: { |
| 64 | projectId: auth.projectId, |
| 65 | branchId: auth.branchId, |
| 66 | databaseName: selection === "selected" ? project.databaseName : undefined, |
| 67 | }, |
| 68 | database: selection === "explicit" ? "selected_app" : undefined, |
| 69 | authProvider: "neon_auth", |
| 70 | settings: { db_max_rows: rows }, |
| 71 | }); |
| 72 | return { project, api }; |
| 73 | }); |
| 74 | const first = yield* stack.deploy(program("infer")); |
| 75 | expect(first.api.database).toBe("selected_app"); |
| 76 | const request = { |
| 77 | project_id: first.api.projectId, |
| 78 | branch_id: first.api.branchId, |
| 79 | database_name: first.api.database, |
| 80 | }; |
| 81 | const database = yield* SDK.createProjectBranchDatabase({ |
| 82 | project_id: request.project_id, |
| 83 | branch_id: request.branch_id, |
| 84 | database: { name: "another_app", owner_name: first.project.roleName }, |
| 85 | }); |
| 86 | yield* waitForOperations(database.operations); |
| 87 | const selected = yield* stack.deploy(program("selected")); |
| 88 | expect(selected.api.url).toBe(first.api.url); |
| 89 | const explicit = yield* stack.deploy(program("explicit")); |
| 90 | expect(explicit.api.url).toBe(first.api.url); |
| 91 | expect( |
| 92 | yield* stack.deploy(program("infer", 50)).pipe( |
| 93 | Effect.as(false), |
| 94 | Effect.catchTag("InvalidDataApiConfiguration", () => Effect.succeed(true)), |
| 95 | ), |
| 96 | ).toBe(true); |
| 97 | expect((yield* SDK.getProjectBranchDataAPI(request)).settings?.db_max_rows).toBe(10); |
| 98 | yield* stack.destroy(); |
| 99 | expect( |
| 100 | yield* SDK.getProjectBranchDataAPI(request).pipe( |
| 101 | Effect.as(false), |
| 102 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 103 | ), |
| 104 | ).toBe(true); |
| 105 | yield* stack.destroy(); |
| 106 | }), |
| 107 | { |
| 108 | tags: [ |
| 109 | "provider:neon", |
| 110 | "provider:neon:auth", |
| 111 | "provider:neon:dataapi", |
| 112 | "provider:neon:project", |
| 113 | "live", |
| 114 | ], |
| 115 | timeout: 120_000, |
| 116 | }, |
| 117 | ); |
| 118 | |
| 119 | test.provider( |
| 120 | "Data API refuses foreign ownership and explicitly adopts only settings", |
| 121 | (stack) => |
| 122 | Effect.gen(function* () { |
| 123 | yield* stack.destroy(); |
| 124 | const base = Effect.gen(function* () { |
| 125 | const project = yield* Project("DataAdoptionProject", { region: "aws-us-east-2" }); |
| 126 | return yield* Auth("DataAdoptionAuth", { project }); |
| 127 | }); |
| 128 | const auth = yield* stack.deploy(base); |
| 129 | const request = { |
| 130 | project_id: auth.projectId, |
| 131 | branch_id: auth.branchId, |
| 132 | database_name: auth.database, |
| 133 | }; |
| 134 | yield* SDK.createProjectBranchDataAPI({ |
| 135 | ...request, |
| 136 | auth_provider: "neon_auth", |
| 137 | add_default_grants: false, |
| 138 | settings: { db_max_rows: 10 }, |
| 139 | }); |
| 140 | const program = (allow: boolean) => |
| 141 | Effect.gen(function* () { |
| 142 | const auth = yield* base; |
| 143 | return yield* DataApi("AdoptedDataApi", { |
| 144 | branch: { projectId: auth.projectId, branchId: auth.branchId }, |
| 145 | database: auth.database, |
| 146 | settings: { db_max_rows: 20 }, |
| 147 | }).pipe(adopt(allow)); |
| 148 | }); |
| 149 | expect( |
| 150 | yield* stack.deploy(program(false)).pipe( |
| 151 | Effect.as(false), |
| 152 | Effect.catchTag("OwnedBySomeoneElse", () => Effect.succeed(true)), |
| 153 | ), |
| 154 | ).toBe(true); |
| 155 | expect((yield* SDK.getProjectBranchDataAPI(request)).settings?.db_max_rows).toBe(10); |
| 156 | const adopted = yield* stack.deploy(program(true)); |
| 157 | expect(adopted.database).toBe(auth.database); |
| 158 | expect((yield* SDK.getProjectBranchDataAPI(request)).settings?.db_max_rows).toBe(20); |
| 159 | yield* stack.destroy(); |
| 160 | expect( |
| 161 | yield* SDK.getProjectBranchDataAPI(request).pipe( |
| 162 | Effect.as(false), |
| 163 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 164 | ), |
| 165 | ).toBe(true); |
| 166 | yield* stack.destroy(); |
| 167 | }), |
| 168 | { |
| 169 | tags: [ |
| 170 | "provider:neon", |
| 171 | "provider:neon:auth", |
| 172 | "provider:neon:dataapi", |
| 173 | "provider:neon:project", |
| 174 | "live", |
| 175 | ], |
| 176 | timeout: 120_000, |
| 177 | }, |
| 178 | ); |
| 179 | |
| 180 | test.provider( |
| 181 | "Data API replaces an unresolved new branch scope while retaining the old branch", |
| 182 | (stack) => |
| 183 | Effect.gen(function* () { |
| 184 | yield* stack.destroy(); |
| 185 | const program = (second: boolean) => |
| 186 | Effect.gen(function* () { |
| 187 | const project = yield* Project("DataScopeProject", { region: "aws-us-east-2" }); |
| 188 | const a = yield* Branch("DataScopeA", { project }); |
| 189 | const authA = yield* Auth("DataScopeAuthA", { branch: a }); |
| 190 | const b = second ? yield* Branch("DataScopeB", { project }) : undefined; |
| 191 | const auth = b ? yield* Auth("DataScopeAuthB", { branch: b }) : authA; |
| 192 | const api = yield* DataApi("DataScopeApi", { |
| 193 | branch: { projectId: auth.projectId, branchId: auth.branchId }, |
| 194 | database: auth.database, |
| 195 | authProvider: "neon_auth", |
| 196 | }); |
| 197 | return { a, b, api }; |
| 198 | }); |
| 199 | const first = yield* stack.deploy(program(false)); |
| 200 | const updated = yield* stack.deploy(program(true)); |
| 201 | expect(updated.api.branchId).toBe(updated.b!.branchId); |
| 202 | expect(updated.a.branchId).toBe(first.a.branchId); |
| 203 | expect( |
| 204 | yield* SDK.getProjectBranchDataAPI({ |
| 205 | project_id: first.api.projectId, |
| 206 | branch_id: first.api.branchId, |
| 207 | database_name: first.api.database, |
| 208 | }).pipe( |
| 209 | Effect.as(false), |
| 210 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 211 | ), |
| 212 | ).toBe(true); |
| 213 | expect( |
| 214 | (yield* SDK.getNeonAuth({ project_id: first.api.projectId, branch_id: first.api.branchId })) |
| 215 | .base_url, |
| 216 | ).toBeDefined(); |
| 217 | yield* stack.destroy(); |
| 218 | expect( |
| 219 | yield* SDK.getProjectBranchDataAPI({ |
| 220 | project_id: updated.api.projectId, |
| 221 | branch_id: updated.api.branchId, |
| 222 | database_name: updated.api.database, |
| 223 | }).pipe( |
| 224 | Effect.as(false), |
| 225 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 226 | ), |
| 227 | ).toBe(true); |
| 228 | yield* stack.destroy(); |
| 229 | }), |
| 230 | { |
| 231 | tags: [ |
| 232 | "provider:neon", |
| 233 | "provider:neon:auth", |
| 234 | "provider:neon:branch", |
| 235 | "provider:neon:dataapi", |
| 236 | "provider:neon:project", |
| 237 | "live", |
| 238 | ], |
| 239 | timeout: 120_000, |
| 240 | }, |
| 241 | ); |
| 242 | |
| 243 | test.provider( |
| 244 | "Data API lifecycle preserves identity and synchronizes observed settings", |
| 245 | (stack) => |
| 246 | Effect.gen(function* () { |
| 247 | yield* stack.destroy(); |
| 248 | const application = (rows: number | undefined) => |
| 249 | Effect.gen(function* () { |
| 250 | const project = yield* Project("DataApiProject", { region: "aws-us-east-2" }); |
| 251 | const branch = yield* Branch("DataApiBranch", { project }); |
| 252 | const auth = yield* Auth("DataApiAuth", { branch }); |
| 253 | const api = yield* DataApi("DataApi", { |
| 254 | branch: { projectId: auth.projectId, branchId: auth.branchId }, |
| 255 | authProvider: "neon_auth", |
| 256 | settings: { db_max_rows: rows }, |
| 257 | }); |
| 258 | return { api, branch }; |
| 259 | }); |
| 260 | const first = yield* stack.deploy(application(10)); |
| 261 | const request = { |
| 262 | project_id: first.api.projectId, |
| 263 | branch_id: first.api.branchId, |
| 264 | database_name: first.api.database, |
| 265 | }; |
| 266 | expect((yield* SDK.getProjectBranchDataAPI(request)).url).toBe(first.api.url); |
| 267 | const unchanged = yield* stack.deploy(application(10)); |
| 268 | expect(unchanged.api.url).toBe(first.api.url); |
| 269 | const updated = yield* stack.deploy(application(20)); |
| 270 | expect(updated.api.url).toBe(first.api.url); |
| 271 | expect((yield* SDK.getProjectBranchDataAPI(request)).settings?.db_max_rows).toBe(20); |
| 272 | expect( |
| 273 | yield* stack.deploy(application(undefined)).pipe( |
| 274 | Effect.as(false), |
| 275 | Effect.catchTag("InvalidDataApiConfiguration", () => Effect.succeed(true)), |
| 276 | ), |
| 277 | ).toBe(true); |
| 278 | expect((yield* SDK.getProjectBranchDataAPI(request)).settings?.db_max_rows).toBe(20); |
| 279 | const http = yield* HttpClient.HttpClient; |
| 280 | const denied = yield* http.get(first.api.url, { |
| 281 | headers: { authorization: "Bearer invalid-user-token" }, |
| 282 | }); |
| 283 | expect(denied.status).toBe(400); |
| 284 | expect(yield* denied.json).toMatchObject({ |
| 285 | message: "Provided authentication token is not a valid JWT encoding", |
| 286 | }); |
| 287 | yield* stack.destroy(); |
| 288 | expect( |
| 289 | yield* SDK.getProjectBranchDataAPI(request).pipe( |
| 290 | Effect.as(false), |
| 291 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 292 | ), |
| 293 | ).toBe(true); |
| 294 | yield* stack.destroy(); |
| 295 | }), |
| 296 | { |
| 297 | tags: [ |
| 298 | "provider:neon", |
| 299 | "provider:neon:auth", |
| 300 | "provider:neon:branch", |
| 301 | "provider:neon:dataapi", |
| 302 | "provider:neon:project", |
| 303 | "live", |
| 304 | ], |
| 305 | timeout: 120_000, |
| 306 | }, |
| 307 | ); |