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

main
ts 142 lines 4.75 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.getProjectsLocationsRuleSets({ 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 "getProjectsLocationsRuleSets 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.getProjectsLocationsRuleSets({
46 name: `${parent}/ruleSets/alchemy-missing-ruleset`,
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 rule set",
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 return yield* GCP.ContentWarehouse.RuleSet("Checks", {
70 location,
71 description: "on create",
72 source: "alchemy",
73 rules: [
74 {
75 description: "require title",
76 triggerType: "ON_CREATE",
77 condition: "true",
78 actions: [
79 {
80 dataValidation: {
81 conditions: { display_name: "true" },
82 },
83 },
84 ],
85 },
86 ],
87 });
88 }),
89 );
90
91 expect(created.ruleSetId).toEqual(expect.any(String));
92 expect(created.name).toContain("/ruleSets/");
93 expect(created.description).toEqual("on create");
94 expect(created.source).toEqual("alchemy");
95 expect((created.rules ?? []).length).toBeGreaterThan(0);
96
97 const fetched = yield* cw.getProjectsLocationsRuleSets({
98 name: created.name,
99 });
100 expect(fetched.name).toEqual(created.name);
101 expect(fetched.description).toContain("alchemy-id=");
102 expect(fetched.description).toContain("on create");
103
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* GCP.ContentWarehouse.RuleSet("Checks", {
107 ruleSetId: created.ruleSetId,
108 location,
109 description: "on update",
110 source: "alchemy-v2",
111 rules: [
112 {
113 description: "require title",
114 triggerType: "ON_UPDATE",
115 condition: "true",
116 actions: [
117 {
118 dataValidation: {
119 conditions: { display_name: "true" },
120 },
121 },
122 ],
123 },
124 ],
125 });
126 }),
127 );
128
129 expect(updated.name).toEqual(created.name);
130 expect(updated.description).toEqual("on update");
131 expect(updated.source).toEqual("alchemy-v2");
132 expect(updated.rules?.[0]?.triggerType).toEqual("ON_UPDATE");
133
134 yield* stack.destroy();
135 const gone = yield* waitUntilGone(created.name);
136 expect(gone).toEqual("gone");
137 }).pipe(logLevel),
138 {
139 tags: ["provider:gcp", "provider:gcp:contentwarehouse", "live"],
140 timeout: 90_000,
141 },
142 );