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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials |
| 12 | // administer the organization (the testing service account does not). |
| 13 | const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(/^organizations\//, ""); |
| 14 | const organization = `organizations/${organizationId}`; |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | logging.getOrganizationsLocationsBucketsLinks({ name }).pipe( |
| 20 | Effect.map((link) => |
| 21 | link.lifecycleState === "DELETE_REQUESTED" ? ("gone" as const) : ("found" as const), |
| 22 | ), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("2 seconds"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider.skipIf(!organizationId)( |
| 32 | "getOrganizationsLocationsBucketsLinks on a missing link fails with NotFound", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | logging.getOrganizationsLocationsBucketsLinks({ |
| 39 | name: `${organization}/locations/global/buckets/_Default/links/alchemy_missing`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | yield* stack.destroy(); |
| 45 | }).pipe(logLevel), |
| 46 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 47 | ); |
| 48 | |
| 49 | test.provider.skipIf(!organizationId || !!process.env.FAST)( |
| 50 | "create, replace, and delete an organization logging bucket link", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | yield* stack.destroy(); |
| 54 | |
| 55 | const created = yield* stack.deploy( |
| 56 | Effect.gen(function* () { |
| 57 | const bucket = yield* GCP.Logging.OrganizationLogBucket("Analytics", { |
| 58 | organization, |
| 59 | analyticsEnabled: true, |
| 60 | description: "analytics parent", |
| 61 | }); |
| 62 | const link = yield* GCP.Logging.OrganizationBucketsLink("Bq", { |
| 63 | organization, |
| 64 | bucket: bucket.name, |
| 65 | description: "log analytics dataset", |
| 66 | }); |
| 67 | return { bucket, link }; |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | expect(created.link.linkId).toEqual(expect.any(String)); |
| 72 | expect(created.link.bucket).toEqual(created.bucket.name); |
| 73 | expect(created.link.name).toEqual(`${created.bucket.name}/links/${created.link.linkId}`); |
| 74 | expect(created.link.description).toEqual("log analytics dataset"); |
| 75 | |
| 76 | const fetched = yield* logging.getOrganizationsLocationsBucketsLinks({ |
| 77 | name: created.link.name, |
| 78 | }); |
| 79 | expect(fetched.description).toContain("alchemy-id="); |
| 80 | |
| 81 | const last = created.link.linkId.at(-1) ?? "a"; |
| 82 | const nextLinkId = `${created.link.linkId.slice(0, -1)}${last === "z" ? "0" : "a"}`; |
| 83 | |
| 84 | const replaced = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | const bucket = yield* GCP.Logging.OrganizationLogBucket("Analytics", { |
| 87 | organization, |
| 88 | bucketId: created.bucket.bucketId, |
| 89 | location: created.bucket.location, |
| 90 | analyticsEnabled: true, |
| 91 | description: "analytics parent", |
| 92 | }); |
| 93 | const link = yield* GCP.Logging.OrganizationBucketsLink("Bq", { |
| 94 | organization, |
| 95 | bucket: bucket.name, |
| 96 | linkId: nextLinkId, |
| 97 | description: "replaced link", |
| 98 | }); |
| 99 | return { bucket, link }; |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | expect(replaced.link.linkId).not.toEqual(created.link.linkId); |
| 104 | |
| 105 | const previousGone = yield* waitUntilGone(created.link.name); |
| 106 | expect(previousGone).toEqual("gone"); |
| 107 | |
| 108 | yield* stack.destroy(); |
| 109 | |
| 110 | const gone = yield* waitUntilGone(replaced.link.name); |
| 111 | expect(gone).toEqual("gone"); |
| 112 | }).pipe(logLevel), |
| 113 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 114 | ); |