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

main
ts 141 lines 5 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { Document, DocumentProvider } from "@alchemy.run/gcp/Firestore/Document";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as firestore from "@distilled.cloud/gcp/firestore_v1";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({
13 providers: DocumentProvider().pipe(Layer.provideMerge(GCP.providers())),
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 // Project setup (one-time): the testing project's `(default)` database is
19 // Datastore mode (the Datastore suites need it), created with
20 // gcloud firestore databases create --database='(default)' \
21 // --location=us-central1 --type=datastore-mode --project=PROJECT_ID
22 // The probes below pin that the Firestore API rejects it with a typed tag.
23 const defaultDocOf = (project: string) =>
24 `projects/${project}/databases/(default)/documents/_alchemy/alchemy-missing`;
25
26 const waitUntilGone = (name: string) =>
27 firestore.getProjectsDatabasesDocuments({ name }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37 test.provider(
38 "getProjectsDatabasesDocuments on the Datastore-mode (default) database fails with DatastoreModeDatabase",
39 (stack) =>
40 Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42
43 yield* stack.destroy();
44
45 const error = yield* Effect.flip(
46 firestore.getProjectsDatabasesDocuments({
47 name: defaultDocOf(project),
48 }),
49 );
50 expect(error._tag).toEqual("DatastoreModeDatabase");
51
52 yield* stack.destroy();
53 }).pipe(logLevel),
54 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
55 );
56
57 test.provider(
58 "createDocumentProjectsDatabasesDocuments on the Datastore-mode (default) database fails with DatastoreModeDatabase",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62
63 yield* stack.destroy();
64
65 const error = yield* Effect.flip(
66 firestore.createDocumentProjectsDatabasesDocuments({
67 parent: `projects/${project}/databases/(default)/documents`,
68 collectionId: "_alchemy",
69 documentId: "alchemy-probe",
70 body: { fields: { env: { stringValue: "probe" } } },
71 }),
72 );
73 expect(error._tag).toEqual("DatastoreModeDatabase");
74
75 yield* stack.destroy();
76 }).pipe(logLevel),
77 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
78 );
79
80 // The testing project's (default) database is Datastore mode, which rejects
81 // the Firestore API, so the lifecycle provisions its own Native database.
82 test.provider(
83 "create, update, and delete a firestore document",
84 (stack) =>
85 Effect.gen(function* () {
86 yield* stack.destroy();
87
88 const created = yield* stack.deploy(
89 Effect.gen(function* () {
90 const database = yield* GCP.Firestore.Database("DocDb", {
91 location: "us-central1",
92 type: "FIRESTORE_NATIVE",
93 });
94 return yield* Document("Flag", {
95 database: database.databaseId,
96 collectionId: "_alchemy",
97 fields: { env: { stringValue: "test" } },
98 });
99 }),
100 );
101
102 expect(created.name).toContain("/documents/");
103 expect(created.collectionId).toEqual("_alchemy");
104 expect(created.fields.env?.stringValue).toEqual("test");
105
106 const fetched = yield* firestore.getProjectsDatabasesDocuments({
107 name: created.name,
108 });
109 expect(fetched.name).toEqual(created.name);
110 expect(fetched.fields?.env?.stringValue).toEqual("test");
111 expect(Object.keys(fetched.fields ?? {})).toEqual(["env"]);
112
113 const updated = yield* stack.deploy(
114 Effect.gen(function* () {
115 const database = yield* GCP.Firestore.Database("DocDb", {
116 location: "us-central1",
117 type: "FIRESTORE_NATIVE",
118 });
119 return yield* Document("Flag", {
120 database: database.databaseId,
121 collectionId: "_alchemy",
122 documentId: created.documentId,
123 fields: {
124 env: { stringValue: "prod" },
125 role: { stringValue: "flag" },
126 },
127 });
128 }),
129 );
130
131 expect(updated.name).toEqual(created.name);
132 expect(updated.fields.env?.stringValue).toEqual("prod");
133 expect(updated.fields.role?.stringValue).toEqual("flag");
134
135 yield* stack.destroy();
136
137 const gone = yield* waitUntilGone(created.name);
138 expect(gone).toEqual("gone");
139 }).pipe(logLevel),
140 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
141 );