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 { Bucket, bucketStorageClient, type BucketProps } from "@alchemy.run/neon/Bucket"; |
| 3 | import { Credential } from "@alchemy.run/neon/Credential"; |
| 4 | import { storageBodyBytes } from "@alchemy.run/neon/Object"; |
| 5 | import { Project } from "@alchemy.run/neon/Project"; |
| 6 | import { providers } from "@alchemy.run/neon/Providers"; |
| 7 | import { makeStorageClient } from "@alchemy.run/neon/Storage"; |
| 8 | import { expect } from "@alchemy.run/test"; |
| 9 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 10 | import * as SDK from "@distilled.cloud/neon"; |
| 11 | import { adopt, Unowned } from "alchemy/AdoptPolicy"; |
| 12 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 13 | import * as Provider from "alchemy/Provider"; |
| 14 | import { ResourceContext } from "alchemy/ResourceContext"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as HttpClient from "effect/http/HttpClient"; |
| 17 | import * as Result from "effect/Result"; |
| 18 | import { resourceContext } from "../Util/ResourceContext.ts"; |
| 19 | |
| 20 | const { test } = Test.make({ providers: providers() }); |
| 21 | |
| 22 | test.provider( |
| 23 | "never-created buckets without branch identity are absent", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | const provider = yield* Provider.findProvider(Bucket); |
| 28 | const cases: BucketProps[] = [ |
| 29 | { branch: { projectId: "", branchId: "" } }, |
| 30 | { branch: { projectId: "project", branchId: "" } }, |
| 31 | { branch: { projectId: "", branchId: "branch" } }, |
| 32 | { project: { projectId: "" } }, |
| 33 | ]; |
| 34 | for (const olds of cases) { |
| 35 | expect( |
| 36 | yield* provider.read!({ |
| 37 | id: "Incomplete", |
| 38 | fqn: "Incomplete", |
| 39 | instanceId: "never-created", |
| 40 | olds, |
| 41 | output: undefined, |
| 42 | }), |
| 43 | ).toBeUndefined(); |
| 44 | } |
| 45 | yield* stack.destroy(); |
| 46 | }), |
| 47 | { tags: ["provider:neon", "provider:neon:bucket", "live"] }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "bucket recovery observes tags even without cached identity or explicit name", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | const instanceId = "0123456789abcdef0123456789abcdef"; |
| 56 | const bucket = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const project = yield* Project("RecoveryProject", { region: "aws-us-east-2" }); |
| 59 | const name = yield* createPhysicalName({ |
| 60 | id: "Recovered", |
| 61 | instanceId, |
| 62 | maxLength: 63, |
| 63 | lowercase: true, |
| 64 | }); |
| 65 | return yield* Bucket("Recovered", { project, name, forceDestroy: true }); |
| 66 | }), |
| 67 | ); |
| 68 | const client = yield* bucketStorageClient(bucket); |
| 69 | const provider = yield* Provider.findProvider(Bucket); |
| 70 | const olds: BucketProps = { |
| 71 | branch: { projectId: bucket.projectId, branchId: bucket.branchId }, |
| 72 | credential: bucket.credential, |
| 73 | forceDestroy: true, |
| 74 | }; |
| 75 | const read = provider.read!({ |
| 76 | id: "Recovered", |
| 77 | fqn: "Recovered", |
| 78 | instanceId, |
| 79 | olds, |
| 80 | output: undefined, |
| 81 | }).pipe(Effect.provideService(ResourceContext, resourceContext(instanceId))); |
| 82 | yield* Effect.gen(function* () { |
| 83 | expect(Unowned.is(yield* read)).toBe(false); |
| 84 | expect((yield* read)?.bucketName).toBe(bucket.bucketName); |
| 85 | yield* client.putTags({}); |
| 86 | expect(Unowned.is(yield* read)).toBe(true); |
| 87 | yield* client.putTags({ "alchemy::stack": "another-stack" }); |
| 88 | expect(Unowned.is(yield* read)).toBe(true); |
| 89 | expect( |
| 90 | Unowned.is( |
| 91 | yield* provider.read!({ |
| 92 | id: "Recovered", |
| 93 | fqn: "Recovered", |
| 94 | instanceId, |
| 95 | olds, |
| 96 | output: bucket, |
| 97 | }), |
| 98 | ), |
| 99 | ).toBe(true); |
| 100 | }).pipe(Effect.ensuring(client.putTags(bucket.tags).pipe(Effect.orDie))); |
| 101 | yield* stack.destroy(); |
| 102 | expect( |
| 103 | yield* SDK.getProject({ project_id: bucket.projectId }).pipe( |
| 104 | Effect.as(false), |
| 105 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 106 | ), |
| 107 | ).toBe(true); |
| 108 | yield* stack.destroy(); |
| 109 | }), |
| 110 | { |
| 111 | tags: ["provider:neon", "provider:neon:bucket", "provider:neon:project", "live"], |
| 112 | timeout: 120_000, |
| 113 | }, |
| 114 | ); |
| 115 | |
| 116 | test.provider( |
| 117 | "bucket tags CORS anonymous access and multipart cleanup", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | yield* stack.destroy(); |
| 121 | const deploy = (updated: boolean) => |
| 122 | stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | const project = yield* Project("BucketProject", { region: "aws-us-east-2" }); |
| 125 | const bucket = yield* Bucket("Private", { |
| 126 | project, |
| 127 | tags: updated ? { phase: "updated" } : { phase: "initial", removed: "yes" }, |
| 128 | cors: updated |
| 129 | ? [] |
| 130 | : [{ AllowedOrigins: ["https://example.com"], AllowedMethods: ["GET", "PUT"] }], |
| 131 | forceDestroy: true, |
| 132 | }); |
| 133 | const publicBucket = yield* Bucket("Public", { |
| 134 | project, |
| 135 | access: "public_read", |
| 136 | forceDestroy: true, |
| 137 | }); |
| 138 | return { project, bucket, publicBucket }; |
| 139 | }), |
| 140 | ); |
| 141 | const first = yield* deploy(false); |
| 142 | const listing = yield* SDK.listProjectBranchBuckets({ |
| 143 | project_id: first.bucket.projectId, |
| 144 | branch_id: first.bucket.branchId, |
| 145 | }); |
| 146 | expect(listing.buckets.some((bucket) => bucket.name === first.bucket.bucketName)).toBe(true); |
| 147 | const client = yield* bucketStorageClient(first.bucket); |
| 148 | yield* client.put("private.txt", "private"); |
| 149 | const publicClient = yield* bucketStorageClient(first.publicBucket); |
| 150 | yield* publicClient.put("public.txt", "public"); |
| 151 | const http = yield* HttpClient.HttpClient; |
| 152 | const privateRead = yield* http.get( |
| 153 | `${first.bucket.endpoint}/${first.bucket.bucketName}/private.txt`, |
| 154 | ); |
| 155 | expect([401, 403]).toContain(privateRead.status); |
| 156 | const publicRead = yield* http.get( |
| 157 | `${first.publicBucket.endpoint}/${first.publicBucket.bucketName}/public.txt`, |
| 158 | ); |
| 159 | expect(publicRead.status).toBe(200); |
| 160 | expect(yield* publicRead.text).toBe("public"); |
| 161 | const updated = yield* deploy(true); |
| 162 | expect(updated.bucket.bucketName).toBe(first.bucket.bucketName); |
| 163 | const observed = yield* (yield* bucketStorageClient(updated.bucket)).getTags(); |
| 164 | expect(observed.TagSet).toContainEqual({ Key: "phase", Value: "updated" }); |
| 165 | expect(observed.TagSet?.some((tag) => tag.Key === "removed")).toBe(false); |
| 166 | expect((yield* client.getCors()).CORSRules ?? []).toEqual([]); |
| 167 | const upload = yield* client.createMultipartUpload("unfinished.bin"); |
| 168 | expect(upload.UploadId).toBeDefined(); |
| 169 | yield* client.uploadPart("unfinished.bin", upload.UploadId!, 1, new Uint8Array([1, 2, 3])); |
| 170 | const completed = yield* client.createMultipartUpload("completed.bin"); |
| 171 | const part = yield* client.uploadPart( |
| 172 | "completed.bin", |
| 173 | completed.UploadId!, |
| 174 | 1, |
| 175 | new Uint8Array([4, 5, 6]), |
| 176 | ); |
| 177 | expect((yield* client.listParts("completed.bin", completed.UploadId!)).Parts?.length).toBe(1); |
| 178 | yield* client.completeMultipartUpload("completed.bin", completed.UploadId!, [ |
| 179 | { ETag: part.ETag, PartNumber: 1 }, |
| 180 | ]); |
| 181 | expect((yield* client.head("completed.bin"))?.ContentLength).toBe(3); |
| 182 | yield* stack.destroy(); |
| 183 | yield* stack.destroy(); |
| 184 | }), |
| 185 | { |
| 186 | tags: ["provider:neon", "provider:neon:bucket", "provider:neon:project", "live"], |
| 187 | timeout: 120_000, |
| 188 | }, |
| 189 | ); |
| 190 | |
| 191 | test.provider( |
| 192 | "nonempty bucket deletion and visibility updates fail without destroying data", |
| 193 | (stack) => |
| 194 | Effect.gen(function* () { |
| 195 | yield* stack.destroy(); |
| 196 | const deploy = (forceDestroy = false, access: "private" | "public_read" = "private") => |
| 197 | stack.deploy( |
| 198 | Effect.gen(function* () { |
| 199 | const project = yield* Project("SafeBucketProject", { region: "aws-us-east-2" }); |
| 200 | return yield* Bucket("SafeBucket", { project, forceDestroy, access }); |
| 201 | }), |
| 202 | ); |
| 203 | const bucket = yield* deploy(); |
| 204 | const client = yield* bucketStorageClient(bucket); |
| 205 | yield* client.put("retained.txt", "preserve me"); |
| 206 | const visibility = yield* deploy(false, "public_read").pipe(Effect.result); |
| 207 | expect(Result.isFailure(visibility)).toBe(true); |
| 208 | const listing = yield* SDK.listProjectBranchBuckets({ |
| 209 | project_id: bucket.projectId, |
| 210 | branch_id: bucket.branchId, |
| 211 | }); |
| 212 | expect(listing.buckets.find((item) => item.name === bucket.bucketName)?.access_level).toBe( |
| 213 | "private", |
| 214 | ); |
| 215 | expect((yield* client.head("retained.txt"))?.ContentLength).toBe(11); |
| 216 | yield* deploy(); |
| 217 | const deletion = yield* stack.destroy().pipe(Effect.result); |
| 218 | expect(Result.isFailure(deletion)).toBe(true); |
| 219 | expect((yield* client.head("retained.txt"))?.ContentLength).toBe(11); |
| 220 | yield* deploy(true); |
| 221 | yield* stack.destroy(); |
| 222 | yield* stack.destroy(); |
| 223 | }), |
| 224 | { |
| 225 | tags: ["provider:neon", "provider:neon:bucket", "provider:neon:project", "live"], |
| 226 | timeout: 120_000, |
| 227 | }, |
| 228 | ); |
| 229 | |
| 230 | test.provider( |
| 231 | "child storage inherits files while writes and deletes remain branch-local", |
| 232 | (stack) => |
| 233 | Effect.gen(function* () { |
| 234 | yield* stack.destroy(); |
| 235 | const parentProgram = Effect.gen(function* () { |
| 236 | const project = yield* Project("StorageLineageProject", { region: "aws-us-east-2" }); |
| 237 | const bucket = yield* Bucket("ParentBucket", { |
| 238 | project, |
| 239 | tags: { lineage: "parent" }, |
| 240 | cors: [{ AllowedOrigins: ["https://parent.example.com"], AllowedMethods: ["GET"] }], |
| 241 | forceDestroy: true, |
| 242 | }); |
| 243 | return { project, bucket }; |
| 244 | }); |
| 245 | const parent = yield* stack.deploy(parentProgram); |
| 246 | const outcome = yield* Effect.gen(function* () { |
| 247 | const parentClient = yield* bucketStorageClient(parent.bucket); |
| 248 | yield* parentClient.put("inherited.txt", "parent"); |
| 249 | const parentTags = yield* parentClient.getTags(); |
| 250 | const parentCors = yield* parentClient.getCors(); |
| 251 | const branchProgram = Effect.gen(function* () { |
| 252 | const { project, bucket } = yield* parentProgram; |
| 253 | const branch = yield* Branch("ChildBranch", { project }); |
| 254 | return { branch, bucket }; |
| 255 | }); |
| 256 | const { branch } = yield* stack.deploy(branchProgram); |
| 257 | const provider = yield* Provider.findProvider(Bucket); |
| 258 | expect( |
| 259 | Unowned.is( |
| 260 | yield* provider.read!({ |
| 261 | id: "ChildBucket", |
| 262 | fqn: "ChildBucket", |
| 263 | instanceId: "inherited-child", |
| 264 | olds: { |
| 265 | branch, |
| 266 | name: parent.bucket.bucketName, |
| 267 | credential: parent.bucket.credential, |
| 268 | }, |
| 269 | output: undefined, |
| 270 | }), |
| 271 | ), |
| 272 | ).toBe(true); |
| 273 | const deployChild = (updated: boolean) => |
| 274 | stack.deploy( |
| 275 | Effect.gen(function* () { |
| 276 | const { branch, bucket: parentBucket } = yield* branchProgram; |
| 277 | const bucket = yield* Bucket("ChildBucket", { |
| 278 | branch, |
| 279 | name: parentBucket.bucketName, |
| 280 | tags: { lineage: updated ? "updated-child" : "child" }, |
| 281 | cors: updated |
| 282 | ? [] |
| 283 | : [{ AllowedOrigins: ["https://child.example.com"], AllowedMethods: ["GET"] }], |
| 284 | forceDestroy: true, |
| 285 | }).pipe(adopt(true)); |
| 286 | return { branch, bucket }; |
| 287 | }), |
| 288 | ); |
| 289 | const child = yield* deployChild(false); |
| 290 | expect(child.bucket.tags["alchemy::branch"]).toBe(child.branch.branchId); |
| 291 | expect(child.bucket.tags.lineage).toBe("child"); |
| 292 | expect(child.bucket.cors).toEqual([ |
| 293 | { AllowedOrigins: ["https://child.example.com"], AllowedMethods: ["GET"] }, |
| 294 | ]); |
| 295 | const childClient = yield* bucketStorageClient(child.bucket); |
| 296 | const inherited = yield* storageBodyBytes((yield* childClient.get("inherited.txt"))?.Body); |
| 297 | expect(yield* Effect.sync(() => new TextDecoder().decode(inherited))).toBe("parent"); |
| 298 | expect(yield* parentClient.getTags()).toEqual(parentTags); |
| 299 | expect(yield* parentClient.getCors()).toEqual(parentCors); |
| 300 | const updated = yield* deployChild(true); |
| 301 | expect(updated.bucket.bucketName).toBe(child.bucket.bucketName); |
| 302 | expect(updated.bucket.tags.lineage).toBe("updated-child"); |
| 303 | expect(updated.bucket.cors).toEqual([]); |
| 304 | expect(yield* parentClient.getTags()).toEqual(parentTags); |
| 305 | expect(yield* parentClient.getCors()).toEqual(parentCors); |
| 306 | const ancestorClient = yield* makeStorageClient( |
| 307 | { |
| 308 | endpoint: child.bucket.endpoint, |
| 309 | region: child.bucket.region, |
| 310 | accessKeyId: parent.bucket.credential.tokenId, |
| 311 | secretAccessKey: parent.bucket.credential.s3SecretAccessKey, |
| 312 | }, |
| 313 | child.bucket.bucketName, |
| 314 | ); |
| 315 | expect((yield* ancestorClient.head("inherited.txt"))?.ContentLength).toBe(6); |
| 316 | yield* childClient.put("inherited.txt", "child"); |
| 317 | const original = yield* storageBodyBytes((yield* parentClient.get("inherited.txt"))?.Body); |
| 318 | expect(yield* Effect.sync(() => new TextDecoder().decode(original))).toBe("parent"); |
| 319 | yield* childClient.delete("inherited.txt"); |
| 320 | expect(yield* childClient.get("inherited.txt")).toBeUndefined(); |
| 321 | expect((yield* parentClient.head("inherited.txt"))?.ContentLength).toBe(6); |
| 322 | yield* stack.deploy(parentProgram); |
| 323 | expect((yield* parentClient.head("inherited.txt"))?.ContentLength).toBe(6); |
| 324 | expect(yield* parentClient.getTags()).toEqual(parentTags); |
| 325 | expect(yield* parentClient.getCors()).toEqual(parentCors); |
| 326 | expect( |
| 327 | yield* SDK.getProjectBranch({ |
| 328 | project_id: branch.projectId, |
| 329 | branch_id: branch.branchId, |
| 330 | }).pipe( |
| 331 | Effect.as(false), |
| 332 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 333 | ), |
| 334 | ).toBe(true); |
| 335 | }).pipe(Effect.result); |
| 336 | yield* stack.destroy(); |
| 337 | expect( |
| 338 | yield* SDK.getProject({ project_id: parent.project.projectId }).pipe( |
| 339 | Effect.as(false), |
| 340 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 341 | ), |
| 342 | ).toBe(true); |
| 343 | yield* Effect.log("Inherited storage fixture project is absent"); |
| 344 | yield* stack.destroy(); |
| 345 | if (Result.isFailure(outcome)) return yield* Effect.fail(outcome.failure); |
| 346 | }), |
| 347 | { |
| 348 | tags: [ |
| 349 | "provider:neon", |
| 350 | "provider:neon:branch", |
| 351 | "provider:neon:bucket", |
| 352 | "provider:neon:object", |
| 353 | "provider:neon:project", |
| 354 | "live", |
| 355 | ], |
| 356 | timeout: 120_000, |
| 357 | }, |
| 358 | ); |
| 359 | |
| 360 | test.provider( |
| 361 | "inherited S3 data remains isolated and inherited tag rejection is typed", |
| 362 | (stack) => |
| 363 | Effect.gen(function* () { |
| 364 | yield* stack.destroy(); |
| 365 | const parentProgram = Effect.gen(function* () { |
| 366 | const project = yield* Project("StorageDataLineageProject", { region: "aws-us-east-2" }); |
| 367 | const bucket = yield* Bucket("SourceBucket", { project, forceDestroy: true }); |
| 368 | return { project, bucket }; |
| 369 | }); |
| 370 | const parent = yield* stack.deploy(parentProgram); |
| 371 | const parentClient = yield* bucketStorageClient(parent.bucket); |
| 372 | yield* parentClient.put("inherited.txt", "parent"); |
| 373 | const child = yield* stack.deploy( |
| 374 | Effect.gen(function* () { |
| 375 | const { project } = yield* parentProgram; |
| 376 | const branch = yield* Branch("DataChild", { project }); |
| 377 | const reader = yield* Credential("ChildReader", { branch, scopes: ["storage:read"] }); |
| 378 | return { branch, reader }; |
| 379 | }), |
| 380 | ); |
| 381 | const storage = yield* SDK.getProjectBranchStorage({ |
| 382 | project_id: child.branch.projectId, |
| 383 | branch_id: child.branch.branchId, |
| 384 | }); |
| 385 | const config = { |
| 386 | endpoint: storage.s3_endpoint, |
| 387 | region: storage.region, |
| 388 | accessKeyId: parent.bucket.credential.tokenId, |
| 389 | secretAccessKey: parent.bucket.credential.s3SecretAccessKey, |
| 390 | }; |
| 391 | const client = yield* makeStorageClient(config, parent.bucket.bucketName); |
| 392 | const reader = yield* makeStorageClient( |
| 393 | { |
| 394 | ...config, |
| 395 | accessKeyId: child.reader.tokenId, |
| 396 | secretAccessKey: child.reader.s3SecretAccessKey, |
| 397 | }, |
| 398 | parent.bucket.bucketName, |
| 399 | ); |
| 400 | const bytes = yield* storageBodyBytes((yield* reader.get("inherited.txt"))?.Body); |
| 401 | expect(yield* Effect.sync(() => new TextDecoder().decode(bytes))).toBe("parent"); |
| 402 | const denied = yield* reader.put("denied.txt", "must not write").pipe(Effect.result); |
| 403 | expect(Result.isFailure(denied)).toBe(true); |
| 404 | if (Result.isFailure(denied)) expect(denied.failure._tag).toBe("AccessDeniedException"); |
| 405 | const tagging = yield* client.putTags({ phase: "child" }).pipe(Effect.result); |
| 406 | expect(Result.isFailure(tagging)).toBe(true); |
| 407 | if (Result.isFailure(tagging)) expect(tagging.failure._tag).toBe("NoSuchBucket"); |
| 408 | yield* client.put("inherited.txt", "child"); |
| 409 | expect((yield* parentClient.head("inherited.txt"))?.ContentLength).toBe(6); |
| 410 | yield* client.delete("inherited.txt"); |
| 411 | expect(yield* client.get("inherited.txt")).toBeUndefined(); |
| 412 | const original = yield* storageBodyBytes((yield* parentClient.get("inherited.txt"))?.Body); |
| 413 | expect(yield* Effect.sync(() => new TextDecoder().decode(original))).toBe("parent"); |
| 414 | yield* stack.deploy(parentProgram); |
| 415 | expect((yield* parentClient.head("inherited.txt"))?.ContentLength).toBe(6); |
| 416 | yield* stack.destroy(); |
| 417 | yield* stack.destroy(); |
| 418 | }), |
| 419 | { |
| 420 | tags: [ |
| 421 | "provider:neon", |
| 422 | "provider:neon:branch", |
| 423 | "provider:neon:bucket", |
| 424 | "provider:neon:credential", |
| 425 | "provider:neon:object", |
| 426 | "provider:neon:project", |
| 427 | "live", |
| 428 | ], |
| 429 | timeout: 120_000, |
| 430 | }, |
| 431 | ); |
| 432 | |
| 433 | test.provider( |
| 434 | "forceDestroy paginates more than one thousand objects", |
| 435 | (stack) => |
| 436 | Effect.gen(function* () { |
| 437 | yield* stack.destroy(); |
| 438 | const bucket = yield* stack.deploy( |
| 439 | Effect.gen(function* () { |
| 440 | const project = yield* Project("PaginatedBucketProject", { region: "aws-us-east-2" }); |
| 441 | return yield* Bucket("PaginatedBucket", { project, forceDestroy: true }); |
| 442 | }), |
| 443 | ); |
| 444 | const client = yield* bucketStorageClient(bucket); |
| 445 | yield* Effect.forEach( |
| 446 | Array.from({ length: 1001 }, (_, index) => index), |
| 447 | (index) => client.put(`page/${String(index).padStart(4, "0")}`, "x"), |
| 448 | { concurrency: 24 }, |
| 449 | ); |
| 450 | const first = yield* client.list({ limit: 1000 }); |
| 451 | expect(first.IsTruncated).toBe(true); |
| 452 | expect(first.Contents).toHaveLength(1000); |
| 453 | yield* stack.destroy(); |
| 454 | yield* stack.destroy(); |
| 455 | }), |
| 456 | { |
| 457 | tags: ["provider:neon", "provider:neon:bucket", "provider:neon:project", "live"], |
| 458 | timeout: 120_000, |
| 459 | }, |
| 460 | ); |