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 zeroTrust from "@distilled.cloud/cloudflare/zero-trust"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Device posture integrations require a reachable third-party tenant — |
| 16 | // Cloudflare validates the configured credentials against the live |
| 17 | // provider API at create time. Without one, `POST |
| 18 | // /accounts/{id}/devices/posture/integration` fails with HTTP 400 code |
| 19 | // 2046 "invalid posture integration request: invalid credentials: ..." |
| 20 | // — surfaced as the typed `InvalidPostureIntegrationConfig` error |
| 21 | // (observed: a custom_s2s probe against https://example.com/posture with |
| 22 | // a real Access service token returns exactly this tag because the |
| 23 | // endpoint answers 405 instead of a posture payload). The lifecycle test |
| 24 | // is gated behind a real provider supplied via env; the probe test |
| 25 | // always runs and pins the typed tag. |
| 26 | const external = !!process.env.CLOUDFLARE_TEST_POSTURE_INTEGRATION; |
| 27 | |
| 28 | test.provider.skipIf(external)( |
| 29 | "creates without a real provider surface the typed InvalidPostureIntegrationConfig error", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 33 | |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | // A real Access service token isolates the probe to the third-party |
| 37 | // validation step (dummy Access creds would fail earlier). |
| 38 | const { token } = yield* stack.deploy( |
| 39 | Effect.gen(function* () { |
| 40 | const token = yield* Cloudflare.Access.ServiceToken("ProbeToken", {}); |
| 41 | return { token }; |
| 42 | }), |
| 43 | ); |
| 44 | expect(token.clientId).toBeDefined(); |
| 45 | expect(token.clientSecret).toBeDefined(); |
| 46 | |
| 47 | const error = yield* zeroTrust |
| 48 | .createDevicePostureIntegration({ |
| 49 | accountId, |
| 50 | name: "alchemy-posture-probe", |
| 51 | type: "custom_s2s", |
| 52 | interval: "10m", |
| 53 | config: { |
| 54 | apiUrl: "https://example.com/posture", |
| 55 | clientSecret: "dummy-secret", |
| 56 | accessClientId: token.clientId, |
| 57 | accessClientSecret: Redacted.value(token.clientSecret!), |
| 58 | }, |
| 59 | }) |
| 60 | .pipe(Effect.flip); |
| 61 | expect(error._tag).toEqual("InvalidPostureIntegrationConfig"); |
| 62 | if (error._tag === "InvalidPostureIntegrationConfig") { |
| 63 | expect(error.message).toContain("invalid posture integration request"); |
| 64 | } |
| 65 | |
| 66 | yield* stack.destroy(); |
| 67 | }).pipe(logLevel), |
| 68 | { |
| 69 | tags: [ |
| 70 | "provider:cloudflare", |
| 71 | "provider:cloudflare:access", |
| 72 | "provider:cloudflare:devices", |
| 73 | "live", |
| 74 | ], |
| 75 | timeout: 90_000, |
| 76 | }, |
| 77 | ); |
| 78 | |
| 79 | test.provider.skipIf(!external)( |
| 80 | "create, update in place, and destroy a posture integration", |
| 81 | (stack) => |
| 82 | Effect.gen(function* () { |
| 83 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 84 | |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | // Requires a reachable custom_s2s endpoint configured via env. |
| 88 | const apiUrl = process.env.CLOUDFLARE_TEST_POSTURE_API_URL!; |
| 89 | const secret = process.env.CLOUDFLARE_TEST_POSTURE_SECRET!; |
| 90 | |
| 91 | const created = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | const token = yield* Cloudflare.Access.ServiceToken("S2sToken", {}); |
| 94 | const integration = yield* Cloudflare.Devices.DevicePostureIntegration("Custom", { |
| 95 | type: "custom_s2s", |
| 96 | interval: "10m", |
| 97 | config: { |
| 98 | apiUrl, |
| 99 | clientSecret: Redacted.make(secret), |
| 100 | accessClientId: token.clientId, |
| 101 | accessClientSecret: token.clientSecret!, |
| 102 | }, |
| 103 | }); |
| 104 | return { token, integration }; |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(created.integration.integrationId).toBeDefined(); |
| 109 | expect(created.integration.accountId).toEqual(accountId); |
| 110 | expect(created.integration.interval).toEqual("10m"); |
| 111 | |
| 112 | const live = yield* zeroTrust.getDevicePostureIntegration({ |
| 113 | accountId, |
| 114 | integrationId: created.integration.integrationId, |
| 115 | }); |
| 116 | expect(live.id).toEqual(created.integration.integrationId); |
| 117 | |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const gone = yield* zeroTrust |
| 121 | .getDevicePostureIntegration({ |
| 122 | accountId, |
| 123 | integrationId: created.integration.integrationId, |
| 124 | }) |
| 125 | .pipe(Effect.flip); |
| 126 | expect(gone._tag).toEqual("DevicePostureIntegrationNotFound"); |
| 127 | }).pipe(logLevel), |
| 128 | { |
| 129 | tags: [ |
| 130 | "provider:cloudflare", |
| 131 | "provider:cloudflare:access", |
| 132 | "provider:cloudflare:devices", |
| 133 | "live", |
| 134 | ], |
| 135 | timeout: 90_000, |
| 136 | }, |
| 137 | ); |
| 138 | |
| 139 | // Canonical `list()` test (account collection): `list()` enumerates every |
| 140 | // posture integration in the ambient account via the distilled paginated |
| 141 | // list op and returns the exact `read` Attributes shape. This read-only |
| 142 | // assertion always runs — without an entitled account / external provider |
| 143 | // the account simply has no integrations, so `list()` returns a well-typed |
| 144 | // empty array (Zero Trust `Forbidden` is mapped to `[]`). |
| 145 | test.provider( |
| 146 | "list enumerates posture integrations in the account", |
| 147 | (stack) => |
| 148 | Effect.gen(function* () { |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | const provider = yield* Provider.findProvider(Cloudflare.Devices.DevicePostureIntegration); |
| 152 | const all = yield* provider.list(); |
| 153 | |
| 154 | expect(Array.isArray(all)).toBe(true); |
| 155 | for (const item of all) { |
| 156 | expect(typeof item.integrationId).toBe("string"); |
| 157 | expect(typeof item.accountId).toBe("string"); |
| 158 | expect(typeof item.type).toBe("string"); |
| 159 | } |
| 160 | |
| 161 | yield* stack.destroy(); |
| 162 | }).pipe(logLevel), |
| 163 | { |
| 164 | tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"], |
| 165 | timeout: 90_000, |
| 166 | }, |
| 167 | ); |
| 168 | |
| 169 | // When an entitled account + reachable external provider is supplied via |
| 170 | // env, deploy a real integration and assert `list()` includes it. |
| 171 | test.provider.skipIf(!external)( |
| 172 | "list includes a deployed posture integration", |
| 173 | (stack) => |
| 174 | Effect.gen(function* () { |
| 175 | yield* stack.destroy(); |
| 176 | |
| 177 | const apiUrl = process.env.CLOUDFLARE_TEST_POSTURE_API_URL!; |
| 178 | const secret = process.env.CLOUDFLARE_TEST_POSTURE_SECRET!; |
| 179 | |
| 180 | const { integration } = yield* stack.deploy( |
| 181 | Effect.gen(function* () { |
| 182 | const token = yield* Cloudflare.Access.ServiceToken("ListS2sToken", {}); |
| 183 | const integration = yield* Cloudflare.Devices.DevicePostureIntegration("ListCustom", { |
| 184 | type: "custom_s2s", |
| 185 | interval: "10m", |
| 186 | config: { |
| 187 | apiUrl, |
| 188 | clientSecret: Redacted.make(secret), |
| 189 | accessClientId: token.clientId, |
| 190 | accessClientSecret: token.clientSecret!, |
| 191 | }, |
| 192 | }); |
| 193 | return { integration }; |
| 194 | }), |
| 195 | ); |
| 196 | |
| 197 | const provider = yield* Provider.findProvider(Cloudflare.Devices.DevicePostureIntegration); |
| 198 | const all = yield* provider.list(); |
| 199 | |
| 200 | expect(all.some((i) => i.integrationId === integration.integrationId)).toBe(true); |
| 201 | |
| 202 | yield* stack.destroy(); |
| 203 | }).pipe(logLevel), |
| 204 | { |
| 205 | tags: [ |
| 206 | "provider:cloudflare", |
| 207 | "provider:cloudflare:access", |
| 208 | "provider:cloudflare:devices", |
| 209 | "live", |
| 210 | ], |
| 211 | timeout: 90_000, |
| 212 | }, |
| 213 | ); |