forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.ts
More file actions
191 lines (162 loc) · 5.62 KB
/
Copy pathconfig.test.ts
File metadata and controls
191 lines (162 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { LilyConfigError } from '../src/errors/sdk-error';
import { resolveLilySdkConfig } from '../src/config/resolve-config';
import { SDK_VERSION } from '../src/version';
describe('resolveLilySdkConfig', () => {
const originalEnv = process.env;
beforeEach(() => {
process.env = { ...originalEnv };
});
afterEach(() => {
process.env = originalEnv;
});
it('uses explicit baseUrl over env', () => {
process.env.LILY_API_URL = 'https://env.example.com';
const config = resolveLilySdkConfig({ baseUrl: 'https://explicit.example.com' });
expect(config.baseUrl.toString()).toBe('https://explicit.example.com/');
});
it('falls back to LILY_API_URL when baseUrl is omitted', () => {
process.env.LILY_API_URL = 'https://env.example.com';
const config = resolveLilySdkConfig({});
expect(config.baseUrl.toString()).toBe('https://env.example.com/');
});
it('throws when neither baseUrl nor env is provided', () => {
delete process.env.LILY_API_URL;
expect(() => resolveLilySdkConfig({})).toThrow(LilyConfigError);
});
it('resolves apiKey and authToken from env when not explicit', () => {
process.env.LILY_API_URL = 'https://api.example.com';
process.env.LILY_API_KEY = 'env-key';
process.env.LILY_AUTH_TOKEN = 'env-token';
const config = resolveLilySdkConfig({});
expect(config.apiKey).toBe('env-key');
expect(config.authToken).toBe('env-token');
});
it('prefers explicit credentials over env', () => {
process.env.LILY_API_URL = 'https://api.example.com';
process.env.LILY_API_KEY = 'env-key';
process.env.LILY_AUTH_TOKEN = 'env-token';
const config = resolveLilySdkConfig({
apiKey: 'explicit-key',
authToken: 'explicit-token',
});
expect(config.baseUrl.toString()).toBe('https://api.lily.test/');
expect(config.timeoutMs).toBe(10_000);
expect(config.retry.retries).toBe(2);
expect(config.userAgent).toBe(`lily-sdk/${SDK_VERSION}`);
});
it('derives default user-agent from package version', () => {
const config = resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
fetch: globalThis.fetch,
});
expect(config.userAgent).toBe(`lily-sdk/${SDK_VERSION}`);
expect(SDK_VERSION).toMatch(/^\d+\.\d+\.\d+/);
});
it('allows explicit userAgent to override default', () => {
const config = resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
fetch: globalThis.fetch,
userAgent: 'custom-agent/1.0',
});
expect(config.userAgent).toBe('custom-agent/1.0');
});
it('preserves a path prefix and appends a trailing slash', () => {
const withoutSlash = resolveLilySdkConfig({
baseUrl: 'https://host/lily/api',
fetch: globalThis.fetch,
});
const withSlash = resolveLilySdkConfig({
baseUrl: 'https://host/lily/api/',
fetch: globalThis.fetch,
});
expect(withoutSlash.baseUrl.toString()).toBe('https://host/lily/api/');
expect(withSlash.baseUrl.toString()).toBe('https://host/lily/api/');
});
it.each(['http://api.lily.test', 'https://api.lily.test'])(
'accepts an HTTP base url: %s',
(baseUrl) => {
const config = resolveLilySdkConfig({
baseUrl,
fetch: globalThis.fetch,
});
expect(config.baseUrl.toString()).toBe(`${baseUrl}/`);
},
);
it.each(['ftp:', 'file:', 'ws:'])('rejects the %s scheme', (scheme) => {
const resolve = () =>
resolveLilySdkConfig({
baseUrl: `${scheme}//example.test`,
fetch: globalThis.fetch,
});
expect(resolve).toThrow(LilyConfigError);
expect(resolve).toThrow(scheme);
});
it('throws when base url is invalid', () => {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'not-a-url',
fetch: globalThis.fetch,
}),
).toThrow(LilyConfigError);
});
it('throws when timeout is invalid', () => {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
timeoutMs: 0,
fetch: globalThis.fetch,
}),
).toThrow('`timeoutMs` must be a positive number.');
});
it('throws when fetch implementation is missing', () => {
const originalFetch = globalThis.fetch;
// @ts-expect-error - intentionally removing fetch to test validation
delete globalThis.fetch;
try {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
}),
).toThrow(LilyConfigError);
} finally {
globalThis.fetch = originalFetch;
}
});
it('throws when retry.retries is not an integer', () => {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
fetch: globalThis.fetch,
retry: { retries: 1.5 },
}),
).toThrow('`retry.retries` must be a non-negative integer.');
});
it('throws when retry.retryDelayMs is negative', () => {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
fetch: globalThis.fetch,
retry: { retryDelayMs: -1 },
}),
).toThrow('`retry.retryDelayMs` must be a non-negative number.');
});
});
it('throws when retry.retries is not an integer', () => {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
fetch: globalThis.fetch,
retry: { retries: 1.5 },
}),
).toThrow(LilyConfigError);
});
it('throws when retry.retryDelayMs is negative', () => {
expect(() =>
resolveLilySdkConfig({
baseUrl: 'https://api.lily.test',
fetch: globalThis.fetch,
retry: { retryDelayMs: -100 },
}),
).toThrow(LilyConfigError);
});