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