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

main
ts 93 lines 3.79 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 Provider from "alchemy/Provider";
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 // This file stays read-only on the standing test zone: the detection
16 // lifecycle (including a `list()` presence assertion) runs in
17 // LeakedCredentialCheck.test.ts, serialized with the other tests that flip
18 // the same zone-level LCC toggle. Files run concurrently, so deploying a
19 // detection here would race those toggle mutations. Supply
20 // CLOUDFLARE_TEST_LCC_DETECTION_ZONE_ID=<another zone> to deploy here.
21 const detectionZoneId = process.env.CLOUDFLARE_TEST_LCC_DETECTION_ZONE_ID;
22
23 const resolveZoneId = Effect.gen(function* () {
24 const { accountId } = yield* yield* CloudflareEnvironment;
25 const zone = yield* findZoneByName({ accountId, name: zoneName });
26 if (!zone) {
27 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
28 }
29 return zone.id;
30 });
31
32 describe.sequential(
33 "LeakedCredentialDetection",
34 {
35 tags: [
36 "provider:cloudflare",
37 "provider:cloudflare:leakedcredentialcheck",
38 "provider:cloudflare:zone",
39 "live",
40 ],
41 },
42 () => {
43 // Canonical `list()` test (zone-scoped collection): there is no account-wide
44 // enumeration API for custom detections, so `list()` fans out over every
45 // zone via `listAllZones` and exhaustively paginates the per-zone list,
46 // skipping zones whose LCC toggle is off (typed
47 // `LeakedCredentialChecksDisabled`) or that 403 (`Forbidden`).
48 //
49 // Without an override zone the assertion is read-only (a well-typed
50 // array). With one, deploy a detection and assert it appears.
51 test.provider("list enumerates custom detections across all zones", (stack) =>
52 Effect.gen(function* () {
53 const zoneId = yield* resolveZoneId;
54
55 yield* stack.destroy();
56
57 const provider = yield* Provider.findProvider(
58 Cloudflare.LeakedCredentialCheck.LeakedCredentialDetection,
59 );
60
61 if (detectionZoneId) {
62 const usernameExpr = 'lookup_json_string(http.request.body.raw, "user")';
63 const passwordExpr = 'lookup_json_string(http.request.body.raw, "pass")';
64
65 const detection = yield* stack.deploy(
66 Effect.gen(function* () {
67 const check = yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", {
68 zoneId: detectionZoneId,
69 enabled: true,
70 });
71 return yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialDetection(
72 "ListDetection",
73 { zoneId: check.zoneId, username: usernameExpr, password: passwordExpr },
74 );
75 }),
76 );
77
78 const all = yield* provider.list();
79 expect(all.some((d) => d.detectionId === detection.detectionId)).toBe(true);
80 } else {
81 // Read-only assertion: the result is a well-typed array (empty on the
82 // standing zone between runs). `zoneId` is resolved to prove the
83 // standing test zone exists in the enumeration scope.
84 expect(zoneId).toBeTruthy();
85 const all = yield* provider.list();
86 expect(Array.isArray(all)).toBe(true);
87 }
88
89 yield* stack.destroy();
90 }).pipe(logLevel),
91 );
92 },
93 );