The best way to control Claude Code, Codex and more from any computer or phone

main
ts 109 lines 3.83 KB
Raw
1 import { PgConnection } from "@effect/sql-pg"
2 import { assert, describe, it } from "@effect/vitest"
3 import { Effect, Fiber, Redacted } from "effect"
4 import * as TestClock from "effect/testing/TestClock"
5
6 describe("PgConnection config", () => {
7 it.effect("rejects sub-second keepalive before connecting", () =>
8 Effect.gen(function*() {
9 let connected = false
10 const error = yield* Effect.flip(PgConnection.make({
11 username: "test",
12 keepAlive: "999 millis",
13 stream: () => {
14 connected = true
15 throw new Error("unexpected connection")
16 }
17 }))
18 assert.isFalse(connected)
19 assert.strictEqual(error.reason._tag, "ConnectionError")
20 }))
21
22 it.effect.each([
23 { startupParameters: { REPLICATION: "database" } },
24 { startupParameters: { client_encoding: "LATIN1" } },
25 { startupParameters: { "": "value" } },
26 { startupParameters: { search_path: "public\0private" } },
27 { startupOptions: "-c lock_timeout=2345\0" },
28 { url: Redacted.make("postgres://test@localhost/db?options=%00") }
29 ])("rejects invalid startup config %j before connecting", (config) =>
30 Effect.gen(function*() {
31 let connected = false
32 const error = yield* Effect.flip(PgConnection.make({
33 ...config,
34 username: "test",
35 stream: () => {
36 connected = true
37 throw new Error("unexpected connection")
38 }
39 }))
40 assert.isFalse(connected)
41 assert.strictEqual(error.reason._tag, "ConnectionError")
42 }))
43
44 it.effect("interrupts a stalled password provider when connectTimeout expires", () =>
45 Effect.gen(function*() {
46 let connected = false
47 let interrupted = false
48 const fiber = yield* PgConnection.make({
49 username: "test",
50 password: Effect.never.pipe(Effect.onInterrupt(() =>
51 Effect.sync(() => {
52 interrupted = true
53 })
54 )),
55 connectTimeout: "1 second",
56 stream: () => {
57 connected = true
58 throw new Error("unexpected connection")
59 }
60 }).pipe(Effect.flip, Effect.forkScoped)
61
62 yield* TestClock.adjust("1 second")
63 const error = yield* Fiber.join(fiber)
64
65 assert.isTrue(interrupted)
66 assert.isFalse(connected)
67 assert.strictEqual(error.reason._tag, "ConnectionError")
68 assert.strictEqual(error.reason.message, "PgConnection: Connection timed out")
69 assert.isTrue(error.isRetryable)
70 }))
71
72 it.effect.each(["prefer", "allow"])("accepts sslmode=%s in a URL", (sslmode) =>
73 Effect.gen(function*() {
74 let connected = false
75 const error = yield* Effect.flip(PgConnection.make({
76 url: Redacted.make(`postgres://user@localhost/db?sslmode=${sslmode}`),
77 stream: () => {
78 connected = true
79 throw new Error("test connection")
80 }
81 }))
82 assert.isTrue(connected)
83 assert.strictEqual(error.reason._tag, "ConnectionError")
84 assert.strictEqual(error.reason.message, "PgConnection: Failed to connect")
85 }))
86
87 it.effect("rejects an unrecognized sslmode before connecting", () =>
88 Effect.gen(function*() {
89 let connected = false
90 const error = yield* Effect.flip(PgConnection.make({
91 url: Redacted.make("postgresql://user@localhost/db?sslmode=invalid"),
92 stream: () => {
93 connected = true
94 throw new Error("test connection")
95 }
96 }))
97 assert.isFalse(connected)
98 assert.strictEqual(error.reason._tag, "ConnectionError")
99 assert.strictEqual(error.reason.message, "PgConnection: Unrecognized sslmode in URL: \"invalid\"")
100 }))
101
102 it.effect("rejects a non-postgres URL protocol", () =>
103 Effect.gen(function*() {
104 const error = yield* Effect.flip(PgConnection.make({
105 url: Redacted.make("mysql://user@localhost/db")
106 }))
107 assert.strictEqual(error.reason._tag, "ConnectionError")
108 }))
109 })