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 Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as r2 from "@distilled.cloud/cloudflare/r2"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { stageSuffix } from "../../names.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers(), state: Cloudflare.state() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const zoneName = process.env.CLOUDFLARE_TEST_R2_DOMAIN_ZONE_NAME ?? "alchemy-test-2.us"; |
| 17 | const suffix = stageSuffix(); |
| 18 | // A custom-domain hostname maps one-to-one to a bucket at the Cloudflare zone |
| 19 | // level, so it is a *global* resource. These suites run concurrently |
| 20 | // (`sequence.concurrent`), so every test must claim a hostname no other test |
| 21 | // uses — otherwise the tests race to attach the same hostname to different |
| 22 | // buckets and lose with `Conflict: Domain already in use`. |
| 23 | const domain = zoneName ? `alchemy-r2-test-single-${suffix}.${zoneName}` : undefined; |
| 24 | const domain2 = zoneName ? `alchemy-r2-test-multi-a-${suffix}.${zoneName}` : undefined; |
| 25 | const domain3 = zoneName ? `alchemy-r2-test-multi-b-${suffix}.${zoneName}` : undefined; |
| 26 | |
| 27 | test.provider( |
| 28 | "creates, updates, and deletes a bucket custom domain", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 32 | |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const bucket = yield* stack.deploy( |
| 36 | Effect.gen(function* () { |
| 37 | return yield* Cloudflare.R2.Bucket("DomainBucket", { |
| 38 | forceDestroy: true, |
| 39 | domains: [{ name: domain! }], |
| 40 | }); |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | expect(bucket.domains).toHaveLength(1); |
| 45 | expect(bucket.domains[0]?.domain).toEqual(domain); |
| 46 | expect(bucket.domains[0]?.enabled).toEqual(true); |
| 47 | |
| 48 | const actual = yield* r2.getBucketDomainCustom({ |
| 49 | accountId, |
| 50 | bucketName: bucket.bucketName, |
| 51 | domain: domain!, |
| 52 | jurisdiction: bucket.jurisdiction, |
| 53 | }); |
| 54 | expect(actual.domain).toEqual(domain); |
| 55 | |
| 56 | const updated = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* Cloudflare.R2.Bucket("DomainBucket", { |
| 59 | forceDestroy: true, |
| 60 | domains: [{ name: domain!, enabled: false }], |
| 61 | }); |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | expect(updated.domains[0]?.enabled).toEqual(false); |
| 66 | |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | const deleted = yield* r2 |
| 70 | .getBucketDomainCustom({ |
| 71 | accountId, |
| 72 | bucketName: bucket.bucketName, |
| 73 | domain: domain!, |
| 74 | jurisdiction: bucket.jurisdiction, |
| 75 | }) |
| 76 | .pipe( |
| 77 | Effect.map(() => false), |
| 78 | Effect.catchTag("DomainNotFound", () => Effect.succeed(true)), |
| 79 | Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)), |
| 80 | ); |
| 81 | expect(deleted).toEqual(true); |
| 82 | |
| 83 | yield* waitForBucketToBeDeleted(bucket.bucketName, accountId); |
| 84 | }).pipe(logLevel), |
| 85 | { tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"] }, |
| 86 | ); |
| 87 | |
| 88 | test.provider( |
| 89 | "creates, updates, and deletes a bucket with multiple custom domains", |
| 90 | (stack) => |
| 91 | Effect.gen(function* () { |
| 92 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | const bucket = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* Cloudflare.R2.Bucket("MultiDomainBucket", { |
| 99 | forceDestroy: true, |
| 100 | domains: [{ name: domain2! }, { name: domain3! }], |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(bucket.domains).toHaveLength(2); |
| 106 | const domainNames = bucket.domains.map((d) => d.domain).sort(); |
| 107 | expect(domainNames).toEqual([domain2, domain3].sort()); |
| 108 | expect(bucket.domains.every((d) => d.enabled)).toEqual(true); |
| 109 | |
| 110 | for (const name of [domain2!, domain3!]) { |
| 111 | const actual = yield* r2.getBucketDomainCustom({ |
| 112 | accountId, |
| 113 | bucketName: bucket.bucketName, |
| 114 | domain: name, |
| 115 | jurisdiction: bucket.jurisdiction, |
| 116 | }); |
| 117 | expect(actual.domain).toEqual(name); |
| 118 | } |
| 119 | |
| 120 | const updated = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Cloudflare.R2.Bucket("MultiDomainBucket", { |
| 123 | forceDestroy: true, |
| 124 | domains: [{ name: domain3!, enabled: false }, { name: domain2! }], |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | const updatedByName = Object.fromEntries(updated.domains.map((d) => [d.domain, d])); |
| 130 | expect(updatedByName[domain3!]?.enabled).toEqual(false); |
| 131 | expect(updatedByName[domain2!]?.enabled).toEqual(true); |
| 132 | |
| 133 | const removed = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* Cloudflare.R2.Bucket("MultiDomainBucket", { |
| 136 | forceDestroy: true, |
| 137 | domains: [{ name: domain2! }], |
| 138 | }); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | expect(removed.domains).toHaveLength(1); |
| 143 | expect(removed.domains[0]?.domain).toEqual(domain2); |
| 144 | |
| 145 | const firstRemoved = yield* r2 |
| 146 | .getBucketDomainCustom({ |
| 147 | accountId, |
| 148 | bucketName: bucket.bucketName, |
| 149 | domain: domain3!, |
| 150 | jurisdiction: bucket.jurisdiction, |
| 151 | }) |
| 152 | .pipe( |
| 153 | Effect.map(() => false), |
| 154 | Effect.catchTag("DomainNotFound", () => Effect.succeed(true)), |
| 155 | ); |
| 156 | expect(firstRemoved).toEqual(true); |
| 157 | |
| 158 | yield* stack.destroy(); |
| 159 | |
| 160 | for (const name of [domain2!, domain3!]) { |
| 161 | const deleted = yield* r2 |
| 162 | .getBucketDomainCustom({ |
| 163 | accountId, |
| 164 | bucketName: bucket.bucketName, |
| 165 | domain: name, |
| 166 | jurisdiction: bucket.jurisdiction, |
| 167 | }) |
| 168 | .pipe( |
| 169 | Effect.map(() => false), |
| 170 | Effect.catchTag("DomainNotFound", () => Effect.succeed(true)), |
| 171 | Effect.catchTag("NoSuchBucket", () => Effect.succeed(true)), |
| 172 | ); |
| 173 | expect(deleted).toEqual(true); |
| 174 | } |
| 175 | |
| 176 | yield* waitForBucketToBeDeleted(bucket.bucketName, accountId); |
| 177 | }).pipe(logLevel), |
| 178 | { tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"] }, |
| 179 | ); |
| 180 | |
| 181 | const waitForBucketToBeDeleted = Effect.fn(function* (bucketName: string, accountId: string) { |
| 182 | yield* r2.getBucket({ accountId, bucketName }).pipe( |
| 183 | Effect.flatMap(() => Effect.fail(new BucketStillExists())), |
| 184 | Effect.retry({ |
| 185 | while: (e): e is BucketStillExists => e instanceof BucketStillExists, |
| 186 | schedule: Schedule.exponential(100), |
| 187 | }), |
| 188 | Effect.catchTag("NoSuchBucket", () => Effect.void), |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | class BucketStillExists extends Data.TaggedError("BucketStillExists") {} |