5.0 KB147 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import {minimalDisambiguousPaths} from '../minimalDisambiguousPaths';
9
10describe('computeDisplayPaths', () => {
11 it('should compute depth 1 paths correctly', () => {
12 const input = ['/a/b.js', '/a/c.js'];
13 const expected = ['b.js', 'c.js'];
14 const actual = minimalDisambiguousPaths(input);
15 expect(actual).toEqual(expected);
16 });
17
18 it('should compute depth 2 paths correctly', () => {
19 const input = ['/a/b.js', '/c/b.js'];
20 const expected = ['/a/b.js', '/c/b.js'];
21 const actual = minimalDisambiguousPaths(input);
22 expect(actual).toEqual(expected);
23 });
24
25 it('should compute a mix of depths 1 to 5 paths correctly', () => {
26 const input = [
27 '/a/b/c/d/e.js',
28 '/z/b/c/d/e.js',
29 'a/y/z/e.js',
30 'a/y/e.js',
31 'a/z/e.js',
32 'a/f.js',
33 ];
34 const expected = ['/a/b/c/d/e.js', '/z/b/c/d/e.js', 'y/z/e.js', 'y/e.js', 'a/z/e.js', 'f.js'];
35 const actual = minimalDisambiguousPaths(input);
36 expect(actual).toEqual(expected);
37 });
38
39 it('should respect alwaysShowLeadingSeparator = false', () => {
40 const input = [
41 '/a/b/c/d/e.js',
42 '/z/b/c/d/e.js',
43 'a/y/z/e.js',
44 'a/y/e.js',
45 'a/z/e.js',
46 'a/f.js',
47 ];
48 const expected = ['/a/b/c/d/e.js', '/z/b/c/d/e.js', 'y/z/e.js', 'y/e.js', 'a/z/e.js', 'f.js'];
49 const actual = minimalDisambiguousPaths(input, {alwaysShowLeadingSeparator: false});
50 expect(actual).toEqual(expected);
51 });
52
53 it('should handle duplicate paths correctly', () => {
54 const input = ['/a/b/c/d/e.js', '/a/b/c/d/e.js'];
55 const expected = ['/a/b/c/d/e.js', '/a/b/c/d/e.js'];
56 const actual = minimalDisambiguousPaths(input);
57 expect(actual).toEqual(expected);
58 });
59
60 it('should handle paths that appear as a subpath of another path correctly', () => {
61 const input = ['/a/b/c/d/e.js', '/c/d/e.js'];
62 const expected = ['/b/c/d/e.js', '/c/d/e.js'];
63 const actual = minimalDisambiguousPaths(input);
64 expect(actual).toEqual(expected);
65 });
66
67 it('should honor the max depth argument', () => {
68 const input = ['/a/b/c/d/e.js', '/z/b/c/d/e.js'];
69 const expected = ['/d/e.js', '/d/e.js'];
70 const actual = minimalDisambiguousPaths(input, {maxDepth: 2});
71 expect(actual).toEqual(expected);
72 });
73
74 it('should handle empty input', () => {
75 const input: string[] = [];
76 const expected: string[] = [];
77 const actual = minimalDisambiguousPaths(input);
78 expect(actual).toEqual(expected);
79 });
80
81 it('should handle a realistic scenario path separators', () => {
82 const input = [
83 '/data/users/x/fbsource/fbcode/foo/bar.js',
84 '/data/users/x/fbsource/fbcode/bar/bar.js',
85 '/Users/y/fbsource/fbcode/foo/bar.js',
86 '/Users/y/fbsource/fbcode/bar/bar.js',
87 ];
88 const expected = [
89 '/x/fbsource/fbcode/foo/bar.js',
90 '/x/fbsource/fbcode/bar/bar.js',
91 '/y/fbsource/fbcode/foo/bar.js',
92 '/y/fbsource/fbcode/bar/bar.js',
93 ];
94 const actual = minimalDisambiguousPaths(input);
95 expect(actual).toEqual(expected);
96 });
97
98 it('should handle root correctly (maintain the "/")', () => {
99 const input = ['/', '/foo'];
100 const expected = ['/', 'foo'];
101 const actual = minimalDisambiguousPaths(input);
102 expect(actual).toEqual(expected);
103 });
104
105 it('should handle relative paths correctly (not insert a new "/")', () => {
106 const input = ['hello/hi.txt', 'baz/bar.js', 'foo/bar.js'];
107 const expected = ['hi.txt', 'baz/bar.js', 'foo/bar.js'];
108 const actual = minimalDisambiguousPaths(input);
109 expect(actual).toEqual(expected);
110 });
111
112 it('should handle trailing slashes correctly', () => {
113 const input = ['/foo/'];
114 const expected = ['foo'];
115 const actual = minimalDisambiguousPaths(input);
116 expect(actual).toEqual(expected);
117 });
118
119 it('should handle windows paths without ambiguity correctly', () => {
120 const input = ['c:\\foo\\baz.js', 'c:\\foo\\bar.js'];
121 const expected = ['baz.js', 'bar.js'];
122 const actual = minimalDisambiguousPaths(input);
123 expect(actual).toEqual(expected);
124 });
125
126 it('should handle windows paths with ambiguity correctly', () => {
127 const input = ['c:\\foo\\bar\\baz.js', 'c:\\foo\\bar2\\baz.js'];
128 const expected = ['\\bar\\baz.js', '\\bar2\\baz.js'];
129 const actual = minimalDisambiguousPaths(input);
130 expect(actual).toEqual(expected);
131 });
132
133 it('should include the prefix on windows when it is showing the full path', () => {
134 const input = ['c:\\foo2\\bar.js', 'c:\\foo\\bar.js'];
135 const expected = ['c:\\foo2\\bar.js', 'c:\\foo\\bar.js'];
136 const actual = minimalDisambiguousPaths(input);
137 expect(actual).toEqual(expected);
138 });
139
140 it('should hide windows path drive prefix when paths are minimized', () => {
141 const input = ['c:\\foo\\bar\\boo.js', 'c:\\foo\\bar2\\blam.js'];
142 const expected = ['boo.js', 'blam.js'];
143 const actual = minimalDisambiguousPaths(input);
144 expect(actual).toEqual(expected);
145 });
146});
147