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 { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 4 | import { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 7 | import * as emailAuth from "@distilled.cloud/cloudflare/email-auth"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 16 | |
| 17 | const resolveZoneId = Effect.gen(function* () { |
| 18 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 19 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 20 | if (!zone) { |
| 21 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 22 | } |
| 23 | return zone.id; |
| 24 | }); |
| 25 | |
| 26 | const listDmarcRecords = (zoneId: string) => |
| 27 | dns.listRecords.items({ zoneId, name: { exact: `_dmarc.${zoneName}` }, type: "TXT" }).pipe( |
| 28 | Stream.filter((r) => r.name === `_dmarc.${zoneName}` && r.type === "TXT"), |
| 29 | Stream.runCollect, |
| 30 | Effect.map((chunk) => Array.from(chunk)), |
| 31 | ); |
| 32 | |
| 33 | describe.sequential( |
| 34 | "DmarcReports", |
| 35 | { |
| 36 | tags: ["provider:cloudflare", "provider:cloudflare:email", "provider:cloudflare:zone", "live"], |
| 37 | }, |
| 38 | () => { |
| 39 | test.provider("enables, disables and destroys without deleting DNS records", (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const zoneId = yield* resolveZoneId; |
| 42 | |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | // Capture the zone's pre-test state so the run leaves no residue. |
| 46 | const before = yield* emailAuth.getDmarcReport({ zoneId }); |
| 47 | |
| 48 | const enabled = yield* stack.deploy( |
| 49 | Cloudflare.Email.DmarcReports("Dmarc", { zone: zoneName }), |
| 50 | ); |
| 51 | expect(enabled.zoneId).toEqual(zoneId); |
| 52 | expect(enabled.enabled).toEqual(true); |
| 53 | expect(enabled.ruaPrefix).toMatch(/^[0-9a-f]{32}$/); |
| 54 | |
| 55 | const live = yield* emailAuth.getDmarcReport({ zoneId }); |
| 56 | expect(live.enabled).toEqual(true); |
| 57 | |
| 58 | // Enabling upserts Cloudflare's RUA address into `_dmarc`. |
| 59 | const records = yield* listDmarcRecords(zoneId); |
| 60 | expect(records.some((r) => String(r.content).includes(enabled.ruaPrefix!))).toEqual(true); |
| 61 | |
| 62 | const disabled = yield* stack.deploy( |
| 63 | Cloudflare.Email.DmarcReports("Dmarc", { zone: zoneName, enabled: false }), |
| 64 | ); |
| 65 | expect(disabled.enabled).toEqual(false); |
| 66 | expect((yield* emailAuth.getDmarcReport({ zoneId })).enabled).toEqual(false); |
| 67 | |
| 68 | // Disabling never deletes DNS records. |
| 69 | const afterDisable = yield* listDmarcRecords(zoneId); |
| 70 | expect(afterDisable.map((r) => r.id).sort()).toEqual(records.map((r) => r.id).sort()); |
| 71 | |
| 72 | // Re-enable so destroy has something to turn off. |
| 73 | yield* stack.deploy(Cloudflare.Email.DmarcReports("Dmarc", { zone: zoneName })); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | expect((yield* emailAuth.getDmarcReport({ zoneId })).enabled).toEqual(false); |
| 78 | const afterDestroy = yield* listDmarcRecords(zoneId); |
| 79 | expect(afterDestroy.map((r) => r.id).sort()).toEqual(records.map((r) => r.id).sort()); |
| 80 | |
| 81 | // Restore the captured state. |
| 82 | if (before.enabled) { |
| 83 | yield* emailAuth.patchDmarcReport({ zoneId, enabled: true }); |
| 84 | } |
| 85 | }).pipe(logLevel), |
| 86 | ); |
| 87 | }, |
| 88 | ); |