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

main
ts 121 lines 4.15 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 cloudtasks from "@distilled.cloud/gcp/cloudtasks_v2";
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 cloudtasks.getProjectsLocationsQueues({ 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 test.provider(
25 "create, update, and delete a queue",
26 (stack) =>
27 Effect.gen(function* () {
28 yield* stack.destroy();
29
30 const created = yield* stack.deploy(
31 Effect.gen(function* () {
32 return yield* GCP.CloudTasks.Queue("Jobs", {
33 location: "us-central1",
34 rateLimits: {
35 maxDispatchesPerSecond: 5,
36 maxConcurrentDispatches: 3,
37 },
38 retryConfig: {
39 maxAttempts: 3,
40 },
41 });
42 }),
43 );
44
45 expect(created.name).toContain("/queues/");
46 expect(created.queueId).toEqual(expect.any(String));
47 expect(created.location).toEqual("us-central1");
48 expect(created.state).toEqual("RUNNING");
49 expect(created.retryConfig?.maxAttempts).toEqual(3);
50 expect(created.rateLimits?.maxDispatchesPerSecond).toEqual(5);
51 expect(created.rateLimits?.maxConcurrentDispatches).toEqual(3);
52
53 const fetched = yield* cloudtasks.getProjectsLocationsQueues({
54 name: created.name,
55 });
56 expect(fetched.name).toEqual(created.name);
57 expect(fetched.retryConfig?.maxAttempts).toEqual(3);
58 expect(fetched.rateLimits?.maxDispatchesPerSecond).toEqual(5);
59 expect(fetched.httpTarget?.headerOverrides ?? []).toEqual([]);
60
61 const updated = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.CloudTasks.Queue("Jobs", {
64 location: "us-central1",
65 rateLimits: {
66 maxDispatchesPerSecond: 8,
67 maxConcurrentDispatches: 4,
68 },
69 retryConfig: {
70 maxAttempts: 6,
71 },
72 stackdriverLoggingConfig: { samplingRatio: 1 },
73 state: "PAUSED",
74 });
75 }),
76 );
77
78 expect(updated.name).toEqual(created.name);
79 expect(updated.retryConfig?.maxAttempts).toEqual(6);
80 expect(updated.rateLimits?.maxDispatchesPerSecond).toEqual(8);
81 expect(updated.rateLimits?.maxConcurrentDispatches).toEqual(4);
82 expect(updated.stackdriverLoggingConfig?.samplingRatio).toEqual(1);
83 expect(updated.state).toEqual("PAUSED");
84
85 const refetched = yield* cloudtasks.getProjectsLocationsQueues({
86 name: created.name,
87 });
88 expect(refetched.retryConfig?.maxAttempts).toEqual(6);
89 expect(refetched.rateLimits?.maxDispatchesPerSecond).toEqual(8);
90 expect(refetched.stackdriverLoggingConfig?.samplingRatio).toEqual(1);
91 expect(refetched.state).toEqual("PAUSED");
92
93 const payload = yield* Effect.sync(() =>
94 Buffer.from(JSON.stringify({ id: "1" }), "utf8").toString("base64"),
95 );
96 const task = yield* cloudtasks.createProjectsLocationsQueuesTasks({
97 parent: created.name,
98 body: {
99 responseView: "FULL",
100 task: {
101 httpRequest: {
102 url: "https://example.com/alchemy-cloudtasks-test",
103 httpMethod: "POST",
104 body: payload,
105 },
106 },
107 },
108 });
109 expect(task.name).toContain("/tasks/");
110 expect(task.httpRequest?.url).toEqual("https://example.com/alchemy-cloudtasks-test");
111
112 yield* stack.destroy();
113
114 const gone = yield* waitUntilGone(created.name);
115 expect(gone).toEqual("gone");
116 }).pipe(logLevel),
117 {
118 tags: ["provider:gcp", "provider:gcp:cloudtasks", "live"],
119 timeout: 90_000,
120 },
121 );