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

main
ts 483 lines 18.6 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 bigquery from "@distilled.cloud/gcp/bigquery_v2";
5 import * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
6 import * as iam from "@distilled.cloud/gcp/unstable/iam_v1";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: GCP.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const waitUntilGone = (name: string) =>
17 pubsub.getProjectsSubscriptions({ subscription: 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
27 const getSubscription = (name: string) =>
28 pubsub.getProjectsSubscriptions({ subscription: name }).pipe(
29 Effect.retry({
30 while: (error) => error._tag === "NotFound",
31 schedule: Schedule.spaced("1 second"),
32 times: 8,
33 }),
34 );
35
36 const pullUntilMessage = (name: string) =>
37 pubsub
38 .pullProjectsSubscriptions({
39 subscription: name,
40 body: { maxMessages: 1, returnImmediately: true },
41 })
42 .pipe(
43 Effect.repeat({
44 schedule: Schedule.spaced("1 second"),
45 until: (response) => (response.receivedMessages?.length ?? 0) > 0,
46 times: 10,
47 }),
48 );
49
50 test.provider(
51 "create, update, and delete a subscription",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const topic = yield* GCP.PubSub.Topic("Events", {
59 labels: { env: "test" },
60 });
61 const subscription = yield* GCP.PubSub.Subscription("Orders", {
62 topic: topic.name,
63 labels: { env: "test" },
64 });
65 return { topic, subscription };
66 }),
67 );
68
69 expect(created.subscription.name).toContain("/subscriptions/");
70 expect(created.subscription.subscriptionId).toEqual(expect.any(String));
71 expect(created.subscription.topic).toEqual(created.topic.name);
72 expect(created.subscription.labels).toMatchObject({ env: "test" });
73 expect(created.subscription.ackDeadlineSeconds).toEqual(10);
74
75 const fetched = yield* getSubscription(created.subscription.name);
76 expect(fetched.name).toEqual(created.subscription.name);
77 expect(fetched.topic).toEqual(created.topic.name);
78 expect(fetched.labels?.env).toEqual("test");
79
80 yield* pubsub.publishProjectsTopics({
81 topic: created.topic.name,
82 body: { messages: [{ data: btoa("hello-pubsub") }] },
83 });
84
85 const pulled = yield* pullUntilMessage(created.subscription.name);
86 const received = pulled.receivedMessages ?? [];
87 expect(received.length).toBeGreaterThan(0);
88 const ackId = received[0]?.ackId;
89 expect(ackId).toEqual(expect.any(String));
90 if (ackId !== undefined) {
91 yield* pubsub.acknowledgeProjectsSubscriptions({
92 subscription: created.subscription.name,
93 body: { ackIds: [ackId] },
94 });
95 }
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 const topic = yield* GCP.PubSub.Topic("Events", {
100 topicId: created.topic.topicId,
101 labels: { env: "test" },
102 });
103 const subscription = yield* GCP.PubSub.Subscription("Orders", {
104 subscriptionId: created.subscription.subscriptionId,
105 topic: topic.name,
106 labels: { env: "prod", role: "orders" },
107 ackDeadlineSeconds: 30,
108 messageRetentionDuration: "86400s",
109 });
110 return { topic, subscription };
111 }),
112 );
113
114 expect(updated.subscription.name).toEqual(created.subscription.name);
115 expect(updated.subscription.topic).toEqual(created.topic.name);
116 expect(updated.subscription.labels).toMatchObject({
117 env: "prod",
118 role: "orders",
119 });
120 expect(updated.subscription.ackDeadlineSeconds).toEqual(30);
121 expect(updated.subscription.messageRetentionDuration).toEqual("86400s");
122
123 const fetchedUpdated = yield* getSubscription(updated.subscription.name);
124 expect(fetchedUpdated.ackDeadlineSeconds).toEqual(30);
125 expect(fetchedUpdated.labels?.env).toEqual("prod");
126 expect(fetchedUpdated.labels?.role).toEqual("orders");
127 expect(fetchedUpdated.messageRetentionDuration).toEqual("86400s");
128
129 yield* stack.destroy();
130
131 const gone = yield* waitUntilGone(created.subscription.name);
132 expect(gone).toEqual("gone");
133 }).pipe(logLevel),
134 { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 90_000 },
135 );
136
137 test.provider(
138 "dead-letters a message after maxDeliveryAttempts",
139 (stack) =>
140 Effect.gen(function* () {
141 yield* stack.destroy();
142
143 const out = yield* stack.deploy(
144 Effect.gen(function* () {
145 const topic = yield* GCP.PubSub.Topic("DlqSource", {});
146 const deadLetters = yield* GCP.PubSub.Topic("DlqTarget", {});
147 const inbox = yield* GCP.PubSub.Subscription("DlqInbox", {
148 topic: deadLetters.name,
149 });
150 const subscription = yield* GCP.PubSub.Subscription("DlqWork", {
151 topic: topic.name,
152 deadLetterPolicy: {
153 deadLetterTopic: deadLetters.name,
154 maxDeliveryAttempts: 5,
155 },
156 });
157 return {
158 topic: topic.name,
159 deadLetters: deadLetters.name,
160 inbox: inbox.name,
161 subscription: subscription.name,
162 };
163 }),
164 );
165
166 // The Pub/Sub service agent forwards dead letters, so it must be
167 // able to publish to the target and ack on the source subscription.
168 const topicPolicy = yield* pubsub.getIamPolicyProjectsTopics({
169 resource: out.deadLetters,
170 });
171 const agent = (topicPolicy.bindings ?? [])
172 .find((binding) => binding.role === "roles/pubsub.publisher")
173 ?.members?.find((member) => member.includes("@gcp-sa-pubsub."));
174 expect(agent).toBeDefined();
175 const subscriptionPolicy = yield* pubsub.getIamPolicyProjectsSubscriptions({
176 resource: out.subscription,
177 });
178 expect(
179 (subscriptionPolicy.bindings ?? []).some(
180 (binding) =>
181 binding.role === "roles/pubsub.subscriber" && binding.members?.includes(agent!),
182 ),
183 ).toBe(true);
184
185 yield* pubsub.publishProjectsTopics({
186 topic: out.topic,
187 body: { messages: [{ data: btoa("poison") }] },
188 });
189
190 // Pull and nack until Pub/Sub gives up on the message.
191 const nackOnce = pubsub
192 .pullProjectsSubscriptions({
193 subscription: out.subscription,
194 body: { maxMessages: 1, returnImmediately: true },
195 })
196 .pipe(
197 Effect.tap(({ receivedMessages = [] }) => {
198 const ackIds = receivedMessages.flatMap((m) => (m.ackId ? [m.ackId] : []));
199 return ackIds.length === 0
200 ? Effect.void
201 : pubsub.modifyAckDeadlineProjectsSubscriptions({
202 subscription: out.subscription,
203 body: { ackIds, ackDeadlineSeconds: 0 },
204 });
205 }),
206 );
207 const deadLetter = pubsub
208 .pullProjectsSubscriptions({
209 subscription: out.inbox,
210 body: { maxMessages: 1, returnImmediately: true },
211 })
212 .pipe(Effect.map(({ receivedMessages = [] }) => receivedMessages[0]));
213
214 const letter = yield* nackOnce.pipe(
215 Effect.andThen(deadLetter),
216 Effect.repeat({
217 schedule: Schedule.spaced("2 seconds"),
218 until: (message) => message !== undefined,
219 times: 60,
220 }),
221 );
222 expect(atob(letter!.message?.data ?? "")).toEqual("poison");
223 expect(letter!.message?.attributes?.CloudPubSubDeadLetterSourceSubscription).toEqual(
224 out.subscription.split("/").pop(),
225 );
226
227 yield* stack.destroy();
228 expect(yield* waitUntilGone(out.subscription)).toEqual("gone");
229 expect(yield* waitUntilGone(out.inbox)).toEqual("gone");
230 }).pipe(logLevel),
231 { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 240_000 },
232 );
233
234 const tableWriters = (table: { project: string; datasetId: string; tableId: string }) =>
235 bigquery
236 .getIamPolicyTables({
237 resource: `projects/${table.project}/datasets/${table.datasetId}/tables/${table.tableId}`,
238 body: {},
239 })
240 .pipe(
241 Effect.map(
242 (policy) =>
243 policy.bindings?.find((binding) => binding.role === "roles/bigquery.dataEditor")
244 ?.members ?? [],
245 ),
246 );
247
248 // `projects/-/` answers 403 for a deleted account; the project id gets 404.
249 const serviceAccountExists = (email: string) =>
250 iam
251 .getProjectsServiceAccounts({
252 name: `projects/${email.split("@")[1]!.split(".")[0]}/serviceAccounts/${email}`,
253 })
254 .pipe(
255 Effect.as(true),
256 Effect.catchTag("NotFound", () => Effect.succeed(false)),
257 );
258
259 // IAM reads lag deletes: poll until the account stops being served.
260 const waitUntilServiceAccountGone = (email: string) =>
261 serviceAccountExists(email).pipe(
262 Effect.repeat({
263 schedule: Schedule.spaced("2 seconds"),
264 until: (exists) => !exists,
265 times: 15,
266 }),
267 );
268
269 test.provider(
270 "writes messages to a BigQuery table",
271 (stack) =>
272 Effect.gen(function* () {
273 yield* stack.destroy();
274
275 const bigqueryStack = (bigqueryConfig?: { writeMetadata: boolean }) =>
276 Effect.gen(function* () {
277 const topic = yield* GCP.PubSub.Topic("Impressions", {});
278 const dataset = yield* GCP.BigQuery.Dataset("ImpressionsRaw", {
279 location: "US-CENTRAL1",
280 forceDestroy: true,
281 });
282 const table = yield* GCP.BigQuery.Table("ImpressionEvent", {
283 datasetId: dataset.datasetId,
284 schema: [
285 { name: "subscription_name", type: "STRING" },
286 { name: "message_id", type: "STRING" },
287 { name: "publish_time", type: "TIMESTAMP" },
288 { name: "data", type: "STRING" },
289 { name: "attributes", type: "STRING" },
290 ],
291 });
292 const subscription = yield* GCP.PubSub.Subscription("ImpressionsBigQuery", {
293 topic: topic.name,
294 bigqueryConfig:
295 bigqueryConfig === undefined ? undefined : { table: table.name, ...bigqueryConfig },
296 });
297 return { topic, table, subscription };
298 });
299
300 // Pub/Sub writes as an account the subscription mints and grants on
301 // the table: nothing in the stack names a principal or a policy.
302 const created = yield* stack.deploy(bigqueryStack({ writeMetadata: true }));
303 const tableRef = `${created.table.project}.${created.table.datasetId}.${created.table.tableId}`;
304 const writer = created.subscription.bigqueryConfig?.serviceAccountEmail;
305 expect(created.subscription.bigqueryConfig).toMatchObject({
306 table: tableRef,
307 useTopicSchema: false,
308 useTableSchema: false,
309 writeMetadata: true,
310 dropUnknownFields: false,
311 serviceAccountEmail: expect.stringMatching(/^alch-.+\.iam\.gserviceaccount\.com$/),
312 });
313 expect(yield* tableWriters(created.table)).toContain(`serviceAccount:${writer}`);
314
315 const fetched = yield* getSubscription(created.subscription.name);
316 expect(fetched.bigqueryConfig?.table).toEqual(tableRef);
317 expect(fetched.bigqueryConfig?.serviceAccountEmail).toEqual(writer);
318
319 const published = yield* pubsub.publishProjectsTopics({
320 topic: created.topic.name,
321 body: { messages: [{ data: btoa("impression-1") }] },
322 });
323 const messageId = published.messageIds?.[0];
324 expect(messageId).toEqual(expect.any(String));
325
326 const rows = yield* bigquery
327 .queryJobs({
328 projectId: created.table.project,
329 body: {
330 query: `SELECT DISTINCT data, message_id FROM ${created.table.datasetId}.${created.table.tableId}`,
331 useLegacySql: false,
332 location: created.table.location,
333 },
334 })
335 .pipe(
336 Effect.map(({ rows = [] }) => rows.map((row) => row.f?.map((cell) => cell.v))),
337 Effect.repeat({
338 schedule: Schedule.spaced("2 seconds"),
339 until: (rows) => rows.length > 0,
340 times: 30,
341 }),
342 );
343 expect(rows).toEqual([["impression-1", messageId]]);
344
345 const updated = yield* stack.deploy(bigqueryStack({ writeMetadata: false }));
346 expect(updated.subscription.name).toEqual(created.subscription.name);
347 expect(updated.subscription.bigqueryConfig).toMatchObject({
348 table: tableRef,
349 writeMetadata: false,
350 serviceAccountEmail: writer,
351 });
352 const fetchedUpdated = yield* getSubscription(updated.subscription.name);
353 expect(fetchedUpdated.bigqueryConfig?.writeMetadata ?? false).toBe(false);
354
355 // Dropping bigqueryConfig turns it back into a pull subscription and
356 // revokes the writer's grant; the writer lives as long as the
357 // subscription, so turning BigQuery back on reuses it.
358 const pull = yield* stack.deploy(bigqueryStack());
359 expect(pull.subscription.name).toEqual(created.subscription.name);
360 expect(pull.subscription.bigqueryConfig).toBeUndefined();
361 expect((yield* getSubscription(pull.subscription.name)).bigqueryConfig?.table ?? "").toEqual(
362 "",
363 );
364 expect(yield* tableWriters(pull.table)).not.toContain(`serviceAccount:${writer}`);
365 expect(yield* serviceAccountExists(writer!)).toBe(true);
366
367 // Destroying the subscription deletes its writer.
368 const again = yield* stack.deploy(bigqueryStack({ writeMetadata: false }));
369 expect(again.subscription.bigqueryConfig?.serviceAccountEmail).toEqual(writer);
370 expect(yield* tableWriters(again.table)).toContain(`serviceAccount:${writer}`);
371
372 yield* stack.destroy();
373 expect(yield* waitUntilGone(created.subscription.name)).toEqual("gone");
374 expect(yield* waitUntilServiceAccountGone(writer!)).toBe(false);
375 }).pipe(logLevel),
376 { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 360_000 },
377 );
378
379 const accountTokenCreators = (account: { project: string; email: string }) =>
380 iam
381 .getIamPolicyProjectsServiceAccounts({
382 resource: `projects/${account.project}/serviceAccounts/${account.email}`,
383 })
384 .pipe(
385 Effect.map(
386 (policy) =>
387 policy.bindings?.find(
388 (binding) => binding.role === "roles/iam.serviceAccountTokenCreator",
389 )?.members ?? [],
390 ),
391 );
392
393 test.provider(
394 "writes to BigQuery as a service account the stack provides",
395 (stack) =>
396 Effect.gen(function* () {
397 yield* stack.destroy();
398
399 const writerStack = (subscribe?: { asAccount: boolean }) =>
400 Effect.gen(function* () {
401 const topic = yield* GCP.PubSub.Topic("Clicks", {});
402 const dataset = yield* GCP.BigQuery.Dataset("ClicksRaw", {
403 location: "US-CENTRAL1",
404 forceDestroy: true,
405 });
406 const table = yield* GCP.BigQuery.Table("ClickEvent", {
407 datasetId: dataset.datasetId,
408 schema: [{ name: "data", type: "STRING" }],
409 });
410 const account = yield* GCP.IAM.ServiceAccount("ClicksWriter", {});
411 // The account already writes to the table for its own reasons:
412 // the subscription uses that grant and must never revoke it.
413 yield* GCP.IAM.Member("ClicksWriterTable", {
414 kind: "bigquery.table",
415 name: table.name,
416 role: "roles/bigquery.dataEditor",
417 member: Output.interpolate`serviceAccount:${account.email}`,
418 });
419 const subscription =
420 subscribe === undefined
421 ? undefined
422 : yield* GCP.PubSub.Subscription("ClicksBigQuery", {
423 topic: topic.name,
424 bigqueryConfig: {
425 table: table.name,
426 serviceAccountEmail: subscribe.asAccount ? account.email : undefined,
427 },
428 });
429 return { topic, table, account, subscription };
430 });
431
432 yield* stack.deploy(writerStack());
433
434 const given = yield* stack.deploy(writerStack({ asAccount: true }));
435 const account = given.account;
436 const agentMember = (members: readonly string[]) =>
437 members.find((member) => member.includes("@gcp-sa-pubsub."));
438 expect(given.subscription?.bigqueryConfig?.serviceAccountEmail).toEqual(account.email);
439 expect(agentMember(yield* accountTokenCreators(account))).toBeDefined();
440 // Only the token-creator grant is the subscription's; the table grant
441 // was already there.
442 expect(given.subscription?.iamGrants?.map((grant) => grant.role)).toEqual([
443 "roles/iam.serviceAccountTokenCreator",
444 ]);
445
446 const published = yield* pubsub.publishProjectsTopics({
447 topic: given.topic.name,
448 body: { messages: [{ data: btoa("click-1") }] },
449 });
450 const rows = yield* bigquery
451 .queryJobs({
452 projectId: given.table.project,
453 body: {
454 query: `SELECT DISTINCT data FROM ${given.table.datasetId}.${given.table.tableId}`,
455 useLegacySql: false,
456 location: given.table.location,
457 },
458 })
459 .pipe(
460 Effect.map(({ rows = [] }) => rows.map((row) => row.f?.map((cell) => cell.v))),
461 Effect.repeat({
462 schedule: Schedule.spaced("2 seconds"),
463 until: (rows) => rows.length > 0,
464 times: 30,
465 }),
466 );
467 expect(published.messageIds?.length).toEqual(1);
468 expect(rows).toEqual([["click-1"]]);
469
470 // Switching to a minted writer revokes what the subscription granted
471 // on the account and keeps what the account already held.
472 const minted = yield* stack.deploy(writerStack({ asAccount: false }));
473 expect(minted.subscription?.name).toEqual(given.subscription?.name);
474 expect(minted.subscription?.bigqueryConfig?.serviceAccountEmail).not.toEqual(account.email);
475 expect(agentMember(yield* accountTokenCreators(account))).toBeUndefined();
476 expect(yield* tableWriters(minted.table)).toContain(`serviceAccount:${account.email}`);
477 expect(yield* serviceAccountExists(account.email)).toBe(true);
478
479 yield* stack.destroy();
480 expect(yield* waitUntilGone(given.subscription!.name)).toEqual("gone");
481 }).pipe(logLevel),
482 { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 360_000 },
483 );