@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 141 lines 5.75 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import { serveProbes } from "../../bindingHost.ts";
4
5 const nativeDatabase = (id: string) =>
6 GCP.Firestore.Database(id, {
7 location: "us-central1",
8 type: "FIRESTORE_NATIVE",
9 });
10
11 /** Database the per-document PatchDocument / GetDocument / DeleteDocument bind. */
12 export const DocsDatabase = nativeDatabase("DocsDatabase");
13 /** Database only {@link GCP.Firestore.ReadDatabase} binds (seeded by the test). */
14 export const ReadOnlyDatabase = nativeDatabase("ReadOnlyDatabase");
15 /** Database only {@link GCP.Firestore.WriteDatabase} binds. */
16 export const WriteOnlyDatabase = nativeDatabase("WriteOnlyDatabase");
17 /** Database only {@link GCP.Firestore.ReadWriteDatabase} binds. */
18 export const ReadWriteOnlyDatabase = nativeDatabase("ReadWriteOnlyDatabase");
19
20 /** Documents the test seeds out of band. */
21 export const SEEDED_GET = "users/seeded";
22 export const SEEDED_DELETE = "users/doomed";
23 export const PATCHED = "users/alice";
24
25 /**
26 * Effect-native Cloud Run service exercising every Firestore binding as its
27 * own runtime service account. Deployed from {@link ../Bindings.test.ts}.
28 */
29 export default class FirestoreBindingsHost extends GCP.Function<FirestoreBindingsHost>()(
30 "FirestoreBindingsHost",
31 { main: import.meta.url, invokerIamDisabled: true },
32 Effect.gen(function* () {
33 const patchDocument = yield* GCP.Firestore.PatchDocument(DocsDatabase);
34 const getDocument = yield* GCP.Firestore.GetDocument(DocsDatabase);
35 const deleteDocument = yield* GCP.Firestore.DeleteDocument(DocsDatabase);
36 const reader = yield* GCP.Firestore.ReadDatabase(ReadOnlyDatabase);
37 const writer = yield* GCP.Firestore.WriteDatabase(WriteOnlyDatabase);
38 const both = yield* GCP.Firestore.ReadWriteDatabase(ReadWriteOnlyDatabase);
39
40 return {
41 fetch: serveProbes({
42 patchDocument: patchDocument({
43 documentPath: PATCHED,
44 body: { fields: { name: { stringValue: "Alice" } } },
45 }),
46 getDocument: getDocument({ documentPath: SEEDED_GET }),
47 deleteDocument: deleteDocument({ documentPath: SEEDED_DELETE }),
48 read: Effect.gen(function* () {
49 const one = yield* reader.get("things/a");
50 const missing = yield* reader.get("things/never");
51 const page1 = yield* reader.list("things", { pageSize: 1 });
52 const page2 = yield* reader.list("things", {
53 pageSize: 1,
54 pageToken: page1.nextPageToken,
55 });
56 const big = yield* reader.query({
57 from: [{ collectionId: "things" }],
58 where: {
59 fieldFilter: {
60 field: { fieldPath: "n" },
61 op: "GREATER_THAN",
62 value: { integerValue: "1" },
63 },
64 },
65 });
66 return {
67 one: one?.fields,
68 missing: missing === undefined,
69 listed: [...page1.documents, ...page2.documents]
70 .map((doc) => doc.name.split("/").pop())
71 .sort(),
72 hasNextPage: page1.nextPageToken !== undefined,
73 big: big.map((doc) => doc.fields.n),
74 };
75 }),
76 write: Effect.gen(function* () {
77 yield* writer.delete("things/created");
78 const created = yield* writer.create("things/created", { n: 1 });
79 const conflict = yield* writer.create("things/created", { n: 2 }).pipe(
80 Effect.map(() => "created"),
81 Effect.catchTag("GCP.Firestore.DocumentAlreadyExists", (error) =>
82 Effect.succeed(error.path),
83 ),
84 );
85 const updated = yield* writer.update("things/created", {
86 "odd key": "x",
87 });
88 const updateMissing = yield* writer.update("things/never", { a: 1 }).pipe(
89 Effect.map(() => "updated"),
90 Effect.catchTag("NotFound", () => Effect.succeed("NotFound")),
91 );
92 yield* writer.set("things/set", { team: "red" });
93 yield* writer.set("things/gone", { team: "blue" });
94 yield* writer.delete("things/gone");
95 yield* writer.delete("things/never-existed");
96 return {
97 created: created.name.endsWith("/documents/things/created"),
98 conflict,
99 updated: updated.fields,
100 updateMissing,
101 };
102 }),
103 readWrite: Effect.gen(function* () {
104 yield* both.delete("scores/a");
105 const missing = yield* both.get("scores/a");
106 yield* both.create("scores/a", { team: "red", n: 1 });
107 yield* both.set("scores/b", { team: "blue", n: 2 });
108 yield* both.update("scores/b", { n: 3 });
109 const b = yield* both.get("scores/b");
110 const listed = yield* both.list("scores");
111 const red = yield* both.query({
112 from: [{ collectionId: "scores" }],
113 where: {
114 fieldFilter: {
115 field: { fieldPath: "team" },
116 op: "EQUAL",
117 value: { stringValue: "red" },
118 },
119 },
120 });
121 yield* both.delete("scores/a");
122 const afterDelete = yield* both.get("scores/a");
123 return {
124 missing: missing === undefined,
125 b: b?.fields,
126 listed: listed.documents.map((doc) => doc.name.split("/").pop()).sort(),
127 red: red.map((doc) => doc.name.split("/").pop()),
128 afterDelete: afterDelete === undefined,
129 };
130 }),
131 }),
132 };
133 }).pipe(
134 Effect.provide(GCP.Firestore.PatchDocumentHttp),
135 Effect.provide(GCP.Firestore.GetDocumentHttp),
136 Effect.provide(GCP.Firestore.DeleteDocumentHttp),
137 Effect.provide(GCP.Firestore.ReadDatabaseHttp),
138 Effect.provide(GCP.Firestore.WriteDatabaseHttp),
139 Effect.provide(GCP.Firestore.ReadWriteDatabaseHttp),
140 ),
141 ) {}