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

main
ts 173 lines 6.21 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as pubsublite from "@distilled.cloud/gcp/pubsublite_v1";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import { currentProject, runLifecycle } from "./common.ts";
10 import PubSubLiteBindingsHost, {
11 COMMITTED_OFFSET,
12 Capacity,
13 Events,
14 Inbox,
15 } from "./fixtures/bindings-host.ts";
16
17 const testOptions = { providers: GCP.providers() };
18 const { test, beforeAll, afterAll } = Test.make(testOptions);
19 const sharedStack = Core.scratchStack(testOptions, "PubSubLiteBindings");
20
21 let baseUrl: string;
22 let member: string;
23 let names: { topic: string; subscription: string; reservation: string };
24
25 /** `[role, condition]` pairs the host holds on the project. */
26 const 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.
40 const 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).
52 describe.skipIf(!runLifecycle)(
53 "PubSubLite Bindings",
54 {
55 tags: ["provider:gcp", "provider:gcp:pubsublite", "provider:gcp:run", "live"],
56 },
57 () => {
58 beforeAll(
59 Effect.gen(function* () {
60 yield* sharedStack.destroy();
61 const out = yield* sharedStack.deploy(
62 Effect.gen(function* () {
63 const host = yield* PubSubLiteBindingsHost;
64 return {
65 uri: host.uri,
66 serviceAccount: host.serviceAccount,
67 topic: (yield* Events).name,
68 subscription: (yield* Inbox).name,
69 reservation: (yield* Capacity).name,
70 };
71 }),
72 );
73 baseUrl = out.uri!;
74 member = `serviceAccount:${out.serviceAccount!}`;
75 names = out;
76 }),
77 { timeout: 900_000 },
78 );
79
80 afterAll(sharedStack.destroy(), { timeout: 600_000 });
81
82 describe("GetTopic", () => {
83 test.provider(
84 "reads the topic",
85 (_stack) =>
86 Effect.gen(function* () {
87 const live = yield* expectProbe<pubsublite.Topic>(baseUrl, "getTopic");
88 expect(live.name).toEqual(names.topic);
89 expect(yield* hostProjectGrants).toEqual(expectedGrants);
90 }),
91 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
92 );
93 });
94
95 describe("GetPartitions", () => {
96 test.provider(
97 "reads the partition count",
98 (_stack) =>
99 Effect.gen(function* () {
100 const live = yield* expectProbe<pubsublite.TopicPartitions>(baseUrl, "getPartitions");
101 expect(live.partitionCount).toEqual("1");
102 expect(yield* hostProjectGrants).toEqual(expectedGrants);
103 }),
104 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
105 );
106 });
107
108 describe("ComputeHeadCursor", () => {
109 test.provider(
110 "computes the head cursor of partition 0",
111 (_stack) =>
112 Effect.gen(function* () {
113 const head = yield* expectProbe<pubsublite.ComputeHeadCursorResponse>(
114 baseUrl,
115 "computeHeadCursor",
116 );
117 expect(head.headCursor?.offset ?? "0").toEqual("0");
118 expect(yield* hostProjectGrants).toEqual(expectedGrants);
119 }),
120 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
121 );
122 });
123
124 describe("GetSubscription", () => {
125 test.provider(
126 "reads the subscription",
127 (_stack) =>
128 Effect.gen(function* () {
129 const live = yield* expectProbe<pubsublite.Subscription>(baseUrl, "getSubscription");
130 expect(live.name).toEqual(names.subscription);
131 expect(live.topic).toEqual(names.topic);
132 expect(yield* hostProjectGrants).toEqual(expectedGrants);
133 }),
134 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
135 );
136 });
137
138 describe("CommitCursor", () => {
139 test.provider(
140 "commits the partition 0 cursor",
141 (_stack) =>
142 Effect.gen(function* () {
143 yield* expectProbe(baseUrl, "commitCursor");
144 const cursors = yield* pubsublite.listCursorProjectsLocationsSubscriptionsCursors({
145 parent: names.subscription,
146 });
147 expect(
148 (cursors.partitionCursors ?? []).map((cursor) => ({
149 partition: cursor.partition,
150 offset: cursor.cursor?.offset,
151 })),
152 ).toEqual([{ partition: "0", offset: COMMITTED_OFFSET }]);
153 expect(yield* hostProjectGrants).toEqual(expectedGrants);
154 }),
155 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
156 );
157 });
158
159 describe("GetReservation", () => {
160 test.provider(
161 "reads the reservation",
162 (_stack) =>
163 Effect.gen(function* () {
164 const live = yield* expectProbe<pubsublite.Reservation>(baseUrl, "getReservation");
165 expect(live.name).toEqual(names.reservation);
166 expect(live.throughputCapacity).toEqual("4");
167 expect(yield* hostProjectGrants).toEqual(expectedGrants);
168 }),
169 { tags: ["provider:gcp", "provider:gcp:pubsublite", "live"] },
170 );
171 });
172 },
173 );