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

main
ts 117 lines 4.51 KB
Raw
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 zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
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 // Ride out 403 blips (`Forbidden`) while the harness-minted token
16 // propagates across Cloudflare's edge.
17 const getLogging = (accountId: string) =>
18 zeroTrust.getGatewayLogging({ accountId }).pipe(
19 Effect.retry({
20 while: (e) => e._tag === "Forbidden",
21 schedule: Schedule.exponential("500 millis"),
22 times: 8,
23 }),
24 );
25
26 test.provider(
27 "manage logging settings, update in place, restore on destroy",
28 (stack) =>
29 Effect.gen(function* () {
30 const { accountId } = yield* yield* CloudflareEnvironment;
31
32 yield* stack.destroy();
33
34 // Capture the account's pre-test state out-of-band so we can
35 // verify the capture-and-restore behaviour at the end.
36 const baseline = yield* getLogging(accountId);
37 const baselineRedactPii = baseline.redactPii ?? undefined;
38 const baselineDnsLogAll = baseline.settingsByRuleType?.dns?.logAll ?? undefined;
39
40 // Drive both managed fields to the opposite of the baseline so the
41 // deploy is guaranteed to change something.
42 const flippedRedactPii = !(baselineRedactPii ?? false);
43 const flippedDnsLogAll = !(baselineDnsLogAll ?? true);
44
45 const logging = yield* stack.deploy(
46 Cloudflare.Gateway.Logging("Logging", {
47 redactPii: flippedRedactPii,
48 settingsByRuleType: {
49 dns: { logAll: flippedDnsLogAll },
50 },
51 }),
52 );
53 expect(logging.accountId).toEqual(accountId);
54 expect(logging.redactPii).toEqual(flippedRedactPii);
55 expect(logging.dns?.logAll).toEqual(flippedDnsLogAll);
56 // The pre-management snapshot was captured for restore.
57 expect(logging.initialSettings.redactPii ?? undefined).toEqual(baselineRedactPii);
58
59 const live = yield* getLogging(accountId);
60 expect(live.redactPii).toEqual(flippedRedactPii);
61 expect(live.settingsByRuleType?.dns?.logAll).toEqual(flippedDnsLogAll);
62
63 // Update in place — flip redactPii back; the dns toggle stays.
64 const updated = yield* stack.deploy(
65 Cloudflare.Gateway.Logging("Logging", {
66 redactPii: !flippedRedactPii,
67 settingsByRuleType: {
68 dns: { logAll: flippedDnsLogAll },
69 },
70 }),
71 );
72 expect(updated.redactPii).toEqual(!flippedRedactPii);
73 expect(updated.dns?.logAll).toEqual(flippedDnsLogAll);
74 // The original capture survives the update.
75 expect(updated.initialSettings).toEqual(logging.initialSettings);
76
77 // Destroy restores the captured pre-management values.
78 yield* stack.destroy();
79 const restored = yield* getLogging(accountId);
80 expect(restored.redactPii ?? undefined).toEqual(baselineRedactPii);
81 expect(restored.settingsByRuleType?.dns?.logAll ?? undefined).toEqual(baselineDnsLogAll);
82 }).pipe(logLevel),
83 {
84 tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"],
85 timeout: 120_000,
86 },
87 );
88
89 // Canonical `list()` test (account singleton): there is no enumeration API
90 // for the Gateway logging settings — the object always exists for the
91 // account. `list()` reads the single instance and returns it as a
92 // one-element array. Assert exactly one well-typed Attributes for the
93 // ambient account.
94 test.provider(
95 "list returns the account's Gateway logging singleton",
96 (stack) =>
97 Effect.gen(function* () {
98 const { accountId } = yield* yield* CloudflareEnvironment;
99
100 yield* stack.destroy();
101
102 const provider = yield* Provider.findProvider(Cloudflare.Gateway.Logging);
103 const all = yield* provider.list();
104
105 expect(all.length).toEqual(1);
106 const [settings] = all;
107 expect(settings.accountId).toEqual(accountId);
108 // The singleton's observed snapshot is its own restore target.
109 expect(settings.initialSettings).toBeDefined();
110
111 yield* stack.destroy();
112 }).pipe(logLevel),
113 {
114 tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"],
115 timeout: 120_000,
116 },
117 );