1import { PgClient } from "@effect/sql-pg"
2import { assert, it } from "@effect/vitest"
3import { Effect } from "effect"
4import * as Reactivity from "effect/reactivity/Reactivity"
5import { Duplex } from "node:stream"
6
7it.effect("withTransaction surfaces stream factory failures instead of defecting", () =>
8 Effect.gen(function*() {
9 const sql = yield* PgClient.make({
10 username: "test",
11 stream: () => {
12 throw new Error("stream factory failed")
13 }
14 })
15
16 const error = yield* Effect.flip(sql.withTransaction(sql`SELECT 1`))
17
18 assert.strictEqual(error.reason._tag, "ConnectionError")
19 assert.strictEqual(error.reason.operation, "connect")
20 }).pipe(
21 Effect.scoped,
22 Effect.provide(Reactivity.layer)
23 ))
24
25it.effect("withTransaction surfaces startup transport failures instead of defecting", () =>
26 Effect.gen(function*() {
27 const sql = yield* PgClient.make({
28 username: "test",
29 stream: () =>
30 new Duplex({
31 read() {},
32 write(_chunk, _encoding, callback) {
33 callback(new Error("startup write failed"))
34 }
35 })
36 })
37
38 const error = yield* Effect.flip(sql.withTransaction(sql`SELECT 1`))
39
40 assert.strictEqual(error.reason._tag, "ConnectionError")
41 assert.strictEqual(error.reason.operation, "connect")
42 }).pipe(
43 Effect.scoped,
44 Effect.provide(Reactivity.layer)
45 ))