The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 lcc from "@distilled.cloud/cloudflare/leaked-credential-checks"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 16 | |
| 17 | // Custom detection locations are plan-gated, but the standing test zone now |
| 18 | // has a non-zero quota, so the detection lifecycle runs there by default. |
| 19 | // Override the zone with CLOUDFLARE_TEST_LCC_DETECTION_ZONE_ID=<zone id>. |
| 20 | const detectionZoneId = process.env.CLOUDFLARE_TEST_LCC_DETECTION_ZONE_ID; |
| 21 | |
| 22 | const resolveZoneId = Effect.gen(function* () { |
| 23 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 24 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 25 | if (!zone) { |
| 26 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 27 | } |
| 28 | return zone.id; |
| 29 | }); |
| 30 | |
| 31 | // The scoped API token the test harness mints propagates eventually- |
| 32 | // consistently across Cloudflare's edge — a fresh token intermittently 403s. |
| 33 | // Ride out the blips on the test's own out-of-band calls by retrying the |
| 34 | // typed `Forbidden` error (part of each operation's error union via |
| 35 | // distilled patches). |
| 36 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 37 | |
| 38 | const getCheck = (zoneId: string) => |
| 39 | lcc.getLeakedCredentialCheck({ zoneId }).pipe( |
| 40 | Effect.retry({ |
| 41 | while: (e) => e._tag === "Forbidden", |
| 42 | schedule: forbiddenRetrySchedule, |
| 43 | times: 8, |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | const setBaseline = (zoneId: string, enabled: boolean) => |
| 48 | lcc.createLeakedCredentialCheck({ zoneId, enabled }).pipe( |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "Forbidden", |
| 51 | schedule: forbiddenRetrySchedule, |
| 52 | times: 8, |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | // Both cases mutate the same zone-level Leaked Credential Check singleton; run them serially so they don't corrupt each other's captured `initialEnabled` under the global concurrent test config. |
| 57 | describe.sequential( |
| 58 | "LeakedCredentialCheck", |
| 59 | { tags: ["provider:cloudflare", "provider:cloudflare:leakedcredentialcheck", "live"] }, |
| 60 | () => { |
| 61 | test.provider( |
| 62 | "enables leaked credential checks and restores the baseline on destroy", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | const zoneId = yield* resolveZoneId; |
| 66 | |
| 67 | yield* stack.destroy(); |
| 68 | // Known baseline: the check defaults to disabled. |
| 69 | yield* setBaseline(zoneId, false); |
| 70 | |
| 71 | const check = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | return yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", { |
| 74 | zoneId, |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(check.zoneId).toEqual(zoneId); |
| 80 | expect(check.enabled).toEqual(true); |
| 81 | // The pre-management value was captured for restore-on-destroy. |
| 82 | expect(check.initialEnabled).toEqual(false); |
| 83 | |
| 84 | // Out-of-band verification via the distilled API. |
| 85 | const live = yield* getCheck(zoneId); |
| 86 | expect(live.enabled).toEqual(true); |
| 87 | |
| 88 | // Update in place — same singleton, initialEnabled survives. |
| 89 | const updated = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | return yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", { |
| 92 | zoneId, |
| 93 | enabled: false, |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | expect(updated.enabled).toEqual(false); |
| 98 | expect(updated.initialEnabled).toEqual(false); |
| 99 | |
| 100 | const disabled = yield* getCheck(zoneId); |
| 101 | expect(disabled.enabled).toEqual(false); |
| 102 | |
| 103 | // Flip back on so destroy has something to restore. |
| 104 | const reEnabled = yield* stack.deploy( |
| 105 | Effect.gen(function* () { |
| 106 | return yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", { |
| 107 | zoneId, |
| 108 | enabled: true, |
| 109 | }); |
| 110 | }), |
| 111 | ); |
| 112 | expect(reEnabled.enabled).toEqual(true); |
| 113 | expect(reEnabled.initialEnabled).toEqual(false); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | // Destroy restored the value the check had before we managed it. |
| 118 | const restored = yield* getCheck(zoneId); |
| 119 | expect(restored.enabled).toEqual(false); |
| 120 | }).pipe(logLevel), |
| 121 | { tags: ["provider:cloudflare:zone"] }, |
| 122 | ); |
| 123 | |
| 124 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 125 | // API for this per-zone setting, so `list()` enumerates every zone via |
| 126 | // `listAllZones` and reads the singleton in each. Assert the result is |
| 127 | // non-empty and contains the standing test zone. |
| 128 | test.provider( |
| 129 | "list enumerates the check across all zones", |
| 130 | (stack) => |
| 131 | Effect.gen(function* () { |
| 132 | const zoneId = yield* resolveZoneId; |
| 133 | |
| 134 | const provider = yield* Provider.findProvider( |
| 135 | Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck, |
| 136 | ); |
| 137 | const all = yield* provider.list(); |
| 138 | |
| 139 | expect(all.length).toBeGreaterThan(0); |
| 140 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 141 | |
| 142 | // `stack` is unused here (the singleton always exists on every zone), |
| 143 | // but keep the destroy bookends so the harness state stays clean. |
| 144 | yield* stack.destroy(); |
| 145 | }).pipe(logLevel), |
| 146 | { tags: ["provider:cloudflare:zone"] }, |
| 147 | ); |
| 148 | |
| 149 | test.provider( |
| 150 | "creates, updates, and destroys a custom detection", |
| 151 | (stack) => |
| 152 | Effect.gen(function* () { |
| 153 | const zoneId = detectionZoneId ?? (yield* resolveZoneId); |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | // Known baseline: the zone toggle is off before we manage it. |
| 157 | yield* setBaseline(zoneId, false); |
| 158 | |
| 159 | const usernameExpr = 'lookup_json_string(http.request.body.raw, "user")'; |
| 160 | const passwordExpr = 'lookup_json_string(http.request.body.raw, "pass")'; |
| 161 | |
| 162 | const detection = yield* stack.deploy( |
| 163 | Effect.gen(function* () { |
| 164 | const check = yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", { |
| 165 | zoneId, |
| 166 | enabled: true, |
| 167 | }); |
| 168 | return yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialDetection("Det", { |
| 169 | // Depend on the check so the toggle deploys first. |
| 170 | zoneId: check.zoneId, |
| 171 | username: usernameExpr, |
| 172 | password: passwordExpr, |
| 173 | }); |
| 174 | }), |
| 175 | ); |
| 176 | |
| 177 | expect(detection.detectionId).not.toEqual(""); |
| 178 | expect(detection.zoneId).toEqual(zoneId); |
| 179 | expect(detection.username).toEqual(usernameExpr); |
| 180 | expect(detection.password).toEqual(passwordExpr); |
| 181 | |
| 182 | // Out-of-band verification via the distilled API. |
| 183 | const live = yield* lcc.getDetection({ zoneId, detectionId: detection.detectionId }); |
| 184 | expect(live.username).toEqual(usernameExpr); |
| 185 | |
| 186 | // A second detection with the same expression pair is rejected |
| 187 | // with the typed duplicate tag (not the catch-all BadRequest). |
| 188 | const duplicate = yield* lcc |
| 189 | .createDetection({ zoneId, username: usernameExpr, password: passwordExpr }) |
| 190 | .pipe(Effect.flip); |
| 191 | expect(duplicate._tag).toEqual("DetectionAlreadyExists"); |
| 192 | |
| 193 | // `list()` enumerates the deployed detection. |
| 194 | const provider = yield* Provider.findProvider( |
| 195 | Cloudflare.LeakedCredentialCheck.LeakedCredentialDetection, |
| 196 | ); |
| 197 | const all = yield* provider.list(); |
| 198 | expect(all.some((d) => d.detectionId === detection.detectionId)).toBe(true); |
| 199 | |
| 200 | // In-place update — the PUT keeps the same detection id. |
| 201 | const newPasswordExpr = 'lookup_json_string(http.request.body.raw, "secret")'; |
| 202 | const updated = yield* stack.deploy( |
| 203 | Effect.gen(function* () { |
| 204 | const check = yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", { |
| 205 | zoneId, |
| 206 | enabled: true, |
| 207 | }); |
| 208 | return yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialDetection("Det", { |
| 209 | zoneId: check.zoneId, |
| 210 | username: usernameExpr, |
| 211 | password: newPasswordExpr, |
| 212 | }); |
| 213 | }), |
| 214 | ); |
| 215 | expect(updated.detectionId).toEqual(detection.detectionId); |
| 216 | expect(updated.password).toEqual(newPasswordExpr); |
| 217 | |
| 218 | yield* stack.destroy(); |
| 219 | |
| 220 | // Destroy restored the toggle to its pre-management value (off), |
| 221 | // which hides detections from the API. Flip it on out-of-band to |
| 222 | // prove the detection itself was deleted, then restore the baseline. |
| 223 | const restored = yield* getCheck(zoneId); |
| 224 | expect(restored.enabled).toEqual(false); |
| 225 | yield* setBaseline(zoneId, true); |
| 226 | const error = yield* lcc |
| 227 | .getDetection({ zoneId, detectionId: detection.detectionId }) |
| 228 | .pipe(Effect.flip); |
| 229 | expect(error._tag).toEqual("DetectionNotFound"); |
| 230 | const remaining = yield* lcc.listDetections({ zoneId }); |
| 231 | expect(remaining.result ?? []).toEqual([]); |
| 232 | yield* setBaseline(zoneId, false); |
| 233 | }).pipe(logLevel), |
| 234 | { tags: ["provider:cloudflare:zone"], timeout: 120_000 }, |
| 235 | ); |
| 236 | }, |
| 237 | ); |