1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as compute from "@distilled.cloud/gcp/compute_v1";
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 = (project: string, name: string) =>
17 compute.getBackendBuckets({ project, backendBucket: 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 backend bucket",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 const assets = yield* GCP.Storage.Bucket("Assets", {
36 location: "US-CENTRAL1",
37 forceDestroy: true,
38 });
39 return yield* GCP.Compute.BackendBucket("Cdn", {
40 bucketName: assets.bucketName,
41 description: "static assets",
42 });
43 }),
44 );
45
46 expect(created.name).toEqual(expect.any(String));
47 expect(created.bucketName).toEqual(expect.any(String));
48 expect(created.description).toEqual("static assets");
49 expect(created.enableCdn).toEqual(false);
50
51 const fetched = yield* compute.getBackendBuckets({
52 project: created.project,
53 backendBucket: created.name,
54 });
55 expect(fetched.name).toEqual(created.name);
56 expect(fetched.bucketName).toEqual(created.bucketName);
57 expect(fetched.description).toContain("alchemy-id=");
58 expect(fetched.description).toContain("static assets");
59
60 const updated = yield* stack.deploy(
61 Effect.gen(function* () {
62 const assets = yield* GCP.Storage.Bucket("Assets", {
63 bucketName: created.bucketName,
64 location: "US-CENTRAL1",
65 forceDestroy: true,
66 });
67 return yield* GCP.Compute.BackendBucket("Cdn", {
68 name: created.name,
69 bucketName: assets.bucketName,
70 description: "cdn origin",
71 enableCdn: true,
72 compressionMode: "AUTOMATIC",
73 customResponseHeaders: ["X-Frame-Options: DENY"],
74 });
75 }),
76 );
77
78 expect(updated.name).toEqual(created.name);
79 expect(updated.bucketName).toEqual(created.bucketName);
80 expect(updated.description).toEqual("cdn origin");
81 expect(updated.enableCdn).toEqual(true);
82 expect(updated.compressionMode).toEqual("AUTOMATIC");
83 expect(updated.customResponseHeaders).toContain("X-Frame-Options: DENY");
84
85 const refetched = yield* compute.getBackendBuckets({
86 project: updated.project,
87 backendBucket: updated.name,
88 });
89 expect(refetched.enableCdn).toEqual(true);
90 expect(refetched.description).toContain("cdn origin");
91 expect(refetched.customResponseHeaders).toContain(
92 "X-Frame-Options: DENY",
93 );
94
95 yield* stack.destroy();
96
97 const gone = yield* waitUntilGone(created.project, created.name);
98 expect(gone).toEqual("gone");
99 }).pipe(logLevel),
100 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
101);