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

main
ts 184 lines 6.33 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 crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as cw from "@distilled.cloud/gcp/contentwarehouse_v1";
8 import * as Effect from "effect/Effect";
9 import { expectProbe } from "../bindingHost.ts";
10 import ContentWarehouseBindingsHost, {
11 Checks,
12 Note,
13 Sales,
14 Welcome,
15 } from "./fixtures/bindings-host.ts";
16
17 const testOptions = { providers: GCP.providers() };
18 const { test, beforeAll, afterAll } = Test.make(testOptions);
19 const sharedStack = Core.scratchStack(testOptions, "ContentWarehouseBindings");
20
21 // Document AI Warehouse is disabled on the testing project: calls fail with
22 // ServiceDisabled "Document AI Warehouse API has not been used in project ...
23 // before or it is disabled" (the service also needs per-project
24 // provisioning). Set GCP_TEST_CONTENTWAREHOUSE=1 on a provisioned project.
25 const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_CONTENTWAREHOUSE;
26
27 let baseUrl: string;
28 let hostAccount: string;
29 let schemaName: string;
30 let documentName: string;
31 let ruleSetName: string;
32 let synonymSetName: string;
33
34 /**
35 * Document AI Warehouse has no per-resource IAM for these types, so every
36 * binding grants on the project: `documentSchemaViewer` (GetDocumentSchema),
37 * `documentViewer` (GetDocument) and `admin` (GetRuleSet / GetSynonymSet —
38 * `ruleSets.get` / `synonymSets.get` exist in no narrower role).
39 */
40 const expectProjectGrants = Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42 const policy = yield* crm.getIamPolicyProjects({
43 resource: `projects/${project}`,
44 body: { options: { requestedPolicyVersion: 3 } },
45 });
46 const roles = (policy.bindings ?? [])
47 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
48 .map((binding) => binding.role)
49 .sort();
50 expect(roles).toEqual([
51 "roles/contentwarehouse.admin",
52 "roles/contentwarehouse.documentSchemaViewer",
53 "roles/contentwarehouse.documentViewer",
54 ]);
55 });
56
57 describe.skipIf(!runLifecycle)(
58 "ContentWarehouse Bindings",
59 {
60 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "provider:gcp:run", "live"],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* sharedStack.destroy();
66 const out = yield* sharedStack.deploy(
67 Effect.gen(function* () {
68 const host = yield* ContentWarehouseBindingsHost;
69 const schema = yield* Note;
70 const document = yield* Welcome;
71 const rules = yield* Checks;
72 const synonyms = yield* Sales;
73 return {
74 uri: host.uri,
75 serviceAccount: host.serviceAccount,
76 schema: schema.name,
77 document: document.name,
78 rules: rules.name,
79 synonyms: synonyms.name,
80 };
81 }),
82 );
83 baseUrl = out.uri!;
84 hostAccount = out.serviceAccount!;
85 schemaName = out.schema;
86 documentName = out.document;
87 ruleSetName = out.rules;
88 synonymSetName = out.synonyms;
89 }),
90 { timeout: 900_000 },
91 );
92
93 afterAll(sharedStack.destroy(), { timeout: 600_000 });
94
95 describe("GetDocumentSchema", () => {
96 test.provider(
97 "reads the schema as the host's service account",
98 (_stack) =>
99 Effect.gen(function* () {
100 const schema = yield* expectProbe<cw.GoogleCloudContentwarehouseV1DocumentSchema>(
101 baseUrl,
102 "getDocumentSchema",
103 );
104 const live = yield* cw.getProjectsLocationsDocumentSchemas({
105 name: schemaName,
106 });
107 expect(schema.name).toEqual(schemaName);
108 expect(schema.displayName).toEqual("binding-note");
109 expect(schema.updateTime).toEqual(live.updateTime);
110 yield* expectProjectGrants;
111 }),
112 {
113 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
114 timeout: 600_000,
115 },
116 );
117 });
118
119 describe("GetDocument", () => {
120 test.provider(
121 "reads the document as the host's service account",
122 (_stack) =>
123 Effect.gen(function* () {
124 const document = yield* expectProbe<cw.GoogleCloudContentwarehouseV1Document>(
125 baseUrl,
126 "getDocument",
127 );
128 expect(document.name).toEqual(documentName);
129 expect(document.displayName).toEqual("binding-welcome");
130 expect(document.plainText).toEqual("hello binding");
131 yield* expectProjectGrants;
132 }),
133 {
134 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
135 timeout: 600_000,
136 },
137 );
138 });
139
140 describe("GetRuleSet", () => {
141 test.provider(
142 "reads the rule set as the host's service account",
143 (_stack) =>
144 Effect.gen(function* () {
145 const rules = yield* expectProbe<cw.GoogleCloudContentwarehouseV1RuleSet>(
146 baseUrl,
147 "getRuleSet",
148 );
149 const live = yield* cw.getProjectsLocationsRuleSets({
150 name: ruleSetName,
151 });
152 expect(rules.name).toEqual(ruleSetName);
153 expect(rules.description).toEqual("binding rules");
154 expect(rules.rules?.length).toEqual(live.rules?.length);
155 yield* expectProjectGrants;
156 }),
157 {
158 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
159 timeout: 600_000,
160 },
161 );
162 });
163
164 describe("GetSynonymSet", () => {
165 test.provider(
166 "reads the synonym set as the host's service account",
167 (_stack) =>
168 Effect.gen(function* () {
169 const synonyms = yield* expectProbe<cw.GoogleCloudContentwarehouseV1SynonymSet>(
170 baseUrl,
171 "getSynonymSet",
172 );
173 expect(synonyms.name).toEqual(synonymSetName);
174 expect(synonyms.synonyms?.[0]?.words).toEqual(["sale", "invoice", "bill"]);
175 yield* expectProjectGrants;
176 }),
177 {
178 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
179 timeout: 600_000,
180 },
181 );
182 });
183 },
184 );