@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

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