1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (name: string) =>
17 pubsub.getProjectsSnapshots({ snapshot: name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27const getSnapshot = (name: string) =>
28 pubsub.getProjectsSnapshots({ snapshot: name }).pipe(
29 Effect.retry({
30 while: (error) => error._tag === "NotFound",
31 schedule: Schedule.spaced("1 second"),
32 times: 8,
33 }),
34 );
35
36test.provider(
37 "create, update, replace, and delete a snapshot",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const created = yield* stack.deploy(
43 Effect.gen(function* () {
44 const topic = yield* GCP.PubSub.Topic("Events", {
45 labels: { env: "test" },
46 });
47 const subscription = yield* GCP.PubSub.Subscription("Orders", {
48 topic: topic.name,
49 labels: { env: "test" },
50 });
51 const archive = yield* GCP.PubSub.Subscription("Archive", {
52 topic: topic.name,
53 labels: { env: "test" },
54 });
55 const snapshot = yield* GCP.PubSub.Snapshot("Checkpoint", {
56 subscription: subscription.name,
57 labels: { env: "test" },
58 });
59 return { topic, subscription, archive, snapshot };
60 }),
61 );
62
63 expect(created.snapshot.name).toContain("/snapshots/");
64 expect(created.snapshot.snapshotId).toEqual(expect.any(String));
65 expect(created.snapshot.topic).toEqual(created.topic.name);
66 expect(created.snapshot.labels).toMatchObject({ env: "test" });
67 expect(created.snapshot.expireTime).toEqual(expect.any(String));
68
69 const fetched = yield* getSnapshot(created.snapshot.name);
70 expect(fetched.name).toEqual(created.snapshot.name);
71 expect(fetched.topic).toEqual(created.topic.name);
72 expect(fetched.labels?.env).toEqual("test");
73
74 const updated = yield* stack.deploy(
75 Effect.gen(function* () {
76 const topic = yield* GCP.PubSub.Topic("Events", {
77 topicId: created.topic.topicId,
78 labels: { env: "test" },
79 });
80 const subscription = yield* GCP.PubSub.Subscription("Orders", {
81 subscriptionId: created.subscription.subscriptionId,
82 topic: topic.name,
83 labels: { env: "test" },
84 });
85 const archive = yield* GCP.PubSub.Subscription("Archive", {
86 subscriptionId: created.archive.subscriptionId,
87 topic: topic.name,
88 labels: { env: "test" },
89 });
90 const snapshot = yield* GCP.PubSub.Snapshot("Checkpoint", {
91 snapshotId: created.snapshot.snapshotId,
92 subscription: subscription.name,
93 labels: { env: "prod", role: "backup" },
94 });
95 return { topic, subscription, archive, snapshot };
96 }),
97 );
98
99 expect(updated.snapshot.name).toEqual(created.snapshot.name);
100 expect(updated.snapshot.topic).toEqual(created.topic.name);
101 expect(updated.snapshot.labels).toMatchObject({
102 env: "prod",
103 role: "backup",
104 });
105
106 const fetchedUpdated = yield* getSnapshot(updated.snapshot.name);
107 expect(fetchedUpdated.labels?.env).toEqual("prod");
108 expect(fetchedUpdated.labels?.role).toEqual("backup");
109
110 const replaced = yield* stack.deploy(
111 Effect.gen(function* () {
112 const topic = yield* GCP.PubSub.Topic("Events", {
113 topicId: created.topic.topicId,
114 labels: { env: "test" },
115 });
116 // Keep both subscriptions deployed across the replace so the
117 // engine does not deadlock on a replace+remove of a dependency.
118 const orders = yield* GCP.PubSub.Subscription("Orders", {
119 subscriptionId: created.subscription.subscriptionId,
120 topic: topic.name,
121 labels: { env: "test" },
122 });
123 const archive = yield* GCP.PubSub.Subscription("Archive", {
124 subscriptionId: created.archive.subscriptionId,
125 topic: topic.name,
126 labels: { env: "test" },
127 });
128 const snapshot = yield* GCP.PubSub.Snapshot("Checkpoint", {
129 snapshotId: created.snapshot.snapshotId,
130 subscription: archive.name,
131 labels: { env: "prod", role: "backup" },
132 });
133 return { topic, orders, archive, snapshot };
134 }),
135 );
136
137 expect(replaced.snapshot.name).toEqual(created.snapshot.name);
138 expect(replaced.snapshot.snapshotId).toEqual(created.snapshot.snapshotId);
139 expect(replaced.snapshot.topic).toEqual(created.topic.name);
140 expect(replaced.snapshot.expireTime).not.toEqual(
141 updated.snapshot.expireTime,
142 );
143
144 const fetchedReplaced = yield* getSnapshot(replaced.snapshot.name);
145 expect(fetchedReplaced.name).toEqual(replaced.snapshot.name);
146 expect(fetchedReplaced.topic).toEqual(created.topic.name);
147 expect(fetchedReplaced.expireTime).toEqual(replaced.snapshot.expireTime);
148
149 yield* stack.destroy();
150
151 const gone = yield* waitUntilGone(created.snapshot.name);
152 expect(gone).toEqual("gone");
153 }).pipe(logLevel),
154 { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 90_000 },
155);