|
1 | 1 | import { describe, expect, test } from 'vitest'; |
| 2 | + |
2 | 3 | import { normalizeConnectionConfig } from '../../src/types/types.js'; |
3 | 4 |
|
4 | 5 | describe('config', () => { |
5 | | - test('Should resolve database', () => { |
| 6 | + test('Should normalize a simple URI', () => { |
6 | 7 | const normalized = normalizeConnectionConfig({ |
7 | 8 | type: 'postgresql', |
8 | 9 | uri: 'postgresql://postgres:postgres@localhost:4321/powersync_test' |
9 | 10 | }); |
10 | 11 | expect(normalized.database).equals('powersync_test'); |
| 12 | + expect(normalized.hostname).equals('localhost'); |
| 13 | + expect(normalized.port).equals(4321); |
| 14 | + expect(normalized.username).equals('postgres'); |
| 15 | + expect(normalized.password).equals('postgres'); |
| 16 | + }); |
| 17 | + |
| 18 | + test('Should normalize an URI with auth', () => { |
| 19 | + const uri = 'postgresql://user:pass@localhost:5432/powersync_test'; |
| 20 | + const normalized = normalizeConnectionConfig({ |
| 21 | + type: 'postgresql', |
| 22 | + uri |
| 23 | + }); |
| 24 | + expect(normalized.database).equals('powersync_test'); |
| 25 | + expect(normalized.username).equals('user'); |
| 26 | + expect(normalized.password).equals('pass'); |
| 27 | + }); |
| 28 | + |
| 29 | + test('Should normalize an URI with query params', () => { |
| 30 | + const normalized = normalizeConnectionConfig({ |
| 31 | + type: 'postgresql', |
| 32 | + uri: 'postgresql://user:pass@host/db?other=param' |
| 33 | + }); |
| 34 | + expect(normalized.database).equals('db'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('Should prioritize username and password that are specified explicitly', () => { |
| 38 | + const uri = 'postgresql://user:pass@localhost:5432/powersync_test'; |
| 39 | + const normalized = normalizeConnectionConfig({ |
| 40 | + type: 'postgresql', |
| 41 | + uri, |
| 42 | + username: 'user2', |
| 43 | + password: 'pass2' |
| 44 | + }); |
| 45 | + expect(normalized.username).equals('user2'); |
| 46 | + expect(normalized.password).equals('pass2'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Should parse connection parameters from URI query string', () => { |
| 50 | + const normalized = normalizeConnectionConfig({ |
| 51 | + type: 'postgresql', |
| 52 | + uri: 'postgresql://user:pass@host/db?connect_timeout=300&keepalives=1&keepalives_idle=60&keepalives_interval=10&keepalives_count=10' |
| 53 | + }); |
| 54 | + expect(normalized.connect_timeout).equals(300); |
| 55 | + expect(normalized.keepalives).equals(1); |
| 56 | + expect(normalized.keepalives_idle).equals(60); |
| 57 | + expect(normalized.keepalives_interval).equals(10); |
| 58 | + expect(normalized.keepalives_count).equals(10); |
| 59 | + }); |
| 60 | + |
| 61 | + test('Should prioritize explicit config over URI query params', () => { |
| 62 | + const normalized = normalizeConnectionConfig({ |
| 63 | + type: 'postgresql', |
| 64 | + uri: 'postgresql://user:pass@host/db?connect_timeout=300&keepalives_idle=60', |
| 65 | + connect_timeout: 600, |
| 66 | + keepalives_idle: 120 |
| 67 | + }); |
| 68 | + expect(normalized.connect_timeout).equals(600); |
| 69 | + expect(normalized.keepalives_idle).equals(120); |
| 70 | + }); |
| 71 | + |
| 72 | + test('Should handle partial query parameters', () => { |
| 73 | + const normalized = normalizeConnectionConfig({ |
| 74 | + type: 'postgresql', |
| 75 | + uri: 'postgresql://user:pass@host/db?connect_timeout=300' |
| 76 | + }); |
| 77 | + expect(normalized.connect_timeout).equals(300); |
| 78 | + expect(normalized.keepalives).toBeUndefined(); |
| 79 | + expect(normalized.keepalives_idle).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('Should ignore invalid query parameter values', () => { |
| 83 | + const normalized = normalizeConnectionConfig({ |
| 84 | + type: 'postgresql', |
| 85 | + uri: 'postgresql://user:pass@host/db?connect_timeout=invalid&keepalives_idle=-5' |
| 86 | + }); |
| 87 | + expect(normalized.connect_timeout).toBeUndefined(); |
| 88 | + expect(normalized.keepalives_idle).toBeUndefined(); |
| 89 | + }); |
| 90 | + |
| 91 | + test('Should handle keepalives=0 to disable keepalives', () => { |
| 92 | + const normalized = normalizeConnectionConfig({ |
| 93 | + type: 'postgresql', |
| 94 | + uri: 'postgresql://user:pass@host/db?keepalives=0' |
| 95 | + }); |
| 96 | + expect(normalized.keepalives).equals(0); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('errors', () => { |
| 100 | + test('Should throw error when no database specified', () => { |
| 101 | + ['postgresql://user:pass@localhost:5432', 'postgresql://user:pass@localhost:5432/'].forEach((uri) => { |
| 102 | + expect(() => |
| 103 | + normalizeConnectionConfig({ |
| 104 | + type: 'postgresql', |
| 105 | + uri |
| 106 | + }) |
| 107 | + ).toThrow('[PSYNC_S1105] Postgres connection: database required'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + test('Should throw error when URI has invalid scheme', () => { |
| 112 | + expect(() => |
| 113 | + normalizeConnectionConfig({ |
| 114 | + type: 'postgresql', |
| 115 | + uri: 'http://user:pass@localhost:5432/powersync_test' |
| 116 | + }) |
| 117 | + ).toThrow('[PSYNC_S1109] Invalid URI - protocol must be postgresql'); |
| 118 | + }); |
| 119 | + |
| 120 | + test('Should throw error when hostname is missing', () => { |
| 121 | + expect(() => |
| 122 | + normalizeConnectionConfig({ |
| 123 | + type: 'postgresql', |
| 124 | + uri: 'postgresql://user:pass@/powersync_test' |
| 125 | + }) |
| 126 | + ).toThrow('[PSYNC_S1106] Postgres connection: hostname required'); |
| 127 | + }); |
| 128 | + |
| 129 | + test('Should throw error when username is missing', () => { |
| 130 | + expect(() => |
| 131 | + normalizeConnectionConfig({ |
| 132 | + type: 'postgresql', |
| 133 | + uri: 'postgresql://localhost:5432/powersync_test' |
| 134 | + }) |
| 135 | + ).toThrow('[PSYNC_S1107] Postgres connection: username required'); |
| 136 | + }); |
| 137 | + |
| 138 | + test('Should throw error when password is missing', () => { |
| 139 | + expect(() => |
| 140 | + normalizeConnectionConfig({ |
| 141 | + type: 'postgresql', |
| 142 | + uri: 'postgresql://user@localhost:5432/powersync_test' |
| 143 | + }) |
| 144 | + ).toThrow('[PSYNC_S1108] Postgres connection: password required'); |
| 145 | + }); |
11 | 146 | }); |
12 | 147 | }); |
0 commit comments