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 * as Prisma from "@alchemy.run/prisma"; |
| 2 | import { AmbiguousBucketAccessKeyError } from "@alchemy.run/prisma/BucketAccessKey"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import { |
| 6 | createBucketKey, |
| 7 | deleteBucket, |
| 8 | deleteBucketKey, |
| 9 | getBucket, |
| 10 | getBucketKeys, |
| 11 | updateBucket, |
| 12 | } from "@distilled.cloud/prisma/management"; |
| 13 | import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy"; |
| 14 | import * as Provider from "alchemy/Provider"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as Redacted from "effect/Redacted"; |
| 17 | import { |
| 18 | expectGone, |
| 19 | expectProjectGone, |
| 20 | failureOf, |
| 21 | forgetState, |
| 22 | markCreating, |
| 23 | patchStateAttr, |
| 24 | } from "./fixtures/Live.ts"; |
| 25 | |
| 26 | const live = Test.make({ providers: Prisma.providers() }); |
| 27 | |
| 28 | const liveTags = [ |
| 29 | "provider:prisma", |
| 30 | "provider:prisma:bucket", |
| 31 | "provider:prisma:branch", |
| 32 | "provider:prisma:project", |
| 33 | "live", |
| 34 | ]; |
| 35 | |
| 36 | const keyTags = [...liveTags, "provider:prisma:bucketaccesskey"]; |
| 37 | |
| 38 | const observeBucket = (bucketId: string) => |
| 39 | getBucket({ bucketId }).pipe(Effect.map((response) => response.data)); |
| 40 | |
| 41 | const expectBucketGone = (bucketId: string) => |
| 42 | expectGone( |
| 43 | getBucket({ bucketId }).pipe( |
| 44 | Effect.as(false), |
| 45 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 46 | ), |
| 47 | ); |
| 48 | |
| 49 | const bucketStack = (props: { name?: string; logicalId?: string; onBranch?: boolean } = {}) => |
| 50 | Effect.gen(function* () { |
| 51 | const project = yield* Prisma.Project("Project", { createDatabase: false }); |
| 52 | const branch = yield* Prisma.Branch("Feature", { project, gitName: "feature/bucket" }); |
| 53 | const bucket = yield* Prisma.Bucket("Uploads", { |
| 54 | project, |
| 55 | ...(props.name === undefined ? {} : { name: props.name }), |
| 56 | ...(props.logicalId === undefined ? {} : { logicalId: props.logicalId }), |
| 57 | ...(props.onBranch ? { branchId: branch.branchId } : {}), |
| 58 | }); |
| 59 | return { project, branch, bucket }; |
| 60 | }); |
| 61 | |
| 62 | const listKeys = (bucketId: string) => |
| 63 | getBucketKeys({ bucketId, limit: 100 }).pipe(Effect.map((page) => page.data)); |
| 64 | |
| 65 | const keyStack = ( |
| 66 | props: { role?: "read" | "read_write"; name?: string; bucket?: "first" | "second" } = {}, |
| 67 | ) => |
| 68 | Effect.gen(function* () { |
| 69 | const project = yield* Prisma.Project("Project", { createDatabase: false }); |
| 70 | const first = yield* Prisma.Bucket("First", { project }); |
| 71 | const second = yield* Prisma.Bucket("Second", { project }); |
| 72 | const key = yield* Prisma.BucketAccessKey("Key", { |
| 73 | bucket: props.bucket === "second" ? second : first, |
| 74 | role: props.role ?? "read_write", |
| 75 | ...(props.name === undefined ? {} : { name: props.name }), |
| 76 | }); |
| 77 | return { project, first, second, key }; |
| 78 | }); |
| 79 | |
| 80 | live.test.provider( |
| 81 | "creates a bucket under its fqn logical ID and renames, moves, and rebinds it in place", |
| 82 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 83 | yield* stack.destroy(); |
| 84 | |
| 85 | const initial = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 86 | expect(initial.bucket.logicalId).toBe("Uploads"); |
| 87 | const created = yield* observeBucket(initial.bucket.bucketId); |
| 88 | expect(created.project.id).toBe(initial.project.projectId); |
| 89 | expect(created.name).toBe("uploads"); |
| 90 | expect(created.logicalId).toBe("Uploads"); |
| 91 | |
| 92 | const renamed = yield* stack.deploy(bucketStack({ name: "uploads-renamed" })); |
| 93 | expect(renamed.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 94 | expect((yield* observeBucket(initial.bucket.bucketId)).name).toBe("uploads-renamed"); |
| 95 | |
| 96 | // Move, rename, and rebind in one deploy. The API refuses logicalId with a |
| 97 | // branch move, so the provider sets it in a second call. |
| 98 | const together = yield* stack.deploy( |
| 99 | bucketStack({ name: "uploads", onBranch: true, logicalId: "media" }), |
| 100 | ); |
| 101 | expect(together.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 102 | const moved = yield* observeBucket(initial.bucket.bucketId); |
| 103 | expect(moved.name).toBe("uploads"); |
| 104 | expect(moved.branchId).toBe(initial.branch.branchId); |
| 105 | expect(moved.logicalId).toBe("media"); |
| 106 | |
| 107 | // Removing the override returns to the fqn. |
| 108 | const reverted = yield* stack.deploy(bucketStack({ name: "uploads", onBranch: true })); |
| 109 | expect(reverted.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 110 | expect((yield* observeBucket(initial.bucket.bucketId)).logicalId).toBe("Uploads"); |
| 111 | |
| 112 | // Omitting branchId leaves the bucket on the branch it has. |
| 113 | const omitted = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 114 | expect(omitted.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 115 | expect((yield* observeBucket(initial.bucket.bucketId)).branchId).toBe(initial.branch.branchId); |
| 116 | |
| 117 | const repeated = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 118 | expect(repeated.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 119 | |
| 120 | // Nuke enumerates buckets through the provider's list. |
| 121 | const listed = yield* (yield* Provider.findProvider(Prisma.Bucket)).list!(); |
| 122 | expect(listed.map((bucket) => bucket.bucketId)).toContain(initial.bucket.bucketId); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | yield* expectBucketGone(initial.bucket.bucketId); |
| 126 | yield* expectProjectGone(initial.project.projectId); |
| 127 | }), |
| 128 | { tags: liveTags, timeout: 180_000 }, |
| 129 | ); |
| 130 | |
| 131 | live.test.provider( |
| 132 | "after lost state, adoption finds the bucket by its logical ID despite a Console rename", |
| 133 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 134 | yield* stack.destroy(); |
| 135 | |
| 136 | const initial = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 137 | yield* updateBucket({ bucketId: initial.bucket.bucketId, displayName: "renamed-in-console" }); |
| 138 | yield* forgetState(stack, "Uploads"); |
| 139 | |
| 140 | // A bucket has no generated name, so nothing proves it is ours. |
| 141 | const refused = yield* failureOf(stack.deploy(bucketStack({ name: "uploads" }))); |
| 142 | expect(refused.errors.some((error) => error instanceof OwnedBySomeoneElse)).toBe(true); |
| 143 | |
| 144 | const adopted = yield* stack.deploy( |
| 145 | Effect.gen(function* () { |
| 146 | const project = yield* Prisma.Project("Project", { createDatabase: false }); |
| 147 | const branch = yield* Prisma.Branch("Feature", { project, gitName: "feature/bucket" }); |
| 148 | const bucket = yield* Prisma.Bucket("Uploads", { project, name: "uploads" }).pipe( |
| 149 | adopt(true), |
| 150 | ); |
| 151 | return { project, branch, bucket }; |
| 152 | }), |
| 153 | ); |
| 154 | expect(adopted.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 155 | const observed = yield* observeBucket(initial.bucket.bucketId); |
| 156 | expect(observed.name).toBe("uploads"); |
| 157 | expect(observed.logicalId).toBe("Uploads"); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | yield* expectBucketGone(initial.bucket.bucketId); |
| 161 | yield* expectProjectGone(initial.project.projectId); |
| 162 | }), |
| 163 | { tags: liveTags, timeout: 180_000 }, |
| 164 | ); |
| 165 | |
| 166 | live.test.provider( |
| 167 | "sets the logical ID on a bucket deployed before logical IDs existed", |
| 168 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 169 | yield* stack.destroy(); |
| 170 | |
| 171 | const initial = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 172 | yield* updateBucket({ bucketId: initial.bucket.bucketId, logicalId: null }); |
| 173 | yield* patchStateAttr(stack, "Uploads", { logicalId: null }); |
| 174 | expect((yield* observeBucket(initial.bucket.bucketId)).logicalId).toBeNull(); |
| 175 | |
| 176 | const stamped = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 177 | expect(stamped.bucket.bucketId).toBe(initial.bucket.bucketId); |
| 178 | expect(stamped.bucket.logicalId).toBe("Uploads"); |
| 179 | expect((yield* observeBucket(initial.bucket.bucketId)).logicalId).toBe("Uploads"); |
| 180 | |
| 181 | yield* stack.destroy(); |
| 182 | yield* expectBucketGone(initial.bucket.bucketId); |
| 183 | yield* expectProjectGone(initial.project.projectId); |
| 184 | }), |
| 185 | { tags: liveTags, timeout: 180_000 }, |
| 186 | ); |
| 187 | |
| 188 | live.test.provider( |
| 189 | "replaces the bucket when its project changes", |
| 190 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 191 | yield* stack.destroy(); |
| 192 | |
| 193 | // Both projects exist before the move, so the new project ID is known at plan time. |
| 194 | const resources = (target: "first" | "second") => |
| 195 | Effect.gen(function* () { |
| 196 | const first = yield* Prisma.Project("First", { createDatabase: false }); |
| 197 | const second = yield* Prisma.Project("Second", { createDatabase: false }); |
| 198 | const bucket = yield* Prisma.Bucket("Uploads", { |
| 199 | project: target === "first" ? first : second, |
| 200 | name: "uploads", |
| 201 | }); |
| 202 | return { first, second, bucket }; |
| 203 | }); |
| 204 | |
| 205 | const initial = yield* stack.deploy(resources("first")); |
| 206 | const replaced = yield* stack.deploy(resources("second")); |
| 207 | expect(replaced.bucket.bucketId).not.toBe(initial.bucket.bucketId); |
| 208 | expect(replaced.bucket.projectId).toBe(initial.second.projectId); |
| 209 | const observed = yield* observeBucket(replaced.bucket.bucketId); |
| 210 | expect(observed.project.id).toBe(initial.second.projectId); |
| 211 | expect(observed.logicalId).toBe("Uploads"); |
| 212 | yield* expectBucketGone(initial.bucket.bucketId); |
| 213 | |
| 214 | yield* stack.destroy(); |
| 215 | yield* expectBucketGone(replaced.bucket.bucketId); |
| 216 | yield* expectProjectGone(initial.first.projectId); |
| 217 | yield* expectProjectGone(initial.second.projectId); |
| 218 | }), |
| 219 | { tags: liveTags, timeout: 180_000 }, |
| 220 | ); |
| 221 | |
| 222 | live.test.provider( |
| 223 | "rejects a logical ID that another bucket on the branch holds", |
| 224 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 225 | yield* stack.destroy(); |
| 226 | |
| 227 | const resources = (secondLogicalId: string) => |
| 228 | Effect.gen(function* () { |
| 229 | const project = yield* Prisma.Project("Project", { createDatabase: false }); |
| 230 | const first = yield* Prisma.Bucket("First", { project, logicalId: "shared" }); |
| 231 | const second = yield* Prisma.Bucket("Second", { project, logicalId: secondLogicalId }); |
| 232 | return { project, first, second }; |
| 233 | }); |
| 234 | |
| 235 | const initial = yield* stack.deploy(resources("second")); |
| 236 | const failure = yield* failureOf(stack.deploy(resources("shared"))); |
| 237 | expect(failure.text).toContain("logical ID 'shared'"); |
| 238 | expect((yield* observeBucket(initial.first.bucketId)).logicalId).toBe("shared"); |
| 239 | |
| 240 | yield* stack.destroy(); |
| 241 | yield* expectBucketGone(initial.first.bucketId); |
| 242 | yield* expectBucketGone(initial.second.bucketId); |
| 243 | yield* expectProjectGone(initial.project.projectId); |
| 244 | }), |
| 245 | { tags: liveTags, timeout: 180_000 }, |
| 246 | ); |
| 247 | |
| 248 | live.test.provider( |
| 249 | "recreates a bucket deleted out of band on the next update and destroys one already gone", |
| 250 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 251 | yield* stack.destroy(); |
| 252 | |
| 253 | const initial = yield* stack.deploy(bucketStack({ name: "uploads" })); |
| 254 | yield* deleteBucket({ bucketId: initial.bucket.bucketId }); |
| 255 | yield* expectBucketGone(initial.bucket.bucketId); |
| 256 | |
| 257 | const recreated = yield* stack.deploy(bucketStack({ name: "uploads-recreated" })); |
| 258 | expect(recreated.bucket.bucketId).not.toBe(initial.bucket.bucketId); |
| 259 | const observed = yield* observeBucket(recreated.bucket.bucketId); |
| 260 | expect(observed.name).toBe("uploads-recreated"); |
| 261 | expect(observed.logicalId).toBe("Uploads"); |
| 262 | |
| 263 | yield* deleteBucket({ bucketId: recreated.bucket.bucketId }); |
| 264 | yield* stack.destroy(); |
| 265 | yield* expectBucketGone(recreated.bucket.bucketId); |
| 266 | yield* expectProjectGone(initial.project.projectId); |
| 267 | }), |
| 268 | { tags: liveTags, timeout: 180_000 }, |
| 269 | ); |
| 270 | |
| 271 | live.test.provider( |
| 272 | "creates a key under its deterministic name and keeps its one-time secret", |
| 273 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 274 | yield* stack.destroy(); |
| 275 | |
| 276 | const initial = yield* stack.deploy(keyStack()); |
| 277 | expect(initial.key.bucketId).toBe(initial.first.bucketId); |
| 278 | expect(Redacted.isRedacted(initial.key.secretAccessKey)).toBe(true); |
| 279 | expect(Redacted.value(initial.key.secretAccessKey).length).toBeGreaterThan(0); |
| 280 | expect(initial.key.accessKeyId.length).toBeGreaterThan(0); |
| 281 | const keys = yield* listKeys(initial.first.bucketId); |
| 282 | const observed = keys.find((key) => key.id === initial.key.bucketAccessKeyId); |
| 283 | expect(observed?.name).toMatch(/^Key-[a-zA-Z0-9]{12}$/); |
| 284 | expect(observed?.role).toBe("read_write"); |
| 285 | |
| 286 | // The secret is revealed once; later deploys keep the persisted one. |
| 287 | const repeated = yield* stack.deploy(keyStack()); |
| 288 | expect(repeated.key.bucketAccessKeyId).toBe(initial.key.bucketAccessKeyId); |
| 289 | expect(Redacted.value(repeated.key.secretAccessKey)).toBe( |
| 290 | Redacted.value(initial.key.secretAccessKey), |
| 291 | ); |
| 292 | |
| 293 | yield* stack.destroy(); |
| 294 | yield* expectBucketGone(initial.first.bucketId); |
| 295 | yield* expectProjectGone(initial.project.projectId); |
| 296 | }), |
| 297 | { tags: keyTags, timeout: 180_000 }, |
| 298 | ); |
| 299 | |
| 300 | live.test.provider( |
| 301 | "replaces a key when its role, name, or bucket changes", |
| 302 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 303 | yield* stack.destroy(); |
| 304 | |
| 305 | const initial = yield* stack.deploy(keyStack()); |
| 306 | const keyGone = (bucketId: string, keyId: string) => |
| 307 | expectGone(listKeys(bucketId).pipe(Effect.map((keys) => !keys.some((k) => k.id === keyId)))); |
| 308 | |
| 309 | const byRole = yield* stack.deploy(keyStack({ role: "read" })); |
| 310 | expect(byRole.key.bucketAccessKeyId).not.toBe(initial.key.bucketAccessKeyId); |
| 311 | yield* keyGone(initial.first.bucketId, initial.key.bucketAccessKeyId); |
| 312 | const readKey = (yield* listKeys(initial.first.bucketId)).find( |
| 313 | (key) => key.id === byRole.key.bucketAccessKeyId, |
| 314 | ); |
| 315 | expect(readKey?.role).toBe("read"); |
| 316 | |
| 317 | const byName = yield* stack.deploy(keyStack({ role: "read", name: "reader" })); |
| 318 | expect(byName.key.bucketAccessKeyId).not.toBe(byRole.key.bucketAccessKeyId); |
| 319 | yield* keyGone(initial.first.bucketId, byRole.key.bucketAccessKeyId); |
| 320 | const named = (yield* listKeys(initial.first.bucketId)).find( |
| 321 | (key) => key.id === byName.key.bucketAccessKeyId, |
| 322 | ); |
| 323 | expect(named?.name).toMatch(/^reader-[a-zA-Z0-9]{12}$/); |
| 324 | |
| 325 | const byBucket = yield* stack.deploy( |
| 326 | keyStack({ role: "read", name: "reader", bucket: "second" }), |
| 327 | ); |
| 328 | expect(byBucket.key.bucketId).toBe(initial.second.bucketId); |
| 329 | yield* keyGone(initial.first.bucketId, byName.key.bucketAccessKeyId); |
| 330 | expect( |
| 331 | (yield* listKeys(initial.second.bucketId)).some( |
| 332 | (key) => key.id === byBucket.key.bucketAccessKeyId, |
| 333 | ), |
| 334 | ).toBe(true); |
| 335 | |
| 336 | // A key revoked out of band does not block destroy. |
| 337 | yield* deleteBucketKey({ |
| 338 | bucketId: initial.second.bucketId, |
| 339 | keyId: byBucket.key.bucketAccessKeyId, |
| 340 | }); |
| 341 | yield* stack.destroy(); |
| 342 | yield* expectBucketGone(initial.first.bucketId); |
| 343 | yield* expectBucketGone(initial.second.bucketId); |
| 344 | yield* expectProjectGone(initial.project.projectId); |
| 345 | }), |
| 346 | { tags: keyTags, timeout: 240_000 }, |
| 347 | ); |
| 348 | |
| 349 | live.test.provider( |
| 350 | "revokes the orphaned key of an interrupted create before minting a fresh one", |
| 351 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 352 | yield* stack.destroy(); |
| 353 | |
| 354 | const initial = yield* stack.deploy(keyStack()); |
| 355 | // The key exists, but its one-time secret never reached state. |
| 356 | yield* markCreating(stack, "Key"); |
| 357 | |
| 358 | const recovered = yield* stack.deploy(keyStack()); |
| 359 | expect(recovered.key.bucketAccessKeyId).not.toBe(initial.key.bucketAccessKeyId); |
| 360 | const keys = yield* listKeys(initial.first.bucketId); |
| 361 | expect(keys.map((key) => key.id)).toEqual([recovered.key.bucketAccessKeyId]); |
| 362 | |
| 363 | yield* stack.destroy(); |
| 364 | yield* expectBucketGone(initial.first.bucketId); |
| 365 | yield* expectProjectGone(initial.project.projectId); |
| 366 | }), |
| 367 | { tags: keyTags, timeout: 180_000 }, |
| 368 | ); |
| 369 | |
| 370 | live.test.provider( |
| 371 | "refuses to recover a key when two keys share its deterministic name", |
| 372 | Effect.fn(function* (stack: Test.ScratchStack) { |
| 373 | yield* stack.destroy(); |
| 374 | |
| 375 | const initial = yield* stack.deploy(keyStack()); |
| 376 | const name = (yield* listKeys(initial.first.bucketId)).find( |
| 377 | (key) => key.id === initial.key.bucketAccessKeyId, |
| 378 | )!.name; |
| 379 | const duplicate = yield* createBucketKey({ |
| 380 | bucketId: initial.first.bucketId, |
| 381 | name, |
| 382 | role: "read", |
| 383 | }); |
| 384 | yield* markCreating(stack, "Key"); |
| 385 | |
| 386 | const failure = yield* failureOf(stack.deploy(keyStack())); |
| 387 | expect(failure.errors.some((error) => error instanceof AmbiguousBucketAccessKeyError)).toBe( |
| 388 | true, |
| 389 | ); |
| 390 | // Neither key was revoked. |
| 391 | const ids = (yield* listKeys(initial.first.bucketId)).map((key) => key.id); |
| 392 | expect(ids).toContain(initial.key.bucketAccessKeyId); |
| 393 | expect(ids).toContain(duplicate.data.id); |
| 394 | |
| 395 | yield* deleteBucketKey({ bucketId: initial.first.bucketId, keyId: duplicate.data.id }); |
| 396 | yield* stack.destroy(); |
| 397 | yield* expectBucketGone(initial.first.bucketId); |
| 398 | yield* expectProjectGone(initial.project.projectId); |
| 399 | }), |
| 400 | { tags: keyTags, timeout: 180_000 }, |
| 401 | ); |