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 { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials |
| 13 | // administer the organization (the testing service account does not). |
| 14 | const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(/^organizations\//, ""); |
| 15 | const organization = `organizations/${organizationId}`; |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | const projectNameOf = (project: string) => `projects/${project}`; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | logging.getOrganizationsLocationsLogScopes({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider.skipIf(!organizationId)( |
| 32 | "getOrganizationsLocationsLogScopes on a missing log scope fails with NotFound", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | logging.getOrganizationsLocationsLogScopes({ |
| 39 | name: `${organization}/locations/global/logScopes/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)( |
| 50 | "create, update, replace, and delete an organization logging log scope", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | const { project } = yield* GcpEnvironment.current; |
| 54 | |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const created = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* GCP.Logging.OrganizationLogScope("App", { |
| 60 | organization, |
| 61 | resourceNames: [projectNameOf(project)], |
| 62 | description: "application logs", |
| 63 | }); |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(created.logScopeId).toEqual(expect.any(String)); |
| 68 | expect(created.location).toEqual("global"); |
| 69 | expect(created.organization).toEqual(organization); |
| 70 | expect(created.name).toEqual( |
| 71 | `${organization}/locations/global/logScopes/${created.logScopeId}`, |
| 72 | ); |
| 73 | expect(created.resourceNames).toEqual([projectNameOf(project)]); |
| 74 | expect(created.description).toEqual("application logs"); |
| 75 | |
| 76 | const fetched = yield* logging.getOrganizationsLocationsLogScopes({ |
| 77 | name: created.name, |
| 78 | }); |
| 79 | expect(fetched.resourceNames).toEqual([projectNameOf(project)]); |
| 80 | expect(fetched.description).toContain("alchemy-id="); |
| 81 | |
| 82 | const updated = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* GCP.Logging.OrganizationLogScope("App", { |
| 85 | organization, |
| 86 | logScopeId: created.logScopeId, |
| 87 | resourceNames: [projectNameOf(project)], |
| 88 | description: "updated application logs", |
| 89 | }); |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(updated.name).toEqual(created.name); |
| 94 | expect(updated.description).toEqual("updated application logs"); |
| 95 | |
| 96 | const last = created.logScopeId.at(-1) ?? "a"; |
| 97 | const nextId = `${created.logScopeId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 98 | |
| 99 | const replaced = yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | return yield* GCP.Logging.OrganizationLogScope("App", { |
| 102 | organization, |
| 103 | logScopeId: nextId, |
| 104 | resourceNames: [projectNameOf(project)], |
| 105 | description: "replaced scope", |
| 106 | }); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | expect(replaced.logScopeId).not.toEqual(created.logScopeId); |
| 111 | |
| 112 | const previousGone = yield* waitUntilGone(created.name); |
| 113 | expect(previousGone).toEqual("gone"); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const gone = yield* waitUntilGone(replaced.name); |
| 118 | expect(gone).toEqual("gone"); |
| 119 | }).pipe(logLevel), |
| 120 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 121 | ); |