|
1 | 1 | import { describe, expect, it } from '@jest/globals';
|
2 |
| -import { sanitizeFilename } from '../lib'; |
| 2 | +import { |
| 3 | + MAX_FILE_PATH_LENGTH, |
| 4 | + isValidLength, |
| 5 | + sanitizeFilename, |
| 6 | + truncateMiddle, |
| 7 | + truncatePathParts, |
| 8 | +} from '../lib'; |
| 9 | + |
| 10 | +describe('isValidLength', () => { |
| 11 | + const testCases: [string, number | undefined, boolean][] = [ |
| 12 | + ['short.txt', undefined, true], // Valid filename with default maxLength |
| 13 | + ['a'.repeat(MAX_FILE_PATH_LENGTH), undefined, true], // Valid filename with maximum maxLength |
| 14 | + ['a'.repeat(MAX_FILE_PATH_LENGTH + 1), undefined, false], // Invalid filename exceeding maxLength |
| 15 | + ['abcdefghij', 10, true], // Valid filename with specified maxLength |
| 16 | + ['abcdefghijk', 10, false], // Invalid filename exceeding specified maxLength |
| 17 | + ['anystring.txt', 0, false], // Invalid filename with maxLength of 0 |
| 18 | + ]; |
| 19 | + |
| 20 | + it.each(testCases)( |
| 21 | + 'should return the exepcted result for the provided string and max length', |
| 22 | + (str: string, maxLength: number | undefined, expectedResult: boolean) => { |
| 23 | + expect(isValidLength(str, maxLength)).toBe(expectedResult); |
| 24 | + } |
| 25 | + ); |
| 26 | +}); |
| 27 | + |
| 28 | +describe('truncateMiddle', () => { |
| 29 | + const longString = |
| 30 | + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac odio ac quam auctor faucibus ut id dolor. Vivamus vel odio eu ligula tempus viverra. Aenean vehicula, ex non varius euismod, elit ex cursus ex, in bibendum quam elit quis tortor. Sed volutpat scelerisque tortor quis blandit. A'; |
| 31 | + const truncatedString = |
| 32 | + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac odio ac quam auctor faucibus ut id dolor. Vivamus vel o..., ex non varius euismod, elit ex cursus ex, in bibendum quam elit quis tortor. Sed volutpat scelerisque tortor quis blandit. A'; |
| 33 | + const testCases = [ |
| 34 | + ['short.txt', MAX_FILE_PATH_LENGTH, 'short.txt'], // Short filename within maximum length |
| 35 | + [longString, MAX_FILE_PATH_LENGTH, truncatedString], // Long filename, truncated with default separator |
| 36 | + ['short.txt', 5, 's...t'], // Short filename within a custom maximum length |
| 37 | + ['longname.txt', 8, 'lon...xt'], // Long filename truncated with custom maximum length and default separator |
| 38 | + ['filename.txt', 10, 'file...txt'], // Medium-length filename truncated with default separator |
| 39 | + ['middleseparator.txt', 15, 'middle...or.txt'], // Medium-length filename truncated with default separator |
| 40 | + ['separatoratstart.txt', 15, 'separa...rt.txt'], // Medium-length filename truncated with default separator |
| 41 | + ] as Array<[string, number, string]>; |
| 42 | + |
| 43 | + it.each(testCases)( |
| 44 | + 'should truncate the string correctly for the provided filename and max length', |
| 45 | + (filename, maxLength, expected) => { |
| 46 | + const result = truncateMiddle(filename, maxLength); |
| 47 | + expect(result).toBe(expected); |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + const truncatedStringWithDashes = |
| 52 | + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac odio ac quam auctor faucibus ut id dolor. Vivamus vel od--, ex non varius euismod, elit ex cursus ex, in bibendum quam elit quis tortor. Sed volutpat scelerisque tortor quis blandit. A'; |
| 53 | + |
| 54 | + const customSeparatorTestCases = [ |
| 55 | + ['short.txt', MAX_FILE_PATH_LENGTH, 'short.txt'], // Short filename within maximum length |
| 56 | + [longString, MAX_FILE_PATH_LENGTH, truncatedStringWithDashes], // Long filename, truncated with default separator |
| 57 | + ['short.txt', 5, 'sh--t'], // Short filename within a custom maximum length |
| 58 | + ['longname.txt', 8, 'lon--txt'], // Long filename truncated with custom maximum length and default separator |
| 59 | + ['filename.txt', 10, 'file--.txt'], // Medium-length filename truncated with default separator |
| 60 | + ['middleseparator.txt', 15, 'middles--or.txt'], // Medium-length filename truncated with default separator |
| 61 | + ['separatoratstart.txt', 15, 'separat--rt.txt'], // Medium-length filename truncated with default separator |
| 62 | + ] as Array<[string, number, string]>; |
| 63 | + |
| 64 | + it.each(customSeparatorTestCases)( |
| 65 | + 'truncates "%s" to "%s" with max length %d and custom separator', |
| 66 | + (filename, maxLength, expected) => { |
| 67 | + const result = truncateMiddle(filename, maxLength, '--'); |
| 68 | + expect(result).toBe(expected); |
| 69 | + } |
| 70 | + ); |
| 71 | +}); |
3 | 72 |
|
4 | 73 | describe('sanitizeFilename', () => {
|
5 |
| - it('should sanitize the string', () => { |
6 |
| - expect(sanitizeFilename('a/b\\c?d*e:f|g<h>i%jk', ' ')).toEqual( |
7 |
| - 'a b c d e f g h i jk' |
8 |
| - ); |
| 74 | + const testCases = [ |
| 75 | + ['validFilename.txt', 'validFilename.txt'], // Valid filename with no invalid characters |
| 76 | + ['file?with?question?mark.txt', 'file-with-question-mark.txt'], // Replace '?' with '-' |
| 77 | + ['file*with*asterisk.txt', 'file-with-asterisk.txt'], // Replace '*' with '-' |
| 78 | + ['file<with<less.txt', 'file-with-less.txt'], // Replace '<' with '-' |
| 79 | + ['file>with>greater.txt', 'file-with-greater.txt'], // Replace '>' with '-' |
| 80 | + ['file:with:colon.txt', 'file-with-colon.txt'], // Replace ':' with '-' |
| 81 | + ['file|with|pipe.txt', 'file-with-pipe.txt'], // Replace '|' with '-' |
| 82 | + ['file%with%percent.txt', 'file-with-percent.txt'], // Replace '%' with '-' |
| 83 | + ['file"with"doublequote.txt', 'file-with-doublequote.txt'], // Replace '"' with '-' |
| 84 | + ['file/with/slash.txt', 'file-with-slash.txt'], // Replace '/' with '-' |
| 85 | + ['file\\with\\backslash.txt', 'file-with-backslash.txt'], // Replace '\\' with '-' |
| 86 | + [ |
| 87 | + 'mixed?chars*and<slashes>and:stuff|"here%.txt', |
| 88 | + 'mixed-chars-and-slashes-and-stuff--here-.txt', |
| 89 | + ], // Replace multiple invalid characters with '-' |
| 90 | + ]; |
| 91 | + |
| 92 | + it.each(testCases)( |
| 93 | + 'should sanitize the string as expected', |
| 94 | + (input, expected) => { |
| 95 | + const result = sanitizeFilename(input); |
| 96 | + expect(result).toBe(expected); |
| 97 | + } |
| 98 | + ); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('truncatePathParts function', () => { |
| 102 | + const longFolderName = |
| 103 | + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu'; |
| 104 | + const truncatedLongFolderName = |
| 105 | + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu'; |
| 106 | + const longFilename = |
| 107 | + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopq.json'; |
| 108 | + const truncatedLongFilename = |
| 109 | + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst...yzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopq.json'; |
| 110 | + it.each([ |
| 111 | + ['folder1/file.txt', 'folder1/file.txt'], |
| 112 | + [ |
| 113 | + `${longFolderName}/${longFolderName}/folder3/file.txt`, |
| 114 | + `${truncatedLongFolderName}/${truncatedLongFolderName}/folder3/file.txt`, |
| 115 | + ], |
| 116 | + [`folder1/${longFilename}`, `folder1/${truncatedLongFilename}`], |
| 117 | + ])('should truncate path parts', (input, expected) => { |
| 118 | + const result = truncatePathParts(input); |
| 119 | + expect(result).toEqual(expected); |
9 | 120 | });
|
10 | 121 | });
|
0 commit comments