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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as vulnScanner from "@distilled.cloud/cloudflare/vulnerability-scanner"; |
| 6 | import { adopt } from "alchemy/AdoptPolicy"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { testName } from "../../names.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | // Deterministic per-test names — same on every run, disjoint between tests. |
| 19 | const SET_DEFAULT = testName("alchemy-vulnscanner-credset-default"); |
| 20 | const SET_CRED = testName("alchemy-vulnscanner-credset-withcred"); |
| 21 | const SET_LIST = testName("alchemy-vulnscanner-credset-list"); |
| 22 | const SET_MOVE_A = testName("alchemy-vulnscanner-credset-move-a"); |
| 23 | const SET_MOVE_B = testName("alchemy-vulnscanner-credset-move-b"); |
| 24 | const CRED_NAME = testName("alchemy-vulnscanner-credential"); |
| 25 | const CRED_MOVE = testName("alchemy-vulnscanner-credential-move"); |
| 26 | |
| 27 | const accountIdOf = Effect.gen(function* () { |
| 28 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 29 | return accountId; |
| 30 | }); |
| 31 | |
| 32 | // Freshly minted scoped tokens propagate eventually-consistently across |
| 33 | // Cloudflare's edge; ride out 403 blips on out-of-band verification calls. |
| 34 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 35 | |
| 36 | const getCredentialSet = (accountId: string, credentialSetId: string) => |
| 37 | vulnScanner.getCredentialSet({ accountId, credentialSetId }).pipe( |
| 38 | Effect.retry({ |
| 39 | while: (e) => e._tag === "Forbidden", |
| 40 | schedule: forbiddenRetrySchedule, |
| 41 | times: 8, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | // Typed "is it gone?" probes — not-found tags map to undefined. |
| 46 | const findCredentialSet = (accountId: string, credentialSetId: string) => |
| 47 | getCredentialSet(accountId, credentialSetId).pipe( |
| 48 | Effect.catchTag("CredentialSetNotFound", () => Effect.succeed(undefined)), |
| 49 | ); |
| 50 | |
| 51 | const getCredential = (accountId: string, credentialSetId: string, credentialId: string) => |
| 52 | vulnScanner.getCredentialSetCredential({ accountId, credentialSetId, credentialId }).pipe( |
| 53 | Effect.retry({ |
| 54 | while: (e) => e._tag === "Forbidden", |
| 55 | schedule: forbiddenRetrySchedule, |
| 56 | times: 8, |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | const findCredential = (accountId: string, credentialSetId: string, credentialId: string) => |
| 61 | getCredential(accountId, credentialSetId, credentialId).pipe( |
| 62 | Effect.catchTag(["CredentialNotFound", "CredentialSetNotFound"], () => |
| 63 | Effect.succeed(undefined), |
| 64 | ), |
| 65 | ); |
| 66 | |
| 67 | test.provider( |
| 68 | "create, rename, and destroy a credential set", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | const accountId = yield* accountIdOf; |
| 72 | |
| 73 | yield* stack.destroy(); |
| 74 | |
| 75 | const initial = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | return yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("DefaultSet", { |
| 78 | name: SET_DEFAULT, |
| 79 | }).pipe(adopt(true)); |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | expect(initial.credentialSetId).toBeDefined(); |
| 84 | expect(initial.accountId).toEqual(accountId); |
| 85 | expect(initial.name).toEqual(SET_DEFAULT); |
| 86 | |
| 87 | // Out-of-band verification against the live API. |
| 88 | const live = yield* getCredentialSet(accountId, initial.credentialSetId); |
| 89 | expect(live.id).toEqual(initial.credentialSetId); |
| 90 | expect(live.name).toEqual(SET_DEFAULT); |
| 91 | |
| 92 | // Rename in place — same UUID. |
| 93 | const renamed = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | return yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("DefaultSet", { |
| 96 | name: `${SET_DEFAULT}-renamed`, |
| 97 | }).pipe(adopt(true)); |
| 98 | }), |
| 99 | ); |
| 100 | expect(renamed.credentialSetId).toEqual(initial.credentialSetId); |
| 101 | expect(renamed.name).toEqual(`${SET_DEFAULT}-renamed`); |
| 102 | |
| 103 | const liveRenamed = yield* getCredentialSet(accountId, renamed.credentialSetId); |
| 104 | expect(liveRenamed.name).toEqual(`${SET_DEFAULT}-renamed`); |
| 105 | |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | const gone = yield* findCredentialSet(accountId, initial.credentialSetId); |
| 109 | expect(gone).toBeUndefined(); |
| 110 | }).pipe(logLevel), |
| 111 | { tags: ["provider:cloudflare", "provider:cloudflare:vulnerabilityscanner", "live"] }, |
| 112 | ); |
| 113 | |
| 114 | test.provider( |
| 115 | "list enumerates the deployed credential set", |
| 116 | (stack) => |
| 117 | Effect.gen(function* () { |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const deployed = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("ListSet", { |
| 123 | name: SET_LIST, |
| 124 | }).pipe(adopt(true)); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | const provider = yield* Provider.findProvider( |
| 129 | Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet, |
| 130 | ); |
| 131 | const all = yield* provider.list(); |
| 132 | |
| 133 | // Exhaustively-paginated account collection contains the deployed set, |
| 134 | // each element shaped exactly like `read`'s Attributes. |
| 135 | expect(all.some((x) => x.credentialSetId === deployed.credentialSetId)).toBe(true); |
| 136 | const found = all.find((x) => x.credentialSetId === deployed.credentialSetId); |
| 137 | expect(found?.name).toEqual(SET_LIST); |
| 138 | expect(found?.accountId).toEqual(deployed.accountId); |
| 139 | |
| 140 | yield* stack.destroy(); |
| 141 | }).pipe(logLevel), |
| 142 | { tags: ["provider:cloudflare", "provider:cloudflare:vulnerabilityscanner", "live"] }, |
| 143 | ); |
| 144 | |
| 145 | test.provider( |
| 146 | "credential lifecycle — create, mutate in place, rotate value, destroy", |
| 147 | (stack) => |
| 148 | Effect.gen(function* () { |
| 149 | const accountId = yield* accountIdOf; |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | const initial = yield* stack.deploy( |
| 154 | Effect.gen(function* () { |
| 155 | const set = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("CredSet", { |
| 156 | name: SET_CRED, |
| 157 | }).pipe(adopt(true)); |
| 158 | const credential = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredential("Cred", { |
| 159 | credentialSetId: set.credentialSetId, |
| 160 | name: CRED_NAME, |
| 161 | location: "header", |
| 162 | locationName: "authorization", |
| 163 | value: Redacted.make("Bearer alchemy-test-v1"), |
| 164 | }).pipe(adopt(true)); |
| 165 | return { set, credential }; |
| 166 | }), |
| 167 | ); |
| 168 | |
| 169 | expect(initial.credential.credentialId).toBeDefined(); |
| 170 | expect(initial.credential.credentialSetId).toEqual(initial.set.credentialSetId); |
| 171 | expect(initial.credential.location).toEqual("header"); |
| 172 | expect(initial.credential.locationName).toEqual("authorization"); |
| 173 | |
| 174 | // Out-of-band verification — the value is write-only and never echoed. |
| 175 | const live = yield* getCredential( |
| 176 | accountId, |
| 177 | initial.set.credentialSetId, |
| 178 | initial.credential.credentialId, |
| 179 | ); |
| 180 | expect(live.name).toEqual(CRED_NAME); |
| 181 | expect(live.location).toEqual("header"); |
| 182 | expect(live.locationName).toEqual("authorization"); |
| 183 | |
| 184 | // Mutate observable fields + rotate the write-only value in place. |
| 185 | const updated = yield* stack.deploy( |
| 186 | Effect.gen(function* () { |
| 187 | const set = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("CredSet", { |
| 188 | name: SET_CRED, |
| 189 | }).pipe(adopt(true)); |
| 190 | const credential = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredential("Cred", { |
| 191 | credentialSetId: set.credentialSetId, |
| 192 | name: CRED_NAME, |
| 193 | location: "cookie", |
| 194 | locationName: "session_id", |
| 195 | value: Redacted.make("alchemy-test-v2"), |
| 196 | }).pipe(adopt(true)); |
| 197 | return { set, credential }; |
| 198 | }), |
| 199 | ); |
| 200 | |
| 201 | // Same credential updated in place — not a replacement. |
| 202 | expect(updated.credential.credentialId).toEqual(initial.credential.credentialId); |
| 203 | expect(updated.credential.location).toEqual("cookie"); |
| 204 | expect(updated.credential.locationName).toEqual("session_id"); |
| 205 | |
| 206 | const liveUpdated = yield* getCredential( |
| 207 | accountId, |
| 208 | updated.set.credentialSetId, |
| 209 | updated.credential.credentialId, |
| 210 | ); |
| 211 | expect(liveUpdated.location).toEqual("cookie"); |
| 212 | expect(liveUpdated.locationName).toEqual("session_id"); |
| 213 | |
| 214 | yield* stack.destroy(); |
| 215 | |
| 216 | const credGone = yield* findCredential( |
| 217 | accountId, |
| 218 | initial.set.credentialSetId, |
| 219 | initial.credential.credentialId, |
| 220 | ); |
| 221 | expect(credGone).toBeUndefined(); |
| 222 | const setGone = yield* findCredentialSet(accountId, initial.set.credentialSetId); |
| 223 | expect(setGone).toBeUndefined(); |
| 224 | }).pipe(logLevel), |
| 225 | { tags: ["provider:cloudflare", "provider:cloudflare:vulnerabilityscanner", "live"] }, |
| 226 | ); |
| 227 | |
| 228 | test.provider( |
| 229 | "moving a credential to a different set triggers replacement", |
| 230 | (stack) => |
| 231 | Effect.gen(function* () { |
| 232 | const accountId = yield* accountIdOf; |
| 233 | |
| 234 | yield* stack.destroy(); |
| 235 | |
| 236 | // Keep BOTH sets deployed across the replacement step — replacing a |
| 237 | // resource while removing its old dependency in the same deploy is a |
| 238 | // known engine deadlock. |
| 239 | const initial = yield* stack.deploy( |
| 240 | Effect.gen(function* () { |
| 241 | const setA = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("SetA", { |
| 242 | name: SET_MOVE_A, |
| 243 | }).pipe(adopt(true)); |
| 244 | const setB = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("SetB", { |
| 245 | name: SET_MOVE_B, |
| 246 | }).pipe(adopt(true)); |
| 247 | const credential = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredential( |
| 248 | "MovingCred", |
| 249 | { |
| 250 | credentialSetId: setA.credentialSetId, |
| 251 | name: CRED_MOVE, |
| 252 | location: "header", |
| 253 | locationName: "x-api-key", |
| 254 | value: Redacted.make("alchemy-move-v1"), |
| 255 | }, |
| 256 | ).pipe(adopt(true)); |
| 257 | return { setA, setB, credential }; |
| 258 | }), |
| 259 | ); |
| 260 | |
| 261 | expect(initial.credential.credentialSetId).toEqual(initial.setA.credentialSetId); |
| 262 | |
| 263 | const moved = yield* stack.deploy( |
| 264 | Effect.gen(function* () { |
| 265 | const setA = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("SetA", { |
| 266 | name: SET_MOVE_A, |
| 267 | }).pipe(adopt(true)); |
| 268 | const setB = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredentialSet("SetB", { |
| 269 | name: SET_MOVE_B, |
| 270 | }).pipe(adopt(true)); |
| 271 | const credential = yield* Cloudflare.VulnerabilityScanner.VulnScannerCredential( |
| 272 | "MovingCred", |
| 273 | { |
| 274 | credentialSetId: setB.credentialSetId, |
| 275 | name: CRED_MOVE, |
| 276 | location: "header", |
| 277 | locationName: "x-api-key", |
| 278 | value: Redacted.make("alchemy-move-v1"), |
| 279 | }, |
| 280 | ).pipe(adopt(true)); |
| 281 | return { setA, setB, credential }; |
| 282 | }), |
| 283 | ); |
| 284 | |
| 285 | // The parent set is the credential's identity — a new physical |
| 286 | // credential exists under set B. |
| 287 | expect(moved.credential.credentialSetId).toEqual(moved.setB.credentialSetId); |
| 288 | expect(moved.credential.credentialId).not.toEqual(initial.credential.credentialId); |
| 289 | |
| 290 | // The old credential was deleted from set A as part of replacement. |
| 291 | const oldGone = yield* findCredential( |
| 292 | accountId, |
| 293 | initial.setA.credentialSetId, |
| 294 | initial.credential.credentialId, |
| 295 | ); |
| 296 | expect(oldGone).toBeUndefined(); |
| 297 | |
| 298 | const liveNew = yield* getCredential( |
| 299 | accountId, |
| 300 | moved.setB.credentialSetId, |
| 301 | moved.credential.credentialId, |
| 302 | ); |
| 303 | expect(liveNew.name).toEqual(CRED_MOVE); |
| 304 | |
| 305 | yield* stack.destroy(); |
| 306 | |
| 307 | const setAGone = yield* findCredentialSet(accountId, initial.setA.credentialSetId); |
| 308 | expect(setAGone).toBeUndefined(); |
| 309 | const setBGone = yield* findCredentialSet(accountId, initial.setB.credentialSetId); |
| 310 | expect(setBGone).toBeUndefined(); |
| 311 | }).pipe(logLevel), |
| 312 | { tags: ["provider:cloudflare", "provider:cloudflare:vulnerabilityscanner", "live"] }, |
| 313 | ); |