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 { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 7 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 8 | import * as storagetransfer from "@distilled.cloud/gcp/storagetransfer_v1"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { expectProbe } from "../bindingHost.ts"; |
| 12 | import StorageTransferBindingsHost, { |
| 13 | Copy, |
| 14 | SEED_KEY, |
| 15 | SEED_TEXT, |
| 16 | Sink, |
| 17 | Source, |
| 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, "StorageTransferBindings"); |
| 23 | |
| 24 | let baseUrl: string; |
| 25 | let hostAccount: string; |
| 26 | let project: string; |
| 27 | let jobName: string; |
| 28 | let sinkBucket: string; |
| 29 | let transferAccount: string; |
| 30 | |
| 31 | /** Project-level roles held by the host's service account. */ |
| 32 | const projectRoles = () => |
| 33 | resourcemanager |
| 34 | .getIamPolicyProjects({ |
| 35 | resource: `projects/${project}`, |
| 36 | body: { options: { requestedPolicyVersion: 3 } }, |
| 37 | }) |
| 38 | .pipe( |
| 39 | Effect.map((policy) => |
| 40 | (policy.bindings ?? []) |
| 41 | .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`)) |
| 42 | .map((binding) => ({ |
| 43 | role: binding.role, |
| 44 | condition: binding.condition?.expression, |
| 45 | })) |
| 46 | .sort((a, b) => (a.role ?? "").localeCompare(b.role ?? "")), |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | /** |
| 51 | * Let the Google-managed transfer agent read the source and write the |
| 52 | * sink (the transfer itself runs as that agent, not as the host). |
| 53 | */ |
| 54 | const grantBucketRole = (bucket: string, member: string, role: string) => |
| 55 | Effect.gen(function* () { |
| 56 | const policy = yield* storage.getIamPolicyBuckets({ |
| 57 | bucket, |
| 58 | optionsRequestedPolicyVersion: 3, |
| 59 | }); |
| 60 | const bindings = [...(policy.bindings ?? [])]; |
| 61 | const existing = bindings.find((binding) => binding.role === role); |
| 62 | if (existing?.members?.includes(member)) return; |
| 63 | if (existing) { |
| 64 | existing.members = [...(existing.members ?? []), member]; |
| 65 | } else { |
| 66 | bindings.push({ role, members: [member] }); |
| 67 | } |
| 68 | yield* storage.setIamPolicyBuckets({ |
| 69 | bucket, |
| 70 | body: { ...policy, bindings }, |
| 71 | }); |
| 72 | }).pipe( |
| 73 | Effect.retry({ |
| 74 | while: (error) => error._tag === "Conflict", |
| 75 | times: 4, |
| 76 | schedule: Schedule.spaced("500 millis"), |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | describe( |
| 81 | "StorageTransfer Bindings", |
| 82 | { |
| 83 | tags: ["provider:gcp", "provider:gcp:storagetransfer", "provider:gcp:run", "live"], |
| 84 | }, |
| 85 | () => { |
| 86 | beforeAll( |
| 87 | Effect.gen(function* () { |
| 88 | yield* sharedStack.destroy(); |
| 89 | // The job's buckets must admit the transfer agent before the job |
| 90 | // exists, so deploy them first. |
| 91 | const buckets = yield* sharedStack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | const source = yield* Source; |
| 94 | const sink = yield* Sink; |
| 95 | const { project } = yield* GcpEnvironment.current; |
| 96 | return { |
| 97 | project, |
| 98 | source: source.bucketName, |
| 99 | sink: sink.bucketName, |
| 100 | }; |
| 101 | }), |
| 102 | ); |
| 103 | // Out-of-band calls in a hook need the provider services. |
| 104 | yield* Core.withProviders( |
| 105 | Effect.gen(function* () { |
| 106 | const account = yield* storagetransfer.getGoogleServiceAccounts({ |
| 107 | projectId: buckets.project, |
| 108 | }); |
| 109 | const member = `serviceAccount:${account.accountEmail}`; |
| 110 | yield* grantBucketRole(buckets.source, member, "roles/storage.objectViewer"); |
| 111 | yield* grantBucketRole(buckets.source, member, "roles/storage.legacyBucketReader"); |
| 112 | yield* grantBucketRole(buckets.sink, member, "roles/storage.legacyBucketWriter"); |
| 113 | yield* grantBucketRole(buckets.sink, member, "roles/storage.objectAdmin"); |
| 114 | }), |
| 115 | testOptions, |
| 116 | "StorageTransferBindings", |
| 117 | ); |
| 118 | const out = yield* sharedStack.deploy( |
| 119 | Effect.gen(function* () { |
| 120 | const host = yield* StorageTransferBindingsHost; |
| 121 | const job = yield* Copy; |
| 122 | return { |
| 123 | uri: host.uri, |
| 124 | serviceAccount: host.serviceAccount, |
| 125 | project: host.project, |
| 126 | job: job.name, |
| 127 | }; |
| 128 | }), |
| 129 | ); |
| 130 | baseUrl = out.uri!; |
| 131 | hostAccount = out.serviceAccount!; |
| 132 | project = out.project; |
| 133 | jobName = out.job; |
| 134 | sinkBucket = buckets.sink; |
| 135 | }), |
| 136 | { timeout: 900_000 }, |
| 137 | ); |
| 138 | |
| 139 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 140 | |
| 141 | describe("GetGoogleServiceAccount", () => { |
| 142 | test.provider( |
| 143 | "returns the project's transfer agent as the host", |
| 144 | (_stack) => |
| 145 | Effect.gen(function* () { |
| 146 | const out = yield* expectProbe<{ accountEmail: string }>( |
| 147 | baseUrl, |
| 148 | "getGoogleServiceAccount", |
| 149 | ); |
| 150 | const account = yield* storagetransfer.getGoogleServiceAccounts({ |
| 151 | projectId: project, |
| 152 | }); |
| 153 | expect(out.accountEmail).toEqual(account.accountEmail); |
| 154 | expect(out.accountEmail).toMatch( |
| 155 | /^project-\d+@storage-transfer-service\.iam\.gserviceaccount\.com$/, |
| 156 | ); |
| 157 | expect(yield* projectRoles()).toContainEqual({ |
| 158 | role: "roles/storagetransfer.viewer", |
| 159 | condition: undefined, |
| 160 | }); |
| 161 | }), |
| 162 | { |
| 163 | tags: ["provider:gcp", "provider:gcp:storagetransfer", "live"], |
| 164 | timeout: 600_000, |
| 165 | }, |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("RunTransferJob", () => { |
| 170 | test.provider( |
| 171 | "runs the bound job as the host and the object lands in the sink", |
| 172 | (_stack) => |
| 173 | Effect.gen(function* () { |
| 174 | const started = yield* expectProbe<{ name: string }>(baseUrl, "runTransferJob"); |
| 175 | expect(started.name).toMatch(/^transferOperations\//); |
| 176 | |
| 177 | const operation = yield* storagetransfer |
| 178 | .getTransferOperations({ name: started.name }) |
| 179 | .pipe( |
| 180 | Effect.repeat({ |
| 181 | schedule: Schedule.spaced("5 seconds"), |
| 182 | until: (current) => current.done === true, |
| 183 | times: 30, |
| 184 | }), |
| 185 | ); |
| 186 | expect(operation.done).toEqual(true); |
| 187 | expect(operation.error).toEqual(undefined); |
| 188 | expect(operation.metadata?.transferJobName).toEqual(jobName); |
| 189 | |
| 190 | const copied = yield* storage.getObjects({ |
| 191 | bucket: sinkBucket, |
| 192 | object: SEED_KEY, |
| 193 | }); |
| 194 | expect(copied.size).toEqual(String(SEED_TEXT.length)); |
| 195 | |
| 196 | expect(yield* projectRoles()).toEqual([ |
| 197 | { role: "roles/storagetransfer.user", condition: undefined }, |
| 198 | { role: "roles/storagetransfer.viewer", condition: undefined }, |
| 199 | ]); |
| 200 | }), |
| 201 | { |
| 202 | tags: ["provider:gcp", "provider:gcp:storagetransfer", "live"], |
| 203 | timeout: 600_000, |
| 204 | }, |
| 205 | ); |
| 206 | }); |
| 207 | }, |
| 208 | ); |