The best way to control Claude Code, Codex and more from any computer or phone

main
ts 138 lines 4.48 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as ssm from "@distilled.cloud/gcp/securesourcemanager_v1";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import { currentProject, logLevel, missingRepoOf, runLifecycle } from "./common.ts";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const waitUntilGone = (name: string) =>
12 ssm.getProjectsLocationsRepositoriesBranchRules({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("2 seconds"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22 test.provider(
23 "getProjectsLocationsRepositoriesBranchRules on a missing rule fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 const project = yield* currentProject;
27 const missingRepo = missingRepoOf(project);
28 yield* stack.destroy();
29
30 const error = yield* Effect.flip(
31 ssm.getProjectsLocationsRepositoriesBranchRules({
32 name: `${missingRepo}/branchRules/alchemy-missing-rule`,
33 }),
34 );
35 expect(error._tag).toEqual("NotFound");
36
37 yield* stack.destroy();
38 }).pipe(logLevel),
39 {
40 tags: ["provider:gcp", "provider:gcp:securesourcemanager", "live"],
41 timeout: 90_000,
42 },
43 );
44
45 test.provider(
46 "create against a missing repository is rejected with a typed tag",
47 (stack) =>
48 Effect.gen(function* () {
49 const project = yield* currentProject;
50 const missingRepo = missingRepoOf(project);
51 yield* stack.destroy();
52
53 const error = yield* Effect.flip(
54 stack.deploy(
55 Effect.gen(function* () {
56 return yield* GCP.SecureSourceManager.RepositoriesBranchRule("Main", {
57 repository: missingRepo,
58 includePattern: "main",
59 requirePullRequest: true,
60 });
61 }),
62 ),
63 );
64 expect(error._tag).toEqual("NotFound");
65
66 yield* stack.destroy();
67 }).pipe(logLevel),
68 {
69 tags: ["provider:gcp", "provider:gcp:securesourcemanager", "live"],
70 timeout: 90_000,
71 },
72 );
73
74 test.provider.skipIf(!runLifecycle)(
75 "create, update, and delete a branch rule",
76 (stack) =>
77 Effect.gen(function* () {
78 const project = yield* currentProject;
79 const missingRepo = missingRepoOf(project);
80 yield* stack.destroy();
81
82 const created = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.SecureSourceManager.RepositoriesBranchRule("Main", {
85 repository: process.env.GCP_TEST_SECURE_SOURCE_MANAGER_REPO ?? missingRepo,
86 includePattern: "main",
87 requirePullRequest: true,
88 minimumApprovalsCount: 1,
89 annotations: { env: "test" },
90 });
91 }),
92 );
93
94 expect(created.name).toContain("/branchRules/");
95 expect(created.includePattern).toEqual("main");
96 expect(created.requirePullRequest).toEqual(true);
97 expect(created.minimumApprovalsCount).toEqual(1);
98 expect(created.annotations).toMatchObject({ env: "test" });
99
100 const fetched = yield* ssm.getProjectsLocationsRepositoriesBranchRules({
101 name: created.name,
102 });
103 expect(fetched.name).toEqual(created.name);
104 expect(fetched.annotations?.env).toEqual("test");
105 expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String));
106
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 return yield* GCP.SecureSourceManager.RepositoriesBranchRule("Main", {
110 repository: created.repository,
111 branchRuleId: created.branchRuleId,
112 includePattern: "main",
113 requirePullRequest: true,
114 minimumApprovalsCount: 2,
115 requireCommentsResolved: true,
116 annotations: { env: "prod", team: "platform" },
117 });
118 }),
119 );
120
121 expect(updated.branchRuleId).toEqual(created.branchRuleId);
122 expect(updated.minimumApprovalsCount).toEqual(2);
123 expect(updated.requireCommentsResolved).toEqual(true);
124 expect(updated.annotations).toMatchObject({
125 env: "prod",
126 team: "platform",
127 });
128
129 yield* stack.destroy();
130
131 const gone = yield* waitUntilGone(created.name);
132 expect(gone).toEqual("gone");
133 }).pipe(logLevel),
134 {
135 tags: ["provider:gcp", "provider:gcp:securesourcemanager", "live"],
136 timeout: 120_000,
137 },
138 );