sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ GCP/ Logging/ BucketsLink.test.ts

import * as GCP from "@alchemy.run/gcp";
import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as logging from "@distilled.cloud/gcp/logging_v2";
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 = (name: string) =>
  logging.getProjectsLocationsBucketsLinks({ name }).pipe(
    Effect.map((link) =>
      link.lifecycleState === "DELETE_REQUESTED" ? ("gone" as const) : ("found" as const),
    ),
    Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
    Effect.repeat({
      schedule: Schedule.spaced("2 seconds"),
      until: (status) => status === "gone",
      times: 10,
    }),
  );

test.provider.skipIf(!!process.env.FAST)(
  "create and delete a logging bucket link",
  (stack) =>
    Effect.gen(function* () {
      const { project } = yield* GcpEnvironment.current;

      yield* stack.destroy();

      const created = yield* stack.deploy(
        Effect.gen(function* () {
          const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
            analyticsEnabled: true,
            description: "analytics parent",
          });
          const link = yield* GCP.Logging.BucketsLink("Analytics", {
            bucket: bucket.name,
            description: "bigquery analytics",
          });
          return { bucket, link };
        }),
      );

      expect(created.link.linkId).toEqual(expect.any(String));
      expect(created.link.linkId).toMatch(/^[a-z][a-z0-9_]*$/);
      expect(created.link.bucket).toEqual(created.bucket.name);
      expect(created.link.project).toEqual(project);
      expect(created.link.location).toEqual("global");
      expect(created.link.name).toEqual(`${created.bucket.name}/links/${created.link.linkId}`);
      expect(created.link.description).toEqual("bigquery analytics");
      expect(created.link.lifecycleState).toEqual("ACTIVE");

      const fetched = yield* logging.getProjectsLocationsBucketsLinks({
        name: created.link.name,
      });
      expect(fetched.name).toEqual(created.link.name);
      expect(fetched.lifecycleState).toEqual("ACTIVE");
      expect(fetched.description).toContain("alchemy-id=");
      expect(fetched.description).toContain("bigquery analytics");

      yield* stack.destroy();

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