aboutsummaryrefslogtreecommitdiff
path: root/webAO/__tests__/encoding.test.ts
blob: 36f34cfc1837f0ffccb4c09bbd479d5b4b8800e7 (plain)
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
import { escapeChat, unescapeChat, safeTags, decodeChat, prepChat } from '../encoding';

describe('encode/decode', () => {
  it('should escape special characters correctly', () => {
    const input = '#&$%';
    const expectedOutput = '<num><and><dollar><percent>';
    expect(escapeChat(input)).toBe(expectedOutput);
  });

  it('should unescape special characters correctly', () => {
    const input = '<num><and><dollar><percent>';
    const expectedOutput = '#&$%';
    expect(unescapeChat(input)).toBe(expectedOutput);
  });
});

describe('safeTags', () => {
  it('should replace < with < and > with >', () => {
    const input = '<div>Hello</div>';
    const expectedOutput = '<div>Hello</div>';
    expect(safeTags(input)).toBe(expectedOutput);
  });

  it('should handle empty strings correctly', () => {
    expect(safeTags('')).toBe('');
  });
});

describe('decodeChat', () => {
  it('should decode escaped unicode characters', () => {
    const input = '\\u0041\\u0026\\u003F';
    const expectedOutput = 'A&?';
    expect(decodeChat(input)).toBe(expectedOutput);
  });

  it('should handle no unicode to decode', () => {
    const input = 'Hello World!';
    expect(decodeChat(input)).toBe(input);
  });
});

describe('prepChat', () => {
  it('should apply safeTags, unescapeChat and decodeChat correctly', () => {
    const input = '<num><and>A<percent>';
    const expectedOutput = '#&A%'; // Output after applying all functions in order
    expect(prepChat(input)).toBe(expectedOutput);
  });

  it('should handle empty strings correctly', () => {
    expect(prepChat('')).toBe('');
  });
});