The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as spanner from "@distilled.cloud/gcp/spanner_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { expectProbe } from "../bindingHost.ts"; |
| 8 | import SpannerBindingsHost, { App, Db, ITEMS_DDL, Schema } from "./fixtures/bindings-host.ts"; |
| 9 | |
| 10 | const testOptions = { providers: GCP.providers() }; |
| 11 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 12 | const sharedStack = Core.scratchStack(testOptions, "SpannerBindings"); |
| 13 | |
| 14 | let baseUrl: string; |
| 15 | let member: string; |
| 16 | let names: { instance: string; schema: string; app: string }; |
| 17 | |
| 18 | type Policy = { bindings?: { role?: string; members?: string[] }[] }; |
| 19 | |
| 20 | const rolesOf = (policy: Policy) => |
| 21 | (policy.bindings ?? []) |
| 22 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 23 | .map((binding) => binding.role) |
| 24 | .sort(); |
| 25 | |
| 26 | const databaseRoles = (database: string) => |
| 27 | spanner |
| 28 | .getIamPolicyProjectsInstancesDatabases({ |
| 29 | resource: database, |
| 30 | body: { options: { requestedPolicyVersion: 3 } }, |
| 31 | }) |
| 32 | .pipe(Effect.map(rolesOf)); |
| 33 | |
| 34 | const instanceRoles = Effect.suspend(() => |
| 35 | spanner |
| 36 | .getIamPolicyProjectsInstances({ |
| 37 | resource: names.instance, |
| 38 | body: { options: { requestedPolicyVersion: 3 } }, |
| 39 | }) |
| 40 | .pipe(Effect.map(rolesOf)), |
| 41 | ); |
| 42 | |
| 43 | describe( |
| 44 | "Spanner Bindings", |
| 45 | { |
| 46 | tags: ["provider:gcp", "provider:gcp:spanner", "provider:gcp:run", "live"], |
| 47 | }, |
| 48 | () => { |
| 49 | beforeAll( |
| 50 | Effect.gen(function* () { |
| 51 | yield* sharedStack.destroy(); |
| 52 | const out = yield* sharedStack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | const host = yield* SpannerBindingsHost; |
| 55 | return { |
| 56 | uri: host.uri, |
| 57 | serviceAccount: host.serviceAccount, |
| 58 | instance: (yield* Db).name, |
| 59 | schema: (yield* Schema).name, |
| 60 | app: (yield* App).name, |
| 61 | }; |
| 62 | }), |
| 63 | ); |
| 64 | baseUrl = out.uri!; |
| 65 | member = `serviceAccount:${out.serviceAccount!}`; |
| 66 | names = out; |
| 67 | }), |
| 68 | { timeout: 1_200_000 }, |
| 69 | ); |
| 70 | |
| 71 | afterAll(sharedStack.destroy(), { timeout: 900_000 }); |
| 72 | |
| 73 | describe("GetInstance", () => { |
| 74 | test.provider( |
| 75 | "reads the instance, granted viewer on the instance only", |
| 76 | (_stack) => |
| 77 | Effect.gen(function* () { |
| 78 | const live = yield* expectProbe<spanner.Instance>(baseUrl, "getInstance"); |
| 79 | expect(live.name).toEqual(names.instance); |
| 80 | expect(live.processingUnits).toEqual(100); |
| 81 | expect(yield* instanceRoles).toEqual(["roles/spanner.viewer"]); |
| 82 | }), |
| 83 | { |
| 84 | tags: ["provider:gcp", "provider:gcp:spanner", "live"], |
| 85 | timeout: 600_000, |
| 86 | }, |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | describe("GetDdl", () => { |
| 91 | test.provider( |
| 92 | "reads the schema, granted databaseReader on the database only", |
| 93 | (_stack) => |
| 94 | Effect.gen(function* () { |
| 95 | const ddl = yield* expectProbe<spanner.GetDatabaseDdlResponse>(baseUrl, "getDdl"); |
| 96 | expect(ddl.statements).toEqual([ITEMS_DDL]); |
| 97 | |
| 98 | const direct = yield* spanner.getDdlProjectsInstancesDatabases({ |
| 99 | database: names.schema, |
| 100 | }); |
| 101 | expect(ddl.statements).toEqual(direct.statements); |
| 102 | |
| 103 | expect(yield* databaseRoles(names.schema)).toEqual(["roles/spanner.databaseReader"]); |
| 104 | }), |
| 105 | { |
| 106 | tags: ["provider:gcp", "provider:gcp:spanner", "live"], |
| 107 | timeout: 600_000, |
| 108 | }, |
| 109 | ); |
| 110 | }); |
| 111 | |
| 112 | describe("ExecuteSql", () => { |
| 113 | test.provider( |
| 114 | "queries the database, granted databaseUser on the database only", |
| 115 | (_stack) => |
| 116 | Effect.gen(function* () { |
| 117 | const result = yield* expectProbe<spanner.ResultSet>(baseUrl, "executeSql"); |
| 118 | expect(result.metadata?.rowType?.fields?.[0]?.name).toEqual("n"); |
| 119 | expect(result.rows).toEqual([["0"]]); |
| 120 | |
| 121 | expect(yield* databaseRoles(names.app)).toEqual(["roles/spanner.databaseUser"]); |
| 122 | // Nothing leaks onto the other database or the instance. |
| 123 | expect(yield* databaseRoles(names.schema)).toEqual(["roles/spanner.databaseReader"]); |
| 124 | expect(yield* instanceRoles).toEqual(["roles/spanner.viewer"]); |
| 125 | }), |
| 126 | { |
| 127 | tags: ["provider:gcp", "provider:gcp:spanner", "live"], |
| 128 | timeout: 600_000, |
| 129 | }, |
| 130 | ); |
| 131 | }); |
| 132 | }, |
| 133 | ); |