-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathutils.test.ts
More file actions
105 lines (91 loc) · 2.75 KB
/
utils.test.ts
File metadata and controls
105 lines (91 loc) · 2.75 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
import { expect, test, vi } from 'vitest';
vi.mock('@sourcebot/shared', () => ({
createLogger: () => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
}));
import { buildFileTree, isPathValid, normalizePath } from './utils';
test('normalizePath adds a trailing slash and strips leading slashes', () => {
expect(normalizePath('/a/b')).toBe('a/b/');
});
test('normalizePath keeps an existing trailing slash', () => {
expect(normalizePath('a/b/')).toBe('a/b/');
});
test('normalizePath returns empty string for root', () => {
expect(normalizePath('/')).toBe('');
});
test('isPathValid rejects traversal and null bytes', () => {
expect(isPathValid('a/../b')).toBe(false);
expect(isPathValid('a/\0b')).toBe(false);
});
test('isPathValid allows normal paths', () => {
expect(isPathValid('a/b')).toBe(true);
});
test('isPathValid allows paths with dots', () => {
expect(isPathValid('a/b/c.txt')).toBe(true);
expect(isPathValid('a/b/c...')).toBe(true);
expect(isPathValid('a/b/c.../d')).toBe(true);
expect(isPathValid('a/b/..c')).toBe(true);
expect(isPathValid('a/b/[..path]')).toBe(true);
});
test('buildFileTree handles a empty flat list', () => {
const flatList: { type: string, path: string }[] = [];
const tree = buildFileTree(flatList);
expect(tree).toMatchObject({
name: 'root',
type: 'tree',
path: '',
});
});
test('buildFileTree builds a sorted tree from a flat list', () => {
const flatList: { type: string, path: string }[] = [
{ type: 'blob', path: 'a' },
{ type: 'tree', path: 'b' },
{ type: 'tree', path: 'b/c' },
{ type: 'tree', path: 'd' },
{ type: 'blob', path: 'd/e' }
];
const tree = buildFileTree(flatList);
expect(tree).toMatchObject({
name: 'root',
type: 'tree',
path: '',
children: [
{
name: 'b',
type: 'tree',
path: 'b',
children: [
{
name: 'c',
type: 'tree',
path: 'b/c',
children: [],
},
],
},
{
name: 'd',
type: 'tree',
path: 'd',
children: [
{
name: 'e',
type: 'blob',
path: 'd/e',
children: [],
},
],
},
{
name: 'a',
type: 'blob',
path: 'a',
children: [],
}
],
});
});