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

main
ts 153 lines 5.72 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 developerconnect from "@distilled.cloud/gcp/developerconnect_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 // Git links need a connection whose GitHub App install is COMPLETE; a fresh
13 // connection stays PENDING_USER_OAUTH and link create fails with BadRequest
14 // ("connection must have installation_state COMPLETE"). Set
15 // GCP_TEST_DEVELOPERCONNECT_GITHUB_TOKEN_SECRET (OAuth token secret version)
16 // and GCP_TEST_DEVELOPERCONNECT_GITHUB_INSTALLATION_ID to run.
17 const githubTokenSecret = process.env.GCP_TEST_DEVELOPERCONNECT_GITHUB_TOKEN_SECRET;
18 const githubInstallationId = process.env.GCP_TEST_DEVELOPERCONNECT_GITHUB_INSTALLATION_ID;
19 const runLinkLifecycle = !!githubTokenSecret && !!githubInstallationId;
20 const githubConfig = {
21 githubApp: "DEVELOPER_CONNECT",
22 authorizerCredential: { oauthTokenSecretVersion: githubTokenSecret },
23 appInstallationId: githubInstallationId,
24 };
25
26 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
27
28 const cloneUri =
29 process.env.GCP_DEVELOPERCONNECT_CLONE_URI ?? "https://github.com/octocat/Hello-World.git";
30
31 const waitUntilGone = (name: string) =>
32 developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks({ name }).pipe(
33 Effect.as("found" as const),
34 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
35 Effect.repeat({
36 schedule: Schedule.spaced("1 second"),
37 until: (status) => status === "gone",
38 times: 10,
39 }),
40 );
41
42 const waitUntilConnectionGone = (name: string) =>
43 developerconnect.getProjectsLocationsConnections({ name }).pipe(
44 Effect.as("found" as const),
45 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
46 Effect.repeat({
47 schedule: Schedule.spaced("1 second"),
48 until: (status) => status === "gone",
49 times: 10,
50 }),
51 );
52
53 test.provider(
54 "getProjectsLocationsConnectionsGitRepositoryLinks on a missing link fails with a typed tag",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 yield* stack.destroy();
59
60 const error = yield* Effect.flip(
61 developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks({
62 name: `projects/${project}/locations/us-central1/connections/alchemy-missing-connection/gitRepositoryLinks/alchemy-missing-link`,
63 }),
64 );
65 expect(error._tag).toEqual("NotFound");
66
67 yield* stack.destroy();
68 }).pipe(logLevel),
69 {
70 tags: ["provider:gcp", "provider:gcp:developerconnect", "live"],
71 timeout: 90_000,
72 },
73 );
74
75 test.provider.skipIf(!runLinkLifecycle)(
76 "create, update, replace, and delete a git repository link",
77 (stack) =>
78 Effect.gen(function* () {
79 yield* stack.destroy();
80
81 const created = yield* stack.deploy(
82 Effect.gen(function* () {
83 const github = yield* GCP.DeveloperConnect.Connection("Github", {
84 location: "us-central1",
85 githubConfig,
86 labels: { env: "test" },
87 });
88 const source = yield* GCP.DeveloperConnect.ConnectionsGitRepositoryLink("Source", {
89 connection: github.name,
90 cloneUri,
91 labels: { env: "test" },
92 });
93 return { github, source };
94 }),
95 );
96
97 expect(created.source.gitRepositoryLinkId).toEqual(expect.any(String));
98 expect(created.source.name).toContain("/gitRepositoryLinks/");
99 expect(created.source.connection).toEqual(created.github.name);
100 expect(created.source.cloneUri).toEqual(cloneUri);
101 expect(created.source.labels).toMatchObject({ env: "test" });
102
103 const fetched = yield* developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks({
104 name: created.source.name,
105 });
106 expect(fetched.name).toEqual(created.source.name);
107 expect(fetched.cloneUri).toEqual(cloneUri);
108 expect(fetched.labels?.env).toEqual("test");
109 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
110
111 const updated = yield* stack.deploy(
112 Effect.gen(function* () {
113 const github = yield* GCP.DeveloperConnect.Connection("Github", {
114 connectionId: created.github.connectionId,
115 location: "us-central1",
116 githubConfig,
117 labels: { env: "test" },
118 });
119 const source = yield* GCP.DeveloperConnect.ConnectionsGitRepositoryLink("Source", {
120 connection: github.name,
121 gitRepositoryLinkId: created.source.gitRepositoryLinkId,
122 cloneUri,
123 labels: { env: "prod", role: "source" },
124 });
125 return { github, source };
126 }),
127 );
128
129 expect(updated.source.connection).toEqual(created.github.name);
130 expect(updated.source.labels).toMatchObject({
131 env: "prod",
132 role: "source",
133 });
134
135 const fetchedUpdate =
136 yield* developerconnect.getProjectsLocationsConnectionsGitRepositoryLinks({
137 name: updated.source.name,
138 });
139 expect(fetchedUpdate.labels?.env).toEqual("prod");
140 expect(fetchedUpdate.labels?.role).toEqual("source");
141
142 yield* stack.destroy();
143
144 const gone = yield* waitUntilGone(updated.source.name);
145 expect(gone).toEqual("gone");
146 const connectionGone = yield* waitUntilConnectionGone(created.github.name);
147 expect(connectionGone).toEqual("gone");
148 }).pipe(logLevel),
149 {
150 tags: ["provider:gcp", "provider:gcp:developerconnect", "live"],
151 timeout: 90_000,
152 },
153 );