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

main
ts 193 lines 6.81 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as firebasedataconnect from "@distilled.cloud/gcp/firebasedataconnect_v1";
8 import * as Effect from "effect/Effect";
9 import { callProbe, type ProbeOutcome } from "../bindingHost.ts";
10 import DataConnectBindingsHost, { App, Queries } from "./fixtures/bindings-host.ts";
11
12 const testOptions = { providers: GCP.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "DataConnectBindings");
15
16 let baseUrl: string;
17 let serviceName: string;
18 let connectorName: string;
19 let hostAccount: string;
20
21 /**
22 * Roles the host's service account holds on the project. Data Connect has
23 * no resource-level IAM, so every binding grants on the project.
24 */
25 const hostProjectRoles = Effect.gen(function* () {
26 const { project } = yield* GcpEnvironment.current;
27 const policy = yield* resourcemanager.getIamPolicyProjects({
28 resource: `projects/${project}`,
29 body: { options: { requestedPolicyVersion: 3 } },
30 });
31 return (policy.bindings ?? [])
32 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
33 .map((binding) => ({
34 role: binding.role,
35 condition: binding.condition?.expression,
36 }))
37 .sort((a, b) => (a.role ?? "").localeCompare(b.role ?? ""));
38 });
39
40 const expectedRoles = [
41 { role: "roles/firebasedataconnect.dataAdmin", condition: undefined },
42 { role: "roles/firebasedataconnect.dataViewer", condition: undefined },
43 ];
44
45 /** Tag of a direct (deployer-credential) call, for comparison. */
46 const tagOf = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) =>
47 effect.pipe(
48 Effect.map(() => "ok"),
49 Effect.catch((error) => Effect.succeed(error._tag)),
50 );
51
52 /**
53 * The test service's datasource is unlinked (no Cloud SQL), so data-plane
54 * calls that pass IAM reach the service and are rejected with
55 * `BadRequest: MISSING_DATASOURCE`. A missing grant surfaces as
56 * `Forbidden`/`PermissionDenied` (which `callProbe` retries until it
57 * propagates, then returns).
58 */
59 const expectAuthorized = (outcome: ProbeOutcome<unknown>) => {
60 if (outcome.ok) return "ok";
61 expect(outcome.error._tag).toEqual("BadRequest");
62 expect(outcome.error.message ?? "").toContain("MISSING_DATASOURCE");
63 return outcome.error._tag;
64 };
65
66 describe(
67 "FirebaseDataConnect Bindings",
68 {
69 tags: ["provider:gcp", "provider:gcp:firebasedataconnect", "provider:gcp:run", "live"],
70 },
71 () => {
72 beforeAll(
73 Effect.gen(function* () {
74 yield* sharedStack.destroy();
75 const out = yield* sharedStack.deploy(
76 Effect.gen(function* () {
77 const host = yield* DataConnectBindingsHost;
78 const service = yield* App;
79 const connector = yield* Queries;
80 return {
81 uri: host.uri,
82 serviceAccount: host.serviceAccount,
83 service: service.name,
84 connector: connector.name,
85 };
86 }),
87 );
88 baseUrl = out.uri!;
89 hostAccount = out.serviceAccount!;
90 serviceName = out.service;
91 connectorName = out.connector;
92 }),
93 { timeout: 900_000 },
94 );
95
96 afterAll(sharedStack.destroy(), { timeout: 600_000 });
97
98 describe("ExecuteGraphql", () => {
99 test.provider(
100 "executes GraphQL on the service as the host's service account",
101 (_stack) =>
102 Effect.gen(function* () {
103 const outcome = yield* callProbe(baseUrl, "executeGraphql");
104 const tag = expectAuthorized(outcome);
105 const direct = yield* tagOf(
106 firebasedataconnect.executeGraphqlProjectsLocationsServices({
107 name: serviceName,
108 body: { query: "{ __typename }" },
109 }),
110 );
111 expect(tag).toEqual(direct);
112 expect(yield* hostProjectRoles).toEqual(expectedRoles);
113 }),
114 {
115 tags: ["provider:gcp", "provider:gcp:firebasedataconnect", "live"],
116 timeout: 600_000,
117 },
118 );
119 });
120
121 describe("ExecuteGraphqlRead", () => {
122 test.provider(
123 "executes read-only GraphQL on the service as the host's service account",
124 (_stack) =>
125 Effect.gen(function* () {
126 const outcome = yield* callProbe(baseUrl, "executeGraphqlRead");
127 const tag = expectAuthorized(outcome);
128 const direct = yield* tagOf(
129 firebasedataconnect.executeGraphqlReadProjectsLocationsServices({
130 name: serviceName,
131 body: { query: "{ __typename }" },
132 }),
133 );
134 expect(tag).toEqual(direct);
135 expect(yield* hostProjectRoles).toEqual(expectedRoles);
136 }),
137 {
138 tags: ["provider:gcp", "provider:gcp:firebasedataconnect", "live"],
139 timeout: 600_000,
140 },
141 );
142 });
143
144 describe("ExecuteQuery", () => {
145 test.provider(
146 "executes the connector's query as the host's service account",
147 (_stack) =>
148 Effect.gen(function* () {
149 const outcome = yield* callProbe(baseUrl, "executeQuery");
150 const tag = expectAuthorized(outcome);
151 const direct = yield* tagOf(
152 firebasedataconnect.executeQueryProjectsLocationsServicesConnectors({
153 name: connectorName,
154 body: { operationName: "ListAlchemyNotes" },
155 }),
156 );
157 expect(tag).toEqual(direct);
158 expect(yield* hostProjectRoles).toEqual(expectedRoles);
159 }),
160 {
161 tags: ["provider:gcp", "provider:gcp:firebasedataconnect", "live"],
162 timeout: 600_000,
163 },
164 );
165 });
166
167 describe("ExecuteMutation", () => {
168 test.provider(
169 "executes the connector's mutation as the host's service account",
170 (_stack) =>
171 Effect.gen(function* () {
172 const outcome = yield* callProbe(baseUrl, "executeMutation");
173 const tag = expectAuthorized(outcome);
174 const direct = yield* tagOf(
175 firebasedataconnect.executeMutationProjectsLocationsServicesConnectors({
176 name: connectorName,
177 body: {
178 operationName: "CreateAlchemyNote",
179 variables: { title: "hello" },
180 },
181 }),
182 );
183 expect(tag).toEqual(direct);
184 expect(yield* hostProjectRoles).toEqual(expectedRoles);
185 }),
186 {
187 tags: ["provider:gcp", "provider:gcp:firebasedataconnect", "live"],
188 timeout: 600_000,
189 },
190 );
191 });
192 },
193 );