The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | createServer as createHttpServer, |
| 3 | type RequestListener, |
| 4 | type Server as NodeHttpServer, |
| 5 | } from "node:http"; |
| 6 | import { |
| 7 | PrismaApiError, |
| 8 | PrismaClient, |
| 9 | type PrismaManagementClient, |
| 10 | } from "@alchemy.run/prisma/Client"; |
| 11 | import { |
| 12 | Deployment as PrismaDeployment, |
| 13 | DeploymentProvider, |
| 14 | MAX_DEPLOYMENT_ARTIFACT_BYTES, |
| 15 | readUploadArtifact, |
| 16 | uploadArtifact, |
| 17 | validateDeploymentArtifactBytes, |
| 18 | } from "@alchemy.run/prisma/Deployment"; |
| 19 | import { describe, expect, it } from "@alchemy.run/test"; |
| 20 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 21 | import * as Output from "alchemy/Output"; |
| 22 | import * as Provider from "alchemy/Provider"; |
| 23 | import { encodeState } from "alchemy/State/StateEncoding"; |
| 24 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 25 | import { sha256, sha256Object } from "alchemy/Util/sha256"; |
| 26 | import * as Effect from "effect/Effect"; |
| 27 | import * as FileSystem from "effect/FileSystem"; |
| 28 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 29 | import * as HttpBody from "effect/http/HttpBody"; |
| 30 | import * as HttpClient from "effect/http/HttpClient"; |
| 31 | import * as HttpClientError from "effect/http/HttpClientError"; |
| 32 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 33 | import * as Layer from "effect/Layer"; |
| 34 | import * as Path from "effect/Path"; |
| 35 | import * as Redacted from "effect/Redacted"; |
| 36 | import * as Stream from "effect/Stream"; |
| 37 | import { WebSocketServer } from "ws"; |
| 38 | import { executeArtifactUpload } from "../../../prisma/src/internal/ArtifactUpload.ts"; |
| 39 | import { Credentials } from "../../../prisma/src/internal/Credentials.ts"; |
| 40 | import { |
| 41 | PrismaHttpClientLive, |
| 42 | PrismaUploadClient, |
| 43 | } from "../../../prisma/src/internal/HttpClient.ts"; |
| 44 | import { |
| 45 | dispatchTo, |
| 46 | failure, |
| 47 | makeFakeManagementApi, |
| 48 | notFound, |
| 49 | unhandled, |
| 50 | } from "./fixtures/FakeManagementApi.ts"; |
| 51 | |
| 52 | const currentClient = <T extends object>(client: T): PrismaManagementClient => { |
| 53 | return client as unknown as PrismaManagementClient; |
| 54 | }; |
| 55 | |
| 56 | const fixtureArtifactPath = `${import.meta.dirname}/fixtures/artifact-archive.bin`; |
| 57 | const sameArtifactPath = `${import.meta.dirname}/fixtures/artifact-same-version.bin`; |
| 58 | |
| 59 | const liveProviderContext = Layer.succeed(AlchemyContext, { |
| 60 | dotAlchemy: ".alchemy-test", |
| 61 | dev: false, |
| 62 | adopt: false, |
| 63 | }); |
| 64 | |
| 65 | const deploymentProviderLive = () => DeploymentProvider().pipe(Layer.provide(liveProviderContext)); |
| 66 | |
| 67 | /** |
| 68 | * Serve the Management API from the same hermetic client-shaped handlers this |
| 69 | * suite declares, for the routes Deployment now reaches through distilled |
| 70 | * operations (list/create under /v1/services/{serviceId}/deployments). The handlers |
| 71 | * are synchronous, so the fake runs them directly: an injected |
| 72 | * `PrismaApiError` becomes its real status, `undefined` becomes a 404, |
| 73 | * everything else a `{ data }` (or `{ data, pagination }`) envelope. The |
| 74 | * observe/start/promote/delete paths still resolve the hand-rolled client |
| 75 | * (their helpers are D3), so the `PrismaClient` layer stays provided |
| 76 | * alongside; tests that stub uploads provide `PrismaUploadClient` because |
| 77 | * the ambient `HttpClient` is now this management fake. |
| 78 | */ |
| 79 | const clientBackedApi = (client: any) => |
| 80 | makeFakeManagementApi((request) => { |
| 81 | // segments[0] is the "v1" prefix. |
| 82 | const [head, id, tail] = request.pathname |
| 83 | .split("/") |
| 84 | .filter((segment) => segment.length > 0) |
| 85 | .slice(1); |
| 86 | const body = request.bodyJson as any; |
| 87 | const query = Object.fromEntries(new URLSearchParams(request.search)); |
| 88 | const { call, callVoid, list } = dispatchTo(request); |
| 89 | |
| 90 | if (head === "services" && id !== undefined) { |
| 91 | if (tail === "deployments") { |
| 92 | return request.method === "GET" |
| 93 | ? call(client.listAppDeployments, [id, query], list) |
| 94 | : call(client.createAppDeployment, [id, body]); |
| 95 | } |
| 96 | if (tail === "promote") return call(client.promoteApp, [id, body]); |
| 97 | if (tail === "rollback") return call(client.rollbackApp, [id, body]); |
| 98 | if (tail === undefined && request.method === "GET") { |
| 99 | return call(client.getApp, [id]); |
| 100 | } |
| 101 | } |
| 102 | if (head === "deployments" && id !== undefined) { |
| 103 | if (tail === "start") return call(client.startDeployment, [id]); |
| 104 | if (tail === "stop") return callVoid(client.stopDeployment, [id]); |
| 105 | if (request.method === "GET") return call(client.getDeployment, [id]); |
| 106 | if (request.method === "DELETE") { |
| 107 | return callVoid(client.deleteDeployment, [id]); |
| 108 | } |
| 109 | } |
| 110 | return unhandled(request); |
| 111 | }); |
| 112 | |
| 113 | describe( |
| 114 | "Prisma Deployment", |
| 115 | { tags: ["unit", "provider:prisma", "provider:prisma:deployment", "local"] }, |
| 116 | () => { |
| 117 | it.effect("redacts signed upload URLs from transport failures", () => { |
| 118 | const secret = "SIGNED_QUERY_SECRET_SENTINEL"; |
| 119 | const http = HttpClient.make((request) => |
| 120 | Effect.fail(new Error(`transport failed for ${request.url}`) as never), |
| 121 | ); |
| 122 | |
| 123 | return Effect.gen(function* () { |
| 124 | const error = yield* uploadArtifact( |
| 125 | `https://upload.prisma.test/artifact?signature=${secret}`, |
| 126 | new Uint8Array([1]), |
| 127 | "application/octet-stream", |
| 128 | ).pipe(Effect.flip); |
| 129 | |
| 130 | expect(error.message).toContain("transport failed"); |
| 131 | expect(String(error)).not.toContain(secret); |
| 132 | expect(String(error)).not.toContain("upload.prisma.test"); |
| 133 | expect(JSON.stringify(error)).not.toContain(secret); |
| 134 | }).pipe(Effect.provide(Layer.succeed(HttpClient.HttpClient, http))); |
| 135 | }); |
| 136 | |
| 137 | it.effect("rejects final artifacts above the upload byte limit", () => |
| 138 | Effect.gen(function* () { |
| 139 | const error = yield* validateDeploymentArtifactBytes(new Uint8Array(9), 8).pipe( |
| 140 | Effect.flip, |
| 141 | ); |
| 142 | |
| 143 | expect(error.message).toContain("exceeds the 8 byte upload safety limit"); |
| 144 | }), |
| 145 | ); |
| 146 | |
| 147 | it.effect("does not allow callers to raise the upload hard limit", () => |
| 148 | Effect.gen(function* () { |
| 149 | const error = yield* validateDeploymentArtifactBytes( |
| 150 | new Uint8Array(1), |
| 151 | MAX_DEPLOYMENT_ARTIFACT_BYTES + 1, |
| 152 | ).pipe(Effect.flip); |
| 153 | |
| 154 | expect(error.message).toContain("hard limit"); |
| 155 | }), |
| 156 | ); |
| 157 | |
| 158 | it.effect("rejects symbolic-link artifact paths", () => |
| 159 | Effect.gen(function* () { |
| 160 | const fs = yield* FileSystem.FileSystem; |
| 161 | const path = yield* Path.Path; |
| 162 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-artifact-symlink-" }); |
| 163 | const target = path.join(root, "artifact.tar.gz"); |
| 164 | const link = path.join(root, "artifact-link.tar.gz"); |
| 165 | yield* fs.writeFileString(target, "archive"); |
| 166 | yield* fs.symlink(target, link); |
| 167 | |
| 168 | const error = yield* readUploadArtifact({ artifactPath: link, output: "file" }).pipe( |
| 169 | Effect.flip, |
| 170 | ); |
| 171 | |
| 172 | expect(error.message).toContain("symbolic link"); |
| 173 | }).pipe(Effect.provide(PlatformServices)), |
| 174 | ); |
| 175 | |
| 176 | it.effect("refuses to upload a file changed after validation", () => { |
| 177 | const http = HttpClient.make((request) => { |
| 178 | const body = request.body as HttpBody.HttpBody; |
| 179 | const consume = body._tag === "Stream" ? Stream.runDrain(body.stream) : Effect.void; |
| 180 | return consume.pipe( |
| 181 | Effect.mapError( |
| 182 | (cause) => |
| 183 | new HttpClientError.HttpClientError({ |
| 184 | reason: new HttpClientError.TransportError({ |
| 185 | request, |
| 186 | cause, |
| 187 | description: "test request body stream failed", |
| 188 | }), |
| 189 | }), |
| 190 | ), |
| 191 | Effect.as(HttpClientResponse.fromWeb(request, new Response(null))), |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | return Effect.gen(function* () { |
| 196 | const fs = yield* FileSystem.FileSystem; |
| 197 | const path = yield* Path.Path; |
| 198 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-artifact-mutation-" }); |
| 199 | const artifactPath = path.join(root, "artifact.tar.gz"); |
| 200 | yield* fs.writeFileString(artifactPath, "first!!"); |
| 201 | const artifact = yield* readUploadArtifact({ artifactPath, output: "file" }); |
| 202 | yield* fs.writeFileString(artifactPath, "second!"); |
| 203 | |
| 204 | const error = yield* uploadArtifact( |
| 205 | "https://upload.prisma.test/artifact.tar.gz?signature=secret", |
| 206 | artifact!, |
| 207 | "application/gzip", |
| 208 | ).pipe(Effect.flip); |
| 209 | |
| 210 | expect(error.message).toContain("transport failed"); |
| 211 | expect(String(error)).not.toContain("signature=secret"); |
| 212 | }).pipe( |
| 213 | Effect.provide(Layer.succeed(HttpClient.HttpClient, http)), |
| 214 | Effect.provide(PlatformServices), |
| 215 | ); |
| 216 | }); |
| 217 | |
| 218 | it.effect("streams file-backed artifacts with a fixed Content-Length", () => { |
| 219 | let contentLength: string | undefined; |
| 220 | let uploadedBytes = 0; |
| 221 | |
| 222 | return withHttpServer( |
| 223 | (request, response) => { |
| 224 | contentLength = request.headers["content-length"]; |
| 225 | request.on("data", (chunk: Buffer) => { |
| 226 | uploadedBytes += chunk.byteLength; |
| 227 | }); |
| 228 | request.on("end", () => { |
| 229 | response.statusCode = contentLength === undefined ? 411 : 204; |
| 230 | response.end(); |
| 231 | }); |
| 232 | }, |
| 233 | (uploadUrl) => |
| 234 | Effect.gen(function* () { |
| 235 | const fs = yield* FileSystem.FileSystem; |
| 236 | const path = yield* Path.Path; |
| 237 | const root = yield* fs.makeTempDirectory({ |
| 238 | prefix: "alchemy-prisma-fixed-length-upload-", |
| 239 | }); |
| 240 | const artifactPath = path.join(root, "artifact.tar.gz"); |
| 241 | yield* fs.writeFileString(artifactPath, "fixed-length-archive"); |
| 242 | const artifact = yield* readUploadArtifact({ artifactPath, output: "file" }); |
| 243 | |
| 244 | yield* executeArtifactUpload(uploadUrl, artifact!, "application/gzip"); |
| 245 | |
| 246 | expect(contentLength).toBe(String(artifact!.size)); |
| 247 | expect(uploadedBytes).toBe(artifact!.size); |
| 248 | }).pipe( |
| 249 | Effect.provide(PrismaHttpClientLive), |
| 250 | Effect.provide(PlatformServices), |
| 251 | Effect.scoped, |
| 252 | ), |
| 253 | ); |
| 254 | }); |
| 255 | |
| 256 | it.effect("fails when Prisma omits an upload URL for version artifacts", () => { |
| 257 | const calls: Array<[string, unknown?]> = []; |
| 258 | const client = { |
| 259 | createAppDeployment: () => { |
| 260 | calls.push(["createAppDeployment"]); |
| 261 | return Effect.succeed({ |
| 262 | id: "version-1", |
| 263 | type: "deployment" as const, |
| 264 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 265 | foundryVersionId: "foundry-1", |
| 266 | uploadUrl: null, |
| 267 | }); |
| 268 | }, |
| 269 | getDeployment: (id: string) => { |
| 270 | calls.push(["getDeployment", id]); |
| 271 | return Effect.succeed({ |
| 272 | id, |
| 273 | type: "deployment" as const, |
| 274 | serviceId: "service-1", |
| 275 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 276 | foundryVersionId: "foundry-1", |
| 277 | status: "new", |
| 278 | previewDomain: null, |
| 279 | createdAt: "2026-01-01T00:00:00Z", |
| 280 | }); |
| 281 | }, |
| 282 | deleteDeployment: (id: string) => { |
| 283 | calls.push(["deleteDeployment", id]); |
| 284 | return Effect.void; |
| 285 | }, |
| 286 | } as unknown as PrismaManagementClient; |
| 287 | |
| 288 | return Effect.gen(function* () { |
| 289 | const provider = yield* PrismaDeployment.Provider; |
| 290 | const error = yield* provider |
| 291 | .reconcile({ |
| 292 | id: "Version", |
| 293 | fqn: "Version", |
| 294 | instanceId: "00000000000000000000000000000000", |
| 295 | news: { app: "service-1", artifactPath: fixtureArtifactPath }, |
| 296 | olds: undefined, |
| 297 | output: undefined, |
| 298 | session: undefined as never, |
| 299 | bindings: [], |
| 300 | }) |
| 301 | .pipe(Effect.flip); |
| 302 | |
| 303 | expect(error).toBeInstanceOf(Error); |
| 304 | expect(error.message).toContain("did not return an upload URL"); |
| 305 | expect(calls).toContainEqual(["deleteDeployment", "version-1"]); |
| 306 | }).pipe( |
| 307 | Effect.provide(deploymentProviderLive()), |
| 308 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 309 | Effect.provide(clientBackedApi(client).layer), |
| 310 | Effect.provide(PlatformServices), |
| 311 | ); |
| 312 | }); |
| 313 | |
| 314 | it.effect("deletes created deployment when artifact upload fails", () => { |
| 315 | const calls: Array<[string, unknown?]> = []; |
| 316 | const client = { |
| 317 | createAppDeployment: () => { |
| 318 | calls.push(["createAppDeployment"]); |
| 319 | return Effect.succeed({ |
| 320 | id: "version-1", |
| 321 | type: "deployment" as const, |
| 322 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 323 | foundryVersionId: "foundry-1", |
| 324 | uploadUrl: "https://upload.prisma.test/version.tar.gz", |
| 325 | }); |
| 326 | }, |
| 327 | getDeployment: (id: string) => { |
| 328 | calls.push(["getDeployment", id]); |
| 329 | return Effect.succeed({ |
| 330 | id, |
| 331 | type: "deployment" as const, |
| 332 | serviceId: "service-1", |
| 333 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 334 | foundryVersionId: "foundry-1", |
| 335 | status: "new", |
| 336 | previewDomain: null, |
| 337 | createdAt: "2026-01-01T00:00:00Z", |
| 338 | }); |
| 339 | }, |
| 340 | deleteDeployment: (id: string) => { |
| 341 | calls.push(["deleteDeployment", id]); |
| 342 | return Effect.void; |
| 343 | }, |
| 344 | } as unknown as PrismaManagementClient; |
| 345 | const http = HttpClient.make((request) => |
| 346 | Effect.succeed( |
| 347 | HttpClientResponse.fromWeb( |
| 348 | request, |
| 349 | new Response("SIGNED_UPLOAD_SECRET_SENTINEL", { status: 500 }), |
| 350 | ), |
| 351 | ), |
| 352 | ); |
| 353 | |
| 354 | return Effect.gen(function* () { |
| 355 | const provider = yield* PrismaDeployment.Provider; |
| 356 | const error = yield* provider |
| 357 | .reconcile({ |
| 358 | id: "Version", |
| 359 | fqn: "Version", |
| 360 | instanceId: "00000000000000000000000000000000", |
| 361 | news: { app: "service-1", artifactPath: fixtureArtifactPath }, |
| 362 | olds: undefined, |
| 363 | output: undefined, |
| 364 | session: undefined as never, |
| 365 | bindings: [], |
| 366 | }) |
| 367 | .pipe(Effect.flip); |
| 368 | |
| 369 | expect(error).toBeInstanceOf(Error); |
| 370 | expect(error.message).toContain("artifact upload failed"); |
| 371 | expect(error.message).toContain("HTTP 500"); |
| 372 | expect(error.message).toContain("29 bytes"); |
| 373 | expect(error.message).not.toContain("SIGNED_UPLOAD_SECRET_SENTINEL"); |
| 374 | expect(calls).toContainEqual(["deleteDeployment", "version-1"]); |
| 375 | }).pipe( |
| 376 | Effect.provide(deploymentProviderLive()), |
| 377 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 378 | Effect.provide(clientBackedApi(client).layer), |
| 379 | Effect.provide(Layer.succeed(PrismaUploadClient, http)), |
| 380 | Effect.provide(PlatformServices), |
| 381 | ); |
| 382 | }); |
| 383 | |
| 384 | it.effect("preserves reconcile and cleanup failures for an orphaned deployment", () => { |
| 385 | const startError = new PrismaApiError({ |
| 386 | method: "POST", |
| 387 | path: "/v1/deployments/version-1/start", |
| 388 | status: 400, |
| 389 | message: "start failed", |
| 390 | }); |
| 391 | const deleteError = new PrismaApiError({ |
| 392 | method: "DELETE", |
| 393 | path: "/v1/deployments/version-1", |
| 394 | status: 400, |
| 395 | message: "cleanup failed", |
| 396 | }); |
| 397 | const client = { |
| 398 | createAppDeployment: () => |
| 399 | Effect.succeed({ |
| 400 | id: "version-1", |
| 401 | type: "deployment" as const, |
| 402 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 403 | foundryVersionId: "foundry-1", |
| 404 | uploadUrl: null, |
| 405 | }), |
| 406 | getDeployment: (id: string) => |
| 407 | Effect.succeed({ |
| 408 | id, |
| 409 | type: "deployment" as const, |
| 410 | serviceId: "service-1", |
| 411 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 412 | foundryVersionId: "foundry-1", |
| 413 | status: "new", |
| 414 | previewDomain: null, |
| 415 | createdAt: "2026-01-01T00:00:00Z", |
| 416 | }), |
| 417 | startDeployment: () => Effect.fail(startError), |
| 418 | deleteDeployment: () => Effect.fail(deleteError), |
| 419 | } as unknown as PrismaManagementClient; |
| 420 | |
| 421 | return Effect.gen(function* () { |
| 422 | const provider = yield* PrismaDeployment.Provider; |
| 423 | const error = yield* provider |
| 424 | .reconcile({ |
| 425 | id: "Version", |
| 426 | fqn: "Version", |
| 427 | instanceId: "00000000000000000000000000000000", |
| 428 | news: { app: "service-1", skipCodeUpload: true, start: true }, |
| 429 | olds: undefined, |
| 430 | output: undefined, |
| 431 | session: undefined as never, |
| 432 | bindings: [], |
| 433 | }) |
| 434 | .pipe(Effect.flip); |
| 435 | |
| 436 | expect(error).toBeInstanceOf(AggregateError); |
| 437 | expect((error as AggregateError).message).toContain("version-1"); |
| 438 | expect((error as AggregateError).message).toContain("DELETE /v1/deployments/version-1"); |
| 439 | expect((error as AggregateError).errors).toHaveLength(2); |
| 440 | // Over the wire the injected failures decode into the typed errors. |
| 441 | expect(((error as AggregateError).errors[0] as Error).message).toContain("start failed"); |
| 442 | expect(((error as AggregateError).errors[1] as Error).message).toContain("cleanup failed"); |
| 443 | }).pipe( |
| 444 | Effect.provide(deploymentProviderLive()), |
| 445 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 446 | Effect.provide(clientBackedApi(client).layer), |
| 447 | Effect.provide(PlatformServices), |
| 448 | ); |
| 449 | }); |
| 450 | |
| 451 | it.effect("deletes created deployment when start fails", () => { |
| 452 | const calls: Array<[string, unknown?]> = []; |
| 453 | const client = { |
| 454 | createAppDeployment: () => { |
| 455 | calls.push(["createAppDeployment"]); |
| 456 | return Effect.succeed({ |
| 457 | id: "version-1", |
| 458 | type: "deployment" as const, |
| 459 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 460 | foundryVersionId: "foundry-1", |
| 461 | uploadUrl: null, |
| 462 | }); |
| 463 | }, |
| 464 | getDeployment: (id: string) => { |
| 465 | calls.push(["getDeployment", id]); |
| 466 | return Effect.succeed({ |
| 467 | id, |
| 468 | type: "deployment" as const, |
| 469 | serviceId: "service-1", |
| 470 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 471 | foundryVersionId: "foundry-1", |
| 472 | status: "new", |
| 473 | previewDomain: null, |
| 474 | createdAt: "2026-01-01T00:00:00Z", |
| 475 | }); |
| 476 | }, |
| 477 | startDeployment: (id: string) => { |
| 478 | calls.push(["startDeployment", id]); |
| 479 | return Effect.fail( |
| 480 | new PrismaApiError({ |
| 481 | method: "POST", |
| 482 | path: `/v1/deployments/${id}/start`, |
| 483 | status: 400, |
| 484 | message: "start failed", |
| 485 | }), |
| 486 | ); |
| 487 | }, |
| 488 | deleteDeployment: (id: string) => { |
| 489 | calls.push(["deleteDeployment", id]); |
| 490 | return Effect.void; |
| 491 | }, |
| 492 | } as unknown as PrismaManagementClient; |
| 493 | |
| 494 | return Effect.gen(function* () { |
| 495 | const provider = yield* PrismaDeployment.Provider; |
| 496 | const error = yield* provider |
| 497 | .reconcile({ |
| 498 | id: "Version", |
| 499 | fqn: "Version", |
| 500 | instanceId: "00000000000000000000000000000000", |
| 501 | news: { app: "service-1", skipCodeUpload: true, start: true }, |
| 502 | olds: undefined, |
| 503 | output: undefined, |
| 504 | session: undefined as never, |
| 505 | bindings: [], |
| 506 | }) |
| 507 | .pipe(Effect.flip); |
| 508 | |
| 509 | // Over the wire the injected failure decodes into the typed error. |
| 510 | expect(error._tag).toBe("BadRequest"); |
| 511 | expect(error.message).toBe("start failed"); |
| 512 | expect(calls).toContainEqual(["deleteDeployment", "version-1"]); |
| 513 | }).pipe( |
| 514 | Effect.provide(deploymentProviderLive()), |
| 515 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 516 | Effect.provide(clientBackedApi(client).layer), |
| 517 | Effect.provide(PlatformServices), |
| 518 | ); |
| 519 | }); |
| 520 | |
| 521 | it.effect("preserves a deployment when promotion commit state is ambiguous", () => { |
| 522 | const calls: Array<[string, unknown?]> = []; |
| 523 | let status = "new"; |
| 524 | const client = { |
| 525 | createAppDeployment: () => { |
| 526 | calls.push(["createAppDeployment"]); |
| 527 | return Effect.succeed({ |
| 528 | id: "version-1", |
| 529 | type: "deployment" as const, |
| 530 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 531 | foundryVersionId: "foundry-1", |
| 532 | uploadUrl: null, |
| 533 | }); |
| 534 | }, |
| 535 | getDeployment: (id: string) => { |
| 536 | calls.push(["getDeployment", id]); |
| 537 | return Effect.succeed({ |
| 538 | id, |
| 539 | type: "deployment" as const, |
| 540 | serviceId: "service-1", |
| 541 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 542 | foundryVersionId: "foundry-1", |
| 543 | status, |
| 544 | previewDomain: "version-1.preview.prisma.build", |
| 545 | createdAt: "2026-01-01T00:00:00Z", |
| 546 | }); |
| 547 | }, |
| 548 | listAppDeployments: () => |
| 549 | Effect.succeed([ |
| 550 | { |
| 551 | id: "version-1", |
| 552 | type: "deployment" as const, |
| 553 | serviceId: "service-1", |
| 554 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 555 | foundryVersionId: "foundry-1", |
| 556 | createdAt: "2026-01-01T00:00:00Z", |
| 557 | }, |
| 558 | ]), |
| 559 | startDeployment: (id: string) => |
| 560 | Effect.sync(() => { |
| 561 | calls.push(["startDeployment", id]); |
| 562 | status = "running"; |
| 563 | return { previewDomain: "version-1.preview.prisma.build" }; |
| 564 | }), |
| 565 | getApp: (id: string) => { |
| 566 | calls.push(["getApp", id]); |
| 567 | return Effect.succeed({ |
| 568 | id, |
| 569 | type: "app" as const, |
| 570 | url: `https://api.prisma.test/v1/services/${id}`, |
| 571 | name: "api", |
| 572 | region: { id: "us-east-1", name: "US East" }, |
| 573 | projectId: "project-1", |
| 574 | branchId: null, |
| 575 | latestDeploymentId: null, |
| 576 | appEndpointDomain: "api.prisma.build", |
| 577 | createdAt: "2026-01-01T00:00:00Z", |
| 578 | }); |
| 579 | }, |
| 580 | promoteApp: (appId: string, { deploymentId }: { deploymentId: string }) => { |
| 581 | calls.push(["promoteApp", { appId, deploymentId }]); |
| 582 | return Effect.fail( |
| 583 | new PrismaApiError({ |
| 584 | method: "POST", |
| 585 | path: `/v1/services/${appId}/promote`, |
| 586 | status: 400, |
| 587 | message: "promote failed", |
| 588 | }), |
| 589 | ); |
| 590 | }, |
| 591 | rollbackApp: () => |
| 592 | Effect.fail( |
| 593 | new PrismaApiError({ |
| 594 | method: "POST", |
| 595 | path: "/v1/services/service-1/rollback", |
| 596 | status: 400, |
| 597 | message: "promotion recovery failed", |
| 598 | }), |
| 599 | ), |
| 600 | stopDeployment: (id: string) => |
| 601 | Effect.sync(() => { |
| 602 | calls.push(["stopDeployment", id]); |
| 603 | status = "stopped"; |
| 604 | }), |
| 605 | deleteDeployment: (id: string) => { |
| 606 | calls.push(["deleteDeployment", id]); |
| 607 | return Effect.void; |
| 608 | }, |
| 609 | } as unknown as PrismaManagementClient; |
| 610 | |
| 611 | return Effect.gen(function* () { |
| 612 | const provider = yield* PrismaDeployment.Provider; |
| 613 | const error = yield* provider |
| 614 | .reconcile({ |
| 615 | id: "Version", |
| 616 | fqn: "Version", |
| 617 | instanceId: "00000000000000000000000000000000", |
| 618 | news: { app: "service-1", skipCodeUpload: true, promote: true }, |
| 619 | olds: undefined, |
| 620 | output: undefined, |
| 621 | session: undefined as never, |
| 622 | bindings: [], |
| 623 | }) |
| 624 | .pipe(Effect.flip); |
| 625 | |
| 626 | expect(error).toBeInstanceOf(AggregateError); |
| 627 | expect((error as AggregateError).message).toContain("commit state"); |
| 628 | expect(calls).not.toContainEqual(["deleteDeployment", "version-1"]); |
| 629 | }).pipe( |
| 630 | Effect.provide(deploymentProviderLive()), |
| 631 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 632 | Effect.provide(clientBackedApi(client).layer), |
| 633 | Effect.provide(PlatformServices), |
| 634 | ); |
| 635 | }); |
| 636 | |
| 637 | it.effect("uploads version artifact bytes from artifactPath", () => { |
| 638 | let uploaded: { url: string; contentType: string | undefined; bytes: Uint8Array } | undefined; |
| 639 | const client = { |
| 640 | createAppDeployment: (_appId: string, input: unknown) => |
| 641 | Effect.succeed({ |
| 642 | id: "version-1", |
| 643 | type: "deployment" as const, |
| 644 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 645 | foundryVersionId: "foundry-1", |
| 646 | uploadUrl: "https://upload.prisma.test/version.tar.gz", |
| 647 | input, |
| 648 | }), |
| 649 | getDeployment: (id: string) => |
| 650 | Effect.succeed({ |
| 651 | id, |
| 652 | type: "deployment" as const, |
| 653 | serviceId: "service-1", |
| 654 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 655 | foundryVersionId: "foundry-1", |
| 656 | status: "new", |
| 657 | previewDomain: "version-1.preview.prisma.build", |
| 658 | createdAt: "2026-01-01T00:00:00Z", |
| 659 | }), |
| 660 | } as unknown as PrismaManagementClient; |
| 661 | const http = HttpClient.make((request) => |
| 662 | Effect.gen(function* () { |
| 663 | const body = request.body as HttpBody.HttpBody; |
| 664 | const bytes = |
| 665 | body._tag === "Uint8Array" |
| 666 | ? body.body |
| 667 | : body._tag === "Stream" |
| 668 | ? yield* Stream.runCollect(body.stream).pipe( |
| 669 | Effect.orDie, |
| 670 | Effect.map((chunks) => { |
| 671 | const output = new Uint8Array( |
| 672 | chunks.reduce((total, chunk) => total + chunk.byteLength, 0), |
| 673 | ); |
| 674 | let offset = 0; |
| 675 | for (const chunk of chunks) { |
| 676 | output.set(chunk, offset); |
| 677 | offset += chunk.byteLength; |
| 678 | } |
| 679 | return output; |
| 680 | }), |
| 681 | ) |
| 682 | : new Uint8Array(); |
| 683 | uploaded = { |
| 684 | url: request.url, |
| 685 | contentType: |
| 686 | body._tag === "Uint8Array" || body._tag === "Stream" ? body.contentType : undefined, |
| 687 | bytes, |
| 688 | }; |
| 689 | return HttpClientResponse.fromWeb(request, new Response(null)); |
| 690 | }), |
| 691 | ); |
| 692 | |
| 693 | return Effect.gen(function* () { |
| 694 | const fs = yield* FileSystem.FileSystem; |
| 695 | const path = yield* Path.Path; |
| 696 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-version-artifact-" }); |
| 697 | const artifactPath = path.join(root, "version.tar.gz"); |
| 698 | yield* fs.writeFileString(artifactPath, "version-archive"); |
| 699 | |
| 700 | const provider = yield* PrismaDeployment.Provider; |
| 701 | const output = yield* provider.reconcile({ |
| 702 | id: "Version", |
| 703 | fqn: "Version", |
| 704 | instanceId: "00000000000000000000000000000000", |
| 705 | news: { app: "service-1", artifactPath }, |
| 706 | olds: undefined, |
| 707 | output: undefined, |
| 708 | session: undefined as never, |
| 709 | bindings: [], |
| 710 | }); |
| 711 | |
| 712 | expect(output.deploymentId).toBe("version-1"); |
| 713 | expect(output.artifactHash).toBeDefined(); |
| 714 | expect(uploaded?.url).toBe("https://upload.prisma.test/version.tar.gz"); |
| 715 | expect(uploaded?.contentType).toBe("application/octet-stream"); |
| 716 | expect(new TextDecoder().decode(uploaded?.bytes)).toBe("version-archive"); |
| 717 | }).pipe( |
| 718 | Effect.provide(deploymentProviderLive()), |
| 719 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 720 | Effect.provide(clientBackedApi(client).layer), |
| 721 | Effect.provide(Layer.succeed(PrismaUploadClient, http)), |
| 722 | Effect.provide(PlatformServices), |
| 723 | ); |
| 724 | }); |
| 725 | |
| 726 | it.effect("reads a saved deployment through the canonical route", () => { |
| 727 | const calls: Array<[string, unknown?]> = []; |
| 728 | const client = { |
| 729 | listAppDeployments: (appId: string) => |
| 730 | Effect.sync(() => { |
| 731 | calls.push(["listAppDeployments", appId]); |
| 732 | return [ |
| 733 | { |
| 734 | id: "version-1", |
| 735 | type: "deployment" as const, |
| 736 | serviceId: "service-1", |
| 737 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 738 | foundryVersionId: "foundry-1", |
| 739 | createdAt: "2026-01-01T00:00:00Z", |
| 740 | }, |
| 741 | ]; |
| 742 | }), |
| 743 | getDeployment: (id: string) => |
| 744 | Effect.sync(() => { |
| 745 | calls.push(["getDeployment", id]); |
| 746 | return { |
| 747 | id, |
| 748 | type: "deployment" as const, |
| 749 | serviceId: "service-1", |
| 750 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 751 | foundryVersionId: "foundry-1", |
| 752 | status: "stopped", |
| 753 | previewDomain: "version-1.preview.prisma.build", |
| 754 | createdAt: "2026-01-01T00:00:00Z", |
| 755 | }; |
| 756 | }), |
| 757 | } as unknown as PrismaManagementClient; |
| 758 | |
| 759 | return Effect.gen(function* () { |
| 760 | const provider = yield* Provider.findProvider(PrismaDeployment); |
| 761 | const output = yield* provider.read!({ |
| 762 | id: "Version", |
| 763 | fqn: "Version", |
| 764 | instanceId: "00000000000000000000000000000000", |
| 765 | olds: { app: "service-1" }, |
| 766 | output: { |
| 767 | deploymentId: "version-1", |
| 768 | appId: "service-1", |
| 769 | foundryVersionId: "foundry-1", |
| 770 | status: "running", |
| 771 | previewDomain: undefined, |
| 772 | artifactHash: undefined, |
| 773 | appEndpointDomain: undefined, |
| 774 | createdAt: "2026-01-01T00:00:00Z", |
| 775 | }, |
| 776 | }); |
| 777 | |
| 778 | expect(output?.deploymentId).toBe("version-1"); |
| 779 | expect(output?.status).toBe("stopped"); |
| 780 | expect(calls).toEqual([ |
| 781 | ["getDeployment", "version-1"], |
| 782 | ["listAppDeployments", "service-1"], |
| 783 | ]); |
| 784 | }).pipe( |
| 785 | Effect.provide(deploymentProviderLive()), |
| 786 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 787 | Effect.provide(clientBackedApi(client).layer), |
| 788 | Effect.provide(PlatformServices), |
| 789 | ); |
| 790 | }); |
| 791 | |
| 792 | it.effect("does not adopt an unrelated latest deployment on a cold read", () => { |
| 793 | const client = { |
| 794 | listAppDeployments: () => Effect.die("cold reads must not enumerate or adopt deployments"), |
| 795 | getDeployment: () => Effect.die("cold reads have no persisted deployment identity"), |
| 796 | } as unknown as PrismaManagementClient; |
| 797 | |
| 798 | return Effect.gen(function* () { |
| 799 | const provider = yield* Provider.findProvider(PrismaDeployment); |
| 800 | const output = yield* provider.read!({ |
| 801 | id: "Deployment", |
| 802 | fqn: "Deployment", |
| 803 | instanceId: "00000000000000000000000000000000", |
| 804 | olds: { app: "app-1" }, |
| 805 | output: undefined, |
| 806 | }); |
| 807 | |
| 808 | expect(output).toBeUndefined(); |
| 809 | }).pipe( |
| 810 | Effect.provide(deploymentProviderLive()), |
| 811 | Effect.provide(Layer.succeed(PrismaClient, client)), |
| 812 | Effect.provide(clientBackedApi(client).layer), |
| 813 | Effect.provide(PlatformServices), |
| 814 | ); |
| 815 | }); |
| 816 | |
| 817 | it.effect("uses the output App ID when reading a saved deployment", () => { |
| 818 | const calls: Array<[string, unknown?]> = []; |
| 819 | const client = { |
| 820 | listAppDeployments: (appId: string) => |
| 821 | Effect.sync(() => { |
| 822 | calls.push(["listAppDeployments", appId]); |
| 823 | return [ |
| 824 | { |
| 825 | id: "version-1", |
| 826 | type: "deployment" as const, |
| 827 | serviceId: "service-from-output", |
| 828 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 829 | foundryVersionId: "foundry-1", |
| 830 | createdAt: "2026-01-01T00:00:00Z", |
| 831 | }, |
| 832 | ]; |
| 833 | }), |
| 834 | getDeployment: (id: string) => |
| 835 | Effect.sync(() => { |
| 836 | calls.push(["getDeployment", id]); |
| 837 | return { |
| 838 | id, |
| 839 | type: "deployment" as const, |
| 840 | serviceId: "service-from-output", |
| 841 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 842 | foundryVersionId: "foundry-1", |
| 843 | status: "running", |
| 844 | previewDomain: "version-1.preview.prisma.build", |
| 845 | createdAt: "2026-01-01T00:00:00Z", |
| 846 | }; |
| 847 | }), |
| 848 | } as unknown as PrismaManagementClient; |
| 849 | |
| 850 | return Effect.gen(function* () { |
| 851 | const provider = yield* Provider.findProvider(PrismaDeployment); |
| 852 | const output = yield* provider.read!({ |
| 853 | id: "Version", |
| 854 | fqn: "Version", |
| 855 | instanceId: "00000000000000000000000000000000", |
| 856 | olds: { app: "service-from-olds" }, |
| 857 | output: { |
| 858 | deploymentId: "version-1", |
| 859 | appId: "service-from-output", |
| 860 | foundryVersionId: "foundry-1", |
| 861 | status: "stopped", |
| 862 | previewDomain: undefined, |
| 863 | artifactHash: undefined, |
| 864 | appEndpointDomain: undefined, |
| 865 | createdAt: "2026-01-01T00:00:00Z", |
| 866 | }, |
| 867 | }); |
| 868 | |
| 869 | expect(output?.appId).toBe("service-from-output"); |
| 870 | expect(output?.status).toBe("running"); |
| 871 | expect(calls).toEqual([ |
| 872 | ["getDeployment", "version-1"], |
| 873 | ["listAppDeployments", "service-from-output"], |
| 874 | ]); |
| 875 | }).pipe( |
| 876 | Effect.provide(deploymentProviderLive()), |
| 877 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 878 | Effect.provide(clientBackedApi(client).layer), |
| 879 | Effect.provide(PlatformServices), |
| 880 | ); |
| 881 | }); |
| 882 | |
| 883 | it.effect("falls back to the foundry version ID when the saved deployment is gone", () => { |
| 884 | const calls: Array<[string, unknown?]> = []; |
| 885 | const notFound = (path: string) => |
| 886 | new PrismaApiError({ method: "GET", path, status: 404, message: "not found" }); |
| 887 | const client = { |
| 888 | listAppDeployments: (appId: string, query: unknown) => |
| 889 | Effect.sync(() => { |
| 890 | calls.push(["listAppDeployments", { appId, query }]); |
| 891 | return [ |
| 892 | { |
| 893 | id: "version-new", |
| 894 | type: "deployment" as const, |
| 895 | serviceId: "service-from-output", |
| 896 | url: "https://api.prisma.test/v1/deployments/version-new", |
| 897 | foundryVersionId: "foundry-1", |
| 898 | createdAt: "2026-01-01T00:00:00Z", |
| 899 | }, |
| 900 | ]; |
| 901 | }), |
| 902 | getDeployment: (id: string) => |
| 903 | Effect.gen(function* () { |
| 904 | calls.push(["getDeployment", id]); |
| 905 | if (id === "version-old") { |
| 906 | return yield* Effect.fail(notFound(`/v1/deployments/${id}`)); |
| 907 | } |
| 908 | return { |
| 909 | id, |
| 910 | type: "deployment" as const, |
| 911 | serviceId: "service-from-output", |
| 912 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 913 | foundryVersionId: "foundry-1", |
| 914 | status: "running", |
| 915 | previewDomain: "version-new.preview.prisma.build", |
| 916 | createdAt: "2026-01-01T00:00:00Z", |
| 917 | }; |
| 918 | }), |
| 919 | } as unknown as PrismaManagementClient; |
| 920 | |
| 921 | return Effect.gen(function* () { |
| 922 | const provider = yield* Provider.findProvider(PrismaDeployment); |
| 923 | const output = yield* provider.read!({ |
| 924 | id: "Version", |
| 925 | fqn: "Version", |
| 926 | instanceId: "00000000000000000000000000000000", |
| 927 | olds: { app: "service-from-olds" }, |
| 928 | output: { |
| 929 | deploymentId: "version-old", |
| 930 | appId: "service-from-output", |
| 931 | foundryVersionId: "foundry-1", |
| 932 | status: "stopped", |
| 933 | previewDomain: undefined, |
| 934 | artifactHash: undefined, |
| 935 | appEndpointDomain: undefined, |
| 936 | createdAt: "2026-01-01T00:00:00Z", |
| 937 | }, |
| 938 | }); |
| 939 | |
| 940 | expect(output?.deploymentId).toBe("version-new"); |
| 941 | expect(output?.appId).toBe("service-from-output"); |
| 942 | expect(calls).toEqual([ |
| 943 | ["getDeployment", "version-old"], |
| 944 | [ |
| 945 | "listAppDeployments", |
| 946 | // Over the wire, query params arrive as strings. |
| 947 | { appId: "service-from-output", query: { limit: "100" } }, |
| 948 | ], |
| 949 | ["getDeployment", "version-new"], |
| 950 | ]); |
| 951 | }).pipe( |
| 952 | Effect.provide(deploymentProviderLive()), |
| 953 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 954 | Effect.provide(clientBackedApi(client).layer), |
| 955 | Effect.provide(PlatformServices), |
| 956 | ); |
| 957 | }); |
| 958 | |
| 959 | it.effect("rejects ambiguous Foundry-version recovery matches", () => { |
| 960 | const client = { |
| 961 | getDeployment: (id: string) => |
| 962 | Effect.fail( |
| 963 | new PrismaApiError({ |
| 964 | method: "GET", |
| 965 | path: `/v1/deployments/${id}`, |
| 966 | status: 404, |
| 967 | message: "not found", |
| 968 | }), |
| 969 | ), |
| 970 | listAppDeployments: () => |
| 971 | Effect.succeed([ |
| 972 | { |
| 973 | id: "deployment-a", |
| 974 | type: "deployment" as const, |
| 975 | serviceId: "service-1", |
| 976 | url: "https://api.prisma.test/v1/deployments/deployment-a", |
| 977 | foundryVersionId: "foundry-1", |
| 978 | createdAt: "2026-01-01T00:00:00Z", |
| 979 | }, |
| 980 | { |
| 981 | id: "deployment-b", |
| 982 | type: "deployment" as const, |
| 983 | serviceId: "service-1", |
| 984 | url: "https://api.prisma.test/v1/deployments/deployment-b", |
| 985 | foundryVersionId: "foundry-1", |
| 986 | createdAt: "2026-01-01T00:00:01Z", |
| 987 | }, |
| 988 | ]), |
| 989 | } as unknown as PrismaManagementClient; |
| 990 | |
| 991 | return Effect.gen(function* () { |
| 992 | const provider = yield* Provider.findProvider(PrismaDeployment); |
| 993 | const error = yield* provider.read!({ |
| 994 | id: "Deployment", |
| 995 | fqn: "Deployment", |
| 996 | instanceId: "00000000000000000000000000000000", |
| 997 | olds: { app: "app-1" }, |
| 998 | output: { |
| 999 | deploymentId: "deployment-missing", |
| 1000 | appId: "app-1", |
| 1001 | foundryVersionId: "foundry-1", |
| 1002 | status: "new", |
| 1003 | previewDomain: null, |
| 1004 | appEndpointDomain: undefined, |
| 1005 | createdAt: undefined, |
| 1006 | }, |
| 1007 | }).pipe(Effect.flip); |
| 1008 | |
| 1009 | expect(error.message).toContain("ambiguous recovery match"); |
| 1010 | }).pipe( |
| 1011 | Effect.provide(deploymentProviderLive()), |
| 1012 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1013 | Effect.provide(clientBackedApi(client).layer), |
| 1014 | Effect.provide(PlatformServices), |
| 1015 | ); |
| 1016 | }); |
| 1017 | |
| 1018 | it.effect("replaces when artifactPath contents change", () => { |
| 1019 | const client = {} as PrismaManagementClient; |
| 1020 | |
| 1021 | return Effect.gen(function* () { |
| 1022 | const fs = yield* FileSystem.FileSystem; |
| 1023 | const path = yield* Path.Path; |
| 1024 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-version-diff-" }); |
| 1025 | const artifactPath = path.join(root, "version.tar.gz"); |
| 1026 | yield* fs.writeFileString(artifactPath, "new-version"); |
| 1027 | |
| 1028 | const provider = yield* PrismaDeployment.Provider; |
| 1029 | const diff = yield* provider.diff!({ |
| 1030 | id: "Version", |
| 1031 | fqn: "Version", |
| 1032 | instanceId: "00000000000000000000000000000000", |
| 1033 | olds: { app: "service-1", artifactPath }, |
| 1034 | news: { app: "service-1", artifactPath }, |
| 1035 | oldBindings: [], |
| 1036 | newBindings: [], |
| 1037 | output: { |
| 1038 | deploymentId: "version-1", |
| 1039 | appId: "service-1", |
| 1040 | foundryVersionId: "foundry-1", |
| 1041 | status: "new", |
| 1042 | previewDomain: null, |
| 1043 | artifactHash: "old-hash", |
| 1044 | appEndpointDomain: undefined, |
| 1045 | createdAt: "2026-01-01T00:00:00Z", |
| 1046 | }, |
| 1047 | } as never); |
| 1048 | |
| 1049 | expect(diff).toEqual({ action: "replace" }); |
| 1050 | }).pipe( |
| 1051 | Effect.provide(deploymentProviderLive()), |
| 1052 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1053 | Effect.provide(clientBackedApi(client).layer), |
| 1054 | Effect.provide(PlatformServices), |
| 1055 | ); |
| 1056 | }); |
| 1057 | |
| 1058 | it.effect("replaces changed artifacts even when app is unresolved", () => { |
| 1059 | const client = {} as PrismaManagementClient; |
| 1060 | |
| 1061 | return Effect.gen(function* () { |
| 1062 | const fs = yield* FileSystem.FileSystem; |
| 1063 | const path = yield* Path.Path; |
| 1064 | const root = yield* fs.makeTempDirectory({ |
| 1065 | prefix: "alchemy-prisma-version-unresolved-diff-", |
| 1066 | }); |
| 1067 | const artifactPath = path.join(root, "version.tar.gz"); |
| 1068 | yield* fs.writeFileString(artifactPath, "new-version"); |
| 1069 | |
| 1070 | const provider = yield* PrismaDeployment.Provider; |
| 1071 | const diff = yield* provider.diff!({ |
| 1072 | id: "Version", |
| 1073 | fqn: "Version", |
| 1074 | instanceId: "00000000000000000000000000000000", |
| 1075 | olds: { app: "service-1", artifactPath }, |
| 1076 | news: { app: Output.asOutput("service-1"), artifactPath }, |
| 1077 | oldBindings: [], |
| 1078 | newBindings: [], |
| 1079 | output: { |
| 1080 | deploymentId: "version-1", |
| 1081 | appId: "service-1", |
| 1082 | foundryVersionId: "foundry-1", |
| 1083 | status: "new", |
| 1084 | previewDomain: null, |
| 1085 | artifactHash: "old-hash", |
| 1086 | appEndpointDomain: undefined, |
| 1087 | createdAt: "2026-01-01T00:00:00Z", |
| 1088 | }, |
| 1089 | } as never); |
| 1090 | |
| 1091 | expect(diff).toEqual({ action: "replace" }); |
| 1092 | }).pipe( |
| 1093 | Effect.provide(deploymentProviderLive()), |
| 1094 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1095 | Effect.provide(clientBackedApi(client).layer), |
| 1096 | Effect.provide(PlatformServices), |
| 1097 | ); |
| 1098 | }); |
| 1099 | |
| 1100 | it.effect("replaces when the persisted App changes from an unresolved old prop", () => { |
| 1101 | const client = {} as PrismaManagementClient; |
| 1102 | |
| 1103 | return Effect.gen(function* () { |
| 1104 | const artifact = new TextEncoder().encode("same-version\n"); |
| 1105 | const artifactHash = yield* sha256Object({ |
| 1106 | artifact: yield* sha256(artifact), |
| 1107 | contentType: "application/octet-stream", |
| 1108 | }); |
| 1109 | const provider = yield* PrismaDeployment.Provider; |
| 1110 | const diff = yield* provider.diff!({ |
| 1111 | id: "Deployment", |
| 1112 | fqn: "Deployment", |
| 1113 | instanceId: "00000000000000000000000000000000", |
| 1114 | olds: { app: Output.asOutput("app-old"), artifactPath: sameArtifactPath }, |
| 1115 | news: { app: "app-new", artifactPath: sameArtifactPath }, |
| 1116 | oldBindings: [], |
| 1117 | newBindings: [], |
| 1118 | output: { |
| 1119 | deploymentId: "deployment-1", |
| 1120 | appId: "app-old", |
| 1121 | foundryVersionId: "foundry-1", |
| 1122 | status: "new", |
| 1123 | previewDomain: null, |
| 1124 | artifactHash, |
| 1125 | appEndpointDomain: undefined, |
| 1126 | createdAt: "2026-01-01T00:00:00Z", |
| 1127 | }, |
| 1128 | } as never); |
| 1129 | |
| 1130 | expect(diff).toEqual({ action: "replace" }); |
| 1131 | }).pipe( |
| 1132 | Effect.provide(deploymentProviderLive()), |
| 1133 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1134 | Effect.provide(clientBackedApi(client).layer), |
| 1135 | Effect.provide(PlatformServices), |
| 1136 | ); |
| 1137 | }); |
| 1138 | |
| 1139 | it.effect("does not replace when artifact bytes are unchanged", () => { |
| 1140 | const client = {} as PrismaManagementClient; |
| 1141 | |
| 1142 | return Effect.gen(function* () { |
| 1143 | const artifact = new TextEncoder().encode("same-version\n"); |
| 1144 | const artifactHash = yield* sha256Object({ |
| 1145 | artifact: yield* sha256(artifact), |
| 1146 | contentType: "application/octet-stream", |
| 1147 | }); |
| 1148 | |
| 1149 | const provider = yield* PrismaDeployment.Provider; |
| 1150 | const diff = yield* provider.diff!({ |
| 1151 | id: "Version", |
| 1152 | fqn: "Version", |
| 1153 | instanceId: "00000000000000000000000000000000", |
| 1154 | olds: { app: "service-1", artifactPath: sameArtifactPath }, |
| 1155 | news: { app: "service-1", artifactPath: sameArtifactPath }, |
| 1156 | oldBindings: [], |
| 1157 | newBindings: [], |
| 1158 | output: { |
| 1159 | deploymentId: "version-1", |
| 1160 | appId: "service-1", |
| 1161 | foundryVersionId: "foundry-1", |
| 1162 | status: "new", |
| 1163 | previewDomain: null, |
| 1164 | artifactHash, |
| 1165 | appEndpointDomain: undefined, |
| 1166 | createdAt: "2026-01-01T00:00:00Z", |
| 1167 | }, |
| 1168 | } as never); |
| 1169 | |
| 1170 | expect(diff).toBeUndefined(); |
| 1171 | }).pipe( |
| 1172 | Effect.provide(deploymentProviderLive()), |
| 1173 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1174 | Effect.provide(clientBackedApi(client).layer), |
| 1175 | Effect.provide(PlatformServices), |
| 1176 | ); |
| 1177 | }); |
| 1178 | |
| 1179 | it.effect("replays promotion to repair Compute endpoint drift", () => { |
| 1180 | const calls: Array<[string, unknown?]> = []; |
| 1181 | let latestDeploymentId: string | null = null; |
| 1182 | let status = "new"; |
| 1183 | |
| 1184 | const service = () => ({ |
| 1185 | id: "service-1", |
| 1186 | type: "app" as const, |
| 1187 | url: "https://api.prisma.test/v1/services/service-1", |
| 1188 | name: "api", |
| 1189 | region: { id: "us-east-1", name: "US East" }, |
| 1190 | projectId: "project-1", |
| 1191 | branchId: null, |
| 1192 | latestDeploymentId, |
| 1193 | appEndpointDomain: "api.prisma.build", |
| 1194 | createdAt: "2026-01-01T00:00:00Z", |
| 1195 | }); |
| 1196 | |
| 1197 | const client = { |
| 1198 | createAppDeployment: (appId: string, input: unknown) => { |
| 1199 | calls.push(["createAppDeployment", { appId, input }]); |
| 1200 | return Effect.succeed({ |
| 1201 | id: "version-1", |
| 1202 | type: "deployment" as const, |
| 1203 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 1204 | foundryVersionId: "foundry-1", |
| 1205 | uploadUrl: null, |
| 1206 | }); |
| 1207 | }, |
| 1208 | getDeployment: (id: string) => { |
| 1209 | calls.push(["getDeployment", id]); |
| 1210 | return Effect.succeed({ |
| 1211 | id, |
| 1212 | type: "deployment" as const, |
| 1213 | serviceId: "service-1", |
| 1214 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 1215 | foundryVersionId: "foundry-1", |
| 1216 | status, |
| 1217 | previewDomain: "version-1.preview.prisma.build", |
| 1218 | createdAt: "2026-01-01T00:00:00Z", |
| 1219 | }); |
| 1220 | }, |
| 1221 | listAppDeployments: () => |
| 1222 | Effect.succeed([ |
| 1223 | { |
| 1224 | id: "version-1", |
| 1225 | type: "deployment" as const, |
| 1226 | serviceId: "service-1", |
| 1227 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 1228 | foundryVersionId: "foundry-1", |
| 1229 | createdAt: "2026-01-01T00:00:00Z", |
| 1230 | }, |
| 1231 | ]), |
| 1232 | startDeployment: (id: string) => |
| 1233 | Effect.sync(() => { |
| 1234 | calls.push(["startDeployment", id]); |
| 1235 | status = "running"; |
| 1236 | return { previewDomain: "version-1.preview.prisma.build" }; |
| 1237 | }), |
| 1238 | getApp: (id: string) => { |
| 1239 | calls.push(["getApp", id]); |
| 1240 | return Effect.succeed(service()); |
| 1241 | }, |
| 1242 | promoteApp: (appId: string, { deploymentId }: { deploymentId: string }) => |
| 1243 | Effect.sync(() => { |
| 1244 | calls.push(["promoteApp", { appId, deploymentId }]); |
| 1245 | latestDeploymentId = deploymentId; |
| 1246 | return { appEndpointDomain: "api.prisma.build" }; |
| 1247 | }), |
| 1248 | } as unknown as PrismaManagementClient; |
| 1249 | |
| 1250 | const news = { app: "service-1", skipCodeUpload: true, promote: true }; |
| 1251 | |
| 1252 | return Effect.gen(function* () { |
| 1253 | const provider = yield* PrismaDeployment.Provider; |
| 1254 | const first = yield* provider.reconcile({ |
| 1255 | id: "Version", |
| 1256 | fqn: "Version", |
| 1257 | instanceId: "00000000000000000000000000000000", |
| 1258 | news, |
| 1259 | olds: undefined, |
| 1260 | output: undefined, |
| 1261 | session: undefined as never, |
| 1262 | bindings: [], |
| 1263 | }); |
| 1264 | |
| 1265 | // Simulate an out-of-band stop between deployments. Reconcile must |
| 1266 | // restore both running state and App routing even though props match. |
| 1267 | status = "stopped"; |
| 1268 | |
| 1269 | const second = yield* provider.reconcile({ |
| 1270 | id: "Version", |
| 1271 | fqn: "Version", |
| 1272 | instanceId: "00000000000000000000000000000000", |
| 1273 | news, |
| 1274 | olds: news, |
| 1275 | output: first, |
| 1276 | session: undefined as never, |
| 1277 | bindings: [], |
| 1278 | }); |
| 1279 | |
| 1280 | expect(first.deploymentId).toBe("version-1"); |
| 1281 | expect(second.deploymentId).toBe("version-1"); |
| 1282 | expect(second.appEndpointDomain).toBe("api.prisma.build"); |
| 1283 | expect(calls.filter(([name]) => name === "startDeployment")).toEqual([ |
| 1284 | ["startDeployment", "version-1"], |
| 1285 | ["startDeployment", "version-1"], |
| 1286 | ]); |
| 1287 | expect(calls.filter(([name]) => name === "promoteApp")).toEqual([ |
| 1288 | ["promoteApp", { appId: "service-1", deploymentId: "version-1" }], |
| 1289 | ["promoteApp", { appId: "service-1", deploymentId: "version-1" }], |
| 1290 | ]); |
| 1291 | expect(calls.filter(([name]) => name === "createAppDeployment")).toHaveLength(1); |
| 1292 | }).pipe( |
| 1293 | Effect.provide(deploymentProviderLive()), |
| 1294 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1295 | Effect.provide(clientBackedApi(client).layer), |
| 1296 | Effect.provide(PlatformServices), |
| 1297 | ); |
| 1298 | }); |
| 1299 | |
| 1300 | it.effect("replaces a terminal failed deployment before deleting the failed generation", () => { |
| 1301 | const calls: Array<[string, unknown?]> = []; |
| 1302 | let latestDeploymentId: string | null = "version-failed"; |
| 1303 | const deployments = new Map< |
| 1304 | string, |
| 1305 | { |
| 1306 | id: string; |
| 1307 | type: "deployment"; |
| 1308 | serviceId: string; |
| 1309 | url: string; |
| 1310 | foundryVersionId: string; |
| 1311 | status: string; |
| 1312 | previewDomain: string | null; |
| 1313 | createdAt: string; |
| 1314 | } |
| 1315 | >([ |
| 1316 | [ |
| 1317 | "version-failed", |
| 1318 | { |
| 1319 | id: "version-failed", |
| 1320 | type: "deployment", |
| 1321 | serviceId: "service-1", |
| 1322 | url: "https://api.prisma.test/v1/deployments/version-failed", |
| 1323 | foundryVersionId: "foundry-failed", |
| 1324 | status: "failed", |
| 1325 | previewDomain: "version-failed.preview.prisma.build", |
| 1326 | createdAt: "2026-01-01T00:00:00Z", |
| 1327 | }, |
| 1328 | ], |
| 1329 | ]); |
| 1330 | const client = { |
| 1331 | createAppDeployment: (appId: string, input: unknown) => |
| 1332 | Effect.sync(() => { |
| 1333 | calls.push(["createAppDeployment", { appId, input }]); |
| 1334 | deployments.set("version-replacement", { |
| 1335 | id: "version-replacement", |
| 1336 | type: "deployment", |
| 1337 | serviceId: "service-1", |
| 1338 | url: "https://api.prisma.test/v1/deployments/version-replacement", |
| 1339 | foundryVersionId: "foundry-replacement", |
| 1340 | status: "new", |
| 1341 | previewDomain: "version-replacement.preview.prisma.build", |
| 1342 | createdAt: "2026-01-02T00:00:00Z", |
| 1343 | }); |
| 1344 | return { |
| 1345 | id: "version-replacement", |
| 1346 | type: "deployment" as const, |
| 1347 | url: "https://api.prisma.test/v1/deployments/version-replacement", |
| 1348 | foundryVersionId: "foundry-replacement", |
| 1349 | uploadUrl: null, |
| 1350 | }; |
| 1351 | }), |
| 1352 | getDeployment: (id: string) => |
| 1353 | Effect.sync(() => { |
| 1354 | calls.push(["getDeployment", id]); |
| 1355 | return deployments.get(id)!; |
| 1356 | }), |
| 1357 | listAppDeployments: () => |
| 1358 | Effect.succeed( |
| 1359 | Array.from(deployments.values()).map( |
| 1360 | ({ id, type, serviceId, url, foundryVersionId, createdAt }) => ({ |
| 1361 | id, |
| 1362 | type, |
| 1363 | serviceId, |
| 1364 | url, |
| 1365 | foundryVersionId, |
| 1366 | createdAt, |
| 1367 | }), |
| 1368 | ), |
| 1369 | ), |
| 1370 | startDeployment: (id: string) => |
| 1371 | Effect.sync(() => { |
| 1372 | calls.push(["startDeployment", id]); |
| 1373 | deployments.get(id)!.status = "running"; |
| 1374 | return { previewDomain: deployments.get(id)!.previewDomain! }; |
| 1375 | }), |
| 1376 | promoteApp: (appId: string, { deploymentId }: { deploymentId: string }) => |
| 1377 | Effect.sync(() => { |
| 1378 | calls.push(["promoteApp", { appId, deploymentId }]); |
| 1379 | expect(deployments.has("version-failed")).toBe(true); |
| 1380 | latestDeploymentId = deploymentId; |
| 1381 | return { appEndpointDomain: "api.prisma.build", reassignedDomains: 0 }; |
| 1382 | }), |
| 1383 | getApp: (appId: string) => |
| 1384 | Effect.succeed({ |
| 1385 | id: appId, |
| 1386 | type: "app" as const, |
| 1387 | url: `https://api.prisma.test/v1/services/${appId}`, |
| 1388 | name: "api", |
| 1389 | region: { id: "us-east-1", name: "US East" }, |
| 1390 | projectId: "project-1", |
| 1391 | branchId: "branch-main", |
| 1392 | latestDeploymentId, |
| 1393 | appEndpointDomain: "api.prisma.build", |
| 1394 | createdAt: "2026-01-01T00:00:00Z", |
| 1395 | }), |
| 1396 | deleteDeployment: (id: string) => |
| 1397 | Effect.sync(() => { |
| 1398 | calls.push(["deleteDeployment", id]); |
| 1399 | deployments.delete(id); |
| 1400 | }), |
| 1401 | } as unknown as PrismaManagementClient; |
| 1402 | const news = { app: "service-1", skipCodeUpload: true, promote: true } as const; |
| 1403 | const failedOutput: PrismaDeployment["Attributes"] = { |
| 1404 | deploymentId: "version-failed", |
| 1405 | appId: "service-1", |
| 1406 | foundryVersionId: "foundry-failed", |
| 1407 | status: "failed", |
| 1408 | previewDomain: "version-failed.preview.prisma.build", |
| 1409 | artifactHash: undefined, |
| 1410 | appEndpointDomain: "api.prisma.build", |
| 1411 | createdAt: "2026-01-01T00:00:00Z", |
| 1412 | }; |
| 1413 | |
| 1414 | return Effect.gen(function* () { |
| 1415 | const provider = yield* PrismaDeployment.Provider; |
| 1416 | const action = yield* provider.diff!({ |
| 1417 | id: "Version", |
| 1418 | fqn: "Version", |
| 1419 | instanceId: "00000000000000000000000000000000", |
| 1420 | olds: news, |
| 1421 | news, |
| 1422 | oldBindings: [], |
| 1423 | newBindings: [], |
| 1424 | output: failedOutput, |
| 1425 | } as never); |
| 1426 | expect(action).toEqual({ action: "replace" }); |
| 1427 | |
| 1428 | // The engine executes replacement create-before-delete. Reconcile the |
| 1429 | // replacement to its requested running/promoted state before invoking |
| 1430 | // delete for the failed generation. |
| 1431 | const replacement = yield* provider.reconcile({ |
| 1432 | id: "Version", |
| 1433 | fqn: "Version", |
| 1434 | instanceId: "11111111111111111111111111111111", |
| 1435 | news, |
| 1436 | olds: undefined, |
| 1437 | output: undefined, |
| 1438 | session: undefined as never, |
| 1439 | bindings: [], |
| 1440 | }); |
| 1441 | expect(replacement.deploymentId).toBe("version-replacement"); |
| 1442 | expect(replacement.status).toBe("running"); |
| 1443 | expect(deployments.has("version-failed")).toBe(true); |
| 1444 | |
| 1445 | yield* provider.delete({ |
| 1446 | id: "Version", |
| 1447 | fqn: "Version", |
| 1448 | instanceId: "00000000000000000000000000000000", |
| 1449 | olds: news, |
| 1450 | output: failedOutput, |
| 1451 | session: undefined as never, |
| 1452 | bindings: [], |
| 1453 | }); |
| 1454 | |
| 1455 | const createIndex = calls.findIndex(([operation]) => operation === "createAppDeployment"); |
| 1456 | const promoteIndex = calls.findIndex(([operation]) => operation === "promoteApp"); |
| 1457 | const deleteIndex = calls.findIndex( |
| 1458 | ([operation, id]) => operation === "deleteDeployment" && id === "version-failed", |
| 1459 | ); |
| 1460 | expect(createIndex).toBeGreaterThanOrEqual(0); |
| 1461 | expect(createIndex).toBeLessThan(promoteIndex); |
| 1462 | expect(promoteIndex).toBeLessThan(deleteIndex); |
| 1463 | expect(deployments.has("version-failed")).toBe(false); |
| 1464 | expect(deployments.get("version-replacement")?.status).toBe("running"); |
| 1465 | expect(calls).not.toContainEqual(["startDeployment", "version-failed"]); |
| 1466 | }).pipe( |
| 1467 | Effect.provide(deploymentProviderLive()), |
| 1468 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1469 | Effect.provide(clientBackedApi(client).layer), |
| 1470 | Effect.provide(PlatformServices), |
| 1471 | ); |
| 1472 | }); |
| 1473 | |
| 1474 | it.live("reports a deployment that is still stopping as a delete in progress", () => { |
| 1475 | const calls: Array<[string, string]> = []; |
| 1476 | let status = "running"; |
| 1477 | // The stop wait reads the wall clock, so jump it instead of sleeping two minutes. |
| 1478 | const realNow = Date.now; |
| 1479 | let skippedMs = 0; |
| 1480 | const client = { |
| 1481 | getDeployment: (id: string) => { |
| 1482 | calls.push(["getDeployment", id]); |
| 1483 | if (status === "stopping") skippedMs += 61_000; |
| 1484 | return Effect.succeed({ |
| 1485 | id, |
| 1486 | type: "deployment" as const, |
| 1487 | serviceId: "service-1", |
| 1488 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 1489 | foundryVersionId: "foundry-1", |
| 1490 | status, |
| 1491 | previewDomain: null, |
| 1492 | createdAt: "2026-01-01T00:00:00Z", |
| 1493 | }); |
| 1494 | }, |
| 1495 | listAppDeployments: () => |
| 1496 | Effect.succeed([ |
| 1497 | { |
| 1498 | id: "version-old", |
| 1499 | type: "deployment" as const, |
| 1500 | serviceId: "service-1", |
| 1501 | url: "https://api.prisma.test/v1/deployments/version-old", |
| 1502 | foundryVersionId: "foundry-1", |
| 1503 | createdAt: "2026-01-01T00:00:00Z", |
| 1504 | }, |
| 1505 | ]), |
| 1506 | stopDeployment: (id: string) => { |
| 1507 | calls.push(["stopDeployment", id]); |
| 1508 | status = "stopping"; |
| 1509 | return Effect.void; |
| 1510 | }, |
| 1511 | deleteDeployment: (id: string) => { |
| 1512 | calls.push(["deleteDeployment", id]); |
| 1513 | return Effect.void; |
| 1514 | }, |
| 1515 | } as unknown as PrismaManagementClient; |
| 1516 | |
| 1517 | return Effect.gen(function* () { |
| 1518 | Date.now = () => realNow() + skippedMs; |
| 1519 | const provider = yield* PrismaDeployment.Provider; |
| 1520 | const error = yield* provider |
| 1521 | .delete({ |
| 1522 | id: "Version", |
| 1523 | fqn: "Version", |
| 1524 | instanceId: "00000000000000000000000000000000", |
| 1525 | olds: { app: "service-1", skipCodeUpload: true }, |
| 1526 | output: { |
| 1527 | deploymentId: "version-old", |
| 1528 | appId: "service-1", |
| 1529 | foundryVersionId: "foundry-1", |
| 1530 | status: "running", |
| 1531 | previewDomain: undefined, |
| 1532 | artifactHash: undefined, |
| 1533 | triggersHash: undefined, |
| 1534 | appEndpointDomain: undefined, |
| 1535 | createdAt: "2026-01-01T00:00:00Z", |
| 1536 | }, |
| 1537 | session: undefined as never, |
| 1538 | bindings: [], |
| 1539 | }) |
| 1540 | .pipe(Effect.flip); |
| 1541 | |
| 1542 | expect(error).toBeInstanceOf(Provider.DeleteInProgress); |
| 1543 | expect(error.message).toContain("last status: 'stopping'"); |
| 1544 | expect(calls).toContainEqual(["stopDeployment", "version-old"]); |
| 1545 | expect(calls).not.toContainEqual(["deleteDeployment", "version-old"]); |
| 1546 | }).pipe( |
| 1547 | Effect.ensuring( |
| 1548 | Effect.sync(() => { |
| 1549 | Date.now = realNow; |
| 1550 | }), |
| 1551 | ), |
| 1552 | Effect.provide(deploymentProviderLive()), |
| 1553 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1554 | Effect.provide(clientBackedApi(client).layer), |
| 1555 | Effect.provide(PlatformServices), |
| 1556 | ); |
| 1557 | }); |
| 1558 | |
| 1559 | it.effect("rejects promotion when start is explicitly disabled", () => { |
| 1560 | const client = {} as PrismaManagementClient; |
| 1561 | |
| 1562 | return Effect.gen(function* () { |
| 1563 | const provider = yield* PrismaDeployment.Provider; |
| 1564 | const error = yield* provider |
| 1565 | .reconcile({ |
| 1566 | id: "Version", |
| 1567 | fqn: "Version", |
| 1568 | instanceId: "00000000000000000000000000000000", |
| 1569 | news: { app: "service-1", skipCodeUpload: true, start: false, promote: true }, |
| 1570 | olds: undefined, |
| 1571 | output: undefined, |
| 1572 | session: undefined as never, |
| 1573 | bindings: [], |
| 1574 | }) |
| 1575 | .pipe(Effect.flip); |
| 1576 | |
| 1577 | expect(error).toBeInstanceOf(Error); |
| 1578 | expect(error.message).toContain("promote cannot be combined with start: false"); |
| 1579 | }).pipe( |
| 1580 | Effect.provide(deploymentProviderLive()), |
| 1581 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1582 | Effect.provide(clientBackedApi(client).layer), |
| 1583 | Effect.provide(PlatformServices), |
| 1584 | ); |
| 1585 | }); |
| 1586 | |
| 1587 | it.effect("rejects invalid deployment HTTP ports", () => { |
| 1588 | const client = {} as PrismaManagementClient; |
| 1589 | |
| 1590 | return Effect.gen(function* () { |
| 1591 | const provider = yield* PrismaDeployment.Provider; |
| 1592 | for (const http of [0, 65_536, 1.5]) { |
| 1593 | const error = yield* provider |
| 1594 | .reconcile({ |
| 1595 | id: "Deployment", |
| 1596 | fqn: "Deployment", |
| 1597 | instanceId: "00000000000000000000000000000000", |
| 1598 | news: { app: "app-1", skipCodeUpload: true, portMapping: { http } }, |
| 1599 | olds: undefined, |
| 1600 | output: undefined, |
| 1601 | session: undefined as never, |
| 1602 | bindings: [], |
| 1603 | }) |
| 1604 | .pipe(Effect.flip); |
| 1605 | expect(error.message).toContain( |
| 1606 | "portMapping.http must be an integer between 1 and 65535", |
| 1607 | ); |
| 1608 | } |
| 1609 | }).pipe( |
| 1610 | Effect.provide(deploymentProviderLive()), |
| 1611 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1612 | Effect.provide(clientBackedApi(client).layer), |
| 1613 | Effect.provide(PlatformServices), |
| 1614 | ); |
| 1615 | }); |
| 1616 | |
| 1617 | it.effect("allows null HTTP port to request the Foundry 8080 default", () => { |
| 1618 | const calls: Array<[string, unknown?]> = []; |
| 1619 | const client = { |
| 1620 | createAppDeployment: (appId: string, input: unknown) => |
| 1621 | Effect.sync(() => { |
| 1622 | calls.push(["createAppDeployment", { appId, input }]); |
| 1623 | return { |
| 1624 | id: "deployment-1", |
| 1625 | type: "deployment" as const, |
| 1626 | url: "https://api.prisma.test/v1/deployments/deployment-1", |
| 1627 | foundryVersionId: "foundry-1", |
| 1628 | uploadUrl: null, |
| 1629 | }; |
| 1630 | }), |
| 1631 | getDeployment: () => |
| 1632 | Effect.succeed({ |
| 1633 | id: "deployment-1", |
| 1634 | type: "deployment" as const, |
| 1635 | serviceId: "service-1", |
| 1636 | url: "https://api.prisma.test/v1/deployments/deployment-1", |
| 1637 | foundryVersionId: "foundry-1", |
| 1638 | status: "new", |
| 1639 | previewDomain: null, |
| 1640 | createdAt: "2026-01-01T00:00:00Z", |
| 1641 | }), |
| 1642 | } as unknown as PrismaManagementClient; |
| 1643 | |
| 1644 | return Effect.gen(function* () { |
| 1645 | const provider = yield* PrismaDeployment.Provider; |
| 1646 | const output = yield* provider.reconcile({ |
| 1647 | id: "Deployment", |
| 1648 | fqn: "Deployment", |
| 1649 | instanceId: "00000000000000000000000000000000", |
| 1650 | news: { app: "app-1", skipCodeUpload: true, portMapping: { http: null } }, |
| 1651 | olds: undefined, |
| 1652 | output: undefined, |
| 1653 | session: undefined as never, |
| 1654 | bindings: [], |
| 1655 | }); |
| 1656 | |
| 1657 | expect(output.deploymentId).toBe("deployment-1"); |
| 1658 | expect(calls).toEqual([ |
| 1659 | [ |
| 1660 | "createAppDeployment", |
| 1661 | { appId: "app-1", input: { portMapping: { http: null }, skipCodeUpload: true } }, |
| 1662 | ], |
| 1663 | ]); |
| 1664 | }).pipe( |
| 1665 | Effect.provide(deploymentProviderLive()), |
| 1666 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1667 | Effect.provide(clientBackedApi(client).layer), |
| 1668 | Effect.provide(PlatformServices), |
| 1669 | ); |
| 1670 | }); |
| 1671 | |
| 1672 | it.effect("forces reconcile while start or promotion is asserted", () => { |
| 1673 | const client = {} as PrismaManagementClient; |
| 1674 | |
| 1675 | return Effect.gen(function* () { |
| 1676 | const provider = yield* PrismaDeployment.Provider; |
| 1677 | const base = { |
| 1678 | id: "Deployment", |
| 1679 | fqn: "Deployment", |
| 1680 | instanceId: "00000000000000000000000000000000", |
| 1681 | oldBindings: [], |
| 1682 | newBindings: [], |
| 1683 | output: { |
| 1684 | deploymentId: "deployment-1", |
| 1685 | appId: "app-1", |
| 1686 | foundryVersionId: "foundry-1", |
| 1687 | status: "running", |
| 1688 | previewDomain: null, |
| 1689 | appEndpointDomain: undefined, |
| 1690 | createdAt: undefined, |
| 1691 | }, |
| 1692 | }; |
| 1693 | const start = yield* provider.diff!({ |
| 1694 | ...base, |
| 1695 | olds: { app: "app-1", skipCodeUpload: true, start: true }, |
| 1696 | news: { app: "app-1", skipCodeUpload: true, start: true }, |
| 1697 | } as never); |
| 1698 | const promote = yield* provider.diff!({ |
| 1699 | ...base, |
| 1700 | olds: { app: "app-1", skipCodeUpload: true, promote: true }, |
| 1701 | news: { app: "app-1", skipCodeUpload: true, promote: true }, |
| 1702 | } as never); |
| 1703 | |
| 1704 | expect(start).toEqual({ action: "update" }); |
| 1705 | expect(promote).toEqual({ action: "update" }); |
| 1706 | }).pipe( |
| 1707 | Effect.provide(deploymentProviderLive()), |
| 1708 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1709 | Effect.provide(clientBackedApi(client).layer), |
| 1710 | Effect.provide(PlatformServices), |
| 1711 | ); |
| 1712 | }); |
| 1713 | |
| 1714 | it.effect("rejects a deployment with no artifact source", () => { |
| 1715 | const client = {} as PrismaManagementClient; |
| 1716 | |
| 1717 | return Effect.gen(function* () { |
| 1718 | const provider = yield* PrismaDeployment.Provider; |
| 1719 | const error = yield* provider |
| 1720 | .reconcile({ |
| 1721 | id: "Deployment", |
| 1722 | fqn: "Deployment", |
| 1723 | instanceId: "00000000000000000000000000000000", |
| 1724 | news: { app: "app-1" }, |
| 1725 | olds: undefined, |
| 1726 | output: undefined, |
| 1727 | session: undefined as never, |
| 1728 | bindings: [], |
| 1729 | }) |
| 1730 | .pipe(Effect.flip); |
| 1731 | |
| 1732 | expect(error.message).toContain("requires artifactPath or skipCodeUpload: true"); |
| 1733 | }).pipe( |
| 1734 | Effect.provide(deploymentProviderLive()), |
| 1735 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1736 | Effect.provide(clientBackedApi(client).layer), |
| 1737 | Effect.provide(PlatformServices), |
| 1738 | ); |
| 1739 | }); |
| 1740 | |
| 1741 | it.effect("starts a deployment through the canonical route", () => { |
| 1742 | const calls: Array<[string, unknown?]> = []; |
| 1743 | let status = "new"; |
| 1744 | const client = { |
| 1745 | createAppDeployment: (appId: string, input: unknown) => { |
| 1746 | calls.push(["createAppDeployment", { appId, input }]); |
| 1747 | return Effect.succeed({ |
| 1748 | id: "version-1", |
| 1749 | type: "deployment" as const, |
| 1750 | url: "https://api.prisma.test/v1/deployments/version-1", |
| 1751 | foundryVersionId: "foundry-1", |
| 1752 | uploadUrl: null, |
| 1753 | }); |
| 1754 | }, |
| 1755 | getDeployment: (id: string) => { |
| 1756 | calls.push(["getDeployment", id]); |
| 1757 | return Effect.succeed({ |
| 1758 | id, |
| 1759 | type: "deployment" as const, |
| 1760 | serviceId: "service-1", |
| 1761 | url: `https://api.prisma.test/v1/deployments/${id}`, |
| 1762 | foundryVersionId: "foundry-1", |
| 1763 | status, |
| 1764 | previewDomain: "version-1.preview.prisma.build", |
| 1765 | createdAt: "2026-01-01T00:00:00Z", |
| 1766 | }); |
| 1767 | }, |
| 1768 | startDeployment: (id: string) => |
| 1769 | Effect.sync(() => { |
| 1770 | calls.push(["startDeployment", id]); |
| 1771 | status = "running"; |
| 1772 | return { previewDomain: "version-1.preview.prisma.build" }; |
| 1773 | }), |
| 1774 | } as unknown as PrismaManagementClient; |
| 1775 | |
| 1776 | return Effect.gen(function* () { |
| 1777 | const provider = yield* PrismaDeployment.Provider; |
| 1778 | const output = yield* provider.reconcile({ |
| 1779 | id: "Version", |
| 1780 | fqn: "Version", |
| 1781 | instanceId: "00000000000000000000000000000000", |
| 1782 | news: { app: "service-1", skipCodeUpload: true, start: true }, |
| 1783 | olds: undefined, |
| 1784 | output: undefined, |
| 1785 | session: undefined as never, |
| 1786 | bindings: [], |
| 1787 | }); |
| 1788 | |
| 1789 | expect(output.status).toBe("running"); |
| 1790 | expect(calls).toEqual([ |
| 1791 | [ |
| 1792 | "createAppDeployment", |
| 1793 | { appId: "service-1", input: { portMapping: undefined, skipCodeUpload: true } }, |
| 1794 | ], |
| 1795 | ["getDeployment", "version-1"], |
| 1796 | ["startDeployment", "version-1"], |
| 1797 | ["getDeployment", "version-1"], |
| 1798 | ]); |
| 1799 | }).pipe( |
| 1800 | Effect.provide(deploymentProviderLive()), |
| 1801 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1802 | Effect.provide(clientBackedApi(client).layer), |
| 1803 | Effect.provide(PlatformServices), |
| 1804 | ); |
| 1805 | }); |
| 1806 | |
| 1807 | it.effect("tails Deployment logs through the provider", () => |
| 1808 | withWebSocketServer((server) => |
| 1809 | Effect.gen(function* () { |
| 1810 | const url = yield* listenUrl(server); |
| 1811 | let authorization: string | undefined; |
| 1812 | let requestUrl: string | undefined; |
| 1813 | |
| 1814 | server.on("connection", (socket, request) => { |
| 1815 | authorization = request.headers.authorization; |
| 1816 | requestUrl = request.url; |
| 1817 | socket.send( |
| 1818 | JSON.stringify({ |
| 1819 | type: "log", |
| 1820 | text: "direct version log", |
| 1821 | byteStart: 0, |
| 1822 | byteEnd: 18, |
| 1823 | }), |
| 1824 | ); |
| 1825 | socket.send( |
| 1826 | JSON.stringify({ |
| 1827 | type: "terminal", |
| 1828 | kind: "end", |
| 1829 | code: "vm_stopped", |
| 1830 | message: "done", |
| 1831 | retryable: false, |
| 1832 | cursor: null, |
| 1833 | }), |
| 1834 | ); |
| 1835 | }); |
| 1836 | |
| 1837 | const provider = yield* Provider.findProvider(PrismaDeployment).pipe( |
| 1838 | Effect.provide(deploymentProviderLive()), |
| 1839 | Effect.provide(makeFakeManagementApi(unhandled).layer), |
| 1840 | Effect.provide(PlatformServices), |
| 1841 | ); |
| 1842 | const lines = yield* provider.tail!({ |
| 1843 | id: "Deployment", |
| 1844 | fqn: "Deployment", |
| 1845 | instanceId: "00000000000000000000000000000000", |
| 1846 | props: { app: "service-1" }, |
| 1847 | output: { |
| 1848 | deploymentId: "version-1", |
| 1849 | appId: "service-1", |
| 1850 | foundryVersionId: "foundry-1", |
| 1851 | status: "running", |
| 1852 | previewDomain: "version-1.preview.prisma.build", |
| 1853 | appEndpointDomain: undefined, |
| 1854 | createdAt: "2026-01-01T00:00:00Z", |
| 1855 | }, |
| 1856 | }).pipe( |
| 1857 | // The carved-out logs client resolves the distilled Credentials |
| 1858 | // service directly: the test WebSocket server is the API base. |
| 1859 | Stream.provideService( |
| 1860 | Credentials, |
| 1861 | Effect.succeed({ apiToken: Redacted.make("version-token"), apiBaseUrl: url }), |
| 1862 | ), |
| 1863 | Stream.runCollect, |
| 1864 | ); |
| 1865 | |
| 1866 | expect(lines.map((line) => line.message)).toEqual(["direct version log"]); |
| 1867 | expect(authorization).toBe("Bearer version-token"); |
| 1868 | expect(requestUrl).toBe("/v1/deployments/version-1/logs"); |
| 1869 | }).pipe(Effect.provide(FetchHttpClient.layer)), |
| 1870 | ), |
| 1871 | ); |
| 1872 | it.effect("records a redacted triggers fingerprint and replaces when a value changes", () => { |
| 1873 | const secret = "REDEPLOY_SECRET_SENTINEL"; |
| 1874 | const client = redeployClient(); |
| 1875 | |
| 1876 | return Effect.gen(function* () { |
| 1877 | const provider = yield* PrismaDeployment.Provider; |
| 1878 | const output = yield* provider.reconcile({ |
| 1879 | id: "Deployment", |
| 1880 | fqn: "Deployment", |
| 1881 | instanceId: "00000000000000000000000000000000", |
| 1882 | news: { |
| 1883 | app: "app-1", |
| 1884 | skipCodeUpload: true, |
| 1885 | triggers: { DATABASE_URL: Redacted.make(secret) }, |
| 1886 | }, |
| 1887 | olds: undefined, |
| 1888 | output: undefined, |
| 1889 | session: undefined as never, |
| 1890 | bindings: [], |
| 1891 | }); |
| 1892 | |
| 1893 | expect(Redacted.isRedacted(output.triggersHash!)).toBe(true); |
| 1894 | const fingerprint = Redacted.value(output.triggersHash!); |
| 1895 | expect(fingerprint).not.toBe(secret); |
| 1896 | expect(fingerprint).not.toContain(secret); |
| 1897 | // encodeState unwraps Redacted, so this is what a state store writes. |
| 1898 | expect(JSON.stringify(encodeState(output))).not.toContain(secret); |
| 1899 | |
| 1900 | const unchanged = yield* provider.diff!({ |
| 1901 | ...triggersDiffBase(output.triggersHash), |
| 1902 | olds: { |
| 1903 | app: "app-1", |
| 1904 | skipCodeUpload: true, |
| 1905 | triggers: { DATABASE_URL: Redacted.make(secret) }, |
| 1906 | }, |
| 1907 | news: { |
| 1908 | app: "app-1", |
| 1909 | skipCodeUpload: true, |
| 1910 | triggers: { DATABASE_URL: Redacted.make(secret) }, |
| 1911 | }, |
| 1912 | } as never); |
| 1913 | const changed = yield* provider.diff!({ |
| 1914 | ...triggersDiffBase(output.triggersHash), |
| 1915 | olds: { |
| 1916 | app: "app-1", |
| 1917 | skipCodeUpload: true, |
| 1918 | triggers: { DATABASE_URL: Redacted.make(secret) }, |
| 1919 | }, |
| 1920 | news: { |
| 1921 | app: "app-1", |
| 1922 | skipCodeUpload: true, |
| 1923 | triggers: { DATABASE_URL: Redacted.make(`${secret}-rotated`) }, |
| 1924 | }, |
| 1925 | } as never); |
| 1926 | |
| 1927 | expect(unchanged).toBeUndefined(); |
| 1928 | expect(changed).toEqual({ action: "replace" }); |
| 1929 | }).pipe( |
| 1930 | Effect.provide(deploymentProviderLive()), |
| 1931 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1932 | Effect.provide(clientBackedApi(client).layer), |
| 1933 | Effect.provide(PlatformServices), |
| 1934 | ); |
| 1935 | }); |
| 1936 | |
| 1937 | it.effect("replaces when a plain triggers value changes", () => { |
| 1938 | const client = {} as PrismaManagementClient; |
| 1939 | |
| 1940 | return Effect.gen(function* () { |
| 1941 | const provider = yield* PrismaDeployment.Provider; |
| 1942 | const diff = yield* provider.diff!({ |
| 1943 | ...triggersDiffBase(Redacted.make("recorded-fingerprint")), |
| 1944 | olds: { app: "app-1", skipCodeUpload: true, triggers: { FEATURE_FLAG: "off" } }, |
| 1945 | news: { app: "app-1", skipCodeUpload: true, triggers: { FEATURE_FLAG: "on" } }, |
| 1946 | } as never); |
| 1947 | |
| 1948 | expect(diff).toEqual({ action: "replace" }); |
| 1949 | }).pipe( |
| 1950 | Effect.provide(deploymentProviderLive()), |
| 1951 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1952 | Effect.provide(clientBackedApi(client).layer), |
| 1953 | Effect.provide(PlatformServices), |
| 1954 | ); |
| 1955 | }); |
| 1956 | |
| 1957 | it.effect("keeps a deployment recorded before triggers existed when nothing changed", () => { |
| 1958 | const client = {} as PrismaManagementClient; |
| 1959 | |
| 1960 | return Effect.gen(function* () { |
| 1961 | const provider = yield* PrismaDeployment.Provider; |
| 1962 | const diff = yield* provider.diff!({ |
| 1963 | ...triggersDiffBase(undefined), |
| 1964 | olds: { app: "app-1", skipCodeUpload: true, triggers: { FEATURE_FLAG: "on" } }, |
| 1965 | news: { app: "app-1", skipCodeUpload: true, triggers: { FEATURE_FLAG: "on" } }, |
| 1966 | } as never); |
| 1967 | |
| 1968 | expect(diff).toBeUndefined(); |
| 1969 | }).pipe( |
| 1970 | Effect.provide(deploymentProviderLive()), |
| 1971 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 1972 | Effect.provide(clientBackedApi(client).layer), |
| 1973 | Effect.provide(PlatformServices), |
| 1974 | ); |
| 1975 | }); |
| 1976 | |
| 1977 | it.effect("replaces when triggers cannot be resolved while planning", () => { |
| 1978 | const client = {} as PrismaManagementClient; |
| 1979 | |
| 1980 | return Effect.gen(function* () { |
| 1981 | const provider = yield* PrismaDeployment.Provider; |
| 1982 | const recorded = yield* provider.diff!({ |
| 1983 | ...triggersDiffBase(Redacted.make("recorded-fingerprint")), |
| 1984 | olds: { |
| 1985 | app: "app-1", |
| 1986 | skipCodeUpload: true, |
| 1987 | triggers: { DATABASE_URL: "postgres://old" }, |
| 1988 | }, |
| 1989 | news: { |
| 1990 | app: "app-1", |
| 1991 | skipCodeUpload: true, |
| 1992 | triggers: { DATABASE_URL: Output.asOutput("postgres://new") }, |
| 1993 | }, |
| 1994 | } as never); |
| 1995 | const neverRecorded = yield* provider.diff!({ |
| 1996 | ...triggersDiffBase(undefined), |
| 1997 | olds: { |
| 1998 | app: "app-1", |
| 1999 | skipCodeUpload: true, |
| 2000 | triggers: { DATABASE_URL: "postgres://old" }, |
| 2001 | }, |
| 2002 | news: { |
| 2003 | app: "app-1", |
| 2004 | skipCodeUpload: true, |
| 2005 | triggers: { DATABASE_URL: Output.asOutput("postgres://new") }, |
| 2006 | }, |
| 2007 | } as never); |
| 2008 | |
| 2009 | expect(recorded).toEqual({ action: "replace" }); |
| 2010 | expect(neverRecorded).toBeUndefined(); |
| 2011 | }).pipe( |
| 2012 | Effect.provide(deploymentProviderLive()), |
| 2013 | Effect.provide(Layer.succeed(PrismaClient, currentClient(client))), |
| 2014 | Effect.provide(clientBackedApi(client).layer), |
| 2015 | Effect.provide(PlatformServices), |
| 2016 | ); |
| 2017 | }); |
| 2018 | }, |
| 2019 | ); |
| 2020 | |
| 2021 | const redeployClient = () => |
| 2022 | ({ |
| 2023 | createAppDeployment: () => |
| 2024 | Effect.succeed({ |
| 2025 | id: "deployment-1", |
| 2026 | type: "deployment" as const, |
| 2027 | url: "https://api.prisma.test/v1/deployments/deployment-1", |
| 2028 | foundryVersionId: "foundry-1", |
| 2029 | }), |
| 2030 | getDeployment: (id: string) => |
| 2031 | Effect.succeed({ |
| 2032 | id, |
| 2033 | type: "deployment" as const, |
| 2034 | serviceId: "app-1", |
| 2035 | url: "https://api.prisma.test/v1/deployments/deployment-1", |
| 2036 | foundryVersionId: "foundry-1", |
| 2037 | status: "new", |
| 2038 | previewDomain: null, |
| 2039 | createdAt: "2026-01-01T00:00:00Z", |
| 2040 | }), |
| 2041 | }) as unknown as PrismaManagementClient; |
| 2042 | |
| 2043 | const triggersDiffBase = (triggersHash: Redacted.Redacted<string> | undefined) => ({ |
| 2044 | id: "Deployment", |
| 2045 | fqn: "Deployment", |
| 2046 | instanceId: "00000000000000000000000000000000", |
| 2047 | oldBindings: [], |
| 2048 | newBindings: [], |
| 2049 | output: { |
| 2050 | deploymentId: "deployment-1", |
| 2051 | appId: "app-1", |
| 2052 | foundryVersionId: "foundry-1", |
| 2053 | status: "new", |
| 2054 | previewDomain: null, |
| 2055 | artifactHash: undefined, |
| 2056 | triggersHash, |
| 2057 | appEndpointDomain: undefined, |
| 2058 | createdAt: "2026-01-01T00:00:00Z", |
| 2059 | }, |
| 2060 | }); |
| 2061 | |
| 2062 | const withWebSocketServer = <A, E, R>(f: (server: WebSocketServer) => Effect.Effect<A, E, R>) => |
| 2063 | Effect.acquireUseRelease( |
| 2064 | Effect.sync(() => new WebSocketServer({ host: "127.0.0.1", port: 0 })), |
| 2065 | f, |
| 2066 | (server) => |
| 2067 | Effect.callback<void>((resume) => { |
| 2068 | server.close(() => resume(Effect.void)); |
| 2069 | }).pipe(Effect.ignore), |
| 2070 | ); |
| 2071 | |
| 2072 | const listenUrl = (server: WebSocketServer) => |
| 2073 | Effect.callback<string, Error>((resume) => { |
| 2074 | const complete = () => { |
| 2075 | cleanup(); |
| 2076 | const address = server.address(); |
| 2077 | if (address && typeof address === "object") { |
| 2078 | resume(Effect.succeed(`http://127.0.0.1:${address.port}`)); |
| 2079 | } else { |
| 2080 | resume(Effect.fail(new Error("WebSocket server has no TCP address"))); |
| 2081 | } |
| 2082 | }; |
| 2083 | const fail = (cause: unknown) => { |
| 2084 | cleanup(); |
| 2085 | resume(Effect.fail(cause instanceof Error ? cause : new Error(String(cause)))); |
| 2086 | }; |
| 2087 | const cleanup = () => { |
| 2088 | server.off("listening", complete); |
| 2089 | server.off("error", fail); |
| 2090 | }; |
| 2091 | |
| 2092 | if (server.address()) { |
| 2093 | complete(); |
| 2094 | return; |
| 2095 | } |
| 2096 | |
| 2097 | server.once("listening", complete); |
| 2098 | server.once("error", fail); |
| 2099 | return Effect.sync(cleanup); |
| 2100 | }); |
| 2101 | |
| 2102 | const withHttpServer = <A, E, R>( |
| 2103 | handler: RequestListener, |
| 2104 | f: (url: string) => Effect.Effect<A, E, R>, |
| 2105 | ) => |
| 2106 | Effect.acquireUseRelease( |
| 2107 | Effect.sync(() => createHttpServer(handler)), |
| 2108 | (server) => Effect.flatMap(listenHttpServerUrl(server), f), |
| 2109 | (server) => |
| 2110 | Effect.callback<void>((resume) => { |
| 2111 | server.close(() => resume(Effect.void)); |
| 2112 | }).pipe(Effect.ignore), |
| 2113 | ); |
| 2114 | |
| 2115 | const listenHttpServerUrl = (server: NodeHttpServer) => |
| 2116 | Effect.callback<string, Error>((resume) => { |
| 2117 | const complete = () => { |
| 2118 | cleanup(); |
| 2119 | const address = server.address(); |
| 2120 | if (address && typeof address === "object") { |
| 2121 | resume(Effect.succeed(`http://127.0.0.1:${address.port}`)); |
| 2122 | } else { |
| 2123 | resume(Effect.fail(new Error("HTTP server has no TCP address"))); |
| 2124 | } |
| 2125 | }; |
| 2126 | const fail = (cause: unknown) => { |
| 2127 | cleanup(); |
| 2128 | resume(Effect.fail(cause instanceof Error ? cause : new Error(String(cause)))); |
| 2129 | }; |
| 2130 | const cleanup = () => { |
| 2131 | server.off("listening", complete); |
| 2132 | server.off("error", fail); |
| 2133 | }; |
| 2134 | |
| 2135 | server.once("listening", complete); |
| 2136 | server.once("error", fail); |
| 2137 | server.listen(0, "127.0.0.1"); |
| 2138 | return Effect.sync(cleanup); |
| 2139 | }); |