The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as integrations from "@distilled.cloud/gcp/integrations_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const location = "us-central1"; |
| 15 | |
| 16 | // Updating a Salesforce instance that points at a placeholder org fails with |
| 17 | // InternalServerError ("Unknown Error."); set |
| 18 | // GCP_TEST_INTEGRATIONS_SFDC=1 when the test org ids resolve to a real |
| 19 | // Salesforce org. |
| 20 | const runSfdcLifecycle = !!process.env.GCP_TEST_INTEGRATIONS_SFDC; |
| 21 | |
| 22 | // Salesforce instances must reference at least one auth config ("Auth config |
| 23 | // is not present in the request" otherwise). |
| 24 | const sfdcCredential = { |
| 25 | credentialType: "USERNAME_AND_PASSWORD" as const, |
| 26 | usernameAndPassword: { username: "alchemy", password: "test-secret" }, |
| 27 | }; |
| 28 | |
| 29 | const waitUntilGone = (name: string) => |
| 30 | integrations.getProjectsLocationsSfdcInstances({ name }).pipe( |
| 31 | Effect.map((row) => |
| 32 | (row.deleteTime ?? "").length > 0 ? ("gone" as const) : ("found" as const), |
| 33 | ), |
| 34 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 35 | Effect.repeat({ |
| 36 | schedule: Schedule.spaced("1 second"), |
| 37 | until: (status) => status === "gone", |
| 38 | times: 10, |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | test.provider( |
| 43 | "getProjectsLocationsSfdcInstances on a missing instance fails with a typed tag", |
| 44 | (stack) => |
| 45 | Effect.gen(function* () { |
| 46 | const { project } = yield* GcpEnvironment.current; |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | const error = yield* Effect.flip( |
| 50 | integrations.getProjectsLocationsSfdcInstances({ |
| 51 | name: `projects/${project}/locations/${location}/sfdcInstances/alchemy-missing-sfdc`, |
| 52 | }), |
| 53 | ); |
| 54 | expect(error._tag).toEqual("NotFound"); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { |
| 59 | tags: ["provider:gcp", "provider:gcp:integrations", "live"], |
| 60 | timeout: 90_000, |
| 61 | }, |
| 62 | ); |
| 63 | |
| 64 | test.provider.skipIf(!runSfdcLifecycle)( |
| 65 | "create, update, and delete an SFDC instance", |
| 66 | (stack) => |
| 67 | Effect.gen(function* () { |
| 68 | const { project } = yield* GcpEnvironment.current; |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const created = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | const auth = yield* GCP.Integrations.AuthConfig("SalesforceAuth", { |
| 74 | location, |
| 75 | decryptedCredential: sfdcCredential, |
| 76 | }); |
| 77 | return yield* GCP.Integrations.SfdcInstance("Salesforce", { |
| 78 | authConfigId: [auth.authConfigId], |
| 79 | location, |
| 80 | displayName: "alchemy-sfdc", |
| 81 | description: "production salesforce", |
| 82 | sfdcOrgId: "00Dxx0000000001", |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.sfdcInstanceId).toEqual(expect.any(String)); |
| 88 | expect(created.location).toEqual(location); |
| 89 | expect(created.project).toEqual(project); |
| 90 | expect(created.name).toEqual( |
| 91 | `projects/${project}/locations/${location}/sfdcInstances/${created.sfdcInstanceId}`, |
| 92 | ); |
| 93 | expect(created.displayName).toEqual("alchemy-sfdc"); |
| 94 | expect(created.description).toEqual("production salesforce"); |
| 95 | expect(created.sfdcOrgId).toEqual("00Dxx0000000001"); |
| 96 | |
| 97 | const fetched = yield* integrations.getProjectsLocationsSfdcInstances({ |
| 98 | name: created.name, |
| 99 | }); |
| 100 | // The API echoes names keyed by project number. |
| 101 | expect(fetched.name?.split("/").slice(2)).toEqual(created.name.split("/").slice(2)); |
| 102 | expect(fetched.description).toContain("alchemy-id="); |
| 103 | expect(fetched.sfdcOrgId).toEqual("00Dxx0000000001"); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | const auth = yield* GCP.Integrations.AuthConfig("SalesforceAuth", { |
| 108 | location, |
| 109 | decryptedCredential: sfdcCredential, |
| 110 | }); |
| 111 | return yield* GCP.Integrations.SfdcInstance("Salesforce", { |
| 112 | authConfigId: [auth.authConfigId], |
| 113 | sfdcInstanceId: created.sfdcInstanceId, |
| 114 | location, |
| 115 | displayName: "alchemy-sfdc-v2", |
| 116 | description: "production salesforce v2", |
| 117 | sfdcOrgId: "00Dxx0000000002", |
| 118 | }); |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | expect(updated.name).toEqual(created.name); |
| 123 | expect(updated.displayName).toEqual("alchemy-sfdc-v2"); |
| 124 | expect(updated.description).toEqual("production salesforce v2"); |
| 125 | expect(updated.sfdcOrgId).toEqual("00Dxx0000000002"); |
| 126 | |
| 127 | yield* stack.destroy(); |
| 128 | |
| 129 | const gone = yield* waitUntilGone(created.name); |
| 130 | expect(gone).toEqual("gone"); |
| 131 | }).pipe(logLevel), |
| 132 | { |
| 133 | tags: ["provider:gcp", "provider:gcp:integrations", "live"], |
| 134 | timeout: 90_000, |
| 135 | }, |
| 136 | ); |