1import * as Cloudflare from "@/Cloudflare";
2import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment";
3import * as Provider from "@/Provider";
4import * as Test from "@/Test/Alchemy";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8
9const { test } = Test.make({ providers: Cloudflare.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16// Account collection (pattern b): Gateway rules live under
17// `/accounts/{id}/gateway/rules`. Deploy one rule, then resolve the provider
18// with the typed `Provider.findProvider` helper and assert `list()` returns
19// the exhaustively-paginated set hydrated into the exact `read` Attributes
20// shape (so the deployed rule's id is present).
21test.provider(
22 "list enumerates the deployed Gateway rule",
23 (stack) =>
24 Effect.gen(function* () {
25 const { accountId } = yield* yield* CloudflareEnvironment;
26
27 yield* stack.destroy();
28
29 const rule = yield* stack.deploy(
30 Cloudflare.Gateway.Rule("ListRule", {
31 name: "alchemy-zt-rule-list",
32 action: "block",
33 filters: ["dns"],
34 traffic: 'any(dns.domains[*] == "list-test.alchemy-test.example")',
35 enabled: true,
36 }),
37 );
38
39 expect(rule.ruleId).toBeTruthy();
40 expect(rule.accountId).toEqual(accountId);
41
42 const provider = yield* Provider.findProvider(Cloudflare.Gateway.Rule);
43 const all = yield* provider.list();
44
45 // The deployed rule appears in the exhaustively-paginated result, and the
46 // hydrated element matches the `read` Attributes shape exactly.
47 const found = all.find((r) => r.ruleId === rule.ruleId);
48 expect(found).toBeDefined();
49 expect(found?.accountId).toEqual(accountId);
50 expect(found?.action).toEqual("block");
51 expect(found?.name).toEqual("alchemy-zt-rule-list");
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
56);