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
19const waitUntilGone = (name: string) =>
20 integrations.getProjectsLocationsTemplates({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("1 second"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30test.provider(
31 "getProjectsLocationsTemplates on a missing template fails with a typed tag",
32 (stack) =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35 yield* stack.destroy();
36
37 const error = yield* Effect.flip(
38 integrations.getProjectsLocationsTemplates({
39 name: `projects/${project}/locations/${location}/templates/alchemy-missing-template`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:integrations", "live"],
48 timeout: 90_000,
49 },
50);
51
52test.provider(
53 "create, update, and delete an integration template",
54 (stack) =>
55 Effect.gen(function* () {
56 const { project } = yield* GcpEnvironment.current;
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.Integrations.Template("Orders", {
62 location,
63 displayName: "order-sync",
64 description: "sync orders",
65 tags: ["orders"],
66 categories: ["SALES_AND_MARKETING"],
67 visibility: "PRIVATE",
68 });
69 }),
70 );
71
72 expect(created.templateId).toEqual(expect.any(String));
73 expect(created.location).toEqual(location);
74 expect(created.project).toEqual(project);
75 expect(created.name).toEqual(
76 `projects/${project}/locations/${location}/templates/${created.templateId}`,
77 );
78 expect(created.displayName).toEqual("order-sync");
79 expect(created.description).toEqual("sync orders");
80 expect(created.tags).toContain("orders");
81 expect(created.categories).toContain("SALES_AND_MARKETING");
82 expect(created.visibility).toEqual("PRIVATE");
83
84 const fetched = yield* integrations.getProjectsLocationsTemplates({
85 name: created.name,
86 });
87 expect(fetched.name).toEqual(created.name);
88 expect(fetched.description).toContain("alchemy-id=");
89 expect(fetched.displayName).toEqual("order-sync");
90
91 const updated = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* GCP.Integrations.Template("Orders", {
94 templateId: created.templateId,
95 location,
96 displayName: "order-sync-v2",
97 description: "sync orders v2",
98 tags: ["orders", "v2"],
99 categories: ["SALES_AND_MARKETING", "UTILITY"],
100 visibility: "PRIVATE",
101 });
102 }),
103 );
104
105 expect(updated.name).toEqual(created.name);
106 expect(updated.displayName).toEqual("order-sync-v2");
107 expect(updated.description).toEqual("sync orders v2");
108 expect(updated.tags).toEqual(expect.arrayContaining(["orders", "v2"]));
109 expect(updated.categories).toContain("UTILITY");
110
111 yield* stack.destroy();
112
113 const gone = yield* waitUntilGone(created.name);
114 expect(gone).toEqual("gone");
115 }).pipe(logLevel),
116 {
117 tags: ["provider:gcp", "provider:gcp:integrations", "live"],
118 timeout: 90_000,
119 },
120);