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 AWS from "@alchemy.run/aws"; |
| 2 | import { Namespace, Table, TableBucket } from "@alchemy.run/aws/S3Tables"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as s3tables from "@distilled.cloud/aws/s3tables"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import type { ScopedPlanStatusSession } from "alchemy/Report"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Result from "effect/Result"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | const stubSession = { |
| 15 | note: () => Effect.void, |
| 16 | } as unknown as ScopedPlanStatusSession; |
| 17 | |
| 18 | // Auto-generated table-bucket names are `${testNameSlug}-${id}-${stage}-` |
| 19 | // truncated + a random instance-id suffix. A run killed mid-deploy (before |
| 20 | // state persists) leaves buckets that `stack.destroy()` cannot see, and the |
| 21 | // suffix changes on every run so a re-run never reclaims them by name. Sweep |
| 22 | // by the deterministic prefix at test start so a passing run always implies |
| 23 | // a clean account. MUST match the lifecycle test's name slug below. |
| 24 | const LIFECYCLE_BUCKET_PREFIX = "table-bucket-namespace-table-lifecycle-"; |
| 25 | |
| 26 | const purgeOrphanedTableBuckets = Effect.gen(function* () { |
| 27 | const buckets = yield* s3tables.listTableBuckets.pages({}).pipe( |
| 28 | Stream.runCollect, |
| 29 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.tableBuckets ?? [])), |
| 30 | ); |
| 31 | yield* Effect.forEach( |
| 32 | buckets.filter((b) => b.name.startsWith(LIFECYCLE_BUCKET_PREFIX)), |
| 33 | (bucket) => |
| 34 | Effect.gen(function* () { |
| 35 | const tables = yield* s3tables.listTables.pages({ tableBucketARN: bucket.arn }).pipe( |
| 36 | Stream.runCollect, |
| 37 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.tables ?? [])), |
| 38 | ); |
| 39 | yield* Effect.forEach(tables, (t) => |
| 40 | s3tables |
| 41 | .deleteTable({ |
| 42 | tableBucketARN: bucket.arn, |
| 43 | namespace: t.namespace[0]!, |
| 44 | name: t.name, |
| 45 | }) |
| 46 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)), |
| 47 | ); |
| 48 | const namespaces = yield* s3tables.listNamespaces |
| 49 | .pages({ tableBucketARN: bucket.arn }) |
| 50 | .pipe( |
| 51 | Stream.runCollect, |
| 52 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.namespaces ?? [])), |
| 53 | ); |
| 54 | yield* Effect.forEach(namespaces, (ns) => |
| 55 | s3tables |
| 56 | .deleteNamespace({ |
| 57 | tableBucketARN: bucket.arn, |
| 58 | namespace: ns.namespace[0]!, |
| 59 | }) |
| 60 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)), |
| 61 | ); |
| 62 | yield* s3tables.deleteTableBucket({ tableBucketARN: bucket.arn }).pipe( |
| 63 | // Child deletes are eventually consistent; ride out the window. |
| 64 | Effect.retry({ |
| 65 | while: (e) => e._tag === "ConflictException", |
| 66 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 67 | }), |
| 68 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 69 | ); |
| 70 | }), |
| 71 | ); |
| 72 | }); |
| 73 | |
| 74 | // Poll a getter until it reports the resource is gone (typed |
| 75 | // NotFoundException), bounded so a stuck delete fails fast. |
| 76 | const waitUntilGone = <A, E extends { _tag: string }, R>(read: Effect.Effect<A, E, R>) => |
| 77 | read.pipe( |
| 78 | Effect.flatMap(() => Effect.fail({ _tag: "StillExists" as const } as const)), |
| 79 | Effect.retry({ |
| 80 | while: (e: { _tag: string }) => e._tag === "StillExists", |
| 81 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 82 | }), |
| 83 | Effect.catchIf( |
| 84 | (e): e is E & { _tag: "NotFoundException" } => e._tag === "NotFoundException", |
| 85 | () => Effect.void, |
| 86 | ), |
| 87 | ); |
| 88 | |
| 89 | test.provider( |
| 90 | "table bucket → namespace → table lifecycle", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | yield* stack.destroy(); |
| 94 | yield* purgeOrphanedTableBuckets; |
| 95 | |
| 96 | const { bucket, namespace, table } = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | const bucket = yield* TableBucket("Analytics"); |
| 99 | const namespace = yield* Namespace("Events", { |
| 100 | tableBucket: bucket.tableBucketArn, |
| 101 | }); |
| 102 | const table = yield* Table("PageViews", { |
| 103 | tableBucket: bucket.tableBucketArn, |
| 104 | namespace: namespace.namespace, |
| 105 | schema: { |
| 106 | fields: [ |
| 107 | { name: "id", type: "long", required: true }, |
| 108 | { name: "url", type: "string" }, |
| 109 | { name: "ts", type: "timestamp" }, |
| 110 | ], |
| 111 | }, |
| 112 | }); |
| 113 | return { bucket, namespace, table }; |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(bucket.tableBucketArn).toContain(":bucket/"); |
| 118 | expect(bucket.ownerAccountId).toBeDefined(); |
| 119 | expect(table.tableArn).toContain("/table/"); |
| 120 | expect(table.format).toBe("ICEBERG"); |
| 121 | expect(table.warehouseLocation).toBeDefined(); |
| 122 | |
| 123 | // Out-of-band verification via distilled. |
| 124 | const observedBucket = yield* s3tables.getTableBucket({ |
| 125 | tableBucketARN: bucket.tableBucketArn, |
| 126 | }); |
| 127 | expect(observedBucket.name).toBe(bucket.name); |
| 128 | |
| 129 | const observedNs = yield* s3tables.getNamespace({ |
| 130 | tableBucketARN: bucket.tableBucketArn, |
| 131 | namespace: namespace.namespace, |
| 132 | }); |
| 133 | expect(observedNs.namespace[0]).toBe(namespace.namespace); |
| 134 | |
| 135 | const observedTable = yield* s3tables.getTable({ |
| 136 | tableBucketARN: bucket.tableBucketArn, |
| 137 | namespace: namespace.namespace, |
| 138 | name: table.name, |
| 139 | }); |
| 140 | expect(observedTable.tableARN).toBe(table.tableArn); |
| 141 | |
| 142 | // Idempotent re-deploy: reconciler observes existing cloud state and |
| 143 | // converges without error. |
| 144 | yield* stack.deploy( |
| 145 | Effect.gen(function* () { |
| 146 | const bucket = yield* TableBucket("Analytics"); |
| 147 | const namespace = yield* Namespace("Events", { |
| 148 | tableBucket: bucket.tableBucketArn, |
| 149 | }); |
| 150 | yield* Table("PageViews", { |
| 151 | tableBucket: bucket.tableBucketArn, |
| 152 | namespace: namespace.namespace, |
| 153 | schema: { |
| 154 | fields: [ |
| 155 | { name: "id", type: "long", required: true }, |
| 156 | { name: "url", type: "string" }, |
| 157 | { name: "ts", type: "timestamp" }, |
| 158 | ], |
| 159 | }, |
| 160 | }); |
| 161 | return { bucket }; |
| 162 | }), |
| 163 | ); |
| 164 | |
| 165 | yield* stack.destroy(); |
| 166 | |
| 167 | // The bucket (and everything under it) must be gone. |
| 168 | yield* waitUntilGone(s3tables.getTableBucket({ tableBucketARN: bucket.tableBucketArn })); |
| 169 | }), |
| 170 | { tags: ["provider:aws", "provider:aws:s3tables", "live"], timeout: 180_000 }, |
| 171 | ); |
| 172 | |
| 173 | test.provider( |
| 174 | "table bucket replaces on name change", |
| 175 | (stack) => |
| 176 | Effect.gen(function* () { |
| 177 | yield* stack.destroy(); |
| 178 | |
| 179 | const first = yield* stack.deploy( |
| 180 | Effect.gen(function* () { |
| 181 | return yield* TableBucket("Renamed"); |
| 182 | }), |
| 183 | ); |
| 184 | expect(first.name).toBeDefined(); |
| 185 | yield* s3tables.getTableBucket({ tableBucketARN: first.tableBucketArn }); |
| 186 | |
| 187 | // Derive the replacement name from the engine-generated physical name. |
| 188 | // Reusing fixed bucket names across test runs races AWS's deletion |
| 189 | // tombstone and can return Conflict for minutes after Get reports 404. |
| 190 | const replacementName = `${first.name.slice(0, 55)}-renamed`; |
| 191 | const second = yield* stack.deploy( |
| 192 | Effect.gen(function* () { |
| 193 | return yield* TableBucket("Renamed", { |
| 194 | name: replacementName, |
| 195 | }); |
| 196 | }), |
| 197 | ); |
| 198 | expect(second.name).toBe(replacementName); |
| 199 | expect(second.tableBucketArn).not.toBe(first.tableBucketArn); |
| 200 | |
| 201 | // The old bucket must have been deleted as part of the replacement. |
| 202 | yield* waitUntilGone(s3tables.getTableBucket({ tableBucketARN: first.tableBucketArn })); |
| 203 | |
| 204 | yield* stack.destroy(); |
| 205 | yield* waitUntilGone(s3tables.getTableBucket({ tableBucketARN: second.tableBucketArn })); |
| 206 | }), |
| 207 | { tags: ["provider:aws", "provider:aws:s3tables", "live"], timeout: 180_000 }, |
| 208 | ); |
| 209 | |
| 210 | test.provider( |
| 211 | "ordinary bucket delete protects untracked children; force purges them", |
| 212 | (stack) => |
| 213 | Effect.gen(function* () { |
| 214 | const provider = yield* Provider.findProvider(TableBucket); |
| 215 | const initialDestroy = yield* Effect.result(stack.destroy()); |
| 216 | if (Result.isFailure(initialDestroy)) { |
| 217 | // Recover only this test's deterministic leftovers from an interrupted |
| 218 | // protection assertion. Ordinary destroy intentionally cannot remove |
| 219 | // them, so use the same explicit force path nuke uses. |
| 220 | const stale = (yield* provider.list()).filter((bucket) => |
| 221 | bucket.name.startsWith("ordinary-bucket-delete-protects-untracked-child"), |
| 222 | ); |
| 223 | yield* Effect.forEach( |
| 224 | stale, |
| 225 | (output) => |
| 226 | provider.delete({ |
| 227 | id: "CascadeDeleteRecovery", |
| 228 | fqn: "CascadeDeleteRecovery", |
| 229 | instanceId: "force-delete-recovery", |
| 230 | olds: {}, |
| 231 | output, |
| 232 | session: stubSession, |
| 233 | bindings: [], |
| 234 | force: true, |
| 235 | }), |
| 236 | { discard: true }, |
| 237 | ); |
| 238 | yield* stack.destroy(); |
| 239 | } |
| 240 | |
| 241 | const bucket = yield* stack.deploy( |
| 242 | Effect.gen(function* () { |
| 243 | return yield* TableBucket("CascadeDelete"); |
| 244 | }), |
| 245 | ); |
| 246 | // Simulate a run interrupted before child state was persisted. Global |
| 247 | // nuke can see the bucket but cannot enumerate Namespace providers. |
| 248 | yield* s3tables.createNamespace({ |
| 249 | tableBucketARN: bucket.tableBucketArn, |
| 250 | namespace: ["untracked_child"], |
| 251 | }); |
| 252 | |
| 253 | const deleteInput = { |
| 254 | id: "CascadeDelete", |
| 255 | fqn: "CascadeDelete", |
| 256 | instanceId: "force-delete-test", |
| 257 | olds: {}, |
| 258 | output: bucket, |
| 259 | session: stubSession, |
| 260 | bindings: [], |
| 261 | }; |
| 262 | const protectedDelete = yield* Effect.result(provider.delete(deleteInput)); |
| 263 | expect(Result.isFailure(protectedDelete)).toBe(true); |
| 264 | if (Result.isFailure(protectedDelete)) { |
| 265 | expect(protectedDelete.failure._tag).toBe("BadRequestException"); |
| 266 | } |
| 267 | yield* s3tables.getNamespace({ |
| 268 | tableBucketARN: bucket.tableBucketArn, |
| 269 | namespace: "untracked_child", |
| 270 | }); |
| 271 | |
| 272 | yield* provider.delete({ ...deleteInput, force: true }); |
| 273 | yield* waitUntilGone(s3tables.getTableBucket({ tableBucketARN: bucket.tableBucketArn })); |
| 274 | // Clear the now-stale stack state through the normal idempotent path. |
| 275 | yield* stack.destroy(); |
| 276 | }), |
| 277 | { tags: ["provider:aws", "provider:aws:s3tables", "live"], timeout: 120_000 }, |
| 278 | ); |