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 { |
| 2 | extractConnectionSecrets, |
| 3 | PrismaApiDecodeError, |
| 4 | PrismaApiError, |
| 5 | PrismaClient, |
| 6 | PrismaClientLive, |
| 7 | type PrismaManagementClient, |
| 8 | } from "@alchemy.run/prisma/Client"; |
| 9 | import { PrismaEnvironment } from "@alchemy.run/prisma/PrismaEnvironment"; |
| 10 | import { describe, expect, it } from "@alchemy.run/test"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Fiber from "effect/Fiber"; |
| 13 | import * as HttpBody from "effect/http/HttpBody"; |
| 14 | import * as HttpClient from "effect/http/HttpClient"; |
| 15 | import * as HttpClientError from "effect/http/HttpClientError"; |
| 16 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Redacted from "effect/Redacted"; |
| 19 | import { TestClock } from "effect/testing"; |
| 20 | import { productionManagementApiRoutes } from "./fixtures/ManagementApiContract.ts"; |
| 21 | |
| 22 | interface Captured { |
| 23 | url: string; |
| 24 | method: string; |
| 25 | pathname: string; |
| 26 | search: string; |
| 27 | authorization: string | undefined; |
| 28 | bodyJson: unknown; |
| 29 | } |
| 30 | |
| 31 | const page = <T>(data: T[], hasMore = false, nextCursor: string | null = null) => |
| 32 | json({ data, pagination: { hasMore, nextCursor } }); |
| 33 | |
| 34 | const data = <T>(value: T) => json({ data: value }); |
| 35 | |
| 36 | const json = (value: unknown, init?: ResponseInit) => |
| 37 | new Response(JSON.stringify(value), { |
| 38 | status: 200, |
| 39 | headers: { "content-type": "application/json" }, |
| 40 | ...init, |
| 41 | }); |
| 42 | |
| 43 | const empty = () => new Response(null, { status: 204 }); |
| 44 | |
| 45 | const expectedManagementApiRoutes = [...productionManagementApiRoutes].sort(); |
| 46 | |
| 47 | const routeTemplateFor = (method: string, pathname: string): string => { |
| 48 | const concreteSegments = pathname.split("/"); |
| 49 | const route = productionManagementApiRoutes.find((candidate) => { |
| 50 | const separator = candidate.indexOf(" "); |
| 51 | const candidateMethod = candidate.slice(0, separator); |
| 52 | const templatePath = candidate.slice(separator + 1); |
| 53 | const templateSegments = templatePath.split("/"); |
| 54 | return ( |
| 55 | candidateMethod === method && |
| 56 | templateSegments.length === concreteSegments.length && |
| 57 | templateSegments.every( |
| 58 | (segment, index) => |
| 59 | (/^\{[^}]+\}$/.test(segment) && concreteSegments[index]?.length !== 0) || |
| 60 | segment === concreteSegments[index], |
| 61 | ) |
| 62 | ); |
| 63 | }); |
| 64 | if (!route) { |
| 65 | throw new Error(`Missing route inventory mapping for ${method} ${pathname}`); |
| 66 | } |
| 67 | return route; |
| 68 | }; |
| 69 | const routeInventoryFrom = (captured: Captured[]) => { |
| 70 | const routes = new Set<string>(); |
| 71 | for (const request of captured) { |
| 72 | routes.add(routeTemplateFor(request.method, request.pathname)); |
| 73 | } |
| 74 | routes.add("GET /v1/deployments/{deploymentId}/logs"); |
| 75 | routes.add("GET /v1/builds/{buildId}/logs"); |
| 76 | return [...routes].sort(); |
| 77 | }; |
| 78 | |
| 79 | const fixtureResponse = (request: Captured) => { |
| 80 | if (request.pathname === "/v1/projects" && request.method === "GET") { |
| 81 | return request.search.includes("cursor=cursor-2") |
| 82 | ? page([{ id: "project-2", type: "project", name: "Two" }]) |
| 83 | : page([{ id: "project-1", type: "project", name: "One" }], true, "cursor-2"); |
| 84 | } |
| 85 | |
| 86 | if (request.pathname === "/v1/projects/project-1/databases" && request.method === "POST") { |
| 87 | return data({ id: "database-1", type: "database", name: "main" }); |
| 88 | } |
| 89 | |
| 90 | if (request.pathname === "/v1/databases/database-1/backups" && request.method === "GET") { |
| 91 | return json({ |
| 92 | data: [ |
| 93 | { |
| 94 | id: "backup-1", |
| 95 | type: "backup", |
| 96 | backupType: "full", |
| 97 | createdAt: "2026-01-01T00:00:00Z", |
| 98 | status: "completed", |
| 99 | }, |
| 100 | ], |
| 101 | meta: { |
| 102 | backupRetentionDays: 7, |
| 103 | }, |
| 104 | pagination: { |
| 105 | hasMore: false, |
| 106 | limit: 1, |
| 107 | }, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | if (request.pathname === "/v1/workspaces/workspace-1/integrations" && request.method === "GET") { |
| 112 | return page([{ id: "integration-1", url: "https://example.test" }]); |
| 113 | } |
| 114 | |
| 115 | if (request.pathname === "/v1/domains/domain-1" && request.method === "GET") { |
| 116 | return data({ |
| 117 | id: "domain-1", |
| 118 | type: "custom-domain", |
| 119 | url: "https://api.prisma.test/v1/domains/domain-1", |
| 120 | hostname: "api.example.com", |
| 121 | appId: "app-1", |
| 122 | status: "pending_dns", |
| 123 | foundryStatus: "pending", |
| 124 | failureReason: null, |
| 125 | failureCategory: null, |
| 126 | certExpiresAt: null, |
| 127 | createdAt: "2026-01-01T00:00:00Z", |
| 128 | updatedAt: "2026-01-01T00:00:00Z", |
| 129 | dnsRecords: [], |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | if (request.pathname.startsWith("/v1/regions") && request.method === "GET") { |
| 134 | return json({ |
| 135 | data: [ |
| 136 | { |
| 137 | id: "us-east-1", |
| 138 | type: "region", |
| 139 | name: "US East", |
| 140 | product: "postgres", |
| 141 | status: "available", |
| 142 | }, |
| 143 | ], |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | return json( |
| 148 | { |
| 149 | error: { |
| 150 | message: `Unhandled fixture request ${request.method} ${request.pathname}${request.search}`, |
| 151 | }, |
| 152 | }, |
| 153 | { status: 500 }, |
| 154 | ); |
| 155 | }; |
| 156 | |
| 157 | const layerForHttp = (client: HttpClient.HttpClient, baseUrl = "https://api.prisma.test") => |
| 158 | PrismaClientLive.pipe( |
| 159 | Layer.provide( |
| 160 | Layer.mergeAll( |
| 161 | Layer.succeed(HttpClient.HttpClient, client), |
| 162 | Layer.succeed(PrismaEnvironment, { |
| 163 | type: "serviceToken" as const, |
| 164 | serviceToken: Redacted.make("test-token"), |
| 165 | source: { type: "stored" as const }, |
| 166 | baseUrl, |
| 167 | }), |
| 168 | ), |
| 169 | ), |
| 170 | ); |
| 171 | |
| 172 | const harness = (baseUrl = "https://api.prisma.test") => { |
| 173 | const captured: Captured[] = []; |
| 174 | const client = HttpClient.make((request) => |
| 175 | Effect.sync(() => { |
| 176 | const url = new URL(request.url); |
| 177 | const body = request.body as HttpBody.HttpBody; |
| 178 | const bodyText = body._tag === "Uint8Array" ? new TextDecoder().decode(body.body) : ""; |
| 179 | const entry: Captured = { |
| 180 | url: request.url, |
| 181 | method: request.method, |
| 182 | pathname: url.pathname, |
| 183 | search: url.search, |
| 184 | authorization: request.headers.authorization, |
| 185 | bodyJson: bodyText ? JSON.parse(bodyText) : undefined, |
| 186 | }; |
| 187 | captured.push(entry); |
| 188 | return HttpClientResponse.fromWeb(request, fixtureResponse(entry)); |
| 189 | }), |
| 190 | ); |
| 191 | const layer = layerForHttp(client, baseUrl); |
| 192 | return { layer, captured }; |
| 193 | }; |
| 194 | |
| 195 | const withClient = <A>(f: (client: PrismaManagementClient) => Effect.Effect<A, any, any>) => |
| 196 | Effect.gen(function* () { |
| 197 | const client = yield* PrismaClient; |
| 198 | return yield* f(client); |
| 199 | }); |
| 200 | |
| 201 | const routeCoverageHarness = () => { |
| 202 | const captured: Captured[] = []; |
| 203 | const client = HttpClient.make((request) => |
| 204 | Effect.sync(() => { |
| 205 | const url = new URL(request.url); |
| 206 | const body = request.body as HttpBody.HttpBody; |
| 207 | const bodyText = body._tag === "Uint8Array" ? new TextDecoder().decode(body.body) : ""; |
| 208 | const entry: Captured = { |
| 209 | url: request.url, |
| 210 | method: request.method, |
| 211 | pathname: url.pathname, |
| 212 | search: url.search, |
| 213 | authorization: request.headers.authorization, |
| 214 | bodyJson: bodyText ? JSON.parse(bodyText) : undefined, |
| 215 | }; |
| 216 | captured.push(entry); |
| 217 | |
| 218 | if (entry.pathname.endsWith("/usage")) { |
| 219 | return HttpClientResponse.fromWeb( |
| 220 | request, |
| 221 | json({ |
| 222 | period: { start: "2026-01-01", end: "2026-01-02" }, |
| 223 | metrics: { |
| 224 | operations: { used: 0, unit: "ops" }, |
| 225 | storage: { used: 0, unit: "GiB" }, |
| 226 | }, |
| 227 | generatedAt: "2026-01-02T00:00:00Z", |
| 228 | }), |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | if (entry.pathname.startsWith("/v1/regions") && entry.method === "GET") { |
| 233 | return HttpClientResponse.fromWeb(request, json({ data: [] })); |
| 234 | } |
| 235 | |
| 236 | if (entry.method === "GET") { |
| 237 | return HttpClientResponse.fromWeb(request, page([])); |
| 238 | } |
| 239 | |
| 240 | if ( |
| 241 | entry.method === "DELETE" || |
| 242 | entry.pathname.endsWith("/stop") || |
| 243 | entry.pathname.endsWith("/transfer") |
| 244 | ) { |
| 245 | return HttpClientResponse.fromWeb(request, empty()); |
| 246 | } |
| 247 | |
| 248 | return HttpClientResponse.fromWeb( |
| 249 | request, |
| 250 | data({ |
| 251 | id: "resource-1", |
| 252 | type: "resource", |
| 253 | foundryVersionId: "foundry-1", |
| 254 | uploadUrl: "https://upload.example.test/artifact.tar.gz", |
| 255 | previewDomain: "version-1.example.test", |
| 256 | appEndpointDomain: "app-1.example.test", |
| 257 | }), |
| 258 | ); |
| 259 | }), |
| 260 | ); |
| 261 | const layer = PrismaClientLive.pipe( |
| 262 | Layer.provide( |
| 263 | Layer.mergeAll( |
| 264 | Layer.succeed(HttpClient.HttpClient, client), |
| 265 | Layer.succeed(PrismaEnvironment, { |
| 266 | type: "serviceToken" as const, |
| 267 | serviceToken: Redacted.make("test-token"), |
| 268 | source: { type: "stored" as const }, |
| 269 | baseUrl: "https://api.prisma.test", |
| 270 | }), |
| 271 | ), |
| 272 | ), |
| 273 | ); |
| 274 | return { layer, captured }; |
| 275 | }; |
| 276 | |
| 277 | describe("PrismaClient", { tags: ["unit", "provider:prisma", "local"] }, () => { |
| 278 | it("extracts canonical endpoint secrets and parses direct credentials", () => { |
| 279 | const secrets = extractConnectionSecrets({ |
| 280 | id: "connection-1", |
| 281 | type: "connection", |
| 282 | url: "https://api.prisma.test/v1/connections/connection-1", |
| 283 | name: "api", |
| 284 | createdAt: "2026-01-01T00:00:00Z", |
| 285 | kind: "postgres", |
| 286 | endpoints: { |
| 287 | direct: { |
| 288 | host: "direct.prisma.test", |
| 289 | port: 5432, |
| 290 | connectionString: |
| 291 | "postgres://api:p%40ss@direct.prisma.test:5432/postgres?sslmode=require", |
| 292 | }, |
| 293 | pooled: { |
| 294 | host: "pooled.prisma.test", |
| 295 | port: 5432, |
| 296 | connectionString: "postgres://pooled", |
| 297 | }, |
| 298 | }, |
| 299 | database: { |
| 300 | id: "database-1", |
| 301 | url: "https://api.prisma.test/v1/databases/database-1", |
| 302 | name: "main", |
| 303 | }, |
| 304 | } as unknown as Parameters<typeof extractConnectionSecrets>[0]); |
| 305 | |
| 306 | expect(Redacted.value(secrets.directConnectionString!)).toContain("direct.prisma.test"); |
| 307 | expect(Redacted.value(secrets.pooledConnectionString!)).toBe("postgres://pooled"); |
| 308 | expect(secrets.host).toBe("direct.prisma.test"); |
| 309 | expect(secrets.user).toBe("api"); |
| 310 | expect(Redacted.value(secrets.password!)).toBe("p@ss"); |
| 311 | }); |
| 312 | |
| 313 | it.effect("paginates list endpoints and sends bearer auth", () => { |
| 314 | const { layer, captured } = harness(); |
| 315 | |
| 316 | return withClient((client) => |
| 317 | Effect.gen(function* () { |
| 318 | const projects = yield* client.listProjects({ limit: 1 }); |
| 319 | |
| 320 | expect(projects.map((project: { id: string }) => project.id)).toEqual([ |
| 321 | "project-1", |
| 322 | "project-2", |
| 323 | ]); |
| 324 | expect(captured.map((request) => request.pathname)).toEqual([ |
| 325 | "/v1/projects", |
| 326 | "/v1/projects", |
| 327 | ]); |
| 328 | expect(captured[0]?.search).toBe("?limit=1"); |
| 329 | expect(captured[1]?.search).toBe("?limit=1&cursor=cursor-2"); |
| 330 | expect(captured.every((request) => request.authorization === "Bearer test-token")).toBe( |
| 331 | true, |
| 332 | ); |
| 333 | }), |
| 334 | ).pipe(Effect.provide(layer)); |
| 335 | }); |
| 336 | |
| 337 | it.effect("starts pagination from an explicit cursor", () => { |
| 338 | const { layer, captured } = harness(); |
| 339 | |
| 340 | return withClient((client) => |
| 341 | Effect.gen(function* () { |
| 342 | const projects = yield* client.listProjects({ |
| 343 | limit: 1, |
| 344 | cursor: "cursor-2", |
| 345 | }); |
| 346 | |
| 347 | expect(projects.map((project: { id: string }) => project.id)).toEqual(["project-2"]); |
| 348 | expect(captured.map((request) => request.pathname)).toEqual(["/v1/projects"]); |
| 349 | expect(captured[0]?.search).toBe("?limit=1&cursor=cursor-2"); |
| 350 | }), |
| 351 | ).pipe(Effect.provide(layer)); |
| 352 | }); |
| 353 | |
| 354 | it.effect("rejects pagination that omits a required next cursor", () => { |
| 355 | const http = HttpClient.make((request) => |
| 356 | Effect.succeed(HttpClientResponse.fromWeb(request, page([], true, null))), |
| 357 | ); |
| 358 | |
| 359 | return withClient((client) => client.listProjects()).pipe( |
| 360 | Effect.provide(layerForHttp(http)), |
| 361 | Effect.flip, |
| 362 | Effect.map((error) => { |
| 363 | expect(error).toBeInstanceOf(PrismaApiDecodeError); |
| 364 | expect(error.message).toContain("hasMore was true without a non-empty nextCursor"); |
| 365 | }), |
| 366 | ); |
| 367 | }); |
| 368 | |
| 369 | it.effect("rejects pagination that repeats a cursor", () => { |
| 370 | let attempts = 0; |
| 371 | const http = HttpClient.make((request) => |
| 372 | Effect.sync(() => { |
| 373 | attempts += 1; |
| 374 | return HttpClientResponse.fromWeb(request, page([], true, "repeated-cursor")); |
| 375 | }), |
| 376 | ); |
| 377 | |
| 378 | return withClient((client) => client.listProjects()).pipe( |
| 379 | Effect.provide(layerForHttp(http)), |
| 380 | Effect.flip, |
| 381 | Effect.map((error) => { |
| 382 | expect(error).toBeInstanceOf(PrismaApiDecodeError); |
| 383 | expect(error.message).toContain("nextCursor repeated"); |
| 384 | expect(attempts).toBe(2); |
| 385 | }), |
| 386 | ); |
| 387 | }); |
| 388 | |
| 389 | it.effect("bounds aggregate pagination response bytes", () => { |
| 390 | let attempts = 0; |
| 391 | const payload = "x".repeat(4 * 1024 * 1024 - 1_024); |
| 392 | const http = HttpClient.make((request) => |
| 393 | Effect.sync(() => { |
| 394 | attempts += 1; |
| 395 | return HttpClientResponse.fromWeb( |
| 396 | request, |
| 397 | page([{ id: `project-${attempts}`, payload }], true, `cursor-${attempts}`), |
| 398 | ); |
| 399 | }), |
| 400 | ); |
| 401 | |
| 402 | return withClient((client) => client.listProjects()).pipe( |
| 403 | Effect.provide(layerForHttp(http)), |
| 404 | Effect.flip, |
| 405 | Effect.map((error) => { |
| 406 | expect(error).toBeInstanceOf(PrismaApiDecodeError); |
| 407 | expect(error.message).toContain("67108864 aggregate response byte safety limit"); |
| 408 | if (error instanceof PrismaApiDecodeError) { |
| 409 | expect(error.bodyLength).toBeGreaterThan(64 * 1024 * 1024); |
| 410 | } |
| 411 | expect(attempts).toBe(17); |
| 412 | }), |
| 413 | ); |
| 414 | }); |
| 415 | |
| 416 | it.effect("uses the configured Prisma API base URL", () => { |
| 417 | const { layer, captured } = harness("https://control-plane.prisma.test"); |
| 418 | |
| 419 | return withClient((client) => |
| 420 | Effect.gen(function* () { |
| 421 | yield* client.listProjects({ limit: 1 }); |
| 422 | |
| 423 | expect(captured[0]?.url).toBe("https://control-plane.prisma.test/v1/projects?limit=1"); |
| 424 | }), |
| 425 | ).pipe(Effect.provide(layer)); |
| 426 | }); |
| 427 | |
| 428 | it.effect("preserves current custom-domain wire fields", () => { |
| 429 | const { layer } = harness(); |
| 430 | |
| 431 | return withClient((client) => |
| 432 | Effect.gen(function* () { |
| 433 | const domain = yield* client.getCustomDomain("domain-1"); |
| 434 | expect(domain.appId).toBe("app-1"); |
| 435 | expect(domain.foundryStatus).toBe("pending"); |
| 436 | }), |
| 437 | ).pipe(Effect.provide(layer)); |
| 438 | }); |
| 439 | |
| 440 | it.effect("preserves 200 versus 201 custom-domain create outcomes", () => { |
| 441 | const statuses = [200, 201] as const; |
| 442 | let requestIndex = 0; |
| 443 | const domain = { |
| 444 | id: "domain-1", |
| 445 | type: "custom-domain" as const, |
| 446 | url: "https://api.prisma.test/v1/domains/domain-1", |
| 447 | hostname: "api.example.com", |
| 448 | appId: "app-1", |
| 449 | status: "pending_dns" as const, |
| 450 | foundryStatus: "pending", |
| 451 | failureReason: null, |
| 452 | failureCategory: null, |
| 453 | certExpiresAt: null, |
| 454 | createdAt: "2026-01-01T00:00:00Z", |
| 455 | updatedAt: "2026-01-01T00:00:00Z", |
| 456 | dnsRecords: [], |
| 457 | }; |
| 458 | const http = HttpClient.make((request) => |
| 459 | Effect.sync(() => { |
| 460 | const status = statuses[requestIndex++]; |
| 461 | return HttpClientResponse.fromWeb(request, json({ data: domain }, { status })); |
| 462 | }), |
| 463 | ); |
| 464 | |
| 465 | return withClient((client) => |
| 466 | Effect.gen(function* () { |
| 467 | const existing = yield* client.createAppDomain("app-1", { |
| 468 | hostname: domain.hostname, |
| 469 | }); |
| 470 | const created = yield* client.createAppDomain("app-1", { |
| 471 | hostname: domain.hostname, |
| 472 | }); |
| 473 | |
| 474 | expect(existing).toEqual({ status: 200, domain }); |
| 475 | expect(created).toEqual({ status: 201, domain }); |
| 476 | }), |
| 477 | ).pipe(Effect.provide(layerForHttp(http))); |
| 478 | }); |
| 479 | |
| 480 | it.effect("accepts the full flat database-create response contract", () => { |
| 481 | const database = { |
| 482 | id: "database-1", |
| 483 | type: "database" as const, |
| 484 | url: "https://api.prisma.test/v1/databases/database-1", |
| 485 | name: "main", |
| 486 | status: "failure" as const, |
| 487 | createdAt: "2026-01-01T00:00:00Z", |
| 488 | isDefault: false, |
| 489 | defaultConnectionId: null, |
| 490 | connections: [], |
| 491 | project: { |
| 492 | id: "project-1", |
| 493 | url: "https://api.prisma.test/v1/projects/project-1", |
| 494 | name: "app", |
| 495 | }, |
| 496 | region: null, |
| 497 | source: null, |
| 498 | branchId: null, |
| 499 | }; |
| 500 | const http = HttpClient.make((request) => |
| 501 | Effect.succeed( |
| 502 | HttpClientResponse.fromWeb(request, json({ data: database }, { status: 201 })), |
| 503 | ), |
| 504 | ); |
| 505 | |
| 506 | return withClient((client) => |
| 507 | Effect.gen(function* () { |
| 508 | const result = yield* client.createDatabase({ |
| 509 | projectId: "project-1", |
| 510 | }); |
| 511 | expect(result.status).toBe("failure"); |
| 512 | expect(result.region).toBeNull(); |
| 513 | }), |
| 514 | ).pipe(Effect.provide(layerForHttp(http))); |
| 515 | }); |
| 516 | |
| 517 | it.effect("does not retain secret-bearing malformed response bodies", () => { |
| 518 | const secret = "postgres://admin:super-secret@db.prisma.test/postgres"; |
| 519 | const http = HttpClient.make((request) => |
| 520 | Effect.succeed( |
| 521 | HttpClientResponse.fromWeb( |
| 522 | request, |
| 523 | new Response(`{"data":{"connectionString":"${secret}"}`, { |
| 524 | status: 201, |
| 525 | headers: { "content-type": "application/json" }, |
| 526 | }), |
| 527 | ), |
| 528 | ), |
| 529 | ); |
| 530 | const layer = PrismaClientLive.pipe( |
| 531 | Layer.provide( |
| 532 | Layer.mergeAll( |
| 533 | Layer.succeed(HttpClient.HttpClient, http), |
| 534 | Layer.succeed(PrismaEnvironment, { |
| 535 | type: "serviceToken" as const, |
| 536 | serviceToken: Redacted.make("test-token"), |
| 537 | source: { type: "stored" as const }, |
| 538 | baseUrl: "https://api.prisma.test", |
| 539 | }), |
| 540 | ), |
| 541 | ), |
| 542 | ); |
| 543 | |
| 544 | return withClient((client) => |
| 545 | Effect.gen(function* () { |
| 546 | const error = yield* client |
| 547 | .createConnection({ databaseId: "database-1", name: "api" }) |
| 548 | .pipe(Effect.flip); |
| 549 | expect(error).toBeInstanceOf(PrismaApiDecodeError); |
| 550 | expect(JSON.stringify(error)).not.toContain(secret); |
| 551 | if (error instanceof PrismaApiDecodeError) { |
| 552 | expect(error.bodyLength).toBeGreaterThan(0); |
| 553 | expect("body" in error).toBe(false); |
| 554 | expect("cause" in error).toBe(false); |
| 555 | } |
| 556 | }), |
| 557 | ).pipe(Effect.provide(layer)); |
| 558 | }); |
| 559 | |
| 560 | it.effect("rejects a missing data response envelope", () => { |
| 561 | const http = HttpClient.make((request) => |
| 562 | Effect.succeed(HttpClientResponse.fromWeb(request, json({ project: {} }))), |
| 563 | ); |
| 564 | |
| 565 | return withClient((client) => client.getProject("project-1")).pipe( |
| 566 | Effect.provide(layerForHttp(http)), |
| 567 | Effect.flip, |
| 568 | Effect.map((error) => { |
| 569 | expect(error).toBeInstanceOf(PrismaApiDecodeError); |
| 570 | expect(error.message).toContain("did not contain a data envelope"); |
| 571 | }), |
| 572 | ); |
| 573 | }); |
| 574 | |
| 575 | it.effect("rejects path-confusing resource IDs before sending auth", () => { |
| 576 | const { layer, captured } = harness(); |
| 577 | |
| 578 | return withClient((client) => |
| 579 | Effect.gen(function* () { |
| 580 | const traversal = yield* client.getProject("../workspaces").pipe(Effect.flip); |
| 581 | const embeddedRoute = yield* client.getProject("project-1/databases").pipe(Effect.flip); |
| 582 | const deploymentLog = yield* client |
| 583 | .getDeploymentLogsRequest("deployment-1/../../projects") |
| 584 | .pipe(Effect.flip); |
| 585 | const buildLog = yield* client.getBuildLogsRequest("build-1?token=leak").pipe(Effect.flip); |
| 586 | |
| 587 | for (const error of [traversal, embeddedRoute, deploymentLog, buildLog]) { |
| 588 | expect(error).toBeInstanceOf(PrismaApiError); |
| 589 | expect(error.message).toContain("invalid Prisma Management API"); |
| 590 | } |
| 591 | expect(captured).toEqual([]); |
| 592 | }), |
| 593 | ).pipe(Effect.provide(layer)); |
| 594 | }); |
| 595 | |
| 596 | it.effect("times out an unresponsive management API request", () => { |
| 597 | const http = HttpClient.make(() => Effect.never); |
| 598 | const layer = layerForHttp(http); |
| 599 | |
| 600 | return Effect.gen(function* () { |
| 601 | const fiber = yield* withClient((client) => client.getProject("project-1")).pipe( |
| 602 | Effect.provide(layer), |
| 603 | Effect.flip, |
| 604 | Effect.forkChild({ startImmediately: true }), |
| 605 | ); |
| 606 | yield* TestClock.adjust("11 seconds"); |
| 607 | const error = yield* Fiber.join(fiber); |
| 608 | |
| 609 | expect(error).toBeInstanceOf(PrismaApiError); |
| 610 | if (error instanceof PrismaApiError) { |
| 611 | expect(error.status).toBe(0); |
| 612 | expect(error.message).toContain("timed out after 10 seconds"); |
| 613 | } |
| 614 | }).pipe(Effect.provide(TestClock.layer())); |
| 615 | }); |
| 616 | |
| 617 | it.effect("times out while reading a streaming response body", () => { |
| 618 | const http = HttpClient.make((request) => |
| 619 | Effect.succeed( |
| 620 | HttpClientResponse.fromWeb( |
| 621 | request, |
| 622 | new Response( |
| 623 | new ReadableStream<Uint8Array>({ |
| 624 | start(controller) { |
| 625 | controller.enqueue(new TextEncoder().encode('{"data":')); |
| 626 | }, |
| 627 | }), |
| 628 | { |
| 629 | status: 201, |
| 630 | headers: { "content-type": "application/json" }, |
| 631 | }, |
| 632 | ), |
| 633 | ), |
| 634 | ), |
| 635 | ); |
| 636 | const layer = layerForHttp(http); |
| 637 | |
| 638 | return Effect.gen(function* () { |
| 639 | const fiber = yield* withClient((client) => client.getProject("project-1")).pipe( |
| 640 | Effect.provide(layer), |
| 641 | Effect.flip, |
| 642 | Effect.forkChild({ startImmediately: true }), |
| 643 | ); |
| 644 | yield* TestClock.adjust("11 seconds"); |
| 645 | const error = yield* Fiber.join(fiber); |
| 646 | |
| 647 | expect(error).toBeInstanceOf(PrismaApiError); |
| 648 | if (error instanceof PrismaApiError) { |
| 649 | expect(error.status).toBe(0); |
| 650 | expect(error.message).toContain("timed out after 10 seconds"); |
| 651 | } |
| 652 | }).pipe(Effect.provide(TestClock.layer())); |
| 653 | }); |
| 654 | |
| 655 | it.effect("rejects oversized successful response bodies", () => { |
| 656 | const http = HttpClient.make((request) => |
| 657 | Effect.succeed( |
| 658 | HttpClientResponse.fromWeb( |
| 659 | request, |
| 660 | new Response("x".repeat(4 * 1024 * 1024 + 1), { |
| 661 | status: 201, |
| 662 | headers: { "content-type": "application/json" }, |
| 663 | }), |
| 664 | ), |
| 665 | ), |
| 666 | ); |
| 667 | |
| 668 | return withClient((client) => client.createProject({ name: "api" })).pipe( |
| 669 | Effect.provide(layerForHttp(http)), |
| 670 | Effect.flip, |
| 671 | Effect.map((error) => { |
| 672 | expect(error).toBeInstanceOf(PrismaApiDecodeError); |
| 673 | expect(error.message).toContain("4194304 byte safety limit"); |
| 674 | }), |
| 675 | ); |
| 676 | }); |
| 677 | |
| 678 | it.effect("bounds and redacts oversized API error bodies", () => { |
| 679 | const secret = "do-not-retain-this-error-body"; |
| 680 | const http = HttpClient.make((request) => |
| 681 | Effect.succeed( |
| 682 | HttpClientResponse.fromWeb( |
| 683 | request, |
| 684 | new Response(secret.repeat(3_000), { |
| 685 | status: 400, |
| 686 | headers: { "content-type": "text/plain" }, |
| 687 | }), |
| 688 | ), |
| 689 | ), |
| 690 | ); |
| 691 | |
| 692 | return withClient((client) => client.createProject({ name: "api" })).pipe( |
| 693 | Effect.provide(layerForHttp(http)), |
| 694 | Effect.flip, |
| 695 | Effect.map((error) => { |
| 696 | expect(error).toBeInstanceOf(PrismaApiError); |
| 697 | expect(error.message).toContain("65536 byte safety limit"); |
| 698 | expect(JSON.stringify(error)).not.toContain(secret); |
| 699 | if (error instanceof PrismaApiError) { |
| 700 | expect(error.status).toBe(400); |
| 701 | expect(error.body).toBeUndefined(); |
| 702 | } |
| 703 | }), |
| 704 | ); |
| 705 | }); |
| 706 | |
| 707 | it.effect("does not expose plain-text API error bodies in messages", () => { |
| 708 | const secret = "postgres://admin:plain-text-secret@db.example.test/main"; |
| 709 | const http = HttpClient.make((request) => |
| 710 | Effect.succeed( |
| 711 | HttpClientResponse.fromWeb( |
| 712 | request, |
| 713 | new Response(secret, { |
| 714 | status: 400, |
| 715 | headers: { "content-type": "text/plain" }, |
| 716 | }), |
| 717 | ), |
| 718 | ), |
| 719 | ); |
| 720 | |
| 721 | return withClient((client) => client.createProject({ name: "api" })).pipe( |
| 722 | Effect.provide(layerForHttp(http)), |
| 723 | Effect.flip, |
| 724 | Effect.map((error) => { |
| 725 | expect(error).toBeInstanceOf(PrismaApiError); |
| 726 | expect(error.message).toBe("HTTP 400"); |
| 727 | expect(String(error)).not.toContain(secret); |
| 728 | expect(JSON.stringify(error)).not.toContain(secret); |
| 729 | if (error instanceof PrismaApiError) { |
| 730 | expect(Redacted.value(error.body!)).toBe(secret); |
| 731 | } |
| 732 | }), |
| 733 | ); |
| 734 | }); |
| 735 | |
| 736 | it.effect("exposes only safe codes from structured API errors", () => { |
| 737 | const secret = "postgres://admin:structured-secret@db.example.test/main"; |
| 738 | const responseBody = { |
| 739 | error: { |
| 740 | code: "state:not_found", |
| 741 | message: secret, |
| 742 | hint: secret, |
| 743 | }, |
| 744 | }; |
| 745 | const http = HttpClient.make((request) => |
| 746 | Effect.succeed(HttpClientResponse.fromWeb(request, json(responseBody, { status: 404 }))), |
| 747 | ); |
| 748 | |
| 749 | return withClient((client) => client.createProject({ name: "api" })).pipe( |
| 750 | Effect.provide(layerForHttp(http)), |
| 751 | Effect.flip, |
| 752 | Effect.map((error) => { |
| 753 | expect(error).toBeInstanceOf(PrismaApiError); |
| 754 | expect(error.message).toBe("Prisma Management API request failed (state:not_found)"); |
| 755 | expect(String(error)).not.toContain(secret); |
| 756 | expect(JSON.stringify(error)).not.toContain(secret); |
| 757 | }), |
| 758 | ); |
| 759 | }); |
| 760 | |
| 761 | it.effect("uses canonical app and deployment management routes", () => { |
| 762 | const { layer, captured } = routeCoverageHarness(); |
| 763 | |
| 764 | return withClient((client) => |
| 765 | Effect.gen(function* () { |
| 766 | yield* client.createProjectDatabase("project-1", { |
| 767 | name: "main", |
| 768 | region: "us-east-1", |
| 769 | }); |
| 770 | yield* client.createApp({ |
| 771 | projectId: "project-1", |
| 772 | displayName: "api", |
| 773 | regionId: "us-east-1", |
| 774 | }); |
| 775 | yield* client.createAppDeployment("app-1", { |
| 776 | portMapping: { http: 3000 }, |
| 777 | }); |
| 778 | yield* client.getDeployment("deployment-1"); |
| 779 | yield* client.startDeployment("deployment-1"); |
| 780 | yield* client.stopDeployment("deployment-1"); |
| 781 | yield* client.deleteDeployment("deployment-1"); |
| 782 | yield* client.listWorkspaceIntegrations("workspace-1", { limit: 10 }); |
| 783 | |
| 784 | expect( |
| 785 | captured.map((request) => [request.method, `${request.pathname}${request.search}`]), |
| 786 | ).toEqual([ |
| 787 | ["POST", "/v1/projects/project-1/databases"], |
| 788 | ["POST", "/v1/apps"], |
| 789 | ["POST", "/v1/apps/app-1/deployments"], |
| 790 | ["GET", "/v1/deployments/deployment-1"], |
| 791 | ["POST", "/v1/deployments/deployment-1/start"], |
| 792 | ["POST", "/v1/deployments/deployment-1/stop"], |
| 793 | ["DELETE", "/v1/deployments/deployment-1"], |
| 794 | ["GET", "/v1/workspaces/workspace-1/integrations?limit=10"], |
| 795 | ]); |
| 796 | expect(captured[0]?.bodyJson).toEqual({ |
| 797 | name: "main", |
| 798 | region: "us-east-1", |
| 799 | }); |
| 800 | expect(captured[2]?.bodyJson).toEqual({ |
| 801 | portMapping: { http: 3000 }, |
| 802 | }); |
| 803 | }), |
| 804 | ).pipe(Effect.provide(layer)); |
| 805 | }); |
| 806 | |
| 807 | it.effect("retries transient API failures for destructive requests", () => { |
| 808 | const captured: Captured[] = []; |
| 809 | let attempts = 0; |
| 810 | const http = HttpClient.make((request) => |
| 811 | Effect.sync(() => { |
| 812 | attempts += 1; |
| 813 | const url = new URL(request.url); |
| 814 | captured.push({ |
| 815 | url: request.url, |
| 816 | method: request.method, |
| 817 | pathname: url.pathname, |
| 818 | search: url.search, |
| 819 | authorization: request.headers.authorization, |
| 820 | bodyJson: undefined, |
| 821 | }); |
| 822 | return HttpClientResponse.fromWeb( |
| 823 | request, |
| 824 | attempts < 3 |
| 825 | ? json( |
| 826 | { |
| 827 | error: { |
| 828 | message: "transient platform failure", |
| 829 | }, |
| 830 | }, |
| 831 | { status: 500 }, |
| 832 | ) |
| 833 | : empty(), |
| 834 | ); |
| 835 | }), |
| 836 | ); |
| 837 | const layer = PrismaClientLive.pipe( |
| 838 | Layer.provide( |
| 839 | Layer.mergeAll( |
| 840 | Layer.succeed(HttpClient.HttpClient, http), |
| 841 | Layer.succeed(PrismaEnvironment, { |
| 842 | type: "serviceToken" as const, |
| 843 | serviceToken: Redacted.make("test-token"), |
| 844 | source: { type: "stored" as const }, |
| 845 | baseUrl: "https://api.prisma.test", |
| 846 | }), |
| 847 | ), |
| 848 | ), |
| 849 | ); |
| 850 | |
| 851 | return Effect.gen(function* () { |
| 852 | const fiber = yield* withClient((client) => client.deleteDeployment("deployment-1")).pipe( |
| 853 | Effect.provide(layer), |
| 854 | Effect.forkChild({ startImmediately: true }), |
| 855 | ); |
| 856 | yield* TestClock.adjust("1 second"); |
| 857 | yield* Fiber.join(fiber); |
| 858 | |
| 859 | expect(attempts).toBe(3); |
| 860 | expect(captured.map((request) => request.method)).toEqual(["DELETE", "DELETE", "DELETE"]); |
| 861 | expect(captured.map((request) => request.pathname)).toEqual([ |
| 862 | "/v1/deployments/deployment-1", |
| 863 | "/v1/deployments/deployment-1", |
| 864 | "/v1/deployments/deployment-1", |
| 865 | ]); |
| 866 | }).pipe(Effect.provide(TestClock.layer())); |
| 867 | }); |
| 868 | |
| 869 | it.effect("retries transient API failures for safe lifecycle posts", () => { |
| 870 | const captured: Captured[] = []; |
| 871 | let attempts = 0; |
| 872 | const http = HttpClient.make((request) => |
| 873 | Effect.sync(() => { |
| 874 | attempts += 1; |
| 875 | const url = new URL(request.url); |
| 876 | captured.push({ |
| 877 | url: request.url, |
| 878 | method: request.method, |
| 879 | pathname: url.pathname, |
| 880 | search: url.search, |
| 881 | authorization: request.headers.authorization, |
| 882 | bodyJson: undefined, |
| 883 | }); |
| 884 | return HttpClientResponse.fromWeb( |
| 885 | request, |
| 886 | attempts < 3 |
| 887 | ? json( |
| 888 | { |
| 889 | error: { |
| 890 | message: "transient platform failure", |
| 891 | }, |
| 892 | }, |
| 893 | { status: 500 }, |
| 894 | ) |
| 895 | : data({ |
| 896 | id: "deployment-1", |
| 897 | type: "deployment", |
| 898 | url: "https://api.prisma.test/v1/deployments/deployment-1", |
| 899 | foundryVersionId: "foundry-1", |
| 900 | status: "running", |
| 901 | previewDomain: "version-1.prisma.test", |
| 902 | }), |
| 903 | ); |
| 904 | }), |
| 905 | ); |
| 906 | const layer = PrismaClientLive.pipe( |
| 907 | Layer.provide( |
| 908 | Layer.mergeAll( |
| 909 | Layer.succeed(HttpClient.HttpClient, http), |
| 910 | Layer.succeed(PrismaEnvironment, { |
| 911 | type: "serviceToken" as const, |
| 912 | serviceToken: Redacted.make("test-token"), |
| 913 | source: { type: "stored" as const }, |
| 914 | baseUrl: "https://api.prisma.test", |
| 915 | }), |
| 916 | ), |
| 917 | ), |
| 918 | ); |
| 919 | |
| 920 | return Effect.gen(function* () { |
| 921 | const fiber = yield* withClient((client) => client.startDeployment("deployment-1")).pipe( |
| 922 | Effect.provide(layer), |
| 923 | Effect.forkChild({ startImmediately: true }), |
| 924 | ); |
| 925 | yield* TestClock.adjust("1 second"); |
| 926 | const version = yield* Fiber.join(fiber); |
| 927 | |
| 928 | expect(attempts).toBe(3); |
| 929 | expect(version.previewDomain).toBe("version-1.prisma.test"); |
| 930 | expect(captured.map((request) => request.method)).toEqual(["POST", "POST", "POST"]); |
| 931 | expect(captured.map((request) => request.pathname)).toEqual([ |
| 932 | "/v1/deployments/deployment-1/start", |
| 933 | "/v1/deployments/deployment-1/start", |
| 934 | "/v1/deployments/deployment-1/start", |
| 935 | ]); |
| 936 | }).pipe(Effect.provide(TestClock.layer())); |
| 937 | }); |
| 938 | |
| 939 | it.effect("does not retry transient API failures for create requests", () => { |
| 940 | const captured: Captured[] = []; |
| 941 | let attempts = 0; |
| 942 | const http = HttpClient.make((request) => |
| 943 | Effect.sync(() => { |
| 944 | attempts += 1; |
| 945 | const url = new URL(request.url); |
| 946 | captured.push({ |
| 947 | url: request.url, |
| 948 | method: request.method, |
| 949 | pathname: url.pathname, |
| 950 | search: url.search, |
| 951 | authorization: request.headers.authorization, |
| 952 | bodyJson: undefined, |
| 953 | }); |
| 954 | return HttpClientResponse.fromWeb( |
| 955 | request, |
| 956 | json( |
| 957 | { |
| 958 | error: { |
| 959 | message: "transient platform failure", |
| 960 | }, |
| 961 | }, |
| 962 | { status: 500 }, |
| 963 | ), |
| 964 | ); |
| 965 | }), |
| 966 | ); |
| 967 | const layer = PrismaClientLive.pipe( |
| 968 | Layer.provide( |
| 969 | Layer.mergeAll( |
| 970 | Layer.succeed(HttpClient.HttpClient, http), |
| 971 | Layer.succeed(PrismaEnvironment, { |
| 972 | type: "serviceToken" as const, |
| 973 | serviceToken: Redacted.make("test-token"), |
| 974 | source: { type: "stored" as const }, |
| 975 | baseUrl: "https://api.prisma.test", |
| 976 | }), |
| 977 | ), |
| 978 | ), |
| 979 | ); |
| 980 | |
| 981 | return Effect.gen(function* () { |
| 982 | const error = yield* withClient((client) => |
| 983 | client.createAppDeployment("app-1", { |
| 984 | portMapping: { http: 3000 }, |
| 985 | }), |
| 986 | ).pipe(Effect.provide(layer), Effect.flip); |
| 987 | |
| 988 | expect(error).toBeInstanceOf(PrismaApiError); |
| 989 | expect(attempts).toBe(1); |
| 990 | expect(captured.map((request) => request.method)).toEqual(["POST"]); |
| 991 | expect(captured.map((request) => request.pathname)).toEqual(["/v1/apps/app-1/deployments"]); |
| 992 | }); |
| 993 | }); |
| 994 | |
| 995 | it.effect("does not retry transient API failures for transfer requests", () => { |
| 996 | const captured: Captured[] = []; |
| 997 | let attempts = 0; |
| 998 | const http = HttpClient.make((request) => |
| 999 | Effect.sync(() => { |
| 1000 | attempts += 1; |
| 1001 | const url = new URL(request.url); |
| 1002 | captured.push({ |
| 1003 | url: request.url, |
| 1004 | method: request.method, |
| 1005 | pathname: url.pathname, |
| 1006 | search: url.search, |
| 1007 | authorization: request.headers.authorization, |
| 1008 | bodyJson: undefined, |
| 1009 | }); |
| 1010 | return HttpClientResponse.fromWeb( |
| 1011 | request, |
| 1012 | json( |
| 1013 | { |
| 1014 | error: { |
| 1015 | message: "transient platform failure", |
| 1016 | }, |
| 1017 | }, |
| 1018 | { status: 500 }, |
| 1019 | ), |
| 1020 | ); |
| 1021 | }), |
| 1022 | ); |
| 1023 | const layer = PrismaClientLive.pipe( |
| 1024 | Layer.provide( |
| 1025 | Layer.mergeAll( |
| 1026 | Layer.succeed(HttpClient.HttpClient, http), |
| 1027 | Layer.succeed(PrismaEnvironment, { |
| 1028 | type: "serviceToken" as const, |
| 1029 | serviceToken: Redacted.make("test-token"), |
| 1030 | source: { type: "stored" as const }, |
| 1031 | baseUrl: "https://api.prisma.test", |
| 1032 | }), |
| 1033 | ), |
| 1034 | ), |
| 1035 | ); |
| 1036 | |
| 1037 | return Effect.gen(function* () { |
| 1038 | const error = yield* withClient((client) => |
| 1039 | client.transferProject("project-1", { |
| 1040 | recipientAccessToken: "recipient-token", |
| 1041 | }), |
| 1042 | ).pipe(Effect.provide(layer), Effect.flip); |
| 1043 | |
| 1044 | expect(error).toBeInstanceOf(PrismaApiError); |
| 1045 | expect(attempts).toBe(1); |
| 1046 | expect(captured.map((request) => request.method)).toEqual(["POST"]); |
| 1047 | expect(captured.map((request) => request.pathname)).toEqual([ |
| 1048 | "/v1/projects/project-1/transfer", |
| 1049 | ]); |
| 1050 | }); |
| 1051 | }); |
| 1052 | |
| 1053 | it.effect("retries transient transport failures", () => { |
| 1054 | let attempts = 0; |
| 1055 | const http = HttpClient.make((request) => |
| 1056 | Effect.sync(() => { |
| 1057 | attempts += 1; |
| 1058 | return attempts < 3 |
| 1059 | ? new HttpClientError.HttpClientError({ |
| 1060 | reason: new HttpClientError.TransportError({ |
| 1061 | request, |
| 1062 | cause: new Error("connection reset"), |
| 1063 | description: "test transport failure", |
| 1064 | }), |
| 1065 | }) |
| 1066 | : undefined; |
| 1067 | }).pipe( |
| 1068 | Effect.flatMap((error) => |
| 1069 | error |
| 1070 | ? Effect.fail(error) |
| 1071 | : Effect.succeed(HttpClientResponse.fromWeb(request, page([{ id: "project-1" }]))), |
| 1072 | ), |
| 1073 | ), |
| 1074 | ); |
| 1075 | const layer = PrismaClientLive.pipe( |
| 1076 | Layer.provide( |
| 1077 | Layer.mergeAll( |
| 1078 | Layer.succeed(HttpClient.HttpClient, http), |
| 1079 | Layer.succeed(PrismaEnvironment, { |
| 1080 | type: "serviceToken" as const, |
| 1081 | serviceToken: Redacted.make("test-token"), |
| 1082 | source: { type: "stored" as const }, |
| 1083 | baseUrl: "https://api.prisma.test", |
| 1084 | }), |
| 1085 | ), |
| 1086 | ), |
| 1087 | ); |
| 1088 | |
| 1089 | return Effect.gen(function* () { |
| 1090 | const fiber = yield* withClient((client) => client.listProjects()).pipe( |
| 1091 | Effect.provide(layer), |
| 1092 | Effect.forkChild({ startImmediately: true }), |
| 1093 | ); |
| 1094 | yield* TestClock.adjust("1 second"); |
| 1095 | const projects = yield* Fiber.join(fiber); |
| 1096 | |
| 1097 | expect(attempts).toBe(3); |
| 1098 | expect(projects.map((project) => project.id)).toEqual(["project-1"]); |
| 1099 | }).pipe(Effect.provide(TestClock.layer())); |
| 1100 | }); |
| 1101 | |
| 1102 | it.effect("preserves backup list metadata", () => { |
| 1103 | const { layer, captured } = harness(); |
| 1104 | |
| 1105 | return withClient((client) => |
| 1106 | Effect.gen(function* () { |
| 1107 | const backups = yield* client.listBackups("database-1", { limit: 1 }); |
| 1108 | |
| 1109 | expect(backups).toEqual({ |
| 1110 | data: [ |
| 1111 | { |
| 1112 | id: "backup-1", |
| 1113 | type: "backup", |
| 1114 | backupType: "full", |
| 1115 | createdAt: "2026-01-01T00:00:00Z", |
| 1116 | status: "completed", |
| 1117 | }, |
| 1118 | ], |
| 1119 | meta: { |
| 1120 | backupRetentionDays: 7, |
| 1121 | }, |
| 1122 | pagination: { |
| 1123 | hasMore: false, |
| 1124 | limit: 1, |
| 1125 | }, |
| 1126 | }); |
| 1127 | expect(captured.map((request) => request.pathname)).toEqual([ |
| 1128 | "/v1/databases/database-1/backups", |
| 1129 | ]); |
| 1130 | expect(captured[0]?.search).toBe("?limit=1"); |
| 1131 | }), |
| 1132 | ).pipe(Effect.provide(layer)); |
| 1133 | }); |
| 1134 | |
| 1135 | it.effect("reads non-paginated region endpoints", () => { |
| 1136 | const { layer } = harness(); |
| 1137 | |
| 1138 | return withClient((client) => |
| 1139 | Effect.gen(function* () { |
| 1140 | const regions = yield* client.listRegions({ product: "postgres" }); |
| 1141 | const postgresRegions = yield* client.listPostgresRegions(); |
| 1142 | const accelerateRegions = yield* client.listAccelerateRegions(); |
| 1143 | |
| 1144 | expect(regions.map((region) => region.id)).toEqual(["us-east-1"]); |
| 1145 | expect(postgresRegions.map((region) => region.id)).toEqual(["us-east-1"]); |
| 1146 | expect(accelerateRegions.map((region) => region.id)).toEqual(["us-east-1"]); |
| 1147 | }), |
| 1148 | ).pipe(Effect.provide(layer)); |
| 1149 | }); |
| 1150 | |
| 1151 | it.effect("builds authenticated deployment log stream requests", () => { |
| 1152 | const { layer, captured } = harness("https://api.prisma.test"); |
| 1153 | |
| 1154 | return withClient((client) => |
| 1155 | Effect.gen(function* () { |
| 1156 | const request = yield* client.getDeploymentLogsRequest("deployment-1", { |
| 1157 | tail: 100, |
| 1158 | fromStart: true, |
| 1159 | cursor: "byte-42", |
| 1160 | }); |
| 1161 | expect(request.url).toBe( |
| 1162 | "wss://api.prisma.test/v1/deployments/deployment-1/logs?tail=100&cursor=byte-42&from_start=true", |
| 1163 | ); |
| 1164 | expect(Redacted.value(request.headers.Authorization)).toBe("Bearer test-token"); |
| 1165 | expect(captured).toEqual([]); |
| 1166 | }), |
| 1167 | ).pipe(Effect.provide(layer)); |
| 1168 | }); |
| 1169 | |
| 1170 | it.effect("maps every supported Management API operation to its route", () => { |
| 1171 | const { layer, captured } = routeCoverageHarness(); |
| 1172 | |
| 1173 | return withClient((client) => |
| 1174 | Effect.gen(function* () { |
| 1175 | yield* client.listWorkspaces({ limit: 1 }); |
| 1176 | yield* client.getWorkspace("workspace-1"); |
| 1177 | yield* client.getCurrentPrincipal(); |
| 1178 | yield* client.listRegions({ product: "postgres" }); |
| 1179 | yield* client.listPostgresRegions(); |
| 1180 | yield* client.listAccelerateRegions(); |
| 1181 | |
| 1182 | yield* client.listProjects({ limit: 1 }); |
| 1183 | yield* client.getProject("project-1"); |
| 1184 | yield* client.createProject({ name: "app", region: "us-east-1" }); |
| 1185 | yield* client.updateProject("project-1", { name: "renamed" }); |
| 1186 | yield* client.deleteProject("project-1"); |
| 1187 | yield* client.transferProject("project-1", { |
| 1188 | recipientAccessToken: "recipient-token", |
| 1189 | }); |
| 1190 | |
| 1191 | yield* client.listDatabases({ |
| 1192 | projectId: "project-1", |
| 1193 | branchGitName: "main", |
| 1194 | }); |
| 1195 | yield* client.listProjectDatabases("project-1", { limit: 1 }); |
| 1196 | yield* client.getDatabase("database-1"); |
| 1197 | yield* client.createDatabase({ |
| 1198 | projectId: "project-1", |
| 1199 | name: "main", |
| 1200 | }); |
| 1201 | yield* client.createProjectDatabase("project-1", { |
| 1202 | name: "main", |
| 1203 | source: { |
| 1204 | type: "backup", |
| 1205 | databaseId: "database-source", |
| 1206 | backupId: "backup-1", |
| 1207 | }, |
| 1208 | }); |
| 1209 | yield* client.updateDatabase("database-1", { name: "main-2" }); |
| 1210 | yield* client.deleteDatabase("database-1"); |
| 1211 | yield* client.listBackups("database-1", { limit: 1 }); |
| 1212 | yield* client.restoreDatabase("database-1", { |
| 1213 | source: { |
| 1214 | type: "backup", |
| 1215 | databaseId: "database-source", |
| 1216 | backupId: "backup-1", |
| 1217 | }, |
| 1218 | }); |
| 1219 | yield* client.getDatabaseUsage("database-1", { |
| 1220 | startDate: "2026-01-01", |
| 1221 | }); |
| 1222 | |
| 1223 | yield* client.listConnections({ databaseId: "database-1" }); |
| 1224 | yield* client.listDatabaseConnections("database-1", { limit: 1 }); |
| 1225 | yield* client.getConnection("connection-1"); |
| 1226 | yield* client.createConnection({ |
| 1227 | databaseId: "database-1", |
| 1228 | name: "direct", |
| 1229 | }); |
| 1230 | yield* client.createDatabaseConnection("database-1", { |
| 1231 | name: "direct", |
| 1232 | }); |
| 1233 | yield* client.deleteConnection("connection-1"); |
| 1234 | yield* client.rotateConnection("connection-1"); |
| 1235 | |
| 1236 | yield* client.listBranches("project-1", { gitName: "main" }); |
| 1237 | yield* client.getBranch("branch-1"); |
| 1238 | yield* client.createBranch("project-1", { gitName: "main" }); |
| 1239 | yield* client.updateBranch("branch-1", { isDefault: true }); |
| 1240 | yield* client.deleteBranch("branch-1"); |
| 1241 | |
| 1242 | yield* client.listBuckets({ projectId: "project-1" }); |
| 1243 | yield* client.getBucket("bucket-1"); |
| 1244 | yield* client.createBucket({ |
| 1245 | projectId: "project-1", |
| 1246 | name: "uploads", |
| 1247 | }); |
| 1248 | yield* client.deleteBucket("bucket-1"); |
| 1249 | yield* client.listBucketKeys("bucket-1", { limit: 1 }); |
| 1250 | yield* client.createBucketKey("bucket-1", { role: "read_write" }); |
| 1251 | yield* client.deleteBucketKey("bucket-1", "key-1"); |
| 1252 | |
| 1253 | yield* client.getCustomDomain("domain-1"); |
| 1254 | yield* client.deleteCustomDomain("domain-1"); |
| 1255 | yield* client.retryCustomDomain("domain-1"); |
| 1256 | |
| 1257 | yield* client.listEnvironmentVariables({ |
| 1258 | projectId: "project-1", |
| 1259 | class: "production", |
| 1260 | key: "TOKEN", |
| 1261 | }); |
| 1262 | yield* client.getEnvironmentVariable("env-1"); |
| 1263 | yield* client.createEnvironmentVariable({ |
| 1264 | projectId: "project-1", |
| 1265 | class: "production", |
| 1266 | key: "TOKEN", |
| 1267 | value: "secret", |
| 1268 | }); |
| 1269 | yield* client.updateEnvironmentVariable("env-1", { |
| 1270 | value: "secret-2", |
| 1271 | }); |
| 1272 | yield* client.deleteEnvironmentVariable("env-1"); |
| 1273 | |
| 1274 | yield* client.listIntegrations({ workspaceId: "workspace-1" }); |
| 1275 | yield* client.listWorkspaceIntegrations("workspace-1", { limit: 1 }); |
| 1276 | yield* client.getIntegration("integration-1"); |
| 1277 | yield* client.deleteIntegration("integration-1"); |
| 1278 | yield* client.revokeWorkspaceIntegration("workspace-1", "client-1"); |
| 1279 | |
| 1280 | yield* client.listScmInstallations({ workspaceId: "workspace-1" }); |
| 1281 | yield* client.createScmInstallIntent({ |
| 1282 | provider: "github", |
| 1283 | workspaceId: "workspace-1", |
| 1284 | }); |
| 1285 | yield* client.listScmInstallationRepositories("scminstall-1", { |
| 1286 | limit: 10, |
| 1287 | }); |
| 1288 | |
| 1289 | yield* client.listSourceRepositories({ projectId: "project-1" }); |
| 1290 | yield* client.getSourceRepository("repo-1"); |
| 1291 | yield* client.createSourceRepository({ |
| 1292 | projectId: "project-1", |
| 1293 | provider: "github", |
| 1294 | providerRepositoryId: 123, |
| 1295 | installationId: "scminstall-1", |
| 1296 | }); |
| 1297 | yield* client.deleteSourceRepository("repo-1"); |
| 1298 | |
| 1299 | yield* client.listApps({ |
| 1300 | projectId: "project-1", |
| 1301 | branchGitName: "main", |
| 1302 | }); |
| 1303 | yield* client.getApp("app-1"); |
| 1304 | yield* client.createApp({ |
| 1305 | projectId: "project-1", |
| 1306 | displayName: "web", |
| 1307 | }); |
| 1308 | yield* client.updateApp("app-1", { displayName: "web-2" }); |
| 1309 | yield* client.deleteApp("app-1"); |
| 1310 | yield* client.promoteApp("app-1", { deploymentId: "deployment-1" }); |
| 1311 | yield* client.rollbackApp("app-1", { deploymentId: "deployment-1" }); |
| 1312 | yield* client.listAppDomains("app-1"); |
| 1313 | yield* client.createAppDomain("app-1", { |
| 1314 | hostname: "web.example.com", |
| 1315 | }); |
| 1316 | yield* client.listAppDeployments("app-1", { limit: 1 }); |
| 1317 | yield* client.createAppDeployment("app-1", { |
| 1318 | skipCodeUpload: true, |
| 1319 | }); |
| 1320 | yield* client.getDeployment("deployment-1"); |
| 1321 | yield* client.deleteDeployment("deployment-1"); |
| 1322 | yield* client.startDeployment("deployment-1"); |
| 1323 | yield* client.stopDeployment("deployment-1"); |
| 1324 | const deploymentLogsRequest = yield* client.getDeploymentLogsRequest("deployment-1", { |
| 1325 | tail: 10, |
| 1326 | }); |
| 1327 | expect(deploymentLogsRequest.url).toBe( |
| 1328 | "wss://api.prisma.test/v1/deployments/deployment-1/logs?tail=10", |
| 1329 | ); |
| 1330 | expect(Redacted.value(deploymentLogsRequest.headers.Authorization)).toBe( |
| 1331 | "Bearer test-token", |
| 1332 | ); |
| 1333 | const buildLogsRequest = yield* client.getBuildLogsRequest("build-1", { |
| 1334 | follow: true, |
| 1335 | cursor: "cursor-1", |
| 1336 | }); |
| 1337 | expect(buildLogsRequest.url).toBe( |
| 1338 | "https://api.prisma.test/v1/builds/build-1/logs?follow=true&cursor=cursor-1", |
| 1339 | ); |
| 1340 | expect(Redacted.value(buildLogsRequest.headers.Authorization)).toBe("Bearer test-token"); |
| 1341 | expect(buildLogsRequest.headers.Accept).toBe("application/x-ndjson"); |
| 1342 | |
| 1343 | expect( |
| 1344 | captured.map((request) => [request.method, `${request.pathname}${request.search}`]), |
| 1345 | ).toEqual([ |
| 1346 | ["GET", "/v1/workspaces?limit=1"], |
| 1347 | ["GET", "/v1/workspaces/workspace-1"], |
| 1348 | ["GET", "/v1/me"], |
| 1349 | ["GET", "/v1/regions?product=postgres"], |
| 1350 | ["GET", "/v1/regions/postgres"], |
| 1351 | ["GET", "/v1/regions/accelerate"], |
| 1352 | ["GET", "/v1/projects?limit=1"], |
| 1353 | ["GET", "/v1/projects/project-1"], |
| 1354 | ["POST", "/v1/projects"], |
| 1355 | ["PATCH", "/v1/projects/project-1"], |
| 1356 | ["DELETE", "/v1/projects/project-1"], |
| 1357 | ["POST", "/v1/projects/project-1/transfer"], |
| 1358 | ["GET", "/v1/databases?projectId=project-1&branchGitName=main"], |
| 1359 | ["GET", "/v1/projects/project-1/databases?limit=1"], |
| 1360 | ["GET", "/v1/databases/database-1"], |
| 1361 | ["POST", "/v1/databases"], |
| 1362 | ["POST", "/v1/projects/project-1/databases"], |
| 1363 | ["PATCH", "/v1/databases/database-1"], |
| 1364 | ["DELETE", "/v1/databases/database-1"], |
| 1365 | ["GET", "/v1/databases/database-1/backups?limit=1"], |
| 1366 | ["POST", "/v1/databases/database-1/restore"], |
| 1367 | ["GET", "/v1/databases/database-1/usage?startDate=2026-01-01"], |
| 1368 | ["GET", "/v1/connections?databaseId=database-1"], |
| 1369 | ["GET", "/v1/databases/database-1/connections?limit=1"], |
| 1370 | ["GET", "/v1/connections/connection-1"], |
| 1371 | ["POST", "/v1/connections"], |
| 1372 | ["POST", "/v1/databases/database-1/connections"], |
| 1373 | ["DELETE", "/v1/connections/connection-1"], |
| 1374 | ["POST", "/v1/connections/connection-1/rotate"], |
| 1375 | ["GET", "/v1/projects/project-1/branches?gitName=main"], |
| 1376 | ["GET", "/v1/branches/branch-1"], |
| 1377 | ["POST", "/v1/projects/project-1/branches"], |
| 1378 | ["PATCH", "/v1/branches/branch-1"], |
| 1379 | ["DELETE", "/v1/branches/branch-1"], |
| 1380 | ["GET", "/v1/buckets?projectId=project-1"], |
| 1381 | ["GET", "/v1/buckets/bucket-1"], |
| 1382 | ["POST", "/v1/buckets"], |
| 1383 | ["DELETE", "/v1/buckets/bucket-1"], |
| 1384 | ["GET", "/v1/buckets/bucket-1/keys?limit=1"], |
| 1385 | ["POST", "/v1/buckets/bucket-1/keys"], |
| 1386 | ["DELETE", "/v1/buckets/bucket-1/keys/key-1"], |
| 1387 | ["GET", "/v1/domains/domain-1"], |
| 1388 | ["DELETE", "/v1/domains/domain-1"], |
| 1389 | ["POST", "/v1/domains/domain-1/retry"], |
| 1390 | ["GET", "/v1/environment-variables?projectId=project-1&class=production&key=TOKEN"], |
| 1391 | ["GET", "/v1/environment-variables/env-1"], |
| 1392 | ["POST", "/v1/environment-variables"], |
| 1393 | ["PATCH", "/v1/environment-variables/env-1"], |
| 1394 | ["DELETE", "/v1/environment-variables/env-1"], |
| 1395 | ["GET", "/v1/integrations?workspaceId=workspace-1"], |
| 1396 | ["GET", "/v1/workspaces/workspace-1/integrations?limit=1"], |
| 1397 | ["GET", "/v1/integrations/integration-1"], |
| 1398 | ["DELETE", "/v1/integrations/integration-1"], |
| 1399 | ["DELETE", "/v1/workspaces/workspace-1/integrations/client-1"], |
| 1400 | ["GET", "/v1/scm-installations?workspaceId=workspace-1"], |
| 1401 | ["POST", "/v1/scm-installations/install-intents"], |
| 1402 | ["GET", "/v1/scm-installations/scminstall-1/repositories?limit=10"], |
| 1403 | ["GET", "/v1/source-repositories?projectId=project-1"], |
| 1404 | ["GET", "/v1/source-repositories/repo-1"], |
| 1405 | ["POST", "/v1/source-repositories"], |
| 1406 | ["DELETE", "/v1/source-repositories/repo-1"], |
| 1407 | ["GET", "/v1/apps?projectId=project-1&branchGitName=main"], |
| 1408 | ["GET", "/v1/apps/app-1"], |
| 1409 | ["POST", "/v1/apps"], |
| 1410 | ["PATCH", "/v1/apps/app-1"], |
| 1411 | ["DELETE", "/v1/apps/app-1"], |
| 1412 | ["POST", "/v1/apps/app-1/promote"], |
| 1413 | ["POST", "/v1/apps/app-1/rollback"], |
| 1414 | ["GET", "/v1/apps/app-1/domains"], |
| 1415 | ["POST", "/v1/apps/app-1/domains"], |
| 1416 | ["GET", "/v1/apps/app-1/deployments?limit=1"], |
| 1417 | ["POST", "/v1/apps/app-1/deployments"], |
| 1418 | ["GET", "/v1/deployments/deployment-1"], |
| 1419 | ["DELETE", "/v1/deployments/deployment-1"], |
| 1420 | ["POST", "/v1/deployments/deployment-1/start"], |
| 1421 | ["POST", "/v1/deployments/deployment-1/stop"], |
| 1422 | ]); |
| 1423 | expect(routeInventoryFrom(captured)).toEqual(expectedManagementApiRoutes); |
| 1424 | expect(expectedManagementApiRoutes).toHaveLength(78); |
| 1425 | expect(captured[11]?.bodyJson).toEqual({ |
| 1426 | recipientAccessToken: "recipient-token", |
| 1427 | }); |
| 1428 | const restoreRequest = captured.find( |
| 1429 | (request) => |
| 1430 | request.method === "POST" && request.pathname === "/v1/databases/database-1/restore", |
| 1431 | ); |
| 1432 | expect(restoreRequest?.bodyJson).toEqual({ |
| 1433 | source: { |
| 1434 | type: "backup", |
| 1435 | databaseId: "database-source", |
| 1436 | backupId: "backup-1", |
| 1437 | }, |
| 1438 | }); |
| 1439 | const projectDatabaseRequest = captured.find( |
| 1440 | (request) => |
| 1441 | request.method === "POST" && request.pathname === "/v1/projects/project-1/databases", |
| 1442 | ); |
| 1443 | expect(projectDatabaseRequest?.bodyJson).toEqual({ |
| 1444 | name: "main", |
| 1445 | source: { |
| 1446 | type: "backup", |
| 1447 | databaseId: "database-source", |
| 1448 | backupId: "backup-1", |
| 1449 | }, |
| 1450 | }); |
| 1451 | |
| 1452 | const createEnvRequest = captured.find( |
| 1453 | (request) => |
| 1454 | request.method === "POST" && request.pathname === "/v1/environment-variables", |
| 1455 | ); |
| 1456 | expect(createEnvRequest?.bodyJson).toEqual({ |
| 1457 | projectId: "project-1", |
| 1458 | class: "production", |
| 1459 | key: "TOKEN", |
| 1460 | value: "secret", |
| 1461 | }); |
| 1462 | |
| 1463 | const updateEnvRequest = captured.find( |
| 1464 | (request) => |
| 1465 | request.method === "PATCH" && request.pathname === "/v1/environment-variables/env-1", |
| 1466 | ); |
| 1467 | expect(updateEnvRequest?.bodyJson).toEqual({ |
| 1468 | value: "secret-2", |
| 1469 | }); |
| 1470 | |
| 1471 | const sourceRepositoryRequest = captured.find( |
| 1472 | (request) => request.method === "POST" && request.pathname === "/v1/source-repositories", |
| 1473 | ); |
| 1474 | expect(sourceRepositoryRequest?.bodyJson).toEqual({ |
| 1475 | projectId: "project-1", |
| 1476 | provider: "github", |
| 1477 | providerRepositoryId: 123, |
| 1478 | installationId: "scminstall-1", |
| 1479 | }); |
| 1480 | |
| 1481 | const installIntentRequest = captured.find( |
| 1482 | (request) => |
| 1483 | request.method === "POST" && |
| 1484 | request.pathname === "/v1/scm-installations/install-intents", |
| 1485 | ); |
| 1486 | expect(installIntentRequest?.bodyJson).toEqual({ |
| 1487 | provider: "github", |
| 1488 | workspaceId: "workspace-1", |
| 1489 | }); |
| 1490 | }), |
| 1491 | ).pipe(Effect.provide(layer)); |
| 1492 | }); |
| 1493 | |
| 1494 | it("matches the pinned Management API route contract", () => { |
| 1495 | expect(expectedManagementApiRoutes).toEqual([...productionManagementApiRoutes].sort()); |
| 1496 | }); |
| 1497 | }); |