The best way to control Claude Code, Codex and more from any computer or phone

main
ts 90 lines 3.62 KB
Raw
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 zones from "@distilled.cloud/cloudflare/zones";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 const { test } = Test.make({ providers: Cloudflare.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
14
15 const resolveZoneId = Effect.gen(function* () {
16 const { accountId } = yield* yield* CloudflareEnvironment;
17 const zone = yield* findZoneByName({ accountId, name: zoneName });
18 if (!zone) {
19 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
20 }
21 return zone.id;
22 });
23
24 // Recipients get a subscribe email when they are added, so the test uses a
25 // reserved domain.
26 const recipient = "ct-alerts@example.com";
27 const secondRecipient = "ct-alerts-2@example.com";
28
29 describe.sequential(
30 "CertificateTransparencyAlerting",
31 {
32 tags: ["provider:cloudflare", "provider:cloudflare:ssl", "provider:cloudflare:zone", "live"],
33 },
34 () => {
35 test.provider(
36 "enables alerting by default, updates it in place, and restores the zone on destroy",
37 (stack) =>
38 Effect.gen(function* () {
39 const zoneId = yield* resolveZoneId;
40
41 yield* stack.destroy();
42
43 const original = yield* zones.getCtAlerting({ zoneId });
44 const deployAlerting = (props: { enabled?: boolean; emails?: string[] }) =>
45 stack.deploy(
46 Effect.gen(function* () {
47 return yield* Cloudflare.Ssl.CertificateTransparencyAlerting("CtAlerting", {
48 zoneId,
49 ...props,
50 });
51 }),
52 );
53
54 // `enabled` is omitted: the default turns alerting on.
55 const created = yield* deployAlerting({ emails: [recipient] });
56 expect(created.enabled).toBe(true);
57 expect(created.emails).toEqual([recipient]);
58 expect(created.initialEnabled).toEqual(original.enabled);
59
60 const live = yield* zones.getCtAlerting({ zoneId });
61 expect(live.enabled).toBe(true);
62 expect(live.emails ?? []).toEqual([recipient]);
63
64 // Update the recipients in place; the snapshot of the zone's
65 // original state must survive the update.
66 const updated = yield* deployAlerting({ emails: [recipient, secondRecipient] });
67 expect([...updated.emails].sort()).toEqual([recipient, secondRecipient].sort());
68 expect(updated.initialEnabled).toEqual(original.enabled);
69 expect([...((yield* zones.getCtAlerting({ zoneId })).emails ?? [])].sort()).toEqual(
70 [recipient, secondRecipient].sort(),
71 );
72
73 // Turn alerting off explicitly.
74 const disabled = yield* deployAlerting({
75 enabled: false,
76 emails: [recipient, secondRecipient],
77 });
78 expect(disabled.enabled).toBe(false);
79 expect((yield* zones.getCtAlerting({ zoneId })).enabled).toBe(false);
80
81 yield* stack.destroy();
82
83 const after = yield* zones.getCtAlerting({ zoneId });
84 expect(after.enabled).toEqual(original.enabled);
85 expect([...(after.emails ?? [])].sort()).toEqual([...(original.emails ?? [])].sort());
86 }).pipe(logLevel),
87 { timeout: 240_000 },
88 );
89 },
90 );