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

main
ts 138 lines 5.5 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as clouddeploy from "@distilled.cloud/gcp/clouddeploy_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const waitUntilGone = (name: string) =>
15 clouddeploy.getProjectsLocationsDeliveryPipelinesAutomations({ name }).pipe(
16 Effect.as("found" as const),
17 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
18 Effect.repeat({
19 schedule: Schedule.spaced("2 seconds"),
20 until: (status) => status === "gone",
21 times: 10,
22 }),
23 );
24
25 test.provider(
26 "getProjectsLocationsDeliveryPipelinesAutomations on a missing automation fails with a typed tag",
27 (stack) =>
28 Effect.gen(function* () {
29 const { project } = yield* GcpEnvironment.current;
30
31 yield* stack.destroy();
32
33 const error = yield* Effect.flip(
34 clouddeploy.getProjectsLocationsDeliveryPipelinesAutomations({
35 name: `projects/${project}/locations/us-central1/deliveryPipelines/alchemy-missing-pipeline/automations/alchemy-missing-automation`,
36 }),
37 );
38 expect(error._tag).toEqual("NotFound");
39
40 yield* stack.destroy();
41 }).pipe(logLevel),
42 {
43 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
44 timeout: 90_000,
45 },
46 );
47
48 test.provider(
49 "create, update, and delete a delivery pipeline automation",
50 (stack) =>
51 Effect.gen(function* () {
52 const { project } = yield* GcpEnvironment.current;
53 const serviceAccount =
54 process.env.GCP_TEST_CLOUDDEPLOY_SA ?? `alchemy-testing@${project}.iam.gserviceaccount.com`;
55 const runLocation = `projects/${project}/locations/us-central1`;
56
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const target = yield* GCP.CloudDeploy.Target("Staging", {
62 run: { location: runLocation },
63 labels: { env: "test" },
64 });
65 const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", {
66 serialPipeline: { stages: [{ targetId: target.targetId }] },
67 description: "alchemy-test-automation-pipeline",
68 labels: { env: "test" },
69 });
70 const automation = yield* GCP.CloudDeploy.DeliveryPipelinesAutomation("Promote", {
71 deliveryPipeline: pipeline.name,
72 serviceAccount,
73 selector: { targets: [{ id: "*" }] },
74 rules: [{ promoteReleaseRule: { id: "promote-release" } }],
75 description: "alchemy-test-automation",
76 labels: { env: "test" },
77 });
78 return { target, pipeline, automation };
79 }),
80 );
81
82 expect(created.automation.name).toContain("/automations/");
83 expect(created.automation.automationId).toEqual(expect.any(String));
84 expect(created.automation.deliveryPipeline).toEqual(created.pipeline.name);
85 expect(created.automation.location).toEqual("us-central1");
86 expect(created.automation.description).toEqual("alchemy-test-automation");
87 expect(created.automation.serviceAccount).toEqual(serviceAccount);
88 expect(created.automation.selector?.targets?.[0]?.id).toEqual("*");
89 expect(created.automation.rules[0]?.promoteReleaseRule?.id).toEqual("promote-release");
90 expect(created.automation.labels).toMatchObject({ env: "test" });
91
92 const fetched = yield* clouddeploy.getProjectsLocationsDeliveryPipelinesAutomations({
93 name: created.automation.name,
94 });
95 expect(fetched.name).toEqual(created.automation.name);
96 expect(fetched.description).toEqual("alchemy-test-automation");
97 expect(fetched.labels?.env).toEqual("test");
98
99 const updated = yield* stack.deploy(
100 Effect.gen(function* () {
101 const target = yield* GCP.CloudDeploy.Target("Staging", {
102 targetId: created.target.targetId,
103 run: { location: runLocation },
104 labels: { env: "test" },
105 });
106 const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", {
107 deliveryPipelineId: created.pipeline.deliveryPipelineId,
108 serialPipeline: { stages: [{ targetId: target.targetId }] },
109 description: "alchemy-test-automation-pipeline",
110 labels: { env: "test" },
111 });
112 const automation = yield* GCP.CloudDeploy.DeliveryPipelinesAutomation("Promote", {
113 automationId: created.automation.automationId,
114 deliveryPipeline: pipeline.name,
115 serviceAccount,
116 selector: { targets: [{ id: "*" }] },
117 rules: [{ promoteReleaseRule: { id: "promote-release" } }],
118 description: "alchemy-prod-automation",
119 labels: { env: "prod", role: "promote" },
120 });
121 return automation;
122 }),
123 );
124
125 expect(updated.name).toEqual(created.automation.name);
126 expect(updated.description).toEqual("alchemy-prod-automation");
127 expect(updated.labels).toMatchObject({ env: "prod", role: "promote" });
128
129 yield* stack.destroy();
130
131 const gone = yield* waitUntilGone(created.automation.name);
132 expect(gone).toEqual("gone");
133 }).pipe(logLevel),
134 {
135 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
136 timeout: 120_000,
137 },
138 );