1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as Core from "@/Test/Core";
4import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
5import * as pubsublite from "@distilled.cloud/gcp/pubsublite_v1";
6import { describe, expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import { dockerAvailable, expectProbe } from "../bindingHost.ts";
9import { currentProject, runLifecycle } from "./common.ts";
10import PubSubLiteBindingsHost, {
11 COMMITTED_OFFSET,
12 Capacity,
13 Events,
14 Inbox,
15} from "./fixtures/bindings-host.ts";
16
17const testOptions = { providers: GCP.providers() };
18const { test, beforeAll, afterAll } = Test.make(testOptions);
19const sharedStack = Core.scratchStack(testOptions, "PubSubLiteBindings");
20
21let baseUrl: string;
22let member: string;
23let names: { topic: string; subscription: string; reservation: string };
24
25/** `[role, condition]` pairs the host holds on the project. */
26const hostProjectGrants = Effect.gen(function* () {
27 const project = yield* currentProject;
28 const policy = yield* resourcemanager.getIamPolicyProjects({
29 resource: `projects/${project}`,
30 body: { options: { requestedPolicyVersion: 3 } },
31 });
32 return (policy.bindings ?? [])
33 .filter((binding) => (binding.members ?? []).includes(member))
34 .map((binding) => [binding.role, binding.condition])
35 .sort();
36});
37
38// Pub/Sub Lite has no resource-level IAM; its roles are granted on the
39// project.
40const expectedGrants = [
41 ["roles/pubsublite.subscriber", undefined],
42 ["roles/pubsublite.viewer", undefined],
43];
44
45// Pub/Sub Lite is turned down: creating a topic on the testing project
46// answers 403 PERMISSION_DENIED "Pub/Sub Lite is deprecated and will be
47// turned down in March 2026. New customers and existing customers who have
48// not used Pub/Sub Lite within the 90-day period preceding September 24,
49// 2024 ... will not be able to access the service" (reason
50// DEPRECATED_FOR_UNUSED_CUSTOMERS, typed `PubSubLiteTurnedDown`). Only an
51// allow-listed project can run this (GCP_TEST_PUBSUBLITE=1).
52describe.skipIf(!dockerAvailable || !runLifecycle)(
53 "PubSubLite Bindings",
54 {
55 tags: [
56 "provider:gcp",
57 "provider:gcp:pubsublite",
58 "provider:gcp:run",
59 "live",
60 ],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* sharedStack.destroy();
66 const out = yield* sharedStack.deploy(
67 Effect.gen(function* () {
68 const host = yield* PubSubLiteBindingsHost;
69 return {
70 uri: host.uri,
71 serviceAccount: host.serviceAccount,
72 topic: (yield* Events).name,
73 subscription: (yield* Inbox).name,
74 reservation: (yield* Capacity).name,
75 };
76 }),
77 );
78 baseUrl = out.uri!;
79 member = `serviceAccount:${out.serviceAccount!}`;
80 names = out;
81 }),
82 { timeout: 900_000 },
83 );
84
85 afterAll(sharedStack.destroy(), { timeout: 600_000 });
86
87 describe("GetTopic", () => {
88 test.provider(
89 "reads the topic",
90 (_stack) =>
91 Effect.gen(function* () {
92 const live = yield* expectProbe<pubsublite.Topic>(
93 baseUrl,
94 "getTopic",
95 );
96 expect(live.name).toEqual(names.topic);
97 expect(yield* hostProjectGrants).toEqual(expectedGrants);
98 }),
99 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
100 );
101 });
102
103 describe("GetPartitions", () => {
104 test.provider(
105 "reads the partition count",
106 (_stack) =>
107 Effect.gen(function* () {
108 const live = yield* expectProbe<pubsublite.TopicPartitions>(
109 baseUrl,
110 "getPartitions",
111 );
112 expect(live.partitionCount).toEqual("1");
113 expect(yield* hostProjectGrants).toEqual(expectedGrants);
114 }),
115 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
116 );
117 });
118
119 describe("ComputeHeadCursor", () => {
120 test.provider(
121 "computes the head cursor of partition 0",
122 (_stack) =>
123 Effect.gen(function* () {
124 const head =
125 yield* expectProbe<pubsublite.ComputeHeadCursorResponse>(
126 baseUrl,
127 "computeHeadCursor",
128 );
129 expect(head.headCursor?.offset ?? "0").toEqual("0");
130 expect(yield* hostProjectGrants).toEqual(expectedGrants);
131 }),
132 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
133 );
134 });
135
136 describe("GetSubscription", () => {
137 test.provider(
138 "reads the subscription",
139 (_stack) =>
140 Effect.gen(function* () {
141 const live = yield* expectProbe<pubsublite.Subscription>(
142 baseUrl,
143 "getSubscription",
144 );
145 expect(live.name).toEqual(names.subscription);
146 expect(live.topic).toEqual(names.topic);
147 expect(yield* hostProjectGrants).toEqual(expectedGrants);
148 }),
149 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
150 );
151 });
152
153 describe("CommitCursor", () => {
154 test.provider(
155 "commits the partition 0 cursor",
156 (_stack) =>
157 Effect.gen(function* () {
158 yield* expectProbe(baseUrl, "commitCursor");
159 const cursors =
160 yield* pubsublite.listCursorProjectsLocationsSubscriptionsCursors(
161 {
162 parent: names.subscription,
163 },
164 );
165 expect(
166 (cursors.partitionCursors ?? []).map((cursor) => ({
167 partition: cursor.partition,
168 offset: cursor.cursor?.offset,
169 })),
170 ).toEqual([{ partition: "0", offset: COMMITTED_OFFSET }]);
171 expect(yield* hostProjectGrants).toEqual(expectedGrants);
172 }),
173 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
174 );
175 });
176
177 describe("GetReservation", () => {
178 test.provider(
179 "reads the reservation",
180 (_stack) =>
181 Effect.gen(function* () {
182 const live = yield* expectProbe<pubsublite.Reservation>(
183 baseUrl,
184 "getReservation",
185 );
186 expect(live.name).toEqual(names.reservation);
187 expect(live.throughputCapacity).toEqual("4");
188 expect(yield* hostProjectGrants).toEqual(expectedGrants);
189 }),
190 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
191 );
192 });
193 },
194);