sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ GCP/ ContainerAnalysis/ LocationsOccurrence.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 containeranalysis from "@distilled.cloud/gcp/containeranalysis_v1";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import {
  location,
  logLevel,
  currentProject,
  TEST_ATTESTATION,
  TEST_RESOURCE_URI,
} from "./common.ts";

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

const waitUntilGone = (name: string) =>
  containeranalysis.getProjectsLocationsOccurrences({ 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(
  "getProjectsLocationsOccurrences on a missing occurrence fails with a typed tag",
  (stack) =>
    Effect.gen(function* () {
      const project = yield* currentProject;
      yield* stack.destroy();

      const error = yield* Effect.flip(
        containeranalysis.getProjectsLocationsOccurrences({
          name: `projects/${project}/locations/${location}/occurrences/alchemy-missing-occurrence`,
        }),
      );
      expect(error._tag).toEqual("NotFound");

      yield* stack.destroy();
    }).pipe(logLevel),
  {
    tags: ["provider:gcp", "provider:gcp:containeranalysis", "live"],
    timeout: 90_000,
  },
);

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

      const created = yield* stack.deploy(
        Effect.gen(function* () {
          const note = yield* GCP.ContainerAnalysis.LocationsNote("Authority", {
            location,
            shortDescription: "regional occurrence attestor",
            attestation: { hint: { humanReadableName: "Alchemy Regional" } },
          });
          const occurrence = yield* GCP.ContainerAnalysis.LocationsOccurrence("Signed", {
            location,
            noteName: note.name,
            resourceUri: TEST_RESOURCE_URI,
            remediation: "initial",
            attestation: TEST_ATTESTATION,
          });
          return { note, occurrence };
        }),
      );

      expect(created.occurrence.name).toContain(`/locations/${location}/occurrences/`);
      expect(created.occurrence.location).toEqual(location);
      expect(created.occurrence.noteName).toEqual(created.note.name);
      expect(created.occurrence.resourceUri).toEqual(TEST_RESOURCE_URI);
      expect(created.occurrence.kind).toEqual("ATTESTATION");

      const fetched = yield* containeranalysis.getProjectsLocationsOccurrences({
        name: created.occurrence.name,
      });
      expect(fetched.name).toEqual(created.occurrence.name);
      expect(fetched.remediation).toContain("[alchemy ");
      expect(fetched.kind).toEqual("ATTESTATION");

      const updated = yield* stack.deploy(
        Effect.gen(function* () {
          const note = yield* GCP.ContainerAnalysis.LocationsNote("Authority", {
            noteId: created.note.noteId,
            location,
            shortDescription: "regional occurrence attestor",
            attestation: { hint: { humanReadableName: "Alchemy Regional" } },
          });
          const occurrence = yield* GCP.ContainerAnalysis.LocationsOccurrence("Signed", {
            location,
            noteName: note.name,
            resourceUri: TEST_RESOURCE_URI,
            remediation: "rebuild from a patched base",
            attestation: TEST_ATTESTATION,
          });
          return { note, occurrence };
        }),
      );

      expect(updated.occurrence.name).toEqual(created.occurrence.name);
      expect(updated.occurrence.remediation).toEqual("rebuild from a patched base");

      yield* stack.destroy();
      const gone = yield* waitUntilGone(created.occurrence.name);
      expect(gone).toEqual("gone");
    }).pipe(logLevel),
  {
    tags: ["provider:gcp", "provider:gcp:containeranalysis", "live"],
    timeout: 90_000,
  },
);