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 alerting from "@distilled.cloud/cloudflare/alerting"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const EMAIL = "test@alchemy.run"; |
| 16 | |
| 17 | test.provider( |
| 18 | "create, update, delete notification policy", |
| 19 | (stack) => |
| 20 | Effect.gen(function* () { |
| 21 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 22 | |
| 23 | yield* stack.destroy(); |
| 24 | |
| 25 | const policy = yield* stack.deploy( |
| 26 | Cloudflare.Alerting.NotificationPolicy("SslPolicy", { |
| 27 | alertType: "universal_ssl_event_type", |
| 28 | mechanisms: { email: [{ id: EMAIL }] }, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | expect(policy.policyId).toBeDefined(); |
| 33 | expect(policy.accountId).toEqual(accountId); |
| 34 | expect(policy.alertType).toEqual("universal_ssl_event_type"); |
| 35 | expect(policy.enabled).toBe(true); |
| 36 | |
| 37 | // Verify out-of-band via the API. |
| 38 | const actual = yield* alerting.getPolicy({ accountId, policyId: policy.policyId }); |
| 39 | expect(actual.name).toEqual(policy.name); |
| 40 | expect(actual.alertType).toEqual("universal_ssl_event_type"); |
| 41 | expect(actual.mechanisms?.email?.[0]?.id).toEqual(EMAIL); |
| 42 | |
| 43 | // Update mutable props in place — same id. |
| 44 | const updated = yield* stack.deploy( |
| 45 | Cloudflare.Alerting.NotificationPolicy("SslPolicy", { |
| 46 | alertType: "universal_ssl_event_type", |
| 47 | enabled: false, |
| 48 | description: "paused during migration", |
| 49 | mechanisms: { email: [{ id: EMAIL }] }, |
| 50 | }), |
| 51 | ); |
| 52 | expect(updated.policyId).toEqual(policy.policyId); |
| 53 | expect(updated.enabled).toBe(false); |
| 54 | |
| 55 | const afterUpdate = yield* alerting.getPolicy({ accountId, policyId: policy.policyId }); |
| 56 | expect(afterUpdate.enabled).toBe(false); |
| 57 | expect(afterUpdate.description).toEqual("paused during migration"); |
| 58 | |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | yield* waitForPolicyDeleted(accountId, policy.policyId); |
| 62 | }).pipe(logLevel), |
| 63 | { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] }, |
| 64 | ); |
| 65 | |
| 66 | test.provider( |
| 67 | "replaces policy when alertType changes", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 71 | |
| 72 | yield* stack.destroy(); |
| 73 | |
| 74 | const policy = yield* stack.deploy( |
| 75 | Cloudflare.Alerting.NotificationPolicy("ReplacePolicy", { |
| 76 | alertType: "universal_ssl_event_type", |
| 77 | mechanisms: { email: [{ id: EMAIL }] }, |
| 78 | }), |
| 79 | ); |
| 80 | expect(policy.alertType).toEqual("universal_ssl_event_type"); |
| 81 | |
| 82 | const replaced = yield* stack.deploy( |
| 83 | Cloudflare.Alerting.NotificationPolicy("ReplacePolicy", { |
| 84 | alertType: "incident_alert", |
| 85 | mechanisms: { email: [{ id: EMAIL }] }, |
| 86 | }), |
| 87 | ); |
| 88 | expect(replaced.alertType).toEqual("incident_alert"); |
| 89 | expect(replaced.policyId).not.toEqual(policy.policyId); |
| 90 | |
| 91 | // The replaced (old) policy must be gone. |
| 92 | yield* waitForPolicyDeleted(accountId, policy.policyId); |
| 93 | |
| 94 | const actual = yield* alerting.getPolicy({ accountId, policyId: replaced.policyId }); |
| 95 | expect(actual.alertType).toEqual("incident_alert"); |
| 96 | |
| 97 | yield* stack.destroy(); |
| 98 | |
| 99 | yield* waitForPolicyDeleted(accountId, replaced.policyId); |
| 100 | }).pipe(logLevel), |
| 101 | { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] }, |
| 102 | ); |
| 103 | |
| 104 | // Canonical `list()` test (account-scoped collection): deploy a real policy, |
| 105 | // then assert it appears in the exhaustively-paginated account-wide result. |
| 106 | test.provider( |
| 107 | "list enumerates the deployed notification policy", |
| 108 | (stack) => |
| 109 | Effect.gen(function* () { |
| 110 | yield* stack.destroy(); |
| 111 | |
| 112 | const deployed = yield* stack.deploy( |
| 113 | Cloudflare.Alerting.NotificationPolicy("ListPolicy", { |
| 114 | alertType: "universal_ssl_event_type", |
| 115 | mechanisms: { email: [{ id: EMAIL }] }, |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | const provider = yield* Provider.findProvider(Cloudflare.Alerting.NotificationPolicy); |
| 120 | const all = yield* provider.list(); |
| 121 | |
| 122 | expect(all.some((p) => p.policyId === deployed.policyId)).toBe(true); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | }).pipe(logLevel), |
| 126 | { tags: ["provider:cloudflare", "provider:cloudflare:alerting", "live"] }, |
| 127 | ); |
| 128 | |
| 129 | const waitForPolicyDeleted = (accountId: string, policyId: string) => |
| 130 | alerting.getPolicy({ accountId, policyId }).pipe( |
| 131 | Effect.flatMap(() => Effect.fail({ _tag: "PolicyNotDeleted" } as const)), |
| 132 | Effect.catchTag("PolicyNotFound", () => Effect.void), |
| 133 | Effect.retry({ |
| 134 | while: (e) => e._tag === "PolicyNotDeleted", |
| 135 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 136 | }), |
| 137 | ); |