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 servicedirectory from "@distilled.cloud/gcp/servicedirectory_v1"; |
| 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 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const waitUntilGone = (name: string) => |
| 15 | servicedirectory.getProjectsLocationsNamespacesServicesEndpoints({ name }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("1 second"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | test.provider( |
| 26 | "getProjectsLocationsNamespacesServicesEndpoints on a missing endpoint fails with NotFound", |
| 27 | () => |
| 28 | Effect.gen(function* () { |
| 29 | const { project } = yield* GcpEnvironment.current; |
| 30 | |
| 31 | const error = yield* Effect.flip( |
| 32 | servicedirectory.getProjectsLocationsNamespacesServicesEndpoints({ |
| 33 | name: `projects/${project}/locations/us-central1/namespaces/alchemy-endpoint-missing/services/api/endpoints/missing`, |
| 34 | }), |
| 35 | ); |
| 36 | expect(error._tag).toBe("NotFound"); |
| 37 | }).pipe(logLevel), |
| 38 | { tags: ["provider:gcp", "provider:gcp:servicedirectory", "live"] }, |
| 39 | ); |
| 40 | |
| 41 | test.provider( |
| 42 | "create, update, replace, and delete an endpoint", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | const { project } = yield* GcpEnvironment.current; |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | const created = yield* stack.deploy( |
| 50 | Effect.gen(function* () { |
| 51 | const ns = yield* GCP.ServiceDirectory.Namespace("Services", { |
| 52 | location: "us-central1", |
| 53 | labels: { env: "test" }, |
| 54 | }); |
| 55 | const api = yield* GCP.ServiceDirectory.Service("Api", { |
| 56 | namespace: ns.name, |
| 57 | }); |
| 58 | const endpoint = yield* GCP.ServiceDirectory.Endpoint("Https", { |
| 59 | service: api.name, |
| 60 | address: "10.0.0.1", |
| 61 | port: 8080, |
| 62 | annotations: { protocol: "http" }, |
| 63 | }); |
| 64 | return { ns, api, endpoint }; |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | expect(created.endpoint.name).toContain("/endpoints/"); |
| 69 | expect(created.endpoint.endpointId).toEqual(expect.any(String)); |
| 70 | expect(created.endpoint.service).toEqual(created.api.name); |
| 71 | expect(created.endpoint.namespace).toEqual(created.ns.name); |
| 72 | expect(created.endpoint.location).toEqual("us-central1"); |
| 73 | expect(created.endpoint.project).toEqual(project); |
| 74 | expect(created.endpoint.address).toEqual("10.0.0.1"); |
| 75 | expect(created.endpoint.port).toEqual(8080); |
| 76 | expect(created.endpoint.annotations).toMatchObject({ protocol: "http" }); |
| 77 | expect(created.endpoint.uid).toEqual(expect.any(String)); |
| 78 | |
| 79 | const fetched = yield* servicedirectory.getProjectsLocationsNamespacesServicesEndpoints({ |
| 80 | name: created.endpoint.name, |
| 81 | }); |
| 82 | expect(fetched.name).toEqual(created.endpoint.name); |
| 83 | expect(fetched.address).toEqual("10.0.0.1"); |
| 84 | expect(fetched.port).toEqual(8080); |
| 85 | expect(fetched.annotations?.protocol).toEqual("http"); |
| 86 | expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String)); |
| 87 | expect(fetched.uid).toEqual(created.endpoint.uid); |
| 88 | |
| 89 | const updated = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | const ns = yield* GCP.ServiceDirectory.Namespace("Services", { |
| 92 | namespaceId: created.ns.namespaceId, |
| 93 | location: "us-central1", |
| 94 | labels: { env: "test" }, |
| 95 | }); |
| 96 | const api = yield* GCP.ServiceDirectory.Service("Api", { |
| 97 | namespace: ns.name, |
| 98 | serviceId: created.api.serviceId, |
| 99 | }); |
| 100 | const endpoint = yield* GCP.ServiceDirectory.Endpoint("Https", { |
| 101 | service: api.name, |
| 102 | endpointId: created.endpoint.endpointId, |
| 103 | address: "10.0.0.2", |
| 104 | port: 443, |
| 105 | annotations: { protocol: "https", role: "api" }, |
| 106 | }); |
| 107 | return { ns, api, endpoint }; |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | expect(updated.endpoint.name).toEqual(created.endpoint.name); |
| 112 | expect(updated.endpoint.uid).toEqual(created.endpoint.uid); |
| 113 | expect(updated.endpoint.address).toEqual("10.0.0.2"); |
| 114 | expect(updated.endpoint.port).toEqual(443); |
| 115 | expect(updated.endpoint.annotations).toMatchObject({ |
| 116 | protocol: "https", |
| 117 | role: "api", |
| 118 | }); |
| 119 | |
| 120 | const refetched = yield* servicedirectory.getProjectsLocationsNamespacesServicesEndpoints({ |
| 121 | name: created.endpoint.name, |
| 122 | }); |
| 123 | expect(refetched.address).toEqual("10.0.0.2"); |
| 124 | expect(refetched.port).toEqual(443); |
| 125 | expect(refetched.annotations?.protocol).toEqual("https"); |
| 126 | expect(refetched.annotations?.role).toEqual("api"); |
| 127 | |
| 128 | const last = created.endpoint.endpointId.at(-1) ?? "a"; |
| 129 | const nextEndpointId = `${created.endpoint.endpointId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 130 | |
| 131 | const replaced = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | const ns = yield* GCP.ServiceDirectory.Namespace("Services", { |
| 134 | namespaceId: created.ns.namespaceId, |
| 135 | location: "us-central1", |
| 136 | labels: { env: "test" }, |
| 137 | }); |
| 138 | const api = yield* GCP.ServiceDirectory.Service("Api", { |
| 139 | namespace: ns.name, |
| 140 | serviceId: created.api.serviceId, |
| 141 | }); |
| 142 | const endpoint = yield* GCP.ServiceDirectory.Endpoint("Https", { |
| 143 | service: api.name, |
| 144 | endpointId: nextEndpointId, |
| 145 | address: "10.0.0.3", |
| 146 | port: 8443, |
| 147 | annotations: { protocol: "https" }, |
| 148 | }); |
| 149 | return { ns, api, endpoint }; |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | expect(replaced.endpoint.name).not.toEqual(created.endpoint.name); |
| 154 | expect(replaced.endpoint.endpointId).toEqual(nextEndpointId); |
| 155 | expect(replaced.endpoint.address).toEqual("10.0.0.3"); |
| 156 | expect(replaced.endpoint.port).toEqual(8443); |
| 157 | |
| 158 | const fetchedReplacement = |
| 159 | yield* servicedirectory.getProjectsLocationsNamespacesServicesEndpoints({ |
| 160 | name: replaced.endpoint.name, |
| 161 | }); |
| 162 | expect(fetchedReplacement.name).toEqual(replaced.endpoint.name); |
| 163 | expect(fetchedReplacement.address).toEqual("10.0.0.3"); |
| 164 | |
| 165 | const previousGone = yield* waitUntilGone(created.endpoint.name); |
| 166 | expect(previousGone).toEqual("gone"); |
| 167 | |
| 168 | yield* stack.destroy(); |
| 169 | |
| 170 | const gone = yield* waitUntilGone(replaced.endpoint.name); |
| 171 | expect(gone).toEqual("gone"); |
| 172 | }).pipe(logLevel), |
| 173 | { |
| 174 | tags: ["provider:gcp", "provider:gcp:servicedirectory", "live"], |
| 175 | timeout: 90_000, |
| 176 | }, |
| 177 | ); |