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

main
ts 143 lines 5.09 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 cw from "@distilled.cloud/gcp/contentwarehouse_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 // Document AI Warehouse is disabled on the testing project: calls fail with
15 // ServiceDisabled "Document AI Warehouse API has not been used in project ...
16 // before or it is disabled" (the service also needs per-project
17 // provisioning). Set GCP_TEST_CONTENTWAREHOUSE=1 on a provisioned project.
18 const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_CONTENTWAREHOUSE;
19
20 const location = "us";
21
22 const waitUntilGone = (name: string) =>
23 cw.getProjectsLocationsDocuments({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 // Skipped: the Content Warehouse endpoint answers every request with an HTML
34 // 502 after ~20s (Document AI Warehouse is being retired), so the
35 // ServiceDisabled probe can't complete.
36 test.provider.skip(
37 "getProjectsLocationsDocuments without Document AI Warehouse enabled fails with ServiceDisabled",
38 (stack) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 const parent = `projects/${project}/locations/${location}`;
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 cw.getProjectsLocationsDocuments({
46 name: `${parent}/documents/alchemy-missing-document`,
47 }),
48 );
49 expect(error._tag).toEqual("ServiceDisabled");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 {
54 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
55 timeout: 90_000,
56 },
57 );
58
59 test.provider.skipIf(!runLifecycle)(
60 "create, update, and delete a document",
61 (stack) =>
62 Effect.gen(function* () {
63 const { project } = yield* GcpEnvironment.current;
64 const parent = `projects/${project}/locations/${location}`;
65 yield* stack.destroy();
66
67 const created = yield* stack.deploy(
68 Effect.gen(function* () {
69 const schema = yield* GCP.ContentWarehouse.DocumentSchema("Note", {
70 location,
71 displayName: "note",
72 propertyDefinitions: [
73 {
74 name: "title",
75 isSearchable: true,
76 textTypeOptions: {},
77 },
78 ],
79 });
80 const document = yield* GCP.ContentWarehouse.Document("Welcome", {
81 location,
82 documentSchemaName: schema.name,
83 displayName: "welcome",
84 title: "welcome",
85 plainText: "hello warehouse",
86 });
87 return { schema, document };
88 }),
89 );
90
91 expect(created.document.name).toContain("/documents/");
92 expect(created.document.documentId).toEqual(expect.any(String));
93 expect(created.document.displayName).toEqual("welcome");
94 expect(created.document.plainText).toEqual("hello warehouse");
95 expect(created.document.documentSchemaName).toEqual(created.schema.name);
96
97 const fetched = yield* cw.getProjectsLocationsDocuments({
98 name: created.document.name,
99 });
100 expect(fetched.name).toEqual(created.document.name);
101 expect(fetched.displayName).toEqual(created.document.displayName);
102 expect(fetched.plainText).toEqual("hello warehouse");
103
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 const schema = yield* GCP.ContentWarehouse.DocumentSchema("Note", {
107 documentSchemaId: created.schema.documentSchemaId,
108 location,
109 displayName: "note",
110 propertyDefinitions: [
111 {
112 name: "title",
113 isSearchable: true,
114 textTypeOptions: {},
115 },
116 ],
117 });
118 const document = yield* GCP.ContentWarehouse.Document("Welcome", {
119 documentId: created.document.documentId,
120 referenceId: created.document.referenceId,
121 location,
122 documentSchemaName: schema.name,
123 displayName: "welcome-v2",
124 title: "welcome v2",
125 plainText: "hello again",
126 });
127 return { schema, document };
128 }),
129 );
130
131 expect(updated.document.name).toEqual(created.document.name);
132 expect(updated.document.displayName).toEqual("welcome-v2");
133 expect(updated.document.plainText).toEqual("hello again");
134
135 yield* stack.destroy();
136 const gone = yield* waitUntilGone(created.document.name);
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel),
139 {
140 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
141 timeout: 90_000,
142 },
143 );