opencc-sys 0.5.0+1.4.0

OpenCC bindings for Rust
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
const assert = require('assert');
const childProcess = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { after, describe, it } = require('node:test');

const OpenCC = require('./opencc');
const { prepareArtifacts } = require('../scripts/prepare-node-prebuild-artifacts');

const parseJSON = OpenCC._parseJSON;

const cases = parseJSON(fs.readFileSync('test/testcases/testcases.json', 'utf-8')).cases || [];

function createLocalInstalledShape() {
  const rootDir = path.resolve(__dirname, '..');
  const jiebaPackageDir = path.join(rootDir, 'plugins', 'jieba', 'node');
  const libName = os.platform() === 'win32' ? 'opencc-jieba.dll'
    : os.platform() === 'darwin' ? 'libopencc-jieba.dylib'
      : 'libopencc-jieba.so';
  const requiredFiles = [
    path.join(jiebaPackageDir, 'index.js'),
    path.join(jiebaPackageDir, 'data', 's2twp_jieba.json'),
    path.join(jiebaPackageDir, 'data', 'tw2sp_jieba.json'),
    path.join(jiebaPackageDir, 'data', 'jieba_dict', 'jieba_merged.ocd2'),
    path.join(jiebaPackageDir, 'prebuilds', `${os.platform()}-${os.arch()}`, libName),
  ];
  if (!requiredFiles.every((file) => fs.existsSync(file))) {
    return null;
  }
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-jieba-'));
  const nodeModulesDir = path.join(root, 'node_modules');
  fs.mkdirSync(nodeModulesDir, { recursive: true });
  fs.symlinkSync(rootDir, path.join(nodeModulesDir, 'opencc'), 'dir');
  fs.symlinkSync(jiebaPackageDir, path.join(nodeModulesDir, 'opencc-jieba'), 'dir');
  return root;
}

const testSync = function (tc, cfg, expected) {
  const opencc = new OpenCC(cfg + '.json');
  const converted = opencc.convertSync(tc.input);
  assert.equal(converted, expected);
};

const testAsync = function (tc, cfg, expected) {
  return new Promise(function (resolve, reject) {
    const opencc = new OpenCC(cfg + '.json');
    opencc.convert(tc.input, function (err, converted) {
      if (err) return reject(err);
      try {
        assert.equal(converted, expected);
        resolve();
      } catch (e) {
        reject(e);
      }
    });
  });
};

async function testAsyncPromise(tc, cfg, expected) {
  const opencc = new OpenCC(cfg + '.json');
  const converted = await opencc.convertPromise(tc.input);
  assert.equal(converted, expected);
}

describe('Sync API', function () {
  cases.forEach(function (tc, idx) {
    Object.entries(tc.expected || {}).forEach(function ([cfg, expected]) {
      it('[' + cfg + '] case #' + (idx + 1), function () {
        testSync(tc, cfg, expected);
      });
    });
  });
});

describe('API compatibility', function () {
  it('includes tofu-risk dictionaries by default', function () {
    const opencc = new OpenCC('t2s.json');
    assert.equal(opencc.convertSync(''), '𫝈');
  });

  it('supports JSONC (JSON with comments and trailing commas) configuration files', function () {
    const tempConfigPath = path.join(os.tmpdir(), 'test_comment_config.json');
    fs.writeFileSync(tempConfigPath, `
      // This is a single line comment
      {
        "name": "Test Config", /* This is a multi-line
        comment */
        "segmentation": {
          "type": "mmseg",
          "dict": {
            "type": "inline",
            "entries": {},
          },
        },
        "conversion_chain": [{
          "dict": {
            "type": "inline",
            "entries": {
              "A": "B",
            },
          },
        }],
      }
    `);
    try {
      const opencc = new OpenCC(tempConfigPath, { includeTofuRiskDictionaries: false });
      assert.equal(opencc.convertSync('A'), 'B');

      const openccWithTofu = new OpenCC(tempConfigPath, { includeTofuRiskDictionaries: true });
      assert.equal(openccWithTofu.convertSync('A'), 'B');
    } finally {
      if (fs.existsSync(tempConfigPath)) {
        fs.unlinkSync(tempConfigPath);
      }
    }
  });
});

describe('Async API', function () {
  cases.forEach(function (tc, idx) {
    Object.entries(tc.expected || {}).forEach(function ([cfg, expected]) {
      it('[' + cfg + '] case #' + (idx + 1), function () {
        return testAsync(tc, cfg, expected);
      });
    });
  });
});

describe('Async Promise API', function () {
  cases.forEach(function (tc, idx) {
    Object.entries(tc.expected || {}).forEach(function ([cfg, expected]) {
      it('[' + cfg + '] case #' + (idx + 1), function () {
        return testAsyncPromise(tc, cfg, expected);
      });
    });
  });
});

