The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | PrismaApiError, |
| 3 | PrismaClient, |
| 4 | type PrismaManagementClient, |
| 5 | } from "@alchemy.run/prisma/Client"; |
| 6 | import { connectEnvKeys } from "@alchemy.run/prisma/Connect"; |
| 7 | import { Connection, ConnectionProvider } from "@alchemy.run/prisma/Connection"; |
| 8 | import type { DatabaseConnection, DatabaseConnectionWithSecrets } from "@alchemy.run/prisma/Types"; |
| 9 | import { describe, expect, it } from "@alchemy.run/test"; |
| 10 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 11 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 12 | import * as Provider from "alchemy/Provider"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as Redacted from "effect/Redacted"; |
| 16 | import { |
| 17 | conflict, |
| 18 | data, |
| 19 | json, |
| 20 | makeFakeManagementApi, |
| 21 | notFound, |
| 22 | page, |
| 23 | unhandled, |
| 24 | } from "./fixtures/FakeManagementApi.ts"; |
| 25 | |
| 26 | const createdAt = "2026-01-01T00:00:00.000Z"; |
| 27 | const instanceId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 28 | |
| 29 | const connection = (id: string, name: string): DatabaseConnection => ({ |
| 30 | id, |
| 31 | type: "connection", |
| 32 | url: `https://api.prisma.test/v1/connections/${id}`, |
| 33 | name, |
| 34 | createdAt, |
| 35 | kind: "postgres", |
| 36 | endpoints: { |
| 37 | direct: { host: "db.prisma.test", port: 5432 }, |
| 38 | pooled: { host: "pooled.db.prisma.test", port: 5432 }, |
| 39 | }, |
| 40 | database: { |
| 41 | id: "database-1", |
| 42 | url: "https://api.prisma.test/v1/databases/database-1", |
| 43 | name: "main", |
| 44 | }, |
| 45 | }); |
| 46 | |
| 47 | const withSecrets = (value: DatabaseConnection, suffix: string): DatabaseConnectionWithSecrets => ({ |
| 48 | ...value, |
| 49 | endpoints: { |
| 50 | direct: { host: "db.prisma.test", port: 5432, connectionString: `postgres://direct-${suffix}` }, |
| 51 | pooled: { |
| 52 | host: "pooled.db.prisma.test", |
| 53 | port: 5432, |
| 54 | connectionString: `postgres://pooled-${suffix}`, |
| 55 | }, |
| 56 | }, |
| 57 | }); |
| 58 | |
| 59 | const liveProviderContext = Layer.succeed(AlchemyContext, { |
| 60 | dotAlchemy: ".alchemy-test", |
| 61 | dev: false, |
| 62 | adopt: false, |
| 63 | }); |
| 64 | |
| 65 | const providerLayer = (client: PrismaManagementClient) => |
| 66 | ConnectionProvider().pipe( |
| 67 | Layer.provide(Layer.succeed(PrismaClient, client)), |
| 68 | Layer.provide(liveProviderContext), |
| 69 | ); |
| 70 | |
| 71 | /** |
| 72 | * Serve the Management API's connection routes from the same hermetic |
| 73 | * handlers these suites already declare. The handlers are synchronous, so the |
| 74 | * fake runs them directly and maps their results onto the wire: a handler |
| 75 | * returning `undefined` is a 404, everything else a `{ data }` envelope. |
| 76 | */ |
| 77 | interface ConnectionHandlers { |
| 78 | listDatabaseConnections?: (databaseId: string, query?: any) => Effect.Effect<any, any>; |
| 79 | getConnection?: (id: string) => Effect.Effect<any, any>; |
| 80 | createConnection?: (input: any) => Effect.Effect<any, any>; |
| 81 | rotateConnection?: (id: string) => Effect.Effect<any, any>; |
| 82 | deleteConnection?: (id: string) => Effect.Effect<any, any>; |
| 83 | } |
| 84 | |
| 85 | const connectionApi = (handlers: ConnectionHandlers) => |
| 86 | makeFakeManagementApi((request) => { |
| 87 | const segments = request.pathname.split("/").filter((s) => s.length > 0); |
| 88 | const respond = (value: unknown) => (value === undefined ? notFound("not found") : data(value)); |
| 89 | |
| 90 | if ( |
| 91 | segments.length === 4 && |
| 92 | segments[1] === "databases" && |
| 93 | segments[3] === "connections" && |
| 94 | request.method === "GET" |
| 95 | ) { |
| 96 | return page(Effect.runSync(handlers.listDatabaseConnections!(segments[2]!))); |
| 97 | } |
| 98 | if (request.pathname === "/v1/connections" && request.method === "POST") { |
| 99 | const created = Effect.runSync(handlers.createConnection!(request.bodyJson)); |
| 100 | // A handler may answer with a Response directly to inject a status. |
| 101 | return created instanceof Response ? created : data(created, { status: 201 }); |
| 102 | } |
| 103 | if ( |
| 104 | segments.length === 4 && |
| 105 | segments[1] === "connections" && |
| 106 | segments[3] === "rotate" && |
| 107 | request.method === "POST" |
| 108 | ) { |
| 109 | return respond(Effect.runSync(handlers.rotateConnection!(segments[2]!))); |
| 110 | } |
| 111 | if (segments.length === 3 && segments[1] === "connections") { |
| 112 | const id = segments[2]!; |
| 113 | if (request.method === "GET") { |
| 114 | return respond(Effect.runSync(handlers.getConnection!(id))); |
| 115 | } |
| 116 | if (request.method === "DELETE") { |
| 117 | if (handlers.deleteConnection) { |
| 118 | Effect.runSync(handlers.deleteConnection(id)); |
| 119 | } |
| 120 | return json(null, { status: 204 }); |
| 121 | } |
| 122 | } |
| 123 | return unhandled(request); |
| 124 | }); |
| 125 | |
| 126 | const connectionProps = { database: "database-1", name: "api" }; |
| 127 | |
| 128 | const reconcileInput = ( |
| 129 | output?: Connection["Attributes"], |
| 130 | olds?: typeof connectionProps, |
| 131 | reconcileInstanceId = instanceId, |
| 132 | ) => ({ |
| 133 | id: "Connection", |
| 134 | fqn: "Connection", |
| 135 | instanceId: reconcileInstanceId, |
| 136 | news: connectionProps, |
| 137 | olds, |
| 138 | output, |
| 139 | session: undefined as never, |
| 140 | bindings: [], |
| 141 | }); |
| 142 | |
| 143 | const readInput = ( |
| 144 | connectionsOutput?: Connection["Attributes"], |
| 145 | readInstanceId = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 146 | ) => ({ |
| 147 | id: "Connection", |
| 148 | fqn: "Connection", |
| 149 | instanceId: readInstanceId, |
| 150 | olds: { database: "database-1", name: "api" }, |
| 151 | output: connectionsOutput, |
| 152 | }); |
| 153 | |
| 154 | describe( |
| 155 | "Prisma Connection provider", |
| 156 | { tags: ["unit", "provider:prisma", "provider:prisma:connection", "local"] }, |
| 157 | () => { |
| 158 | it.effect( |
| 159 | "recovers a crash after create without creating another key", |
| 160 | () => { |
| 161 | const connections: DatabaseConnection[] = []; |
| 162 | let creates = 0; |
| 163 | let rotations = 0; |
| 164 | const client = { |
| 165 | listDatabaseConnections: () => Effect.succeed(connections), |
| 166 | createConnection: (input: { name: string }) => |
| 167 | Effect.sync(() => { |
| 168 | creates += 1; |
| 169 | const created = connection("connection-1", input.name); |
| 170 | connections.push(created); |
| 171 | return withSecrets(created, "created"); |
| 172 | }), |
| 173 | getConnection: (id: string) => |
| 174 | Effect.succeed(connections.find((item) => item.id === id)!), |
| 175 | rotateConnection: (id: string) => |
| 176 | Effect.sync(() => { |
| 177 | rotations += 1; |
| 178 | return withSecrets( |
| 179 | connections.find((item) => item.id === id)!, |
| 180 | "rotated", |
| 181 | ); |
| 182 | }), |
| 183 | } as unknown as PrismaManagementClient; |
| 184 | |
| 185 | return Effect.gen(function* () { |
| 186 | const provider = yield* Provider.findProvider(Connection); |
| 187 | const first = yield* provider.reconcile(reconcileInput()); |
| 188 | // Simulate create succeeding but state persistence failing. Refresh |
| 189 | // recovers the deterministic key without its one-time credentials. |
| 190 | const observed = yield* provider.read!(readInput(undefined, instanceId)); |
| 191 | expect(observed).toBeDefined(); |
| 192 | expect(Unowned.is(observed)).toBe(false); |
| 193 | expect(observed?.directConnectionString).toBeUndefined(); |
| 194 | const recovered = yield* provider.reconcile(reconcileInput(observed!)); |
| 195 | |
| 196 | expect(creates).toBe(1); |
| 197 | expect(rotations).toBe(1); |
| 198 | expect(connections[0]?.name).toBe("api-aaaaaaaaaaaa"); |
| 199 | expect(first.connectionId).toBe("connection-1"); |
| 200 | expect(recovered.connectionId).toBe("connection-1"); |
| 201 | expect(Redacted.value(recovered.directConnectionString!)).toBe( |
| 202 | "postgres://direct-rotated", |
| 203 | ); |
| 204 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 205 | }, |
| 206 | { tags: ["provider:prisma:database"] }, |
| 207 | ); |
| 208 | |
| 209 | it.effect( |
| 210 | "recovers credentials after a generated connection create conflict", |
| 211 | () => { |
| 212 | const connections: DatabaseConnection[] = []; |
| 213 | let creates = 0; |
| 214 | let rotations = 0; |
| 215 | const client = { |
| 216 | listDatabaseConnections: () => Effect.succeed(connections), |
| 217 | // Served as a real 409 below; distilled's status matcher is what |
| 218 | // turns it into the `Conflict` tag the recovery path catches. |
| 219 | createConnection: (input: { name: string }) => |
| 220 | Effect.sync(() => { |
| 221 | creates += 1; |
| 222 | connections.push(connection("connection-1", input.name)); |
| 223 | return conflict("already exists"); |
| 224 | }), |
| 225 | rotateConnection: (id: string) => |
| 226 | Effect.sync(() => { |
| 227 | rotations += 1; |
| 228 | return withSecrets( |
| 229 | connections.find((item) => item.id === id)!, |
| 230 | "recovered", |
| 231 | ); |
| 232 | }), |
| 233 | } as unknown as PrismaManagementClient; |
| 234 | |
| 235 | return Effect.gen(function* () { |
| 236 | const provider = yield* Connection.Provider; |
| 237 | const recovered = yield* provider.reconcile(reconcileInput()); |
| 238 | |
| 239 | expect(creates).toBe(1); |
| 240 | expect(rotations).toBe(1); |
| 241 | expect(recovered.connectionId).toBe("connection-1"); |
| 242 | expect(Redacted.value(recovered.directConnectionString!)).toBe( |
| 243 | "postgres://direct-recovered", |
| 244 | ); |
| 245 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 246 | }, |
| 247 | { tags: ["provider:prisma:database"] }, |
| 248 | ); |
| 249 | |
| 250 | it.effect( |
| 251 | "recovers exact generated credentials and supports rotation", |
| 252 | () => { |
| 253 | const existing = connection("connection-1", "api-aaaaaaaaaaaa"); |
| 254 | let rotations = 0; |
| 255 | const client = { |
| 256 | getConnection: () => Effect.succeed(existing), |
| 257 | rotateConnection: () => |
| 258 | Effect.sync(() => { |
| 259 | rotations += 1; |
| 260 | return withSecrets(existing, "adopted"); |
| 261 | }), |
| 262 | } as unknown as PrismaManagementClient; |
| 263 | const output: Connection["Attributes"] = { |
| 264 | connectionId: existing.id, |
| 265 | connectionName: existing.name, |
| 266 | databaseId: existing.database.id, |
| 267 | kind: existing.kind, |
| 268 | createdAt, |
| 269 | directConnectionString: undefined, |
| 270 | pooledConnectionString: undefined, |
| 271 | accelerateConnectionString: undefined, |
| 272 | host: "db.prisma.test", |
| 273 | user: undefined, |
| 274 | password: undefined, |
| 275 | databaseUrl: undefined, |
| 276 | origin: undefined, |
| 277 | pooledOrigin: undefined, |
| 278 | }; |
| 279 | |
| 280 | return Effect.gen(function* () { |
| 281 | const provider = yield* Connection.Provider; |
| 282 | const recovered = yield* provider.reconcile({ |
| 283 | ...reconcileInput(output), |
| 284 | olds: undefined, |
| 285 | }); |
| 286 | expect(Redacted.value(recovered.directConnectionString!)).toBe( |
| 287 | "postgres://direct-adopted", |
| 288 | ); |
| 289 | expect(rotations).toBe(1); |
| 290 | |
| 291 | const stable = yield* provider.reconcile(reconcileInput(recovered, connectionProps)); |
| 292 | expect(Redacted.value(stable.directConnectionString!)).toBe("postgres://direct-adopted"); |
| 293 | expect(rotations).toBe(1); |
| 294 | |
| 295 | const optedIn = yield* provider.reconcile({ |
| 296 | ...reconcileInput(recovered, connectionProps), |
| 297 | news: { ...connectionProps, rotate: true }, |
| 298 | }); |
| 299 | expect(rotations).toBe(2); |
| 300 | expect(Redacted.value(optedIn.directConnectionString!)).toBe("postgres://direct-adopted"); |
| 301 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 302 | }, |
| 303 | { tags: ["provider:prisma:database"] }, |
| 304 | ); |
| 305 | |
| 306 | it.effect("rejects a blank connection name before calling Prisma", () => { |
| 307 | let listed = false; |
| 308 | const client = { |
| 309 | listDatabaseConnections: () => |
| 310 | Effect.sync(() => { |
| 311 | listed = true; |
| 312 | return []; |
| 313 | }), |
| 314 | } as unknown as PrismaManagementClient; |
| 315 | |
| 316 | return Effect.gen(function* () { |
| 317 | const provider = yield* Connection.Provider; |
| 318 | const error = yield* provider |
| 319 | .reconcile({ ...reconcileInput(), news: { database: "database-1", name: " " } }) |
| 320 | .pipe(Effect.flip); |
| 321 | |
| 322 | expect(String(error)).toContain("must contain at least one non-space character"); |
| 323 | expect(listed).toBe(false); |
| 324 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 325 | }); |
| 326 | |
| 327 | it.effect( |
| 328 | "does not rotate again when the rotate flag is reset", |
| 329 | () => { |
| 330 | const existing = connection("connection-1", "api-aaaaaaaaaaaa"); |
| 331 | let rotations = 0; |
| 332 | const client = { |
| 333 | getConnection: () => Effect.succeed(existing), |
| 334 | rotateConnection: () => |
| 335 | Effect.sync(() => { |
| 336 | rotations += 1; |
| 337 | return withSecrets(existing, "unexpected"); |
| 338 | }), |
| 339 | } as unknown as PrismaManagementClient; |
| 340 | const output: Connection["Attributes"] = { |
| 341 | connectionId: existing.id, |
| 342 | connectionName: existing.name, |
| 343 | databaseId: existing.database.id, |
| 344 | kind: existing.kind, |
| 345 | createdAt, |
| 346 | directConnectionString: Redacted.make("postgres://direct-current"), |
| 347 | pooledConnectionString: Redacted.make("postgres://pooled-current"), |
| 348 | accelerateConnectionString: undefined, |
| 349 | host: "db.prisma.test", |
| 350 | user: "app", |
| 351 | password: Redacted.make("current-password"), |
| 352 | databaseUrl: Redacted.make("postgres://pooled-current"), |
| 353 | origin: undefined, |
| 354 | pooledOrigin: undefined, |
| 355 | }; |
| 356 | |
| 357 | return Effect.gen(function* () { |
| 358 | const provider = yield* Connection.Provider; |
| 359 | const recovered = yield* provider.reconcile({ |
| 360 | ...reconcileInput(output), |
| 361 | news: { ...connectionProps, rotate: false }, |
| 362 | olds: { ...connectionProps, rotate: true }, |
| 363 | }); |
| 364 | |
| 365 | expect(rotations).toBe(0); |
| 366 | expect(Redacted.value(recovered.directConnectionString!)).toBe( |
| 367 | "postgres://direct-current", |
| 368 | ); |
| 369 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 370 | }, |
| 371 | { tags: ["provider:prisma:database"] }, |
| 372 | ); |
| 373 | |
| 374 | it.effect( |
| 375 | "rejects a persisted connection with mismatched identity", |
| 376 | () => { |
| 377 | const wrong = { |
| 378 | ...connection("connection-1", "api-aaaaaaaaaaaa"), |
| 379 | database: { |
| 380 | id: "database-foreign", |
| 381 | url: "https://api.prisma.test/v1/databases/database-foreign", |
| 382 | name: "foreign", |
| 383 | }, |
| 384 | }; |
| 385 | let rotations = 0; |
| 386 | const client = { |
| 387 | getConnection: () => Effect.succeed(wrong), |
| 388 | rotateConnection: () => |
| 389 | Effect.sync(() => { |
| 390 | rotations += 1; |
| 391 | return withSecrets(wrong, "unexpected"); |
| 392 | }), |
| 393 | } as unknown as PrismaManagementClient; |
| 394 | const output: Connection["Attributes"] = { |
| 395 | connectionId: wrong.id, |
| 396 | connectionName: wrong.name, |
| 397 | databaseId: "database-foreign", |
| 398 | kind: wrong.kind, |
| 399 | createdAt, |
| 400 | directConnectionString: undefined, |
| 401 | pooledConnectionString: undefined, |
| 402 | accelerateConnectionString: undefined, |
| 403 | host: undefined, |
| 404 | user: undefined, |
| 405 | password: undefined, |
| 406 | databaseUrl: undefined, |
| 407 | origin: undefined, |
| 408 | pooledOrigin: undefined, |
| 409 | }; |
| 410 | |
| 411 | return Effect.gen(function* () { |
| 412 | const provider = yield* Connection.Provider; |
| 413 | const diff = yield* provider.diff!({ |
| 414 | id: "Connection", |
| 415 | fqn: "Connection", |
| 416 | instanceId, |
| 417 | olds: connectionProps, |
| 418 | news: connectionProps, |
| 419 | output, |
| 420 | oldBindings: [], |
| 421 | newBindings: [], |
| 422 | }); |
| 423 | expect(diff).toEqual({ action: "replace" }); |
| 424 | |
| 425 | const error = yield* provider |
| 426 | .reconcile(reconcileInput(output, connectionProps)) |
| 427 | .pipe(Effect.flip); |
| 428 | expect(String(error)).toContain("mismatched identity"); |
| 429 | expect(rotations).toBe(0); |
| 430 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 431 | }, |
| 432 | { tags: ["provider:prisma:database"] }, |
| 433 | ); |
| 434 | |
| 435 | it.effect( |
| 436 | "recovers its deterministic key as owned while creating", |
| 437 | () => { |
| 438 | const existing = connection("connection-1", "api-aaaaaaaaaaaa"); |
| 439 | const client = { |
| 440 | listDatabaseConnections: () => |
| 441 | Effect.succeed([existing, connection("connection-foreign", "api-bbbbbbbbbbbb")]), |
| 442 | } as unknown as PrismaManagementClient; |
| 443 | |
| 444 | return Effect.gen(function* () { |
| 445 | const provider = yield* Provider.findProvider(Connection); |
| 446 | const output = yield* provider.read!(readInput(undefined, instanceId)); |
| 447 | |
| 448 | expect(output?.connectionId).toBe("connection-1"); |
| 449 | expect(Unowned.is(output)).toBe(false); |
| 450 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 451 | }, |
| 452 | { tags: ["provider:prisma:database"] }, |
| 453 | ); |
| 454 | |
| 455 | it.effect( |
| 456 | "reports a foreign generated key as unowned on a cold probe", |
| 457 | () => { |
| 458 | const existing = connection("connection-1", "api-aaaaaaaaaaaa"); |
| 459 | const client = { |
| 460 | listDatabaseConnections: () => Effect.succeed([existing]), |
| 461 | } as unknown as PrismaManagementClient; |
| 462 | |
| 463 | return Effect.gen(function* () { |
| 464 | const provider = yield* Provider.findProvider(Connection); |
| 465 | const output = yield* provider.read!(readInput()); |
| 466 | |
| 467 | expect(output?.connectionId).toBe("connection-1"); |
| 468 | expect(Unowned.is(output)).toBe(true); |
| 469 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 470 | }, |
| 471 | { tags: ["provider:prisma:database"] }, |
| 472 | ); |
| 473 | |
| 474 | it.effect( |
| 475 | "preserves an adopted connection from an older generated instance name", |
| 476 | () => { |
| 477 | const currentInstanceId = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 478 | const existing = connection("connection-1", "api-aaaaaaaaaaaa"); |
| 479 | let creates = 0; |
| 480 | let rotations = 0; |
| 481 | const client = { |
| 482 | listDatabaseConnections: () => Effect.succeed([existing]), |
| 483 | getConnection: () => Effect.succeed(existing), |
| 484 | createConnection: () => |
| 485 | Effect.sync(() => { |
| 486 | creates += 1; |
| 487 | return withSecrets(existing, "unexpected"); |
| 488 | }), |
| 489 | rotateConnection: () => |
| 490 | Effect.sync(() => { |
| 491 | rotations += 1; |
| 492 | return withSecrets(existing, "adopted-old-generated"); |
| 493 | }), |
| 494 | } as unknown as PrismaManagementClient; |
| 495 | |
| 496 | return Effect.gen(function* () { |
| 497 | const provider = yield* Provider.findProvider(Connection); |
| 498 | const observed = yield* provider.read!(readInput(undefined, currentInstanceId)); |
| 499 | expect(observed?.connectionName).toBe("api-aaaaaaaaaaaa"); |
| 500 | expect(Unowned.is(observed)).toBe(true); |
| 501 | |
| 502 | const adopted = yield* provider.reconcile( |
| 503 | reconcileInput(observed!, undefined, currentInstanceId), |
| 504 | ); |
| 505 | expect(adopted.connectionName).toBe("api-aaaaaaaaaaaa"); |
| 506 | expect(adopted.directConnectionString).toBeUndefined(); |
| 507 | expect(creates).toBe(0); |
| 508 | expect(rotations).toBe(0); |
| 509 | |
| 510 | const diff = yield* provider.diff!({ |
| 511 | id: "Connection", |
| 512 | fqn: "Connection", |
| 513 | instanceId: currentInstanceId, |
| 514 | olds: connectionProps, |
| 515 | news: connectionProps, |
| 516 | output: adopted, |
| 517 | oldBindings: [], |
| 518 | newBindings: [], |
| 519 | }); |
| 520 | expect(diff).toBeUndefined(); |
| 521 | |
| 522 | const rotated = yield* provider.reconcile({ |
| 523 | ...reconcileInput(adopted, connectionProps, currentInstanceId), |
| 524 | news: { ...connectionProps, rotate: true }, |
| 525 | }); |
| 526 | expect(rotations).toBe(1); |
| 527 | expect(Redacted.value(rotated.directConnectionString!)).toBe( |
| 528 | "postgres://direct-adopted-old-generated", |
| 529 | ); |
| 530 | |
| 531 | const refreshed = yield* provider.read!(readInput(rotated, currentInstanceId)); |
| 532 | expect(refreshed?.connectionName).toBe("api-aaaaaaaaaaaa"); |
| 533 | expect(Redacted.value(refreshed!.directConnectionString!)).toBe( |
| 534 | "postgres://direct-adopted-old-generated", |
| 535 | ); |
| 536 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 537 | }, |
| 538 | { tags: ["provider:prisma:database"] }, |
| 539 | ); |
| 540 | |
| 541 | it.effect( |
| 542 | "preserves an adopted connection with its natural name", |
| 543 | () => { |
| 544 | const existing = connection("connection-1", "api"); |
| 545 | let creates = 0; |
| 546 | let rotations = 0; |
| 547 | const client = { |
| 548 | listDatabaseConnections: () => Effect.succeed([existing]), |
| 549 | getConnection: () => Effect.succeed(existing), |
| 550 | createConnection: () => |
| 551 | Effect.sync(() => { |
| 552 | creates += 1; |
| 553 | return withSecrets(existing, "unexpected"); |
| 554 | }), |
| 555 | rotateConnection: () => |
| 556 | Effect.sync(() => { |
| 557 | rotations += 1; |
| 558 | return withSecrets(existing, "adopted-natural"); |
| 559 | }), |
| 560 | } as unknown as PrismaManagementClient; |
| 561 | |
| 562 | return Effect.gen(function* () { |
| 563 | const provider = yield* Provider.findProvider(Connection); |
| 564 | const observed = yield* provider.read!(readInput(undefined, instanceId)); |
| 565 | expect(observed?.connectionName).toBe("api"); |
| 566 | expect(Unowned.is(observed)).toBe(true); |
| 567 | |
| 568 | const adopted = yield* provider.reconcile(reconcileInput(observed!)); |
| 569 | expect(adopted.connectionName).toBe("api"); |
| 570 | expect(adopted.directConnectionString).toBeUndefined(); |
| 571 | expect(creates).toBe(0); |
| 572 | expect(rotations).toBe(0); |
| 573 | |
| 574 | const rotated = yield* provider.reconcile({ |
| 575 | ...reconcileInput(adopted, connectionProps), |
| 576 | news: { ...connectionProps, rotate: true }, |
| 577 | }); |
| 578 | expect(rotations).toBe(1); |
| 579 | expect(Redacted.value(rotated.directConnectionString!)).toBe( |
| 580 | "postgres://direct-adopted-natural", |
| 581 | ); |
| 582 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 583 | }, |
| 584 | { tags: ["provider:prisma:database"] }, |
| 585 | ); |
| 586 | |
| 587 | it.effect( |
| 588 | "rejects drift from an adopted physical connection name", |
| 589 | () => { |
| 590 | const persisted = connection("connection-1", "api"); |
| 591 | const renamed = connection("connection-1", "api-bbbbbbbbbbbb"); |
| 592 | const client = { |
| 593 | getConnection: () => Effect.succeed(renamed), |
| 594 | } as unknown as PrismaManagementClient; |
| 595 | const output: Connection["Attributes"] = { |
| 596 | connectionId: persisted.id, |
| 597 | connectionName: persisted.name, |
| 598 | databaseId: persisted.database.id, |
| 599 | kind: persisted.kind, |
| 600 | createdAt: persisted.createdAt, |
| 601 | directConnectionString: undefined, |
| 602 | pooledConnectionString: undefined, |
| 603 | accelerateConnectionString: undefined, |
| 604 | host: persisted.endpoints.direct?.host, |
| 605 | user: undefined, |
| 606 | databaseUrl: undefined, |
| 607 | origin: undefined, |
| 608 | pooledOrigin: undefined, |
| 609 | password: undefined, |
| 610 | }; |
| 611 | |
| 612 | return Effect.gen(function* () { |
| 613 | const provider = yield* Provider.findProvider(Connection); |
| 614 | const error = yield* provider.read!(readInput(output, instanceId)).pipe(Effect.flip); |
| 615 | |
| 616 | expect(String(error)).toContain("no longer matches persisted"); |
| 617 | expect(String(error)).toContain("name 'api'"); |
| 618 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 619 | }, |
| 620 | { tags: ["provider:prisma:database"] }, |
| 621 | ); |
| 622 | |
| 623 | it.effect( |
| 624 | "fails ambiguous generated-name recovery", |
| 625 | () => { |
| 626 | const client = { |
| 627 | listDatabaseConnections: () => |
| 628 | Effect.succeed([ |
| 629 | connection("connection-1", "api-aaaaaaaaaaaa"), |
| 630 | connection("connection-2", "api-bbbbbbbbbbbb"), |
| 631 | ]), |
| 632 | } as unknown as PrismaManagementClient; |
| 633 | |
| 634 | return Effect.gen(function* () { |
| 635 | const provider = yield* Provider.findProvider(Connection); |
| 636 | const error = yield* provider.read!( |
| 637 | readInput(undefined, "cccccccccccccccccccccccccccccccc"), |
| 638 | ).pipe(Effect.flip); |
| 639 | |
| 640 | expect(String(error)).toContain("has 2 connections named"); |
| 641 | expect(String(error)).toContain("<instance-id>"); |
| 642 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 643 | }, |
| 644 | { tags: ["provider:prisma:database"] }, |
| 645 | ); |
| 646 | |
| 647 | it( |
| 648 | "does not collide binding keys after lossy normalization", |
| 649 | () => { |
| 650 | const hyphenated = connectEnvKeys({ FQN: "db-a", LogicalId: "db-a" }); |
| 651 | const underscored = connectEnvKeys({ FQN: "db_a", LogicalId: "db_a" }); |
| 652 | |
| 653 | expect(hyphenated.directConnectionString).not.toBe(underscored.directConnectionString); |
| 654 | }, |
| 655 | { tags: ["provider:prisma:connect"] }, |
| 656 | ); |
| 657 | |
| 658 | it.effect( |
| 659 | "defaults the connection name to the logical ID", |
| 660 | () => { |
| 661 | const created = withSecrets(connection("connection-1", "Connection-aaaaaaaaaaaa"), "new"); |
| 662 | let createdName: string | undefined; |
| 663 | const client = { |
| 664 | getConnection: () => Effect.succeed(undefined), |
| 665 | listDatabaseConnections: () => Effect.succeed([]), |
| 666 | createConnection: (input: { name: string }) => |
| 667 | Effect.sync(() => { |
| 668 | createdName = input.name; |
| 669 | return created; |
| 670 | }), |
| 671 | } as unknown as PrismaManagementClient; |
| 672 | |
| 673 | return Effect.gen(function* () { |
| 674 | const provider = yield* Connection.Provider; |
| 675 | yield* provider.reconcile({ ...reconcileInput(), news: { database: "database-1" } }); |
| 676 | // name omitted -> logical ID prefix + instance identity suffix |
| 677 | expect(createdName).toBe("Connection-aaaaaaaaaaaa"); |
| 678 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 679 | }, |
| 680 | { tags: ["provider:prisma:database"] }, |
| 681 | ); |
| 682 | |
| 683 | it.effect( |
| 684 | "materializes databaseUrl and parsed origins on reconcile", |
| 685 | () => { |
| 686 | const created = withSecrets(connection("connection-1", "api-aaaaaaaaaaaa"), "new"); |
| 687 | const client = { |
| 688 | getConnection: () => Effect.succeed(undefined), |
| 689 | listDatabaseConnections: () => Effect.succeed([]), |
| 690 | createConnection: () => Effect.succeed(created), |
| 691 | } as unknown as PrismaManagementClient; |
| 692 | |
| 693 | return Effect.gen(function* () { |
| 694 | const provider = yield* Connection.Provider; |
| 695 | const attrs = yield* provider.reconcile(reconcileInput()); |
| 696 | |
| 697 | // databaseUrl prefers the pooled endpoint for application traffic. |
| 698 | expect(Redacted.value(attrs.databaseUrl!)).toBe("postgres://pooled-new"); |
| 699 | // origin parses the direct connection string into Hyperdrive's shape. |
| 700 | expect(attrs.origin?.host).toBe("direct-new"); |
| 701 | expect(attrs.origin?.scheme).toBe("postgres"); |
| 702 | expect(attrs.pooledOrigin?.host).toBe("pooled-new"); |
| 703 | }).pipe(Effect.provide(providerLayer(client)), Effect.provide(connectionApi(client).layer)); |
| 704 | }, |
| 705 | { tags: ["provider:prisma:database"] }, |
| 706 | ); |
| 707 | }, |
| 708 | ); |