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