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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1"; |
| 6 | import type { StackServices } from "alchemy/Stack"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ |
| 13 | providers: GCP.providers() as Layer.Layer<GCP.ProviderRequirements, never, StackServices>, |
| 14 | }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | // Runs against an existing Cloud SQL instance (creating one takes well over |
| 19 | // 5 minutes); set GCP_SQL_INSTANCE to its name. |
| 20 | const sqlInstance = process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE; |
| 21 | const runLifecycle = !!sqlInstance && !process.env.FAST; |
| 22 | |
| 23 | const waitUntilGone = (instance: string, backupRunId: string) => |
| 24 | GcpEnvironment.current.pipe( |
| 25 | Effect.flatMap(({ project }) => |
| 26 | sqladmin |
| 27 | .getBackupRuns({ |
| 28 | project, |
| 29 | instance, |
| 30 | id: backupRunId, |
| 31 | }) |
| 32 | .pipe( |
| 33 | Effect.as("found" as const), |
| 34 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 35 | Effect.repeat({ |
| 36 | schedule: Schedule.spaced("2 seconds"), |
| 37 | until: (status) => status === "gone", |
| 38 | times: 10, |
| 39 | }), |
| 40 | ), |
| 41 | ), |
| 42 | ); |
| 43 | |
| 44 | test.provider( |
| 45 | "getBackupRuns on a missing instance fails with SqlInstanceNotAuthorized", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | const { project } = yield* GcpEnvironment.current; |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | const error = yield* Effect.flip( |
| 52 | sqladmin.getBackupRuns({ |
| 53 | project, |
| 54 | instance: "alchemy-sql-instance-does-not-exist", |
| 55 | id: "1", |
| 56 | }), |
| 57 | ); |
| 58 | // Cloud SQL hides unknown instances behind 403 rather than 404. |
| 59 | expect(error._tag).toBe("SqlInstanceNotAuthorized"); |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | }).pipe(logLevel), |
| 63 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 }, |
| 64 | ); |
| 65 | |
| 66 | test.provider( |
| 67 | "listBackupRuns on a missing instance fails with SqlInstanceNotAuthorized", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | const { project } = yield* GcpEnvironment.current; |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const error = yield* Effect.flip( |
| 74 | sqladmin.listBackupRuns({ |
| 75 | project, |
| 76 | instance: "alchemy-sql-instance-does-not-exist", |
| 77 | maxResults: 10, |
| 78 | }), |
| 79 | ); |
| 80 | expect(error._tag).toBe("SqlInstanceNotAuthorized"); |
| 81 | |
| 82 | yield* stack.destroy(); |
| 83 | }).pipe(logLevel), |
| 84 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 }, |
| 85 | ); |
| 86 | |
| 87 | test.provider.skipIf(!runLifecycle)( |
| 88 | "create, update, and delete a sql backup run", |
| 89 | (stack) => |
| 90 | Effect.gen(function* () { |
| 91 | const { project } = yield* GcpEnvironment.current; |
| 92 | yield* stack.destroy(); |
| 93 | |
| 94 | const instance = sqlInstance!; |
| 95 | |
| 96 | const created = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* GCP.SQL.BackupRun("Nightly", { |
| 99 | instance, |
| 100 | description: "alchemy-on-demand", |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(created.backupRunId).toEqual(expect.any(String)); |
| 106 | expect(created.backupRunId.length).toBeGreaterThan(0); |
| 107 | expect(created.instance).toEqual(instance); |
| 108 | expect(created.project).toEqual(project); |
| 109 | expect(created.description).toEqual("alchemy-on-demand"); |
| 110 | |
| 111 | const fetched = yield* sqladmin.getBackupRuns({ |
| 112 | project: created.project, |
| 113 | instance: created.instance, |
| 114 | id: created.backupRunId, |
| 115 | }); |
| 116 | expect(String(fetched.id)).toEqual(created.backupRunId); |
| 117 | expect(fetched.instance).toEqual(instance); |
| 118 | expect(fetched.description).toContain("alchemy-on-demand"); |
| 119 | |
| 120 | const updated = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return yield* GCP.SQL.BackupRun("Nightly", { |
| 123 | instance, |
| 124 | description: "alchemy-on-demand", |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(updated.backupRunId).toEqual(created.backupRunId); |
| 130 | expect(updated.instance).toEqual(created.instance); |
| 131 | |
| 132 | const refetched = yield* sqladmin.getBackupRuns({ |
| 133 | project: updated.project, |
| 134 | instance: updated.instance, |
| 135 | id: updated.backupRunId, |
| 136 | }); |
| 137 | expect(String(refetched.id)).toEqual(updated.backupRunId); |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const gone = yield* waitUntilGone(created.instance, created.backupRunId); |
| 142 | expect(gone).toEqual("gone"); |
| 143 | }).pipe(logLevel), |
| 144 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 }, |
| 145 | ); |