1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as integrations from "@distilled.cloud/gcp/integrations_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const 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.
23const 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).
27const sfdcCredential = {
28 credentialType: "USERNAME_AND_PASSWORD" as const,
29 usernameAndPassword: { username: "alchemy", password: "test-secret" },
30};
31
32const waitUntilGone = (name: string) =>
33 integrations.getProjectsLocationsSfdcInstances({ 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
47test.provider(
48 "getProjectsLocationsSfdcInstances on a missing instance 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.getProjectsLocationsSfdcInstances({
56 name: `projects/${project}/locations/${location}/sfdcInstances/alchemy-missing-sfdc`,
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
69test.provider.skipIf(!runSfdcLifecycle)(
70 "create, update, and delete an SFDC instance",
71 (stack) =>
72 Effect.gen(function* () {
73 const { project } = yield* GcpEnvironment.current;
74 yield* stack.destroy();
75
76 const created = yield* stack.deploy(
77 Effect.gen(function* () {
78 const auth = yield* GCP.Integrations.AuthConfig("SalesforceAuth", {
79 location,
80 decryptedCredential: sfdcCredential,
81 });
82 return yield* GCP.Integrations.SfdcInstance("Salesforce", {
83 authConfigId: [auth.authConfigId],
84 location,
85 displayName: "alchemy-sfdc",
86 description: "production salesforce",
87 sfdcOrgId: "00Dxx0000000001",
88 });
89 }),
90 );
91
92 expect(created.sfdcInstanceId).toEqual(expect.any(String));
93 expect(created.location).toEqual(location);
94 expect(created.project).toEqual(project);
95 expect(created.name).toEqual(
96 `projects/${project}/locations/${location}/sfdcInstances/${created.sfdcInstanceId}`,
97 );
98 expect(created.displayName).toEqual("alchemy-sfdc");
99 expect(created.description).toEqual("production salesforce");
100 expect(created.sfdcOrgId).toEqual("00Dxx0000000001");
101
102 const fetched = yield* integrations.getProjectsLocationsSfdcInstances({
103 name: created.name,
104 });
105 // The API echoes names keyed by project number.
106 expect(fetched.name?.split("/").slice(2)).toEqual(
107 created.name.split("/").slice(2),
108 );
109 expect(fetched.description).toContain("alchemy-id=");
110 expect(fetched.sfdcOrgId).toEqual("00Dxx0000000001");
111
112 const updated = yield* stack.deploy(
113 Effect.gen(function* () {
114 const auth = yield* GCP.Integrations.AuthConfig("SalesforceAuth", {
115 location,
116 decryptedCredential: sfdcCredential,
117 });
118 return yield* GCP.Integrations.SfdcInstance("Salesforce", {
119 authConfigId: [auth.authConfigId],
120 sfdcInstanceId: created.sfdcInstanceId,
121 location,
122 displayName: "alchemy-sfdc-v2",
123 description: "production salesforce v2",
124 sfdcOrgId: "00Dxx0000000002",
125 });
126 }),
127 );
128
129 expect(updated.name).toEqual(created.name);
130 expect(updated.displayName).toEqual("alchemy-sfdc-v2");
131 expect(updated.description).toEqual("production salesforce v2");
132 expect(updated.sfdcOrgId).toEqual("00Dxx0000000002");
133
134 yield* stack.destroy();
135
136 const gone = yield* waitUntilGone(created.name);
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel),
139 {
140 tags: ["provider:gcp", "provider:gcp:integrations", "live"],
141 timeout: 90_000,
142 },
143);