sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ GCP/ Compute/ BackendService.test.ts

import * as GCP from "@alchemy.run/gcp";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as compute from "@distilled.cloud/gcp/compute_v1";
import * as Effect from "effect/Effect";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";

const { test } = Test.make({ providers: GCP.providers() });

const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");

const waitUntilGone = (project: string, name: string) =>
  compute.getBackendServices({ project, backendService: name }).pipe(
    Effect.as("found" as const),
    Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
    Effect.repeat({
      schedule: Schedule.spaced("1 second"),
      until: (status) => status === "gone",
      times: 10,
    }),
  );

test.provider(
  "create, update, and delete a backend service",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      const created = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* GCP.Compute.BackendService("Web", {
            protocol: "HTTP",
            loadBalancingScheme: "EXTERNAL",
            description: "alchemy test",
            labels: { env: "test" },
          });
        }),
      );

      expect(created.name).toEqual(expect.any(String));
      expect(created.protocol).toEqual("HTTP");
      expect(created.loadBalancingScheme).toEqual("EXTERNAL");
      expect(created.timeoutSec).toEqual(30);
      expect(created.enableCDN).toEqual(false);
      expect(created.description).toEqual("alchemy test");
      expect(created.labels).toMatchObject({ env: "test" });

      const fetched = yield* compute.getBackendServices({
        project: created.project,
        backendService: created.name,
      });
      expect(fetched.name).toEqual(created.name);
      expect(fetched.protocol).toEqual("HTTP");
      expect(fetched.description).toContain("alchemy-id=");
      expect(fetched.description).toContain("env=test");
      expect(fetched.description).toContain("alchemy test");
      expect(fetched.timeoutSec).toEqual(30);

      const updated = yield* stack.deploy(
        Effect.gen(function* () {
          return yield* GCP.Compute.BackendService("Web", {
            name: created.name,
            protocol: "HTTP",
            loadBalancingScheme: "EXTERNAL",
            timeoutSec: 60,
            enableCDN: true,
            description: "updated",
            labels: { env: "prod", role: "lb" },
          });
        }),
      );

      expect(updated.name).toEqual(created.name);
      expect(updated.timeoutSec).toEqual(60);
      expect(updated.enableCDN).toEqual(true);
      expect(updated.description).toEqual("updated");
      expect(updated.labels).toMatchObject({ env: "prod", role: "lb" });

      const fetchedUpdate = yield* compute.getBackendServices({
        project: created.project,
        backendService: created.name,
      });
      expect(fetchedUpdate.timeoutSec).toEqual(60);
      expect(fetchedUpdate.enableCDN).toEqual(true);
      expect(fetchedUpdate.description).toContain("env=prod");
      expect(fetchedUpdate.description).toContain("role=lb");
      expect(fetchedUpdate.description).toContain("updated");

      yield* stack.destroy();

      const gone = yield* waitUntilGone(created.project, created.name);
      expect(gone).toEqual("gone");
    }).pipe(logLevel),
  { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
);