@dainprotocol/cli 1.2.36 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,335 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- var utils_1 = require("../utils");
7
- var path_1 = __importDefault(require("path"));
8
- describe('normalizeBaseUrl', function () {
9
- describe('trailing slash handling', function () {
10
- test('removes single trailing slash', function () {
11
- expect((0, utils_1.normalizeBaseUrl)('https://example.com/')).toBe('https://example.com');
12
- });
13
- test('removes multiple trailing slashes', function () {
14
- expect((0, utils_1.normalizeBaseUrl)('https://example.com///')).toBe('https://example.com');
15
- });
16
- test('handles URL without trailing slash', function () {
17
- expect((0, utils_1.normalizeBaseUrl)('https://example.com')).toBe('https://example.com');
18
- });
19
- test('preserves path while removing trailing slash', function () {
20
- expect((0, utils_1.normalizeBaseUrl)('https://example.com/api/v1/')).toBe('https://example.com/api/v1');
21
- });
22
- });
23
- describe('protocol handling', function () {
24
- test('handles https protocol', function () {
25
- expect((0, utils_1.normalizeBaseUrl)('https://example.com')).toBe('https://example.com');
26
- });
27
- test('handles http protocol', function () {
28
- expect((0, utils_1.normalizeBaseUrl)('http://example.com')).toBe('http://example.com');
29
- });
30
- test('handles wss protocol', function () {
31
- expect((0, utils_1.normalizeBaseUrl)('wss://tunnel.example.com')).toBe('wss://tunnel.example.com');
32
- });
33
- test('handles ws protocol', function () {
34
- expect((0, utils_1.normalizeBaseUrl)('ws://localhost:3000')).toBe('ws://localhost:3000');
35
- });
36
- test('fixes malformed triple-slash URLs', function () {
37
- expect((0, utils_1.normalizeBaseUrl)('wss:///tunnel.dain-local.com')).toBe('wss://tunnel.dain-local.com');
38
- });
39
- });
40
- describe('edge cases', function () {
41
- test('handles empty string', function () {
42
- expect((0, utils_1.normalizeBaseUrl)('')).toBe('');
43
- });
44
- test('handles whitespace-only string', function () {
45
- expect((0, utils_1.normalizeBaseUrl)(' ')).toBe('');
46
- });
47
- test('trims whitespace', function () {
48
- expect((0, utils_1.normalizeBaseUrl)(' https://example.com ')).toBe('https://example.com');
49
- });
50
- test('handles localhost with port', function () {
51
- expect((0, utils_1.normalizeBaseUrl)('http://localhost:3000/')).toBe('http://localhost:3000');
52
- });
53
- test('handles URL with query params', function () {
54
- expect((0, utils_1.normalizeBaseUrl)('https://example.com/api?key=value/')).toBe('https://example.com/api?key=value');
55
- });
56
- test('handles invalid URL gracefully (no protocol)', function () {
57
- // Falls back to string manipulation when URL parsing fails
58
- expect((0, utils_1.normalizeBaseUrl)('example.com///')).toBe('example.com');
59
- });
60
- test('handles URL with auth credentials', function () {
61
- expect((0, utils_1.normalizeBaseUrl)('https://user:pass@example.com/')).toBe('https://user:pass@example.com');
62
- });
63
- });
64
- });
65
- describe('joinUrl', function () {
66
- describe('slash normalization', function () {
67
- test('joins base with trailing slash and path with leading slash', function () {
68
- expect((0, utils_1.joinUrl)('https://example.com/', '/api/test')).toBe('https://example.com/api/test');
69
- });
70
- test('joins base without trailing slash and path with leading slash', function () {
71
- expect((0, utils_1.joinUrl)('https://example.com', '/api/test')).toBe('https://example.com/api/test');
72
- });
73
- test('joins base with trailing slash and path without leading slash', function () {
74
- expect((0, utils_1.joinUrl)('https://example.com/', 'api/test')).toBe('https://example.com/api/test');
75
- });
76
- test('joins base without trailing slash and path without leading slash', function () {
77
- expect((0, utils_1.joinUrl)('https://example.com', 'api/test')).toBe('https://example.com/api/test');
78
- });
79
- test('handles multiple leading slashes in path', function () {
80
- expect((0, utils_1.joinUrl)('https://example.com', '///api/test')).toBe('https://example.com/api/test');
81
- });
82
- });
83
- describe('path handling', function () {
84
- test('handles empty path', function () {
85
- expect((0, utils_1.joinUrl)('https://example.com', '')).toBe('https://example.com/');
86
- });
87
- test('handles complex nested paths', function () {
88
- expect((0, utils_1.joinUrl)('https://example.com/v1', '/users/123/profile')).toBe('https://example.com/v1/users/123/profile');
89
- });
90
- test('preserves query parameters in path', function () {
91
- expect((0, utils_1.joinUrl)('https://example.com', '/api?foo=bar')).toBe('https://example.com/api?foo=bar');
92
- });
93
- });
94
- });
95
- describe('normalizeTunnelPublicUrl', function () {
96
- test('keeps valid https tunnel URL unchanged', function () {
97
- expect((0, utils_1.normalizeTunnelPublicUrl)('https://tunnel.dain-local.com/0_229')).toBe('https://tunnel.dain-local.com/0_229');
98
- });
99
- test('adds https protocol when tunnel URL is missing scheme', function () {
100
- expect((0, utils_1.normalizeTunnelPublicUrl)('tunnel.dain-local.com/0_229')).toBe('https://tunnel.dain-local.com/0_229');
101
- });
102
- test('uses http for localhost tunnel URL without scheme', function () {
103
- expect((0, utils_1.normalizeTunnelPublicUrl)('localhost:3001/0_229')).toBe('http://localhost:3001/0_229');
104
- });
105
- });
106
- describe('parseEnvContent', function () {
107
- describe('basic parsing', function () {
108
- test('parses simple key=value pairs', function () {
109
- var content = 'FOO=bar\nBAZ=qux';
110
- var result = (0, utils_1.parseEnvContent)(content);
111
- expect(result).toEqual([
112
- { name: 'FOO', value: 'bar' },
113
- { name: 'BAZ', value: 'qux' },
114
- ]);
115
- });
116
- test('handles single variable', function () {
117
- var result = (0, utils_1.parseEnvContent)('SINGLE=value');
118
- expect(result).toEqual([{ name: 'SINGLE', value: 'value' }]);
119
- });
120
- test('handles empty content', function () {
121
- expect((0, utils_1.parseEnvContent)('')).toEqual([]);
122
- });
123
- });
124
- describe('special values', function () {
125
- test('preserves values with = signs (base64)', function () {
126
- var content = 'JWT_SECRET=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test==';
127
- var result = (0, utils_1.parseEnvContent)(content);
128
- expect(result[0].value).toBe('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test==');
129
- });
130
- test('preserves URLs with query params', function () {
131
- var content = 'DATABASE_URL=postgresql://user:pass@host:5432/db?sslmode=require';
132
- var result = (0, utils_1.parseEnvContent)(content);
133
- expect(result[0].value).toBe('postgresql://user:pass@host:5432/db?sslmode=require');
134
- });
135
- test('handles values with spaces', function () {
136
- var content = 'MESSAGE=Hello World';
137
- var result = (0, utils_1.parseEnvContent)(content);
138
- expect(result[0].value).toBe('Hello World');
139
- });
140
- test('handles quoted values', function () {
141
- var content = 'QUOTED="hello world"';
142
- var result = (0, utils_1.parseEnvContent)(content);
143
- expect(result[0].value).toBe('hello world');
144
- });
145
- test('handles single-quoted values', function () {
146
- var content = "QUOTED='hello world'";
147
- var result = (0, utils_1.parseEnvContent)(content);
148
- expect(result[0].value).toBe('hello world');
149
- });
150
- test('handles values with special characters (non-comment)', function () {
151
- // Note: # starts a comment in dotenv, so use other special chars
152
- var content = 'SPECIAL=!@$%^&*()';
153
- var result = (0, utils_1.parseEnvContent)(content);
154
- expect(result[0].value).toBe('!@$%^&*()');
155
- });
156
- test('handles # in quoted values', function () {
157
- // Quoted values preserve # character
158
- var content = 'SPECIAL="!@#$%^&*()"';
159
- var result = (0, utils_1.parseEnvContent)(content);
160
- expect(result[0].value).toBe('!@#$%^&*()');
161
- });
162
- });
163
- describe('comments and whitespace', function () {
164
- test('ignores comment lines', function () {
165
- var content = '# This is a comment\nFOO=bar\n# Another comment';
166
- var result = (0, utils_1.parseEnvContent)(content);
167
- expect(result).toEqual([{ name: 'FOO', value: 'bar' }]);
168
- });
169
- test('ignores inline comments', function () {
170
- var content = 'FOO=bar # this is a comment';
171
- var result = (0, utils_1.parseEnvContent)(content);
172
- // Note: dotenv.parse treats everything after = as value
173
- expect(result[0].name).toBe('FOO');
174
- });
175
- test('ignores empty lines', function () {
176
- var content = 'FOO=bar\n\n\nBAZ=qux';
177
- var result = (0, utils_1.parseEnvContent)(content);
178
- expect(result).toHaveLength(2);
179
- });
180
- test('handles Windows CRLF line endings', function () {
181
- var content = 'FOO=bar\r\nBAZ=qux\r\n';
182
- var result = (0, utils_1.parseEnvContent)(content);
183
- expect(result).toHaveLength(2);
184
- expect(result[0].value).toBe('bar');
185
- expect(result[1].value).toBe('qux');
186
- });
187
- });
188
- describe('edge cases', function () {
189
- test('filters out empty names', function () {
190
- // This would be invalid env syntax, but we handle it gracefully
191
- var content = 'VALID=value';
192
- var result = (0, utils_1.parseEnvContent)(content);
193
- expect(result.every(function (e) { return e.name !== ''; })).toBe(true);
194
- });
195
- test('handles multiline values with quotes', function () {
196
- var content = 'MULTI="line1\nline2"';
197
- var result = (0, utils_1.parseEnvContent)(content);
198
- expect(result[0].value).toContain('line1');
199
- });
200
- });
201
- describe('production scenarios', function () {
202
- test('parses complex production env file', function () {
203
- var _a, _b, _c;
204
- var content = "# Production environment\nDATABASE_URL=postgresql://user:pass@host:5432/db?schema=public\nJWT_SECRET=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9==\nREDIS_URL=\"redis://localhost:6379\"\nAPI_KEY='sk_live_abc123=='\n\n# Feature flags\nENABLE_FEATURE=true";
205
- var result = (0, utils_1.parseEnvContent)(content);
206
- expect(result).toHaveLength(5);
207
- expect((_a = result.find(function (e) { return e.name === 'DATABASE_URL'; })) === null || _a === void 0 ? void 0 : _a.value).toContain('postgresql://');
208
- expect((_b = result.find(function (e) { return e.name === 'JWT_SECRET'; })) === null || _b === void 0 ? void 0 : _b.value).toContain('==');
209
- expect((_c = result.find(function (e) { return e.name === 'REDIS_URL'; })) === null || _c === void 0 ? void 0 : _c.value).toBe('redis://localhost:6379');
210
- });
211
- });
212
- });
213
- describe('extractOrgId', function () {
214
- describe('standard format: sk_agent_{agentId}_{orgId}_{secret}', function () {
215
- test('extracts org ID from standard format', function () {
216
- expect((0, utils_1.extractOrgId)('sk_agent_agent123_org456_secretkey')).toBe('org456');
217
- });
218
- test('handles org ID with numbers', function () {
219
- expect((0, utils_1.extractOrgId)('sk_agent_a1_o2_s3')).toBe('o2');
220
- });
221
- test('handles long secret with underscores', function () {
222
- expect((0, utils_1.extractOrgId)('sk_agent_myagent_myorg_secret_with_underscores')).toBe('myorg');
223
- });
224
- });
225
- describe('org/agent format: sk_agent_org_{orgId}_agent_{agentId}_{secret}', function () {
226
- test('extracts org ID from org/agent format', function () {
227
- expect((0, utils_1.extractOrgId)('sk_agent_org_myorg_agent_myagent_secret')).toBe('myorg');
228
- });
229
- test('handles org ID with special characters', function () {
230
- expect((0, utils_1.extractOrgId)('sk_agent_org_org-with-dash_agent_agent1_secret')).toBe('org-with-dash');
231
- });
232
- test('handles long secret in org/agent format', function () {
233
- expect((0, utils_1.extractOrgId)('sk_agent_org_testorg_agent_testagent_long_secret_key')).toBe('testorg');
234
- });
235
- });
236
- describe('invalid keys', function () {
237
- test('returns empty string for undefined', function () {
238
- expect((0, utils_1.extractOrgId)(undefined)).toBe('');
239
- });
240
- test('returns empty string for empty string', function () {
241
- expect((0, utils_1.extractOrgId)('')).toBe('');
242
- });
243
- test('returns empty string for invalid prefix', function () {
244
- expect((0, utils_1.extractOrgId)('invalid_agent_a_b_c')).toBe('');
245
- });
246
- test('returns empty string for too few parts', function () {
247
- expect((0, utils_1.extractOrgId)('sk_agent_only_two')).toBe('');
248
- });
249
- test('returns empty string for malformed org format', function () {
250
- expect((0, utils_1.extractOrgId)('sk_agent_org_myorg_notaagent_id_secret')).toBe('');
251
- });
252
- test('returns empty string when secret is missing', function () {
253
- expect((0, utils_1.extractOrgId)('sk_agent_org_myorg_agent_myagent')).toBe('');
254
- });
255
- });
256
- });
257
- describe('resolveOrgId', function () {
258
- var originalEnv = process.env.DAIN_ORG_ID;
259
- afterEach(function () {
260
- if (originalEnv !== undefined) {
261
- process.env.DAIN_ORG_ID = originalEnv;
262
- }
263
- else {
264
- delete process.env.DAIN_ORG_ID;
265
- }
266
- });
267
- test('prioritizes config org-id over everything', function () {
268
- process.env.DAIN_ORG_ID = 'env-org';
269
- var config = {
270
- 'main-file': 'src/index.ts',
271
- 'org-id': 'config-org',
272
- 'api-key': 'sk_agent_a_apiorg_s',
273
- };
274
- expect((0, utils_1.resolveOrgId)(config)).toBe('config-org');
275
- });
276
- test('falls back to env var when config org-id not set', function () {
277
- process.env.DAIN_ORG_ID = 'env-org';
278
- var config = {
279
- 'main-file': 'src/index.ts',
280
- 'api-key': 'sk_agent_a_apiorg_s',
281
- };
282
- expect((0, utils_1.resolveOrgId)(config)).toBe('env-org');
283
- });
284
- test('falls back to API key when env var not set', function () {
285
- delete process.env.DAIN_ORG_ID;
286
- var config = {
287
- 'main-file': 'src/index.ts',
288
- 'api-key': 'sk_agent_a_apiorg_s',
289
- };
290
- expect((0, utils_1.resolveOrgId)(config)).toBe('apiorg');
291
- });
292
- test('returns empty string when nothing available', function () {
293
- delete process.env.DAIN_ORG_ID;
294
- var config = {
295
- 'main-file': 'src/index.ts',
296
- };
297
- expect((0, utils_1.resolveOrgId)(config)).toBe('');
298
- });
299
- test('handles empty string org-id in config', function () {
300
- process.env.DAIN_ORG_ID = 'env-org';
301
- var config = {
302
- 'main-file': 'src/index.ts',
303
- 'org-id': '',
304
- 'api-key': 'sk_agent_a_apiorg_s',
305
- };
306
- // Empty string is falsy, so falls back to env
307
- expect((0, utils_1.resolveOrgId)(config)).toBe('env-org');
308
- });
309
- });
310
- describe('getStaticFilesPath', function () {
311
- test('returns path to static directory in cwd', function () {
312
- var result = (0, utils_1.getStaticFilesPath)();
313
- expect(result).toBe(path_1.default.join(process.cwd(), 'static'));
314
- });
315
- test('is an absolute path', function () {
316
- var result = (0, utils_1.getStaticFilesPath)();
317
- expect(path_1.default.isAbsolute(result)).toBe(true);
318
- });
319
- });
320
- describe('default constants', function () {
321
- test('DEFAULT_TUNNEL_BASE_URL is valid WebSocket URL', function () {
322
- expect(utils_1.DEFAULT_TUNNEL_BASE_URL).toMatch(/^wss?:\/\//);
323
- });
324
- test('DEFAULT_PLATFORM_BASE_URL is valid HTTPS URL', function () {
325
- expect(utils_1.DEFAULT_PLATFORM_BASE_URL).toMatch(/^https:\/\//);
326
- });
327
- test('DEFAULT_API_BASE_URL is valid HTTPS URL', function () {
328
- expect(utils_1.DEFAULT_API_BASE_URL).toMatch(/^https:\/\//);
329
- });
330
- test('default URLs do not have trailing slashes', function () {
331
- expect(utils_1.DEFAULT_TUNNEL_BASE_URL.endsWith('/')).toBe(false);
332
- expect(utils_1.DEFAULT_PLATFORM_BASE_URL.endsWith('/')).toBe(false);
333
- expect(utils_1.DEFAULT_API_BASE_URL.endsWith('/')).toBe(false);
334
- });
335
- });