1import * as Cloudflare from "@/Cloudflare";
2import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment";
3import * as Provider from "@/Provider";
4import * as Test from "@/Test/Alchemy";
5import * as magicTransit from "@distilled.cloud/cloudflare/magic-transit";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import { MinimumLogLevel } from "effect/References";
9
10const { test } = Test.make({ providers: Cloudflare.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// Magic WAN sites (and their WANs) are entitlement-gated. On the standard
18// testing account every site call fails with the typed `MagicWanUnauthorized`
19// error (Cloudflare code 1025) or `Forbidden` (403) depending on token scope.
20// The deploy+list lifecycle test below is gated behind an explicit opt-in env
21// flag for entitled accounts; the read-only list probe always runs and asserts
22// a well-typed array (empty on unentitled accounts).
23const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_WAN;
24
25test.provider(
26 "list returns a well-typed array of site WANs",
27 (stack) =>
28 Effect.gen(function* () {
29 yield* stack.destroy();
30
31 const provider = yield* Provider.findProvider(
32 Cloudflare.MagicTransit.MagicSiteWan,
33 );
34 const all = yield* provider.list();
35
36 // Unentitled accounts surface MagicWanUnauthorized/Forbidden, which list()
37 // maps to []. Either way the result is a well-typed array, never a throw.
38 expect(Array.isArray(all)).toBe(true);
39 for (const wan of all) {
40 expect(typeof wan.wanId).toBe("string");
41 expect(typeof wan.siteId).toBe("string");
42 expect(typeof wan.accountId).toBe("string");
43 }
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 {
48 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
49 timeout: 120_000,
50 },
51);
52
53test.provider.skipIf(!entitled)(
54 "list enumerates the deployed site WAN",
55 (stack) =>
56 Effect.gen(function* () {
57 const { accountId } = yield* yield* CloudflareEnvironment;
58
59 yield* stack.destroy();
60
61 const site = yield* stack.deploy(
62 Cloudflare.MagicTransit.MagicSite("Site", {
63 name: "alchemy-wan-list-site",
64 description: "alchemy magic site wan list test",
65 }),
66 );
67
68 const wan = yield* stack.deploy(
69 Cloudflare.MagicTransit.MagicSiteWan("Wan", {
70 siteId: site.siteId,
71 physport: 1,
72 name: "alchemy-wan-list",
73 priority: 10,
74 }),
75 );
76 expect(wan.wanId).toBeTruthy();
77
78 // Out-of-band sanity check via the distilled API.
79 const live = yield* magicTransit.listSiteWans({
80 accountId,
81 siteId: site.siteId,
82 });
83 expect(live.result.some((w) => w.id === wan.wanId)).toBe(true);
84
85 const provider = yield* Provider.findProvider(
86 Cloudflare.MagicTransit.MagicSiteWan,
87 );
88 const all = yield* provider.list();
89
90 expect(all.some((w) => w.wanId === wan.wanId)).toBe(true);
91
92 yield* stack.destroy();
93 }).pipe(logLevel),
94 {
95 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
96 timeout: 180_000,
97 },
98);