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

main
ts 97 lines 4.1 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as mnm from "@distilled.cloud/cloudflare/magic-network-monitoring";
6 import * as Provider from "alchemy/Provider";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import { testName } from "../../names.ts";
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // The scoped API token the test harness mints propagates eventually-
17 // consistently across Cloudflare's edge — ride out 403 blips
18 // (`Forbidden`, declared in the distilled error union) on the test's own
19 // out-of-band setup calls.
20 const forbiddenRetry = {
21 while: (e: { _tag: string }) => e._tag === "Forbidden",
22 schedule: Schedule.exponential("500 millis"),
23 times: 8,
24 } as const;
25
26 class MnmRuleNotListed extends Data.TaggedError("MnmRuleNotListed")<{}> {}
27
28 // Canonical `list()` test (account-scoped collection): deploy a config +
29 // rule, then resolve the provider via the typed `Provider.findProvider`
30 // helper and assert the deployed rule appears in the exhaustively-
31 // paginated `list()` result.
32 describe.sequential(
33 "MagicNetworkMonitoring.Rule",
34 { tags: ["provider:cloudflare", "provider:cloudflare:magicnetworkmonitoring", "live"] },
35 () => {
36 test.provider(
37 "list enumerates the deployed rule",
38 (stack) =>
39 Effect.gen(function* () {
40 const { accountId } = yield* yield* CloudflareEnvironment;
41
42 yield* stack.destroy();
43 // The config is an account singleton with no ownership markers — a
44 // leftover from an interrupted run would surface as `Unowned` and
45 // block this stack's create, so normalize to "absent" first.
46 yield* mnm.deleteConfig({ accountId }).pipe(
47 Effect.catchTag("MnmConfigNotFound", () => Effect.void),
48 Effect.retry(forbiddenRetry),
49 );
50
51 const { rule } = yield* stack.deploy(
52 Effect.gen(function* () {
53 const config = yield* Cloudflare.MagicNetworkMonitoring.Config("Config", {
54 name: testName("alchemy-mnm-list-test"),
55 defaultSampling: 1,
56 });
57 // Rules cannot exist without the account config — sequence the
58 // rule after the config via its accountId output.
59 const rule = yield* Cloudflare.MagicNetworkMonitoring.Rule("Rule", {
60 accountId: config.accountId,
61 type: "threshold",
62 prefixes: ["10.0.0.0/24"],
63 bandwidthThreshold: 1_000_000,
64 duration: "1m",
65 });
66 return { config, rule };
67 }),
68 );
69
70 const provider = yield* Provider.findProvider(Cloudflare.MagicNetworkMonitoring.Rule);
71 const all = yield* provider.list().pipe(
72 Effect.flatMap((all) =>
73 all.some((r) => r.ruleId === rule.ruleId)
74 ? Effect.succeed(all)
75 : Effect.fail(new MnmRuleNotListed()),
76 ),
77 Effect.retry({
78 while: (e) => e._tag === "MnmRuleNotListed",
79 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
80 }),
81 );
82
83 // The exhaustively-paginated result contains the deployed rule, and
84 // each element carries the full `read` Attributes shape.
85 const found = all.find((r) => r.ruleId === rule.ruleId);
86 expect(found).toBeDefined();
87 expect(found?.accountId).toEqual(accountId);
88 expect(found?.name).toEqual(rule.name);
89 expect(found?.type).toEqual("threshold");
90 expect(found?.prefixes).toEqual(["10.0.0.0/24"]);
91
92 yield* stack.destroy();
93 }).pipe(logLevel),
94 { exclusive: "cloudflare:mnm" },
95 );
96 },
97 );