File size: 15,472 Bytes
4e1096a | 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | import { describe, it, expect } from 'vitest';
import { makeSafeFilename } from '../../utils/misc';
describe('makeSafeFilename', () => {
describe('Basic sanitization', () => {
it('should replace unsafe characters with underscore', () => {
expect(makeSafeFilename('file<name>.txt')).toBe('file_name_.txt');
expect(makeSafeFilename('file>name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file:name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file"name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file/name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file\\name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file|name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file?name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file*name.txt')).toBe('file_name.txt');
});
it('should replace multiple unsafe characters', () => {
expect(makeSafeFilename('file<>:"|?*.txt')).toBe('file_______.txt');
});
it('should use custom replacement character', () => {
expect(makeSafeFilename('file<name>.txt', '-')).toBe('file-name-.txt');
expect(makeSafeFilename('file:name.txt', '')).toBe('filename.txt');
});
it('should handle control characters', () => {
expect(makeSafeFilename('file\x00name.txt')).toBe('file_name.txt');
expect(makeSafeFilename('file\x1Fname.txt')).toBe('file_name.txt');
});
it('should trim whitespace from result', () => {
expect(makeSafeFilename(' filename.txt ')).toBe('filename.txt');
expect(makeSafeFilename('filename.txt ')).toBe('filename.txt');
expect(makeSafeFilename(' filename.txt')).toBe('filename.txt');
});
});
describe('Reserved filenames (Windows)', () => {
it('should handle reserved names case-insensitively', () => {
expect(makeSafeFilename('CON')).toBe('CON_');
expect(makeSafeFilename('con')).toBe('con_');
expect(makeSafeFilename('Con')).toBe('Con_');
expect(makeSafeFilename('PRN')).toBe('PRN_');
expect(makeSafeFilename('AUX')).toBe('AUX_');
expect(makeSafeFilename('NUL')).toBe('NUL_');
});
it('should handle reserved names with port numbers', () => {
expect(makeSafeFilename('COM1')).toBe('COM1_');
expect(makeSafeFilename('COM9')).toBe('COM9_');
expect(makeSafeFilename('LPT1')).toBe('LPT1_');
expect(makeSafeFilename('LPT9')).toBe('LPT9_');
});
it('should not affect reserved names with extensions', () => {
// Reserved names only apply to the base name without extension
const result = makeSafeFilename('CON.txt');
expect(result).toBe('CON.txt'); // This might be CON_.txt depending on implementation
});
it('should not affect similar but non-reserved names', () => {
expect(makeSafeFilename('CONFIG')).toBe('CONFIG');
expect(makeSafeFilename('CONSOLE')).toBe('CONSOLE');
expect(makeSafeFilename('PRINTER')).toBe('PRINTER');
});
});
describe('Multi-byte UTF-8 characters', () => {
it('should preserve single multi-byte characters', () => {
expect(makeSafeFilename('文件.txt')).toBe('文件.txt');
expect(makeSafeFilename('ファイル.txt')).toBe('ファイル.txt');
expect(makeSafeFilename('파일.txt')).toBe('파일.txt');
});
it('should preserve emoji characters', () => {
expect(makeSafeFilename('📚 Book.txt')).toBe('📚 Book.txt');
expect(makeSafeFilename('🎉🎊🎈.txt')).toBe('🎉🎊🎈.txt');
expect(makeSafeFilename('Test 😀.txt')).toBe('Test 😀.txt');
});
it('should handle mixed ASCII and multi-byte characters', () => {
expect(makeSafeFilename('Book-书籍-本.txt')).toBe('Book-书籍-本.txt');
expect(makeSafeFilename('Test_测试_テスト.txt')).toBe('Test_测试_テスト.txt');
});
it('should preserve complex emoji (with modifiers and ZWJ)', () => {
expect(makeSafeFilename('👨👩👧👦.txt')).toBe('👨👩👧👦.txt'); // Family emoji with ZWJ
expect(makeSafeFilename('👍🏽.txt')).toBe('👍🏽.txt'); // Thumbs up with skin tone modifier
});
it('should handle combining characters', () => {
expect(makeSafeFilename('café.txt')).toBe('café.txt'); // é is composed
expect(makeSafeFilename('naïve.txt')).toBe('naïve.txt');
});
});
describe('Byte length truncation (250 bytes max)', () => {
it('should not truncate short filenames', () => {
const shortName = 'short.txt';
expect(makeSafeFilename(shortName)).toBe(shortName);
});
it('should truncate long ASCII filenames', () => {
const longName = 'a'.repeat(260) + '.txt';
const result = makeSafeFilename(longName);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
});
it('should truncate at exactly 250 bytes', () => {
const longName = 'x'.repeat(255); // More than 250 bytes
const result = makeSafeFilename(longName);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBe(250);
});
it('should preserve valid UTF-8 when truncating multi-byte characters', () => {
// Chinese characters: each is 3 bytes in UTF-8
const chineseChars = '书'.repeat(100); // 300 bytes total
const result = makeSafeFilename(chineseChars);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
// Verify no broken UTF-8 by encoding and decoding
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�'); // No replacement characters
});
it('should handle Japanese characters when truncating', () => {
// Japanese hiragana: each is 3 bytes in UTF-8
const japaneseChars = 'あ'.repeat(100); // 300 bytes
const result = makeSafeFilename(japaneseChars);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
it('should handle emoji when truncating', () => {
// Most emoji are 4 bytes in UTF-8
const emojiString = '😀'.repeat(70); // 280 bytes
const result = makeSafeFilename(emojiString);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
it('should handle mixed-width characters when truncating', () => {
// Mix of 1-byte (ASCII), 2-byte (Latin extended), 3-byte (CJK), 4-byte (emoji)
const mixedString = 'Test测试тест😀'.repeat(20); // Over 250 bytes
const result = makeSafeFilename(mixedString);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
it('should handle Korean characters when truncating', () => {
// Korean characters: each is 3 bytes in UTF-8
const koreanChars = '가'.repeat(100); // 300 bytes
const result = makeSafeFilename(koreanChars);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
});
describe('Edge cases', () => {
it('should handle empty string', () => {
expect(makeSafeFilename('')).toBe('');
});
it('should handle string with only unsafe characters', () => {
expect(makeSafeFilename('<>:"|?*#')).toBe('________');
});
it('should handle string that becomes empty after sanitization and trimming', () => {
const result = makeSafeFilename(' ');
expect(result).toBe('');
});
it('should handle very long extension', () => {
const longExt = '.txt'.repeat(50);
const filename = 'file' + longExt;
const result = makeSafeFilename(filename);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
});
it('should handle filename with only whitespace', () => {
expect(makeSafeFilename(' ')).toBe('');
});
it('should handle complex real-world Chinese book title', () => {
const longTitle =
'这是一个非常长的中文书名用来测试文件名处理功能这个标题包含了很多汉字字符'.repeat(3) +
'.epub';
const result = makeSafeFilename(longTitle);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
// Verify UTF-8 validity
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
it('should handle complex real-world Chinese book title', () => {
const longTitle =
'榎宮祐 - NO GAME NO LIFE 遊戲人生 02 遊戲玩家兄妹似乎盯上獸耳娘的國家了'.repeat(3);
const result = makeSafeFilename(longTitle);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
// Verify UTF-8 validity
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
it('should handle complex real-world Chinese book titles with varying zero padding', () => {
const testCases = Array.from({ length: 31 }, (_, i) => {
const padding = '0'.repeat(i) + '2';
return `榎宮祐 - ${'NO GAME NO LIFE'.repeat(12)} 遊戲人生 ${padding} 遊戲玩家兄妹似乎盯上獸耳娘的國家了`;
});
for (const longTitle of testCases) {
const result = makeSafeFilename(longTitle);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
}
});
it('should handle right-to-left text (Arabic)', () => {
const arabicName = 'كتاب'.repeat(50) + '.pdf'; // Over 250 bytes
const result = makeSafeFilename(arabicName);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
});
it('should handle Cyrillic characters', () => {
const cyrillicName = 'Книга'.repeat(60) + '.txt'; // Over 250 bytes
const result = makeSafeFilename(cyrillicName);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
});
it('should handle Thai characters', () => {
const thaiName = 'หนังสือ'.repeat(50) + '.pdf'; // Over 250 bytes
const result = makeSafeFilename(thaiName);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
});
});
describe('Combined sanitization and truncation', () => {
it('should sanitize and truncate in correct order', () => {
const unsafeLongName = '<'.repeat(260) + '.txt';
const result = makeSafeFilename(unsafeLongName);
// Should replace < with _, then truncate
expect(result).not.toContain('<');
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
});
it('should handle reserved name that needs truncation', () => {
// Edge case: reserved name with very long content
const longReservedLike = 'CON' + 'x'.repeat(260);
const result = makeSafeFilename(longReservedLike);
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
});
it('should sanitize, handle reserved names, and truncate multi-byte characters', () => {
const complexName = 'CON:文件'.repeat(50) + '😀'.repeat(20);
const result = makeSafeFilename(complexName);
// Should not contain unsafe characters
expect(result).not.toContain(':');
// Should be within byte limit
const byteLength = new TextEncoder().encode(result).length;
expect(byteLength).toBeLessThanOrEqual(250);
// Should be valid UTF-8
const encoded = new TextEncoder().encode(result);
const decoded = new TextDecoder().decode(encoded);
expect(decoded).toBe(result);
expect(decoded).not.toContain('�');
});
});
describe('Real-world scenarios', () => {
it('should handle typical book title with author', () => {
const bookTitle = 'The Great Gatsby - F. Scott Fitzgerald.epub';
expect(makeSafeFilename(bookTitle)).toBe('The Great Gatsby - F. Scott Fitzgerald.epub');
});
it('should handle Chinese book with long title', () => {
const chineseBook = '红楼梦:中国古典文学四大名著之一(全120回完整版).epub';
const result = makeSafeFilename(chineseBook);
expect(result).toBe(chineseBook); // Should fit within 250 bytes
});
it('should handle Japanese light novel title', () => {
const japaneseTitle = 'ソードアート・オンライン:アリシゼーション編.epub';
expect(makeSafeFilename(japaneseTitle)).toBe(japaneseTitle);
});
it('should handle filename with unsafe characters and emoji', () => {
const unsafeEmoji = '📚 Book: "Title" <Part 1>.epub';
const result = makeSafeFilename(unsafeEmoji);
expect(result).toBe('📚 Book_ _Title_ _Part 1_.epub');
});
it('should handle Windows-style path in filename', () => {
const windowsPath = 'C:\\Users\\Documents\\book.pdf';
const result = makeSafeFilename(windowsPath);
expect(result).toBe('C__Users_Documents_book.pdf');
});
it('should handle URL in filename', () => {
const url = 'https://example.com/book.pdf';
const result = makeSafeFilename(url);
expect(result).toBe('https___example.com_book.pdf');
});
});
});
|