1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_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 = (name: string) =>
17 artifactregistry.getProjectsLocationsRepositories({ 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 repository",
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.ArtifactRegistry.Repository("Images", {
36 location: "us-central1",
37 format: "DOCKER",
38 description: "test docker repository",
39 labels: { env: "test" },
40 });
41 }),
42 );
43
44 expect(created.name).toContain("/repositories/");
45 expect(created.repositoryId).toEqual(expect.any(String));
46 expect(created.location).toEqual("us-central1");
47 expect(created.format).toEqual("DOCKER");
48 expect(created.mode).toEqual("STANDARD_REPOSITORY");
49 expect(created.description).toEqual("test docker repository");
50 expect(created.labels).toMatchObject({ env: "test" });
51 expect(created.immutableTags).toEqual(false);
52 expect(created.registryUri).toEqual(expect.any(String));
53
54 const fetched = yield* artifactregistry.getProjectsLocationsRepositories({
55 name: created.name,
56 });
57 expect(fetched.name).toEqual(created.name);
58 expect(fetched.format).toEqual("DOCKER");
59 expect(fetched.labels?.env).toEqual("test");
60 expect(fetched.description).toEqual("test docker repository");
61
62 const images =
63 yield* artifactregistry.listProjectsLocationsRepositoriesDockerImages({
64 parent: created.name,
65 pageSize: 10,
66 });
67 expect(images.dockerImages ?? []).toEqual([]);
68
69 const updated = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.ArtifactRegistry.Repository("Images", {
72 repositoryId: created.repositoryId,
73 location: "us-central1",
74 format: "DOCKER",
75 description: "prod docker repository",
76 labels: { env: "prod", role: "images" },
77 dockerConfig: { immutableTags: true },
78 cleanupPolicyDryRun: true,
79 });
80 }),
81 );
82
83 expect(updated.name).toEqual(created.name);
84 expect(updated.description).toEqual("prod docker repository");
85 expect(updated.labels).toMatchObject({ env: "prod", role: "images" });
86 expect(updated.immutableTags).toEqual(true);
87 expect(updated.cleanupPolicyDryRun).toEqual(true);
88
89 const refetched =
90 yield* artifactregistry.getProjectsLocationsRepositories({
91 name: created.name,
92 });
93 expect(refetched.description).toEqual("prod docker repository");
94 expect(refetched.labels?.env).toEqual("prod");
95 expect(refetched.labels?.role).toEqual("images");
96 expect(refetched.dockerConfig?.immutableTags).toEqual(true);
97 expect(refetched.cleanupPolicyDryRun).toEqual(true);
98
99 yield* stack.destroy();
100
101 const gone = yield* waitUntilGone(created.name);
102 expect(gone).toEqual("gone");
103 }).pipe(logLevel),
104 {
105 tags: ["provider:gcp", "provider:gcp:artifactregistry", "live"],
106 timeout: 90_000,
107 },
108);