| 1 |
const assert = require('assert'); |
| 2 |
const childProcess = require('child_process'); |
| 3 |
const fs = require('fs'); |
| 4 |
const os = require('os'); |
| 5 |
const path = require('path'); |
| 6 |
const { after, describe, it } = require('node:test'); |
| 7 |
|
| 8 |
const OpenCC = require('./opencc'); |
| 9 |
const { prepareArtifacts } = require('../scripts/prepare-node-prebuild-artifacts'); |
| 10 |
|
| 11 |
const parseJSON = OpenCC._parseJSON; |
| 12 |
|
| 13 |
const cases = parseJSON(fs.readFileSync('test/testcases/testcases.json', 'utf-8')).cases || []; |
| 14 |
|
| 15 |
function createLocalInstalledShape() { |
| 16 |
const rootDir = path.resolve(__dirname, '..'); |
| 17 |
const jiebaPackageDir = path.join(rootDir, 'plugins', 'jieba', 'node'); |
| 18 |
const libName = os.platform() === 'win32' ? 'opencc-jieba.dll' |
| 19 |
: os.platform() === 'darwin' ? 'libopencc-jieba.dylib' |
| 20 |
: 'libopencc-jieba.so'; |
| 21 |
const requiredFiles = [ |
| 22 |
path.join(jiebaPackageDir, 'index.js'), |
| 23 |
path.join(jiebaPackageDir, 'data', 's2twp_jieba.json'), |
| 24 |
path.join(jiebaPackageDir, 'data', 'tw2sp_jieba.json'), |
| 25 |
path.join(jiebaPackageDir, 'data', 'jieba_dict', 'jieba_merged.ocd2'), |
| 26 |
path.join(jiebaPackageDir, 'prebuilds', `${os.platform()}-${os.arch()}`, libName), |
| 27 |
]; |
| 28 |
if (!requiredFiles.every((file) => fs.existsSync(file))) { |
| 29 |
return null; |
| 30 |
} |
| 31 |
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-jieba-')); |
| 32 |
const nodeModulesDir = path.join(root, 'node_modules'); |
| 33 |
fs.mkdirSync(nodeModulesDir, { recursive: true }); |
| 34 |
fs.symlinkSync(rootDir, path.join(nodeModulesDir, 'opencc'), 'dir'); |
| 35 |
fs.symlinkSync(jiebaPackageDir, path.join(nodeModulesDir, 'opencc-jieba'), 'dir'); |
| 36 |
return root; |
| 37 |
} |
| 38 |
|
| 39 |
const testSync = function (tc, cfg, expected) { |
| 40 |
const opencc = new OpenCC(cfg + '.json'); |
| 41 |
const converted = opencc.convertSync(tc.input); |
| 42 |
assert.equal(converted, expected); |
| 43 |
}; |
| 44 |
|
| 45 |
const testAsync = function (tc, cfg, expected) { |
| 46 |
return new Promise(function (resolve, reject) { |
| 47 |
const opencc = new OpenCC(cfg + '.json'); |
| 48 |
opencc.convert(tc.input, function (err, converted) { |
| 49 |
if (err) return reject(err); |
| 50 |
try { |
| 51 |
assert.equal(converted, expected); |
| 52 |
resolve(); |
| 53 |
} catch (e) { |
| 54 |
reject(e); |
| 55 |
} |
| 56 |
}); |
| 57 |
}); |
| 58 |
}; |
| 59 |
|
| 60 |
async function testAsyncPromise(tc, cfg, expected) { |
| 61 |
const opencc = new OpenCC(cfg + '.json'); |
| 62 |
const converted = await opencc.convertPromise(tc.input); |
| 63 |
assert.equal(converted, expected); |
| 64 |
} |
| 65 |
|
| 66 |
describe('Sync API', function () { |
| 67 |
cases.forEach(function (tc, idx) { |
| 68 |
Object.entries(tc.expected || {}).forEach(function ([cfg, expected]) { |
| 69 |
it('[' + cfg + '] case #' + (idx + 1), function () { |
| 70 |
testSync(tc, cfg, expected); |
| 71 |
}); |
| 72 |
}); |
| 73 |
}); |
| 74 |
}); |
| 75 |
|
| 76 |
describe('API compatibility', function () { |
| 77 |
it('includes tofu-risk dictionaries by default', function () { |
| 78 |
const opencc = new OpenCC('t2s.json'); |
| 79 |
assert.equal(opencc.convertSync('㑮'), '𫝈'); |
| 80 |
}); |
| 81 |
|
| 82 |
it('supports inline configuration objects', function () { |
| 83 |
const opencc = new OpenCC({ |
| 84 |
name: 'Inline Object Config', |
| 85 |
segmentation: { |
| 86 |
type: 'mmseg', |
| 87 |
dict: { |
| 88 |
type: 'inline', |
| 89 |
entries: { |
| 90 |
'鼠标': '鼠标', |
| 91 |
}, |
| 92 |
}, |
| 93 |
}, |
| 94 |
conversion_chain: [{ |
| 95 |
dict: { |
| 96 |
type: 'inline', |
| 97 |
entries: { |
| 98 |
'鼠标': '滑鼠', |
| 99 |
}, |
| 100 |
}, |
| 101 |
}], |
| 102 |
}); |
| 103 |
|
| 104 |
assert.equal(opencc.convertSync('鼠标坏了'), '滑鼠坏了'); |
| 105 |
}); |
| 106 |
|
| 107 |
it('uses bundled assets as the default inline configuration directory', function () { |
| 108 |
const opencc = OpenCC.fromConfig({ |
| 109 |
name: 'Inline Object Config With Bundled Assets', |
| 110 |
conversion_chain: [{ |
| 111 |
dict: { type: 'ocd2', file: 'STCharacters.ocd2' }, |
| 112 |
}], |
| 113 |
}); |
| 114 |
|
| 115 |
assert.equal(opencc.convertSync('汉字'), '漢字'); |
| 116 |
}); |
| 117 |
|
| 118 |
it('supports inline configuration objects with relative dictionary files', function () { |
| 119 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-inline-config-')); |
| 120 |
const dict = path.join(dir, 'phrases.txt'); |
| 121 |
fs.writeFileSync(dict, '鼠标\t滑鼠\n', 'utf8'); |
| 122 |
|
| 123 |
try { |
| 124 |
const opencc = new OpenCC({ |
| 125 |
name: 'Inline Object Config With Files', |
| 126 |
segmentation: { |
| 127 |
type: 'mmseg', |
| 128 |
dict: { type: 'text', file: 'phrases.txt' }, |
| 129 |
}, |
| 130 |
conversion_chain: [{ |
| 131 |
dict: { type: 'text', file: 'phrases.txt' }, |
| 132 |
}], |
| 133 |
}, { configDirectory: dir }); |
| 134 |
|
| 135 |
assert.equal(opencc.convertSync('鼠标坏了'), '滑鼠坏了'); |
| 136 |
} finally { |
| 137 |
fs.rmSync(dir, { recursive: true, force: true }); |
| 138 |
} |
| 139 |
}); |
| 140 |
|
| 141 |
it('supports OpenCC.fromConfig with group tofu-risk dictionary filtering', function () { |
| 142 |
const config = { |
| 143 |
name: 'Inline Object Config Tofu Risk', |
| 144 |
conversion_chain: [ |
| 145 |
{ |
| 146 |
dict: { |
| 147 |
type: 'group', |
| 148 |
may_output_tofu: true, |
| 149 |
match_policy: 'short_circuit', |
| 150 |
dicts: [{ |
| 151 |
type: 'inline', |
| 152 |
entries: { |
| 153 |
'A': 'B', |
| 154 |
}, |
| 155 |
}], |
| 156 |
}, |
| 157 |
}, |
| 158 |
{ |
| 159 |
dict: { |
| 160 |
type: 'inline', |
| 161 |
entries: { |
| 162 |
'A': 'C', |
| 163 |
}, |
| 164 |
}, |
| 165 |
}, |
| 166 |
], |
| 167 |
}; |
| 168 |
|
| 169 |
const opencc = OpenCC.fromConfig(config, { includeTofuRiskDictionaries: false }); |
| 170 |
assert.equal(opencc.convertSync('A'), 'C'); |
| 171 |
assert.equal(config.conversion_chain.length, 2); |
| 172 |
}); |
| 173 |
|
| 174 |
it('rejects inline tofu-risk dictionaries consistently with C++ config parsing', function () { |
| 175 |
const result = childProcess.spawnSync(process.execPath, ['-e', ` |
| 176 |
const assert = require('assert'); |
| 177 |
const OpenCC = require('./node/opencc'); |
| 178 |
assert.throws(function () { |
| 179 |
OpenCC.fromConfig({ |
| 180 |
name: 'Inline Object Config Invalid Tofu Risk', |
| 181 |
conversion_chain: [{ |
| 182 |
dict: { |
| 183 |
type: 'inline', |
| 184 |
may_output_tofu: true, |
| 185 |
entries: { |
| 186 |
'A': 'B', |
| 187 |
}, |
| 188 |
}, |
| 189 |
}], |
| 190 |
}, { includeTofuRiskDictionaries: false }); |
| 191 |
}, /Inline dictionary does not support may_output_tofu/); |
| 192 |
`], { |
| 193 |
cwd: path.resolve(__dirname, '..'), |
| 194 |
encoding: 'utf8', |
| 195 |
}); |
| 196 |
assert.equal(result.status, 0, result.stderr); |
| 197 |
}); |
| 198 |
|
| 199 |
it('supports JSONC (JSON with comments and trailing commas) configuration files', function () { |
| 200 |
const tempConfigPath = path.join(os.tmpdir(), 'test_comment_config.json'); |
| 201 |
fs.writeFileSync(tempConfigPath, ` |
| 202 |
// This is a single line comment |
| 203 |
{ |
| 204 |
"name": "Test Config", /* This is a multi-line |
| 205 |
comment */ |
| 206 |
"segmentation": { |
| 207 |
"type": "mmseg", |
| 208 |
"dict": { |
| 209 |
"type": "inline", |
| 210 |
"entries": {}, |
| 211 |
}, |
| 212 |
}, |
| 213 |
"conversion_chain": [{ |
| 214 |
"dict": { |
| 215 |
"type": "inline", |
| 216 |
"entries": { |
| 217 |
"A": "B", |
| 218 |
}, |
| 219 |
}, |
| 220 |
}], |
| 221 |
} |
| 222 |
`); |
| 223 |
try { |
| 224 |
const opencc = new OpenCC(tempConfigPath, { includeTofuRiskDictionaries: false }); |
| 225 |
assert.equal(opencc.convertSync('A'), 'B'); |
| 226 |
|
| 227 |
const openccWithTofu = new OpenCC(tempConfigPath, { includeTofuRiskDictionaries: true }); |
| 228 |
assert.equal(openccWithTofu.convertSync('A'), 'B'); |
| 229 |
} finally { |
| 230 |
if (fs.existsSync(tempConfigPath)) { |
| 231 |
fs.unlinkSync(tempConfigPath); |
| 232 |
} |
| 233 |
} |
| 234 |
}); |
| 235 |
}); |
| 236 |
|
| 237 |
describe('Async API', function () { |
| 238 |
cases.forEach(function (tc, idx) { |
| 239 |
Object.entries(tc.expected || {}).forEach(function ([cfg, expected]) { |
| 240 |
it('[' + cfg + '] case #' + (idx + 1), function () { |
| 241 |
return testAsync(tc, cfg, expected); |
| 242 |
}); |
| 243 |
}); |
| 244 |
}); |
| 245 |
}); |
| 246 |
|
| 247 |
describe('Async Promise API', function () { |
| 248 |
cases.forEach(function (tc, idx) { |
| 249 |
Object.entries(tc.expected || {}).forEach(function ([cfg, expected]) { |
| 250 |
it('[' + cfg + '] case #' + (idx + 1), function () { |
| 251 |
return testAsyncPromise(tc, cfg, expected); |
| 252 |
}); |
| 253 |
}); |
| 254 |
}); |
| 255 |
}); |
| 256 |
|
| 257 |
describe('npm CLI', function () { |
| 258 |
const cli = path.join(__dirname, 'cli.js'); |
| 259 |
|
| 260 |
|
| 261 |
function getAssetsPath() { |
| 262 |
return OpenCC._assetsPath; |
| 263 |
} |
| 264 |
|
| 265 |
it('converts stdin to stdout', function () { |
| 266 |
const result = childProcess.spawnSync(process.execPath, [cli, '-c', 's2t.json'], { |
| 267 |
input: '汉字', |
| 268 |
encoding: 'utf8', |
| 269 |
}); |
| 270 |
assert.equal(result.status, 0, result.stderr); |
| 271 |
assert.equal(result.stdout, '漢字'); |
| 272 |
}); |
| 273 |
|
| 274 |
it('preserves stdin line endings and unknown characters', function () { |
| 275 |
const input = Buffer.from('鼠标=mouse\r\n123\n未登录', 'utf8'); |
| 276 |
const result = childProcess.spawnSync(process.execPath, [cli, '-c', 's2t.json'], { |
| 277 |
input, |
| 278 |
}); |
| 279 |
assert.equal(result.status, 0, result.stderr.toString('utf8')); |
| 280 |
assert.deepEqual(result.stdout, Buffer.from('鼠標=mouse\r\n123\n未登錄', 'utf8')); |
| 281 |
}); |
| 282 |
|
| 283 |
it('appends .json to built-in config names', function () { |
| 284 |
const result = childProcess.spawnSync(process.execPath, [cli, '-c', 's2t'], { |
| 285 |
input: '汉字', |
| 286 |
encoding: 'utf8', |
| 287 |
}); |
| 288 |
assert.equal(result.status, 0, result.stderr); |
| 289 |
assert.equal(result.stdout, '漢字'); |
| 290 |
}); |
| 291 |
|
| 292 |
it('skips tofu-risk dictionaries by default', function () { |
| 293 |
const result = childProcess.spawnSync(process.execPath, [cli, '-c', 't2s.json'], { |
| 294 |
input: '㑮', |
| 295 |
encoding: 'utf8', |
| 296 |
}); |
| 297 |
assert.equal(result.status, 0, result.stderr); |
| 298 |
assert.equal(result.stdout, '㑮'); |
| 299 |
}); |
| 300 |
|
| 301 |
it('includes tofu-risk dictionaries when requested', function () { |
| 302 |
const result = childProcess.spawnSync(process.execPath, [ |
| 303 |
cli, |
| 304 |
'-c', |
| 305 |
't2s.json', |
| 306 |
'--include-tofu-risk-dictionaries', |
| 307 |
], { |
| 308 |
input: '㑮', |
| 309 |
encoding: 'utf8', |
| 310 |
}); |
| 311 |
assert.equal(result.status, 0, result.stderr); |
| 312 |
assert.equal(result.stdout, '𫝈'); |
| 313 |
}); |
| 314 |
|
| 315 |
it('converts input file to output file', function () { |
| 316 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-')); |
| 317 |
const input = path.join(dir, 'input.txt'); |
| 318 |
const output = path.join(dir, 'output.txt'); |
| 319 |
fs.writeFileSync(input, '汉字', 'utf8'); |
| 320 |
const result = childProcess.spawnSync(process.execPath, [ |
| 321 |
cli, |
| 322 |
'--config=s2t.json', |
| 323 |
'--input', |
| 324 |
input, |
| 325 |
'--output', |
| 326 |
output, |
| 327 |
], { |
| 328 |
encoding: 'utf8', |
| 329 |
}); |
| 330 |
assert.equal(result.status, 0, result.stderr); |
| 331 |
assert.equal(fs.readFileSync(output, 'utf8'), '漢字'); |
| 332 |
}); |
| 333 |
|
| 334 |
it('preserves input file line endings and unknown characters', function () { |
| 335 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-')); |
| 336 |
const input = path.join(dir, 'input.txt'); |
| 337 |
const output = path.join(dir, 'output.txt'); |
| 338 |
fs.writeFileSync(input, Buffer.from('鼠标=mouse\r\n123\n未登录', 'utf8')); |
| 339 |
const result = childProcess.spawnSync(process.execPath, [ |
| 340 |
cli, |
| 341 |
'--config=s2t.json', |
| 342 |
'--input', |
| 343 |
input, |
| 344 |
'--output', |
| 345 |
output, |
| 346 |
], { |
| 347 |
encoding: 'utf8', |
| 348 |
}); |
| 349 |
assert.equal(result.status, 0, result.stderr); |
| 350 |
assert.deepEqual( |
| 351 |
fs.readFileSync(output), |
| 352 |
Buffer.from('鼠標=mouse\r\n123\n未登錄', 'utf8') |
| 353 |
); |
| 354 |
}); |
| 355 |
|
| 356 |
it('rejects converting an input file onto itself', function () { |
| 357 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-same-file-')); |
| 358 |
const input = path.join(dir, 'input.txt'); |
| 359 |
fs.writeFileSync(input, '汉字', 'utf8'); |
| 360 |
const result = childProcess.spawnSync(process.execPath, [ |
| 361 |
cli, |
| 362 |
'--config=s2t.json', |
| 363 |
'--input', |
| 364 |
input, |
| 365 |
'--output', |
| 366 |
input, |
| 367 |
], { |
| 368 |
encoding: 'utf8', |
| 369 |
}); |
| 370 |
assert.notEqual(result.status, 0); |
| 371 |
assert.match(result.stderr, /same file/); |
| 372 |
assert.equal(fs.readFileSync(input, 'utf8'), '汉字'); |
| 373 |
}); |
| 374 |
|
| 375 |
it('preserves phrase conversion across stream chunk boundaries', function () { |
| 376 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-boundary-')); |
| 377 |
const input = path.join(dir, 'input.txt'); |
| 378 |
const output = path.join(dir, 'output.txt'); |
| 379 |
fs.writeFileSync(input, 'a'.repeat(65535) + '后台老板', 'utf8'); |
| 380 |
const result = childProcess.spawnSync(process.execPath, [ |
| 381 |
cli, |
| 382 |
'--config=s2t.json', |
| 383 |
'--input', |
| 384 |
input, |
| 385 |
'--output', |
| 386 |
output, |
| 387 |
], { |
| 388 |
encoding: 'utf8', |
| 389 |
}); |
| 390 |
assert.equal(result.status, 0, result.stderr); |
| 391 |
assert.equal(fs.readFileSync(output, 'utf8'), 'a'.repeat(65535) + '後臺老闆'); |
| 392 |
}); |
| 393 |
|
| 394 |
it('resolves custom relative config paths from cwd', function () { |
| 395 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-config-')); |
| 396 |
const assetsPath = getAssetsPath(); |
| 397 |
fs.copyFileSync(path.join(assetsPath, 's2t.json'), path.join(dir, 'custom-s2t.json')); |
| 398 |
fs.copyFileSync(path.join(assetsPath, 'CJK_Compatibility_Ideographs.ocd2'), path.join(dir, 'CJK_Compatibility_Ideographs.ocd2')); |
| 399 |
fs.copyFileSync(path.join(assetsPath, 'STPhrases.ocd2'), path.join(dir, 'STPhrases.ocd2')); |
| 400 |
fs.copyFileSync(path.join(assetsPath, 'STPhrases_GeneratedFromRegionalPhrases.ocd2'), path.join(dir, 'STPhrases_GeneratedFromRegionalPhrases.ocd2')); |
| 401 |
fs.copyFileSync(path.join(assetsPath, 'STCharacters.ocd2'), path.join(dir, 'STCharacters.ocd2')); |
| 402 |
|
| 403 |
const result = childProcess.spawnSync(process.execPath, [cli, '-c', './custom-s2t.json'], { |
| 404 |
cwd: dir, |
| 405 |
input: '汉字', |
| 406 |
encoding: 'utf8', |
| 407 |
}); |
| 408 |
assert.equal(result.status, 0, result.stderr); |
| 409 |
assert.equal(result.stdout, '漢字'); |
| 410 |
}); |
| 411 |
|
| 412 |
it('does not append .json to custom relative config paths', function () { |
| 413 |
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-node-cli-config-stem-')); |
| 414 |
const assetsPath = getAssetsPath(); |
| 415 |
fs.copyFileSync(path.join(assetsPath, 's2t.json'), path.join(dir, 'custom-s2t.json')); |
| 416 |
fs.copyFileSync(path.join(assetsPath, 'STPhrases.ocd2'), path.join(dir, 'STPhrases.ocd2')); |
| 417 |
fs.copyFileSync(path.join(assetsPath, 'STCharacters.ocd2'), path.join(dir, 'STCharacters.ocd2')); |
| 418 |
|
| 419 |
const result = childProcess.spawnSync(process.execPath, [cli, '-c', './custom-s2t'], { |
| 420 |
cwd: dir, |
| 421 |
input: '汉字', |
| 422 |
encoding: 'utf8', |
| 423 |
}); |
| 424 |
assert.notEqual(result.status, 0); |
| 425 |
assert.match(result.stderr, /custom-s2t/); |
| 426 |
}); |
| 427 |
|
| 428 |
it('prints help', function () { |
| 429 |
const result = childProcess.spawnSync(process.execPath, [cli, '--help'], { |
| 430 |
encoding: 'utf8', |
| 431 |
}); |
| 432 |
assert.equal(result.status, 0, result.stderr); |
| 433 |
assert.match(result.stdout, /Usage:/); |
| 434 |
assert.match(result.stdout, /Unsupported in the npm CLI:/); |
| 435 |
assert.match(result.stdout, /--inspect/); |
| 436 |
assert.match(result.stdout, /--segmentation/); |
| 437 |
}); |
| 438 |
|
| 439 |
it('prints version', function () { |
| 440 |
const result = childProcess.spawnSync(process.execPath, [cli, '--version'], { |
| 441 |
encoding: 'utf8', |
| 442 |
}); |
| 443 |
assert.equal(result.status, 0, result.stderr); |
| 444 |
assert.equal(result.stdout.trim(), OpenCC.version); |
| 445 |
}); |
| 446 |
|
| 447 |
it('rejects unsupported diagnostic modes', function () { |
| 448 |
const inspect = childProcess.spawnSync(process.execPath, [cli, '--inspect'], { |
| 449 |
encoding: 'utf8', |
| 450 |
}); |
| 451 |
assert.notEqual(inspect.status, 0); |
| 452 |
assert.match(inspect.stderr, /not supported/); |
| 453 |
|
| 454 |
const segmentation = childProcess.spawnSync(process.execPath, [cli, '--segmentation'], { |
| 455 |
encoding: 'utf8', |
| 456 |
}); |
| 457 |
assert.notEqual(segmentation.status, 0); |
| 458 |
assert.match(segmentation.stderr, /not supported/); |
| 459 |
}); |
| 460 |
|
| 461 |
it('rejects empty inline option values', function () { |
| 462 |
for (const option of ['--config=', '--input=', '--output=']) { |
| 463 |
const result = childProcess.spawnSync(process.execPath, [cli, option], { |
| 464 |
input: '汉字', |
| 465 |
encoding: 'utf8', |
| 466 |
}); |
| 467 |
assert.notEqual(result.status, 0); |
| 468 |
assert.match(result.stderr, /Missing value/); |
| 469 |
} |
| 470 |
}); |
| 471 |
|
| 472 |
describe('Line ending preservation', function () { |
| 473 |
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-line-')); |
| 474 |
|
| 475 |
function hasCRLF(filePath) { |
| 476 |
const content = fs.readFileSync(filePath, 'utf8'); |
| 477 |
|
| 478 |
return /\r\n/.test(content); |
| 479 |
} |
| 480 |
|
| 481 |
function hasLFOnly(filePath) { |
| 482 |
const content = fs.readFileSync(filePath, 'utf8'); |
| 483 |
|
| 484 |
return /\n/.test(content) && !/\r/.test(content); |
| 485 |
} |
| 486 |
|
| 487 |
after(function () { |
| 488 |
|
| 489 |
try { |
| 490 |
const entries = fs.readdirSync(tempDir); |
| 491 |
for (const entry of entries) { |
| 492 |
fs.rmSync(path.join(tempDir, entry), { recursive: true, force: true }); |
| 493 |
} |
| 494 |
fs.rmdirSync(tempDir); |
| 495 |
} catch (e) { |
| 496 |
|
| 497 |
} |
| 498 |
}); |
| 499 |
|
| 500 |
it('preserves LF line ending', function () { |
| 501 |
const input = path.join(tempDir, 'input_lf.txt'); |
| 502 |
const output = path.join(tempDir, 'output_lf.txt'); |
| 503 |
fs.writeFileSync(input, '第一行\n第二行\n', 'utf8'); |
| 504 |
|
| 505 |
const result = childProcess.spawnSync(process.execPath, [ |
| 506 |
cli, |
| 507 |
'-c', 's2t.json', |
| 508 |
'--input', input, |
| 509 |
'--output', output, |
| 510 |
], { |
| 511 |
encoding: 'utf8', |
| 512 |
}); |
| 513 |
|
| 514 |
assert.equal(result.status, 0, result.stderr); |
| 515 |
assert(hasLFOnly(output), 'LF line ending should be preserved'); |
| 516 |
}); |
| 517 |
|
| 518 |
it('preserves CRLF line ending', function () { |
| 519 |
const input = path.join(tempDir, 'input_crlf.txt'); |
| 520 |
const output = path.join(tempDir, 'output_crlf.txt'); |
| 521 |
fs.writeFileSync(input, '第一行\r\n第二行\r\n', 'utf8'); |
| 522 |
|
| 523 |
const result = childProcess.spawnSync(process.execPath, [ |
| 524 |
cli, |
| 525 |
'-c', 's2t.json', |
| 526 |
'--input', input, |
| 527 |
'--output', output, |
| 528 |
], { |
| 529 |
encoding: 'utf8', |
| 530 |
}); |
| 531 |
|
| 532 |
assert.equal(result.status, 0, result.stderr); |
| 533 |
assert(hasCRLF(output), 'CRLF line ending should be preserved'); |
| 534 |
}); |
| 535 |
|
| 536 |
it('preserves CRLF with ASCII content', function () { |
| 537 |
const input = path.join(tempDir, 'input_ascii_crlf.txt'); |
| 538 |
const output = path.join(tempDir, 'output_ascii_crlf.txt'); |
| 539 |
fs.writeFileSync(input, 'hello\r\nworld\r\n', 'utf8'); |
| 540 |
|
| 541 |
const result = childProcess.spawnSync(process.execPath, [ |
| 542 |
cli, |
| 543 |
'-c', 's2t.json', |
| 544 |
'--input', input, |
| 545 |
'--output', output, |
| 546 |
], { |
| 547 |
encoding: 'utf8', |
| 548 |
}); |
| 549 |
|
| 550 |
assert.equal(result.status, 0, result.stderr); |
| 551 |
assert(hasCRLF(output), 'CRLF line ending should be preserved for ASCII'); |
| 552 |
}); |
| 553 |
|
| 554 |
it('preserves LF when no trailing newline', function () { |
| 555 |
const input = path.join(tempDir, 'input_no_trailing.txt'); |
| 556 |
const output = path.join(tempDir, 'output_no_trailing.txt'); |
| 557 |
fs.writeFileSync(input, '第一行\n第二行', 'utf8'); |
| 558 |
|
| 559 |
const result = childProcess.spawnSync(process.execPath, [ |
| 560 |
cli, |
| 561 |
'-c', 's2t.json', |
| 562 |
'--input', input, |
| 563 |
'--output', output, |
| 564 |
], { |
| 565 |
encoding: 'utf8', |
| 566 |
}); |
| 567 |
|
| 568 |
assert.equal(result.status, 0, result.stderr); |
| 569 |
|
| 570 |
assert(!/\r/.test(fs.readFileSync(output, 'utf8')), 'No CR in output'); |
| 571 |
}); |
| 572 |
}); |
| 573 |
}); |
| 574 |
|
| 575 |
describe('Optional opencc-jieba package integration', function () { |
| 576 |
it('loads jieba configs by mode name in the JavaScript API', function (t) { |
| 577 |
const installRoot = createLocalInstalledShape(); |
| 578 |
if (!installRoot) { |
| 579 |
t.skip(); |
| 580 |
return; |
| 581 |
} |
| 582 |
|
| 583 |
const script = [ |
| 584 |
"const OpenCC = require('opencc');", |
| 585 |
"const converter = new OpenCC('s2twp_jieba');", |
| 586 |
"process.stdout.write(converter.convertSync('云计算'));", |
| 587 |
].join(''); |
| 588 |
const result = childProcess.spawnSync(process.execPath, ['-e', script], { |
| 589 |
cwd: installRoot, |
| 590 |
env: { ...process.env }, |
| 591 |
encoding: 'utf8', |
| 592 |
}); |
| 593 |
assert.equal(result.status, 0, result.stderr); |
| 594 |
assert.equal(result.stdout, '雲端計算'); |
| 595 |
}); |
| 596 |
|
| 597 |
it('resolves normalization dict paths in jieba configs', function (t) { |
| 598 |
const installRoot = createLocalInstalledShape(); |
| 599 |
if (!installRoot) { |
| 600 |
t.skip(); |
| 601 |
return; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
const script = [ |
| 608 |
"const OpenCC = require('opencc');", |
| 609 |
"const converter = new OpenCC('s2twp_jieba');", |
| 610 |
"const result = converter.convertSync('豈');", |
| 611 |
"process.stdout.write(result.codePointAt(0).toString(16));", |
| 612 |
].join(''); |
| 613 |
const result = childProcess.spawnSync(process.execPath, ['-e', script], { |
| 614 |
cwd: installRoot, |
| 615 |
env: { ...process.env }, |
| 616 |
encoding: 'utf8', |
| 617 |
}); |
| 618 |
assert.equal(result.status, 0, result.stderr); |
| 619 |
assert.equal(result.stdout, '8c48'); |
| 620 |
}); |
| 621 |
|
| 622 |
it('loads jieba configs by mode name in the npm CLI', function (t) { |
| 623 |
const installRoot = createLocalInstalledShape(); |
| 624 |
if (!installRoot) { |
| 625 |
t.skip(); |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
const result = childProcess.spawnSync(process.execPath, [ |
| 630 |
path.join(installRoot, 'node_modules', 'opencc', 'node', 'cli.js'), |
| 631 |
'-c', |
| 632 |
's2twp_jieba', |
| 633 |
], { |
| 634 |
cwd: installRoot, |
| 635 |
env: { ...process.env }, |
| 636 |
input: '云计算', |
| 637 |
encoding: 'utf8', |
| 638 |
}); |
| 639 |
assert.equal(result.status, 0, result.stderr); |
| 640 |
assert.equal(result.stdout, '雲端計算'); |
| 641 |
}); |
| 642 |
|
| 643 |
it('skips tofu-risk dictionaries in jieba configs by default in the npm CLI', function (t) { |
| 644 |
const installRoot = createLocalInstalledShape(); |
| 645 |
if (!installRoot) { |
| 646 |
t.skip(); |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
const result = childProcess.spawnSync(process.execPath, [ |
| 651 |
path.join(installRoot, 'node_modules', 'opencc', 'node', 'cli.js'), |
| 652 |
'-c', |
| 653 |
'tw2sp_jieba', |
| 654 |
], { |
| 655 |
cwd: installRoot, |
| 656 |
env: { ...process.env }, |
| 657 |
input: '㑮', |
| 658 |
encoding: 'utf8', |
| 659 |
}); |
| 660 |
assert.equal(result.status, 0, result.stderr); |
| 661 |
assert.equal(result.stdout, '㑮'); |
| 662 |
}); |
| 663 |
|
| 664 |
it('includes tofu-risk dictionaries in jieba configs when requested in the npm CLI', function (t) { |
| 665 |
const installRoot = createLocalInstalledShape(); |
| 666 |
if (!installRoot) { |
| 667 |
t.skip(); |
| 668 |
return; |
| 669 |
} |
| 670 |
|
| 671 |
const result = childProcess.spawnSync(process.execPath, [ |
| 672 |
path.join(installRoot, 'node_modules', 'opencc', 'node', 'cli.js'), |
| 673 |
'-c', |
| 674 |
'tw2sp_jieba', |
| 675 |
'--include-tofu-risk-dictionaries', |
| 676 |
], { |
| 677 |
cwd: installRoot, |
| 678 |
env: { ...process.env }, |
| 679 |
input: '㑮', |
| 680 |
encoding: 'utf8', |
| 681 |
}); |
| 682 |
assert.equal(result.status, 0, result.stderr); |
| 683 |
assert.equal(result.stdout, '𫝈'); |
| 684 |
}); |
| 685 |
}); |
| 686 |
|
| 687 |
describe('Node prebuild assets', function () { |
| 688 |
it('collects only runtime json and ocd2 assets', function () { |
| 689 |
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'opencc-prebuild-assets-')); |
| 690 |
const releaseDir = path.join(root, 'build', 'Release'); |
| 691 |
const assetsDir = path.join(root, 'prebuilds', 'assets'); |
| 692 |
|
| 693 |
fs.mkdirSync(releaseDir, { recursive: true }); |
| 694 |
fs.mkdirSync(assetsDir, { recursive: true }); |
| 695 |
fs.writeFileSync(path.join(releaseDir, 's2t.json'), '{}'); |
| 696 |
fs.writeFileSync(path.join(releaseDir, 'STCharacters.ocd2'), 'dict'); |
| 697 |
fs.writeFileSync(path.join(releaseDir, 'STCharacters.txt'), 'source'); |
| 698 |
fs.writeFileSync(path.join(releaseDir, 'README.md'), 'docs'); |
| 699 |
fs.writeFileSync(path.join(assetsDir, 'stale.txt'), 'stale'); |
| 700 |
|
| 701 |
prepareArtifacts(root); |
| 702 |
|
| 703 |
assert.deepEqual(fs.readdirSync(assetsDir).sort(), [ |
| 704 |
'STCharacters.ocd2', |
| 705 |
's2t.json', |
| 706 |
]); |
| 707 |
}); |
| 708 |
}); |