-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingConversionService.test.ts
More file actions
96 lines (69 loc) · 3.42 KB
/
EncodingConversionService.test.ts
File metadata and controls
96 lines (69 loc) · 3.42 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
import { describe, it, expect } from '@jest/globals';
import EncodingConversionService from '../src/EncodingConversionService';
describe('EncodingConversionService', () => {
describe('convertToUtf8', () => {
it('should convert UTF-8 buffer to UTF-8 string', () => {
const content = 'Hello World';
const buffer = Buffer.from(content, 'utf8');
const result = EncodingConversionService.convertToUtf8(buffer, 'utf8');
expect(result).toBe(content);
});
it('should convert ASCII buffer to UTF-8 string', () => {
const content = 'Hello World';
const buffer = Buffer.from(content, 'ascii');
const result = EncodingConversionService.convertToUtf8(buffer, 'ascii');
expect(result).toBe(content);
});
it('should convert Latin-1 buffer to UTF-8 string', () => {
const content = 'Café résumé';
const buffer = Buffer.from(content, 'latin1');
const result = EncodingConversionService.convertToUtf8(buffer, 'latin1');
expect(result).toBe(content);
});
it('should handle Arabic text conversion', () => {
const arabicText = 'مرحبا بالعالم';
const buffer = Buffer.from(arabicText, 'utf8');
const result = EncodingConversionService.convertToUtf8(buffer, 'utf8');
expect(result).toBe(arabicText);
});
it('should handle empty buffer', () => {
const buffer = Buffer.alloc(0);
const result = EncodingConversionService.convertToUtf8(buffer, 'utf8');
expect(result).toBe('');
});
it('should handle Windows-1252 encoding', () => {
const content = 'Smart quotes: "Hello"';
// Create buffer with Windows-1252 specific characters
const buffer = Buffer.from([
0x53, 0x6d, 0x61, 0x72, 0x74, 0x20, 0x71, 0x75, 0x6f, 0x74, 0x65, 0x73, 0x3a, 0x20, 0x93, 0x48, 0x65,
0x6c, 0x6c, 0x6f, 0x94,
]);
const result = EncodingConversionService.convertToUtf8(buffer, 'windows-1252');
expect(result).toContain('Smart quotes:');
expect(result).toContain('Hello');
});
it('should throw error for invalid encoding names', () => {
const content = 'Hello World';
const buffer = Buffer.from(content, 'utf8');
// iconv-lite throws for unknown encodings
expect(() => {
EncodingConversionService.convertToUtf8(buffer, 'invalid-encoding');
}).toThrow('Encoding not recognized');
});
it('should handle binary data gracefully', () => {
const buffer = Buffer.from([0x00, 0x01, 0x02, 0x03, 0xff, 0xfe]);
const result = EncodingConversionService.convertToUtf8(buffer, 'utf8');
expect(typeof result).toBe('string');
expect(result).toBeDefined();
});
it('should preserve line endings during conversion', () => {
const content = 'Line 1\r\nLine 2\nLine 3\r';
const buffer = Buffer.from(content, 'utf8');
const result = EncodingConversionService.convertToUtf8(buffer, 'utf8');
expect(result).toBe(content);
expect(result).toContain('\r\n');
expect(result).toContain('\n');
expect(result).toContain('\r');
});
});
});