describe('npm CLI', function () {
  const cli = path.join(__dirname, 'cli.js');

  // The opencc module already resolves the addon and its adjacent assets dir.
  function getAssetsPath() {
    return OpenCC._assetsPath;
  }

  it('converts stdin to stdout', function () {
    const result = childProcess.spawnSync(process.execPath, [cli, '-c', 's2t.json'], {
      input: '汉字',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '漢字');
  });

  it('preserves stdin line endings and unknown characters', function () {
    const input = Buffer.from('鼠标=mouse\r\n123\n未登录', 'utf8');
    const result = childProcess.spawnSync(process.execPath, [cli, '-c', 's2t.json'], {
      input,
    });
    assert.equal(result.status, 0, result.stderr.toString('utf8'));
    assert.deepEqual(result.stdout, Buffer.from('鼠標=mouse\r\n123\n未登錄', 'utf8'));
  });

  it('appends .json to built-in config names', function () {
    const result = childProcess.spawnSync(process.execPath, [cli, '-c', 's2t'], {
      input: '汉字',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '漢字');
  });

  it('skips tofu-risk dictionaries by default', function () {
    const result = childProcess.spawnSync(process.execPath, [cli, '-c', 't2s.json'], {
      input: '',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '');
  });

  it('includes tofu-risk dictionaries when requested', function () {
    const result = childProcess.spawnSync(process.execPath, [
      cli,
      '-c',
      't2s.json',
      '--include-tofu-risk-dictionaries',
    ], {
      input: '',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '𫝈');
  });

  it('converts input file to output file', function () {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-'));
    const input = path.join(dir, 'input.txt');
    const output = path.join(dir, 'output.txt');
    fs.writeFileSync(input, '汉字', 'utf8');
    const result = childProcess.spawnSync(process.execPath, [
      cli,
      '--config=s2t.json',
      '--input',
      input,
      '--output',
      output,
    ], {
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(fs.readFileSync(output, 'utf8'), '漢字');
  });

  it('preserves input file line endings and unknown characters', function () {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-'));
    const input = path.join(dir, 'input.txt');
    const output = path.join(dir, 'output.txt');
    fs.writeFileSync(input, Buffer.from('鼠标=mouse\r\n123\n未登录', 'utf8'));
    const result = childProcess.spawnSync(process.execPath, [
      cli,
      '--config=s2t.json',
      '--input',
      input,
      '--output',
      output,
    ], {
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.deepEqual(
      fs.readFileSync(output),
      Buffer.from('鼠標=mouse\r\n123\n未登錄', 'utf8')
    );
  });

  it('rejects converting an input file onto itself', function () {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-same-file-'));
    const input = path.join(dir, 'input.txt');
    fs.writeFileSync(input, '汉字', 'utf8');
    const result = childProcess.spawnSync(process.execPath, [
      cli,
      '--config=s2t.json',
      '--input',
      input,
      '--output',
      input,
    ], {
      encoding: 'utf8',
    });
    assert.notEqual(result.status, 0);
    assert.match(result.stderr, /same file/);
    assert.equal(fs.readFileSync(input, 'utf8'), '汉字');
  });

  it('preserves phrase conversion across stream chunk boundaries', function () {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-boundary-'));
    const input = path.join(dir, 'input.txt');
    const output = path.join(dir, 'output.txt');
    fs.writeFileSync(input, 'a'.repeat(65535) + '后台老板', 'utf8');
    const result = childProcess.spawnSync(process.execPath, [
      cli,
      '--config=s2t.json',
      '--input',
      input,
      '--output',
      output,
    ], {
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(fs.readFileSync(output, 'utf8'), 'a'.repeat(65535) + '後臺老闆');
  });

  it('resolves custom relative config paths from cwd', function () {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-config-'));
    const assetsPath = getAssetsPath();
    fs.copyFileSync(path.join(assetsPath, 's2t.json'), path.join(dir, 'custom-s2t.json'));
    fs.copyFileSync(path.join(assetsPath, 'CJK_Compatibility_Ideographs.ocd2'), path.join(dir, 'CJK_Compatibility_Ideographs.ocd2'));
    fs.copyFileSync(path.join(assetsPath, 'STPhrases.ocd2'), path.join(dir, 'STPhrases.ocd2'));
    fs.copyFileSync(path.join(assetsPath, 'STPhrases_GeneratedFromRegionalPhrases.ocd2'), path.join(dir, 'STPhrases_GeneratedFromRegionalPhrases.ocd2'));
    fs.copyFileSync(path.join(assetsPath, 'STCharacters.ocd2'), path.join(dir, 'STCharacters.ocd2'));

    const result = childProcess.spawnSync(process.execPath, [cli, '-c', './custom-s2t.json'], {
      cwd: dir,
      input: '汉字',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '漢字');
  });

  it('does not append .json to custom relative config paths', function () {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-config-stem-'));
    const assetsPath = getAssetsPath();
    fs.copyFileSync(path.join(assetsPath, 's2t.json'), path.join(dir, 'custom-s2t.json'));
    fs.copyFileSync(path.join(assetsPath, 'STPhrases.ocd2'), path.join(dir, 'STPhrases.ocd2'));
    fs.copyFileSync(path.join(assetsPath, 'STCharacters.ocd2'), path.join(dir, 'STCharacters.ocd2'));

    const result = childProcess.spawnSync(process.execPath, [cli, '-c', './custom-s2t'], {
      cwd: dir,
      input: '汉字',
      encoding: 'utf8',
    });
    assert.notEqual(result.status, 0);
    assert.match(result.stderr, /custom-s2t/);
  });

  it('prints help', function () {
    const result = childProcess.spawnSync(process.execPath, [cli, '--help'], {
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.match(result.stdout, /Usage:/);
    assert.match(result.stdout, /Unsupported in the npm CLI:/);
    assert.match(result.stdout, /--inspect/);
    assert.match(result.stdout, /--segmentation/);
  });

  it('prints version', function () {
    const result = childProcess.spawnSync(process.execPath, [cli, '--version'], {
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout.trim(), OpenCC.version);
  });

  it('rejects unsupported diagnostic modes', function () {
    const inspect = childProcess.spawnSync(process.execPath, [cli, '--inspect'], {
      encoding: 'utf8',
    });
    assert.notEqual(inspect.status, 0);
    assert.match(inspect.stderr, /not supported/);

    const segmentation = childProcess.spawnSync(process.execPath, [cli, '--segmentation'], {
      encoding: 'utf8',
    });
    assert.notEqual(segmentation.status, 0);
    assert.match(segmentation.stderr, /not supported/);
  });

  it('rejects empty inline option values', function () {
    for (const option of ['--config=', '--input=', '--output=']) {
      const result = childProcess.spawnSync(process.execPath, [cli, option], {
        input: '汉字',
        encoding: 'utf8',
      });
      assert.notEqual(result.status, 0);
      assert.match(result.stderr, /Missing value/);
    }
  });

  describe('Line ending preservation', function () {
    const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-line-'));

    function hasCRLF(filePath) {
      const content = fs.readFileSync(filePath, 'utf8');
      // Check for CRLF pattern in the file
      return /\r\n/.test(content);
    }

    function hasLFOnly(filePath) {
      const content = fs.readFileSync(filePath, 'utf8');
      // Check for LF without CR
      return /\n/.test(content) && !/\r/.test(content);
    }

    after(function () {
      // Cleanup temp directory
      try {
        const entries = fs.readdirSync(tempDir);
        for (const entry of entries) {
          fs.rmSync(path.join(tempDir, entry), { recursive: true, force: true });
        }
        fs.rmdirSync(tempDir);
      } catch (e) {
        // Ignore cleanup errors
      }
    });

    it('preserves LF line ending', function () {
      const input = path.join(tempDir, 'input_lf.txt');
      const output = path.join(tempDir, 'output_lf.txt');
      fs.writeFileSync(input, '第一行\n第二行\n', 'utf8');

      const result = childProcess.spawnSync(process.execPath, [
        cli,
        '-c', 's2t.json',
        '--input', input,
        '--output', output,
      ], {
        encoding: 'utf8',
      });

      assert.equal(result.status, 0, result.stderr);
      assert(hasLFOnly(output), 'LF line ending should be preserved');
    });

    it('preserves CRLF line ending', function () {
      const input = path.join(tempDir, 'input_crlf.txt');
      const output = path.join(tempDir, 'output_crlf.txt');
      fs.writeFileSync(input, '第一行\r\n第二行\r\n', 'utf8');

      const result = childProcess.spawnSync(process.execPath, [
        cli,
        '-c', 's2t.json',
        '--input', input,
        '--output', output,
      ], {
        encoding: 'utf8',
      });

      assert.equal(result.status, 0, result.stderr);
      assert(hasCRLF(output), 'CRLF line ending should be preserved');
    });

    it('preserves CRLF with ASCII content', function () {
      const input = path.join(tempDir, 'input_ascii_crlf.txt');
      const output = path.join(tempDir, 'output_ascii_crlf.txt');
      fs.writeFileSync(input, 'hello\r\nworld\r\n', 'utf8');

      const result = childProcess.spawnSync(process.execPath, [
        cli,
        '-c', 's2t.json',
        '--input', input,
        '--output', output,
      ], {
        encoding: 'utf8',
      });

      assert.equal(result.status, 0, result.stderr);
      assert(hasCRLF(output), 'CRLF line ending should be preserved for ASCII');
    });

    it('preserves LF when no trailing newline', function () {
      const input = path.join(tempDir, 'input_no_trailing.txt');
      const output = path.join(tempDir, 'output_no_trailing.txt');
      fs.writeFileSync(input, '第一行\n第二行', 'utf8');

      const result = childProcess.spawnSync(process.execPath, [
        cli,
        '-c', 's2t.json',
        '--input', input,
        '--output', output,
      ], {
        encoding: 'utf8',
      });

      assert.equal(result.status, 0, result.stderr);
      // Input had LF, so output should have LF (no CR)
      assert(!/\r/.test(fs.readFileSync(output, 'utf8')), 'No CR in output');
    });
  });
});

describe('Optional opencc-jieba package integration', function () {
  it('loads jieba configs by mode name in the JavaScript API', function (t) {
    const installRoot = createLocalInstalledShape();
    if (!installRoot) {
      t.skip();
      return;
    }

    const script = [
      "const OpenCC = require('opencc');",
      "const converter = new OpenCC('s2twp_jieba');",
      "process.stdout.write(converter.convertSync('云计算'));",
    ].join('');
    const result = childProcess.spawnSync(process.execPath, ['-e', script], {
      cwd: installRoot,
      env: { ...process.env },
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '雲端計算');
  });

  it('resolves normalization dict paths in jieba configs', function (t) {
    const installRoot = createLocalInstalledShape();
    if (!installRoot) {
      t.skip();
      return;
    }

    // U+F900 is a CJK Compatibility Ideograph; normalization maps it to U+8C48.
    // If normalization dict paths are not resolved correctly the converter fails
    // to load entirely, so a successful conversion also proves path resolution.
    const script = [
      "const OpenCC = require('opencc');",
      "const converter = new OpenCC('s2twp_jieba');",
      "const result = converter.convertSync('豈');",
      "process.stdout.write(result.codePointAt(0).toString(16));",
    ].join('');
    const result = childProcess.spawnSync(process.execPath, ['-e', script], {
      cwd: installRoot,
      env: { ...process.env },
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '8c48');
  });

  it('loads jieba configs by mode name in the npm CLI', function (t) {
    const installRoot = createLocalInstalledShape();
    if (!installRoot) {
      t.skip();
      return;
    }

    const result = childProcess.spawnSync(process.execPath, [
      path.join(installRoot, 'node_modules', 'opencc', 'node', 'cli.js'),
      '-c',
      's2twp_jieba',
    ], {
      cwd: installRoot,
      env: { ...process.env },
      input: '云计算',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '雲端計算');
  });

  it('skips tofu-risk dictionaries in jieba configs by default in the npm CLI', function (t) {
    const installRoot = createLocalInstalledShape();
    if (!installRoot) {
      t.skip();
      return;
    }

    const result = childProcess.spawnSync(process.execPath, [
      path.join(installRoot, 'node_modules', 'opencc', 'node', 'cli.js'),
      '-c',
      'tw2sp_jieba',
    ], {
      cwd: installRoot,
      env: { ...process.env },
      input: '',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '');
  });

  it('includes tofu-risk dictionaries in jieba configs when requested in the npm CLI', function (t) {
    const installRoot = createLocalInstalledShape();
    if (!installRoot) {
      t.skip();
      return;
    }

    const result = childProcess.spawnSync(process.execPath, [
      path.join(installRoot, 'node_modules', 'opencc', 'node', 'cli.js'),
      '-c',
      'tw2sp_jieba',
      '--include-tofu-risk-dictionaries',
    ], {
      cwd: installRoot,
      env: { ...process.env },
      input: '',
      encoding: 'utf8',
    });
    assert.equal(result.status, 0, result.stderr);
    assert.equal(result.stdout, '𫝈');
  });
});

describe('Node prebuild assets', function () {
  it('collects only runtime json and ocd2 assets', function () {
    const root = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-prebuild-assets-'));
    const releaseDir = path.join(root, 'build', 'Release');
    const assetsDir = path.join(root, 'prebuilds', 'assets');

    fs.mkdirSync(releaseDir, { recursive: true });
    fs.mkdirSync(assetsDir, { recursive: true });
    fs.writeFileSync(path.join(releaseDir, 's2t.json'), '{}');
    fs.writeFileSync(path.join(releaseDir, 'STCharacters.ocd2'), 'dict');
    fs.writeFileSync(path.join(releaseDir, 'STCharacters.txt'), 'source');
    fs.writeFileSync(path.join(releaseDir, 'README.md'), 'docs');
    fs.writeFileSync(path.join(assetsDir, 'stale.txt'), 'stale');

    prepareArtifacts(root);

    assert.deepEqual(fs.readdirSync(assetsDir).sort(), [
      'STCharacters.ocd2',
      's2t.json',
    ]);
  });
});