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
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { decodeFields, encodeFields } from "@alchemy.run/gcp/Firestore/Values"; |
| 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 firestore from "@distilled.cloud/gcp/firestore_v1"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { expectProbe } from "../bindingHost.ts"; |
| 10 | import FirestoreBindingsHost, { |
| 11 | DocsDatabase, |
| 12 | PATCHED, |
| 13 | ReadOnlyDatabase, |
| 14 | ReadWriteOnlyDatabase, |
| 15 | SEEDED_DELETE, |
| 16 | SEEDED_GET, |
| 17 | WriteOnlyDatabase, |
| 18 | } from "./fixtures/bindings-host.ts"; |
| 19 | |
| 20 | const testOptions = { providers: GCP.providers() }; |
| 21 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 22 | const sharedStack = Core.scratchStack(testOptions, "FirestoreBindings"); |
| 23 | |
| 24 | let baseUrl: string; |
| 25 | let hostAccount: string; |
| 26 | let project: string; |
| 27 | let databases: { |
| 28 | docs: string; |
| 29 | readOnly: string; |
| 30 | writeOnly: string; |
| 31 | readWrite: string; |
| 32 | }; |
| 33 | |
| 34 | /** Write a document out of band, as the deployer. */ |
| 35 | const seed = (database: string, path: string, fields: Record<string, unknown>) => |
| 36 | firestore.patchProjectsDatabasesDocuments({ |
| 37 | name: `${database}/documents/${path}`, |
| 38 | body: { fields: encodeFields(fields) }, |
| 39 | }); |
| 40 | |
| 41 | /** Read a document's fields out of band, `undefined` when it is missing. */ |
| 42 | const readOutOfBand = (database: string, path: string) => |
| 43 | firestore.getProjectsDatabasesDocuments({ name: `${database}/documents/${path}` }).pipe( |
| 44 | Effect.map((document) => decodeFields(document.fields)), |
| 45 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * Roles the host holds on the project under the IAM Condition naming only |
| 50 | * `database` (Firestore has no per-database IAM policy). Also asserts the |
| 51 | * host holds no unconditioned project role. |
| 52 | */ |
| 53 | const scopedRoles = (database: string) => |
| 54 | Effect.gen(function* () { |
| 55 | const policy = yield* crm.getIamPolicyProjects({ |
| 56 | resource: `projects/${project}`, |
| 57 | body: { options: { requestedPolicyVersion: 3 } }, |
| 58 | }); |
| 59 | const hostBindings = (policy.bindings ?? []).filter((binding) => |
| 60 | (binding.members ?? []).includes(`serviceAccount:${hostAccount}`), |
| 61 | ); |
| 62 | expect(hostBindings.filter((binding) => binding.condition === undefined)).toEqual([]); |
| 63 | const condition = `resource.name == "${database}" || resource.name.startsWith("${database}/")`; |
| 64 | return hostBindings |
| 65 | .filter((binding) => binding.condition?.expression === condition) |
| 66 | .map((binding) => binding.role) |
| 67 | .sort(); |
| 68 | }); |
| 69 | |
| 70 | describe.skipIf(!!process.env.FAST)( |
| 71 | "Firestore Bindings", |
| 72 | { |
| 73 | tags: ["provider:gcp", "provider:gcp:firestore", "provider:gcp:run", "live"], |
| 74 | }, |
| 75 | () => { |
| 76 | beforeAll( |
| 77 | Effect.gen(function* () { |
| 78 | yield* sharedStack.destroy(); |
| 79 | const out = yield* sharedStack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | const host = yield* FirestoreBindingsHost; |
| 82 | const docs = yield* DocsDatabase; |
| 83 | return { |
| 84 | uri: host.uri, |
| 85 | serviceAccount: host.serviceAccount, |
| 86 | project: docs.project, |
| 87 | docs: docs.name, |
| 88 | readOnly: (yield* ReadOnlyDatabase).name, |
| 89 | writeOnly: (yield* WriteOnlyDatabase).name, |
| 90 | readWrite: (yield* ReadWriteOnlyDatabase).name, |
| 91 | }; |
| 92 | }), |
| 93 | ); |
| 94 | baseUrl = out.uri!; |
| 95 | hostAccount = out.serviceAccount!; |
| 96 | project = out.project; |
| 97 | databases = { |
| 98 | docs: out.docs, |
| 99 | readOnly: out.readOnly, |
| 100 | writeOnly: out.writeOnly, |
| 101 | readWrite: out.readWrite, |
| 102 | }; |
| 103 | }), |
| 104 | { timeout: 900_000 }, |
| 105 | ); |
| 106 | |
| 107 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 108 | |
| 109 | const expectDocsGrants = Effect.suspend(() => scopedRoles(databases.docs)).pipe( |
| 110 | Effect.map((roles) => |
| 111 | expect(roles).toEqual(["roles/datastore.user", "roles/datastore.viewer"]), |
| 112 | ), |
| 113 | ); |
| 114 | |
| 115 | describe("PatchDocument", () => { |
| 116 | test.provider( |
| 117 | "writes a document as the host's service account, scoped to the database", |
| 118 | (_stack) => |
| 119 | Effect.gen(function* () { |
| 120 | const doc = yield* expectProbe<firestore.Document>(baseUrl, "patchDocument"); |
| 121 | expect(doc.name).toEqual(`${databases.docs}/documents/${PATCHED}`); |
| 122 | expect(yield* readOutOfBand(databases.docs, PATCHED)).toEqual({ |
| 123 | name: "Alice", |
| 124 | }); |
| 125 | yield* expectDocsGrants; |
| 126 | }), |
| 127 | { |
| 128 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 129 | timeout: 600_000, |
| 130 | }, |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | describe("GetDocument", () => { |
| 135 | test.provider( |
| 136 | "reads a seeded document as the host's service account, scoped to the database", |
| 137 | (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | yield* seed(databases.docs, SEEDED_GET, { name: "Seeded" }); |
| 140 | const doc = yield* expectProbe<firestore.Document>(baseUrl, "getDocument"); |
| 141 | expect(doc.fields?.name?.stringValue).toEqual("Seeded"); |
| 142 | yield* expectDocsGrants; |
| 143 | }), |
| 144 | { |
| 145 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 146 | timeout: 600_000, |
| 147 | }, |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("DeleteDocument", () => { |
| 152 | test.provider( |
| 153 | "deletes a seeded document as the host's service account, scoped to the database", |
| 154 | (_stack) => |
| 155 | Effect.gen(function* () { |
| 156 | yield* seed(databases.docs, SEEDED_DELETE, { name: "Doomed" }); |
| 157 | yield* expectProbe(baseUrl, "deleteDocument"); |
| 158 | expect(yield* readOutOfBand(databases.docs, SEEDED_DELETE)).toBeUndefined(); |
| 159 | yield* expectDocsGrants; |
| 160 | }), |
| 161 | { |
| 162 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 163 | timeout: 600_000, |
| 164 | }, |
| 165 | ); |
| 166 | }); |
| 167 | |
| 168 | describe("ReadDatabase", () => { |
| 169 | test.provider( |
| 170 | "gets, lists and queries seeded documents, granted datastore.viewer under the database condition", |
| 171 | (_stack) => |
| 172 | Effect.gen(function* () { |
| 173 | yield* seed(databases.readOnly, "things/a", { n: 1, s: "a" }); |
| 174 | yield* seed(databases.readOnly, "things/b", { n: 2, s: "b" }); |
| 175 | const out = yield* expectProbe(baseUrl, "read"); |
| 176 | expect(out).toEqual({ |
| 177 | one: { n: 1, s: "a" }, |
| 178 | missing: true, |
| 179 | listed: ["a", "b"], |
| 180 | hasNextPage: true, |
| 181 | big: [2], |
| 182 | }); |
| 183 | expect(yield* scopedRoles(databases.readOnly)).toEqual(["roles/datastore.viewer"]); |
| 184 | }), |
| 185 | { |
| 186 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 187 | timeout: 600_000, |
| 188 | }, |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | describe("WriteDatabase", () => { |
| 193 | test.provider( |
| 194 | "creates, updates, sets and deletes documents, granted datastore.user under the database condition", |
| 195 | (_stack) => |
| 196 | Effect.gen(function* () { |
| 197 | const out = yield* expectProbe(baseUrl, "write"); |
| 198 | expect(out).toEqual({ |
| 199 | created: true, |
| 200 | conflict: "things/created", |
| 201 | updated: { n: 1, "odd key": "x" }, |
| 202 | updateMissing: "NotFound", |
| 203 | }); |
| 204 | expect(yield* readOutOfBand(databases.writeOnly, "things/created")).toEqual({ |
| 205 | n: 1, |
| 206 | "odd key": "x", |
| 207 | }); |
| 208 | expect(yield* readOutOfBand(databases.writeOnly, "things/set")).toEqual({ |
| 209 | team: "red", |
| 210 | }); |
| 211 | expect(yield* readOutOfBand(databases.writeOnly, "things/gone")).toBeUndefined(); |
| 212 | expect(yield* scopedRoles(databases.writeOnly)).toEqual(["roles/datastore.user"]); |
| 213 | }), |
| 214 | { |
| 215 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 216 | timeout: 600_000, |
| 217 | }, |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | describe("ReadWriteDatabase", () => { |
| 222 | test.provider( |
| 223 | "round-trips documents, granted datastore.user under the database condition", |
| 224 | (_stack) => |
| 225 | Effect.gen(function* () { |
| 226 | const out = yield* expectProbe(baseUrl, "readWrite"); |
| 227 | expect(out).toEqual({ |
| 228 | missing: true, |
| 229 | b: { team: "blue", n: 3 }, |
| 230 | listed: ["a", "b"], |
| 231 | red: ["a"], |
| 232 | afterDelete: true, |
| 233 | }); |
| 234 | expect(yield* readOutOfBand(databases.readWrite, "scores/b")).toEqual({ |
| 235 | team: "blue", |
| 236 | n: 3, |
| 237 | }); |
| 238 | expect(yield* readOutOfBand(databases.readWrite, "scores/a")).toBeUndefined(); |
| 239 | expect(yield* scopedRoles(databases.readWrite)).toEqual(["roles/datastore.user"]); |
| 240 | }), |
| 241 | { |
| 242 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 243 | timeout: 600_000, |
| 244 | }, |
| 245 | ); |
| 246 | }); |
| 247 | }, |
| 248 | ); |