|
7 | 7 | */ |
8 | 8 | import { test, describe, before, after } from 'node:test'; |
9 | 9 | import assert from 'node:assert/strict'; |
10 | | -import { setApiKey, getApiKey, getOAuthClientId, getOAuthRedirectUri } from '../src/config.js'; |
| 10 | +import { setApiKey, getApiKey, getOAuthClientId, getOAuthRedirectUri, setBaseUrl, getBaseUrl } from '../src/config.js'; |
11 | 11 |
|
12 | 12 | // ── setApiKey – validation (throws before writing to disk) ───────────────── |
13 | 13 |
|
@@ -102,6 +102,89 @@ describe('setApiKey validation', () => { |
102 | 102 | }); |
103 | 103 | }); |
104 | 104 |
|
| 105 | +// ── setBaseUrl – validation (throws before writing to disk) ──────────────── |
| 106 | + |
| 107 | +describe('setBaseUrl validation', () => { |
| 108 | + test('throws for empty string', () => { |
| 109 | + assert.throws( |
| 110 | + () => setBaseUrl(''), |
| 111 | + { message: 'Base URL must be a non-empty string.' } |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + test('throws for whitespace-only string', () => { |
| 116 | + assert.throws( |
| 117 | + () => setBaseUrl(' '), |
| 118 | + { message: 'Base URL must be a non-empty string.' } |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + test('throws for null', () => { |
| 123 | + assert.throws( |
| 124 | + () => setBaseUrl(null), |
| 125 | + { message: 'Base URL must be a non-empty string.' } |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + test('throws for non-string value (number)', () => { |
| 130 | + assert.throws( |
| 131 | + () => setBaseUrl(12345), |
| 132 | + { message: 'Base URL must be a non-empty string.' } |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + test('throws for a value that is not a valid URL', () => { |
| 137 | + assert.throws( |
| 138 | + () => setBaseUrl('not-a-url'), |
| 139 | + /Invalid base URL/ |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + test('throws for a non-http(s) protocol', () => { |
| 144 | + assert.throws( |
| 145 | + () => setBaseUrl('ftp://api.kit.com/v4'), |
| 146 | + { message: 'Base URL must use http or https.' } |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +// ── getBaseUrl – environment variable override + normalization ───────────── |
| 152 | + |
| 153 | +describe('getBaseUrl', () => { |
| 154 | + let _saved; |
| 155 | + |
| 156 | + before(() => { |
| 157 | + _saved = process.env.KIT_API_BASE; |
| 158 | + delete process.env.KIT_API_BASE; |
| 159 | + }); |
| 160 | + |
| 161 | + after(() => { |
| 162 | + if (_saved !== undefined) { |
| 163 | + process.env.KIT_API_BASE = _saved; |
| 164 | + } else { |
| 165 | + delete process.env.KIT_API_BASE; |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + test('returns KIT_API_BASE env var when set', () => { |
| 170 | + process.env.KIT_API_BASE = 'https://api.example.com/v4'; |
| 171 | + assert.equal(getBaseUrl(), 'https://api.example.com/v4'); |
| 172 | + delete process.env.KIT_API_BASE; |
| 173 | + }); |
| 174 | + |
| 175 | + test('strips trailing slashes from the env var value', () => { |
| 176 | + process.env.KIT_API_BASE = 'https://api.example.com/v4/'; |
| 177 | + assert.equal(getBaseUrl(), 'https://api.example.com/v4'); |
| 178 | + delete process.env.KIT_API_BASE; |
| 179 | + }); |
| 180 | + |
| 181 | + test('returns an http(s) URL when neither env nor config overrides it', () => { |
| 182 | + const val = getBaseUrl(); |
| 183 | + assert.equal(typeof val, 'string'); |
| 184 | + assert.match(val, /^https?:\/\//); |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
105 | 188 | // ── getApiKey – environment variable override ────────────────────────────── |
106 | 189 |
|
107 | 190 | describe('getApiKey', () => { |
|
0 commit comments