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

main
ts 149 lines 5.53 KB
Raw
1 import {
2 resolveConnectionOptions,
3 resolvePoolConfig,
4 resolveSsl,
5 } from "@alchemy.run/sql/PostgresTls";
6 import { describe, expect, it } from "@alchemy.run/test";
7 import * as PgClient from "@effect/sql-pg/PgClient";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import * as Result from "effect/Result";
11
12 const url = (s: string) => Redacted.make(s);
13
14 describe("SQL/PostgresTls resolveSsl", { tags: ["unit", "local"] }, () => {
15 it("resolves sslmode=prefer|allow to TLS on when ssl is implicit", () => {
16 for (const mode of ["prefer", "allow"]) {
17 expect(resolveSsl(url(`postgres://u@ep-x.neon.tech/x?sslmode=${mode}`), undefined)).toBe(
18 true,
19 );
20 expect(resolveSsl(url(`postgres://u@127.0.0.1:5432/x?sslmode=${mode}`), undefined)).toBe(
21 true,
22 );
23 }
24 });
25
26 it("leaves every other URL to @effect/sql-pg", () => {
27 expect(resolveSsl(url("postgres://u@db.example.com/x"), undefined)).toBeUndefined();
28 for (const mode of ["disable", "require", "verify-ca", "verify-full"]) {
29 expect(
30 resolveSsl(url(`postgres://u@db.example.com/x?sslmode=${mode}`), undefined),
31 ).toBeUndefined();
32 }
33 });
34
35 it("never overrides an explicit ssl option", () => {
36 const explicit = { rejectUnauthorized: false, servername: "override" };
37 expect(resolveSsl(url("postgres://u@db.example.com/x?sslmode=prefer"), explicit)).toBe(
38 explicit,
39 );
40 expect(resolveSsl(url("postgres://u@db.example.com/x?sslmode=prefer"), false)).toBe(false);
41 expect(resolveSsl(url("postgres://u@db.example.com/x?sslmode=require"), true)).toBe(true);
42 });
43
44 it("passes malformed URLs through untouched", () => {
45 expect(resolveSsl(url("not a url"), undefined)).toBeUndefined();
46 expect(resolveSsl(url("not a url"), true)).toBe(true);
47 });
48 });
49
50 describe("SQL/PostgresTls resolveConnectionOptions", { tags: ["unit", "local"] }, () => {
51 const railwayUrl = url(
52 "postgresql://user:p%40ss@db.railway.internal:5432/railway?sslmode=no-verify&connect_timeout=7&application_name=app",
53 );
54
55 it("preserves Railway no-verify TLS semantics in a driver-compatible URL", () => {
56 const options = resolveConnectionOptions(railwayUrl);
57 const parsed = new URL(Redacted.value(options.url));
58 expect(parsed.searchParams.get("sslmode")).toBe("require");
59 expect(parsed.searchParams.get("connect_timeout")).toBe("7");
60 expect(parsed.searchParams.get("application_name")).toBe("app");
61 expect(parsed.password).toBe("p%40ss");
62 expect(options.ssl).toEqual({ rejectUnauthorized: false });
63 expect(Redacted.value(railwayUrl)).toContain("sslmode=no-verify");
64 });
65
66 it("passes Railway URLs through the actual driver parser before opening a transport", async () => {
67 const stopped = new Error("Local transport sentinel");
68 let attempted = false;
69 const result = await Effect.runPromise(
70 Effect.gen(function* () {
71 const sql = yield* PgClient.PgClient;
72 return yield* sql`select 1`;
73 }).pipe(
74 Effect.provide(
75 PgClient.layer({
76 ...resolveConnectionOptions(railwayUrl),
77 stream: () => {
78 attempted = true;
79 throw stopped;
80 },
81 }),
82 ),
83 Effect.scoped,
84 Effect.result,
85 ),
86 );
87 expect(attempted).toBe(true);
88 expect(Result.isFailure(result)).toBe(true);
89 if (Result.isFailure(result)) {
90 expect(result.failure.reason.cause).toBe(stopped);
91 }
92 });
93
94 it("keeps explicitly configured verification and SNI authoritative", () => {
95 expect(resolveConnectionOptions(railwayUrl, true).ssl).toBe(true);
96 expect(
97 resolveConnectionOptions(railwayUrl, {
98 rejectUnauthorized: true,
99 servername: "custom.example.com",
100 ca: "test-ca",
101 }).ssl,
102 ).toEqual({ rejectUnauthorized: true, servername: "custom.example.com", ca: "test-ca" });
103 expect(resolveConnectionOptions(railwayUrl, false).ssl).toBe(false);
104 expect(
105 new URL(Redacted.value(resolveConnectionOptions(railwayUrl, false).url)).searchParams.get(
106 "sslmode",
107 ),
108 ).toBe("require");
109 });
110
111 it("does not invent SNI for a no-verify IP connection", () => {
112 expect(resolveConnectionOptions(url("postgres://u@[::1]/db?sslmode=no-verify")).ssl).toEqual({
113 rejectUnauthorized: false,
114 });
115 });
116
117 it("preserves unmodified and malformed URLs for driver validation", () => {
118 for (const value of ["postgres://u@db.example/db?sslmode=verify-full", "not a url"]) {
119 const original = url(value);
120 expect(resolveConnectionOptions(original).url).toBe(original);
121 }
122 });
123 });
124
125 describe("SQL/PostgresTls resolvePoolConfig", { tags: ["unit", "local"] }, () => {
126 it("passes client options through with the URL's TLS settings", () => {
127 const config = resolvePoolConfig(url("postgres://u@127.0.0.1:5432/x?sslmode=prefer"), {
128 prepare: false,
129 maxConnections: 3,
130 });
131 expect(config.prepare).toBe(false);
132 expect(config.maxConnections).toBe(3);
133 expect(config.ssl).toBe(true);
134 expect(Redacted.value(config.url!)).toBe("postgres://u@127.0.0.1:5432/x?sslmode=prefer");
135 });
136
137 it("keeps an explicit ssl option over the URL's sslmode", () => {
138 const config = resolvePoolConfig(url("postgres://u@127.0.0.1:5432/x?sslmode=prefer"), {
139 ssl: false,
140 });
141 expect(config.ssl).toBe(false);
142 });
143
144 it("leaves the driver defaults without options", () => {
145 const config = resolvePoolConfig(url("postgres://u@db.example.com/x"));
146 expect(config.prepare).toBeUndefined();
147 expect(config.ssl).toBeUndefined();
148 });
149 });