Skip to content

Commit da390c8

Browse files
committed
test: added unit tests
1 parent 73ad58f commit da390c8

File tree

12 files changed

+2056
-0
lines changed

12 files changed

+2056
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { getFileName, getParentPath, getFullPath } from './file-utils.js'
3+
import type { PiRatFile } from '../models/pirat-file.js'
4+
5+
describe('file-utils', () => {
6+
describe('getFileName', () => {
7+
it('should return the file name from a simple path', () => {
8+
const file: PiRatFile = { driveLetter: 'A', path: 'folder/subfolder/movie.mkv' }
9+
expect(getFileName(file)).toBe('movie.mkv')
10+
})
11+
12+
it('should return the file name from a deeply nested path', () => {
13+
const file: PiRatFile = { driveLetter: 'A', path: 'a/b/c/d/e/file.txt' }
14+
expect(getFileName(file)).toBe('file.txt')
15+
})
16+
17+
it('should return the file name when path has no directories', () => {
18+
const file: PiRatFile = { driveLetter: 'A', path: 'document.pdf' }
19+
expect(getFileName(file)).toBe('document.pdf')
20+
})
21+
22+
it('should handle paths with special characters in file name', () => {
23+
const file: PiRatFile = { driveLetter: 'A', path: 'folder/file with spaces.txt' }
24+
expect(getFileName(file)).toBe('file with spaces.txt')
25+
})
26+
27+
it('should handle paths with dots in folder names', () => {
28+
const file: PiRatFile = { driveLetter: 'A', path: 'folder.v2/subfolder.test/file.mkv' }
29+
expect(getFileName(file)).toBe('file.mkv')
30+
})
31+
})
32+
33+
describe('getParentPath', () => {
34+
it('should return the parent path from a simple path', () => {
35+
const file: PiRatFile = { driveLetter: 'A', path: 'folder/subfolder/movie.mkv' }
36+
expect(getParentPath(file)).toBe('folder/subfolder')
37+
})
38+
39+
it('should return the parent path from a deeply nested path', () => {
40+
const file: PiRatFile = { driveLetter: 'A', path: 'a/b/c/d/e/file.txt' }
41+
expect(getParentPath(file)).toBe('a/b/c/d/e')
42+
})
43+
44+
it('should return the file name when path has no parent directory', () => {
45+
const file: PiRatFile = { driveLetter: 'A', path: 'document.pdf' }
46+
// PathHelper.getParentPath returns the path itself when there's no parent
47+
expect(getParentPath(file)).toBe('document.pdf')
48+
})
49+
50+
it('should handle paths with special characters', () => {
51+
const file: PiRatFile = { driveLetter: 'A', path: 'folder with spaces/file.txt' }
52+
expect(getParentPath(file)).toBe('folder with spaces')
53+
})
54+
})
55+
56+
describe('getFullPath', () => {
57+
it('should combine parent path and file name', () => {
58+
expect(getFullPath('folder/subfolder', 'movie.mkv')).toBe('folder/subfolder/movie.mkv')
59+
})
60+
61+
it('should handle empty parent path', () => {
62+
expect(getFullPath('', 'document.pdf')).toBe('document.pdf')
63+
})
64+
65+
it('should normalize paths with trailing slashes', () => {
66+
expect(getFullPath('folder/', 'file.txt')).toBe('folder/file.txt')
67+
})
68+
69+
it('should handle paths with special characters', () => {
70+
expect(getFullPath('folder with spaces', 'file name.txt')).toBe('folder with spaces/file name.txt')
71+
})
72+
73+
it('should normalize paths with double slashes', () => {
74+
const result = getFullPath('folder', '/file.txt')
75+
expect(result).toBe('folder/file.txt')
76+
})
77+
})
78+
})
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { encode, decode } from './url-encode-decode.js'
3+
4+
describe('url-encode-decode', () => {
5+
describe('encode', () => {
6+
it('should encode a simple string', () => {
7+
const encoded = encode('hello')
8+
expect(typeof encoded).toBe('string')
9+
expect(encoded.length).toBeGreaterThan(0)
10+
})
11+
12+
it('should encode an object', () => {
13+
const encoded = encode({ foo: 'bar' })
14+
expect(typeof encoded).toBe('string')
15+
expect(encoded.length).toBeGreaterThan(0)
16+
})
17+
18+
it('should encode a number', () => {
19+
const encoded = encode(42)
20+
expect(typeof encoded).toBe('string')
21+
expect(encoded.length).toBeGreaterThan(0)
22+
})
23+
24+
it('should encode a boolean', () => {
25+
const encoded = encode(true)
26+
expect(typeof encoded).toBe('string')
27+
expect(encoded.length).toBeGreaterThan(0)
28+
})
29+
30+
it('should encode null', () => {
31+
const encoded = encode(null)
32+
expect(typeof encoded).toBe('string')
33+
expect(encoded.length).toBeGreaterThan(0)
34+
})
35+
36+
it('should encode an array', () => {
37+
const encoded = encode([1, 2, 3])
38+
expect(typeof encoded).toBe('string')
39+
expect(encoded.length).toBeGreaterThan(0)
40+
})
41+
42+
it('should produce URL-safe output', () => {
43+
const encoded = encode({ complex: 'value with spaces & special=chars' })
44+
// URL-safe characters should not need further encoding
45+
expect(encoded).not.toContain(' ')
46+
expect(encoded).not.toContain('&')
47+
expect(encoded).not.toContain('=')
48+
})
49+
})
50+
51+
describe('decode', () => {
52+
it('should decode back to a simple string', () => {
53+
const original = 'hello'
54+
const encoded = encode(original)
55+
const decoded = decode<string>(encoded)
56+
expect(decoded).toBe(original)
57+
})
58+
59+
it('should decode back to an object', () => {
60+
const original = { foo: 'bar', baz: 123 }
61+
const encoded = encode(original)
62+
const decoded = decode<typeof original>(encoded)
63+
expect(decoded).toEqual(original)
64+
})
65+
66+
it('should decode back to a number', () => {
67+
const original = 42
68+
const encoded = encode(original)
69+
const decoded = decode<number>(encoded)
70+
expect(decoded).toBe(original)
71+
})
72+
73+
it('should decode back to a boolean', () => {
74+
const original = true
75+
const encoded = encode(original)
76+
const decoded = decode<boolean>(encoded)
77+
expect(decoded).toBe(original)
78+
})
79+
80+
it('should decode back to null', () => {
81+
const encoded = encode(null)
82+
const decoded = decode<null>(encoded)
83+
expect(decoded).toBeNull()
84+
})
85+
86+
it('should decode back to an array', () => {
87+
const original = [1, 2, 3, 'four']
88+
const encoded = encode(original)
89+
const decoded = decode<typeof original>(encoded)
90+
expect(decoded).toEqual(original)
91+
})
92+
})
93+
94+
describe('roundtrip', () => {
95+
it('should handle unicode characters', () => {
96+
const original = { message: 'Hello 世界' }
97+
const encoded = encode(original)
98+
const decoded = decode<typeof original>(encoded)
99+
expect(decoded).toEqual(original)
100+
})
101+
102+
it('should handle emoji', () => {
103+
const original = { foo: 'bar😉' }
104+
const encoded = encode(original)
105+
const decoded = decode<typeof original>(encoded)
106+
expect(decoded).toEqual(original)
107+
})
108+
109+
it('should handle complex nested objects', () => {
110+
const original = {
111+
user: {
112+
name: 'John Doe',
113+
settings: {
114+
theme: 'dark',
115+
notifications: true,
116+
},
117+
},
118+
items: [1, 2, { nested: 'value' }],
119+
}
120+
const encoded = encode(original)
121+
const decoded = decode<typeof original>(encoded)
122+
expect(decoded).toEqual(original)
123+
})
124+
125+
it('should handle special characters', () => {
126+
const original = {
127+
query: 'search?q=test&page=1',
128+
path: '/folder/file.txt',
129+
special: '<script>alert("xss")</script>',
130+
}
131+
const encoded = encode(original)
132+
const decoded = decode<typeof original>(encoded)
133+
expect(decoded).toEqual(original)
134+
})
135+
136+
it('should handle empty string', () => {
137+
const original = ''
138+
const encoded = encode(original)
139+
const decoded = decode<string>(encoded)
140+
expect(decoded).toBe(original)
141+
})
142+
143+
it('should handle empty object', () => {
144+
const original = {}
145+
const encoded = encode(original)
146+
const decoded = decode<typeof original>(encoded)
147+
expect(decoded).toEqual(original)
148+
})
149+
150+
it('should handle empty array', () => {
151+
const original: unknown[] = []
152+
const encoded = encode(original)
153+
const decoded = decode<typeof original>(encoded)
154+
expect(decoded).toEqual(original)
155+
})
156+
})
157+
})

0 commit comments

Comments
 (0)