File size: 1,663 Bytes
10695bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import assert from 'node:assert/strict';
import { test } from 'node:test';
import {
  decodeBuffer,
  decodeText,
  encodeBuffer,
  encodeText,
  hasUtf8Bom,
} from '../src/index.js';

test('matches the current ParaTranz live converter', () => {
  const source = '测试丣両!#[];=_$/中文';
  const expectedCodePoints = [
    16, 75, 109, 16, 213, 8249, 16, 35, 78, 17, 47, 78, 16, 1, 255,
    35, 91, 93, 59, 61, 95, 36, 47, 16, 45, 78, 16, 8225, 101,
  ];

  const encoded = encodeText(source, { profile: 'current' });
  assert.deepEqual([...encoded].map((character) => character.codePointAt(0)), expectedCodePoints);
  assert.equal(decodeText(encoded), source);
});

test('writes UTF-8 BOM and round-trips localisation syntax', () => {
  const source = 'l_english:\r\n TEST_KEY:0 "大明 §Y万岁§!"\r\n';
  const encoded = encodeBuffer(source);

  assert.equal(hasUtf8Bom(encoded), true);
  assert.deepEqual([...encoded.subarray(0, 3)], [0xef, 0xbb, 0xbf]);
  assert.equal(decodeBuffer(encoded), source);
});

test('rejects malformed escapes and non-BMP input', () => {
  assert.throws(() => decodeText('\u0010x'), /Truncated EU4 escape/);
  assert.throws(() => encodeText('𠀀'), /only supports BMP/);
  assert.throws(() => decodeBuffer(Buffer.from('plain', 'utf8')), /missing.*BOM/);
});

test('compatible profile escapes all bytes reserved by current and legacy converters', () => {
  const lowBytes = [0x21, 0x23, 0x2f, 0x3a];
  for (const lowByte of lowBytes) {
    const source = String.fromCodePoint(0x4e00 | lowByte);
    assert.equal(encodeText(source).codePointAt(0), 0x11);
    assert.equal(decodeText(encodeText(source)), source);
  }
});