Skip to content

Commit 4f9d621

Browse files
committed
test(utils): add tests for normalizeBaseUrl and path resolution
Added comprehensive tests for previously untested utility functions: - normalizeBaseUrl: URL trailing slash normalization and memoization - resolveBasePath: relative-to-absolute path conversion - resolveAbsPaths: batch path resolution with custom base paths Increases test coverage from 22 to 35 tests in utils.test.mts.
1 parent 2b9a869 commit 4f9d621

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

test/unit/utils.test.mts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,118 @@ import { describe, expect, it } from 'vitest'
1414

1515
import {
1616
createRequestBodyForJson,
17+
normalizeBaseUrl,
1718
promiseWithResolvers,
1819
queryToSearchParams,
20+
resolveAbsPaths,
21+
resolveBasePath,
1922
} from '../../src/index'
2023
import { createUserAgentFromPkgJson } from '../../src/user-agent'
2124

25+
// =============================================================================
26+
// URL Normalization
27+
// =============================================================================
28+
29+
describe('URL Normalization', () => {
30+
describe('normalizeBaseUrl', () => {
31+
it('should add trailing slash if missing', () => {
32+
const result = normalizeBaseUrl('https://api.socket.dev')
33+
expect(result).toBe('https://api.socket.dev/')
34+
})
35+
36+
it('should not modify URL that already has trailing slash', () => {
37+
const result = normalizeBaseUrl('https://api.socket.dev/')
38+
expect(result).toBe('https://api.socket.dev/')
39+
})
40+
41+
it('should handle local URLs', () => {
42+
const result = normalizeBaseUrl('http://localhost:3000')
43+
expect(result).toBe('http://localhost:3000/')
44+
})
45+
46+
it('should memoize results for performance', () => {
47+
const url = 'https://test.example.com'
48+
const result1 = normalizeBaseUrl(url)
49+
const result2 = normalizeBaseUrl(url)
50+
// Both calls should return the same reference (memoized)
51+
expect(result1).toBe(result2)
52+
expect(result1).toBe('https://test.example.com/')
53+
})
54+
})
55+
})
56+
57+
// =============================================================================
58+
// Path Resolution
59+
// =============================================================================
60+
61+
describe('Path Resolution', () => {
62+
describe('resolveBasePath', () => {
63+
it('should resolve relative path to absolute', () => {
64+
const result = resolveBasePath('.')
65+
expect(result).toContain('socket-sdk-js')
66+
expect(result.startsWith('/')).toBe(true)
67+
})
68+
69+
it('should resolve nested relative path', () => {
70+
const result = resolveBasePath('./test')
71+
expect(result).toContain('socket-sdk-js')
72+
expect(result.endsWith('/test')).toBe(true)
73+
})
74+
75+
it('should return absolute path unchanged', () => {
76+
const absolutePath = '/tmp/test'
77+
const result = resolveBasePath(absolutePath)
78+
expect(result).toBe('/tmp/test')
79+
})
80+
81+
it('should default to cwd when no argument provided', () => {
82+
const result = resolveBasePath()
83+
expect(result).toContain('socket-sdk-js')
84+
})
85+
})
86+
87+
describe('resolveAbsPaths', () => {
88+
it('should resolve array of relative paths to absolute', () => {
89+
const paths = ['./package.json', './src/index.ts']
90+
const result = resolveAbsPaths(paths)
91+
92+
expect(result).toHaveLength(2)
93+
expect(result[0]).toContain('socket-sdk-js/package.json')
94+
expect(result[1]).toContain('socket-sdk-js/src/index.ts')
95+
result.forEach(p => expect(p.startsWith('/')).toBe(true))
96+
})
97+
98+
it('should handle absolute paths in array', () => {
99+
const paths = ['/tmp/test.txt', '/var/log/app.log']
100+
const result = resolveAbsPaths(paths)
101+
102+
expect(result).toEqual(['/tmp/test.txt', '/var/log/app.log'])
103+
})
104+
105+
it('should resolve relative to specified base path', () => {
106+
const paths = ['file1.txt', 'file2.txt']
107+
const result = resolveAbsPaths(paths, '/custom/base')
108+
109+
expect(result).toHaveLength(2)
110+
expect(result[0]).toBe('/custom/base/file1.txt')
111+
expect(result[1]).toBe('/custom/base/file2.txt')
112+
})
113+
114+
it('should handle empty array', () => {
115+
const result = resolveAbsPaths([])
116+
expect(result).toEqual([])
117+
})
118+
119+
it('should handle mixed absolute and relative paths', () => {
120+
const paths = ['./relative.txt', '/absolute.txt']
121+
const result = resolveAbsPaths(paths, '/base')
122+
123+
expect(result[0]).toBe('/base/relative.txt')
124+
expect(result[1]).toBe('/absolute.txt')
125+
})
126+
})
127+
})
128+
22129
// =============================================================================
23130
// Promise Utilities
24131
// =============================================================================

0 commit comments

Comments
 (0)