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