@@ -11,6 +11,29 @@ const { prepareArtifacts } = require('../scripts/prepare-node-prebuild-artifacts
1111
1212const cases = JSON . parse ( fs . readFileSync ( 'test/testcases/testcases.json' , 'utf-8' ) ) . cases || [ ] ;
1313
14+ function createLocalInstalledShape ( ) {
15+ const rootDir = path . resolve ( __dirname , '..' ) ;
16+ const jiebaPackageDir = path . join ( rootDir , 'plugins' , 'jieba' , 'node' ) ;
17+ const libName = os . platform ( ) === 'win32' ? 'opencc-jieba.dll'
18+ : os . platform ( ) === 'darwin' ? 'libopencc-jieba.dylib'
19+ : 'libopencc-jieba.so' ;
20+ const requiredFiles = [
21+ path . join ( jiebaPackageDir , 'index.js' ) ,
22+ path . join ( jiebaPackageDir , 'data' , 's2twp_jieba.json' ) ,
23+ path . join ( jiebaPackageDir , 'data' , 'jieba_dict' , 'jieba_merged.ocd2' ) ,
24+ path . join ( jiebaPackageDir , 'prebuilds' , `${ os . platform ( ) } -${ os . arch ( ) } ` , libName ) ,
25+ ] ;
26+ if ( ! requiredFiles . every ( ( file ) => fs . existsSync ( file ) ) ) {
27+ return null ;
28+ }
29+ const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'opencc-node-jieba-' ) ) ;
30+ const nodeModulesDir = path . join ( root , 'node_modules' ) ;
31+ fs . mkdirSync ( nodeModulesDir , { recursive : true } ) ;
32+ fs . symlinkSync ( rootDir , path . join ( nodeModulesDir , 'opencc' ) , 'dir' ) ;
33+ fs . symlinkSync ( jiebaPackageDir , path . join ( nodeModulesDir , 'opencc-jieba' ) , 'dir' ) ;
34+ return root ;
35+ }
36+
1437const testSync = function ( tc , cfg , expected , done ) {
1538 const opencc = new OpenCC ( cfg + '.json' ) ;
1639 const converted = opencc . convertSync ( tc . input ) ;
@@ -157,7 +180,6 @@ describe('npm CLI', function () {
157180 assert . match ( result . stdout , / U n s u p p o r t e d i n t h e n p m C L I : / ) ;
158181 assert . match ( result . stdout , / - - i n s p e c t / ) ;
159182 assert . match ( result . stdout , / - - s e g m e n t a t i o n / ) ;
160- assert . match ( result . stdout , / p l u g i n s / ) ;
161183 } ) ;
162184
163185 it ( 'prints version' , function ( ) {
@@ -194,6 +216,44 @@ describe('npm CLI', function () {
194216 } ) ;
195217} ) ;
196218
219+ describe ( 'Optional opencc-jieba package integration' , function ( ) {
220+ it ( 'loads jieba configs by mode name in the JavaScript API' , function ( ) {
221+ const installRoot = createLocalInstalledShape ( ) ;
222+ if ( ! installRoot ) this . skip ( ) ;
223+
224+ const script = [
225+ "const OpenCC = require('opencc');" ,
226+ "const converter = new OpenCC('s2twp_jieba');" ,
227+ "process.stdout.write(converter.convertSync('云计算'));" ,
228+ ] . join ( '' ) ;
229+ const result = childProcess . spawnSync ( process . execPath , [ '-e' , script ] , {
230+ cwd : installRoot ,
231+ env : { ...process . env } ,
232+ encoding : 'utf8' ,
233+ } ) ;
234+ assert . equal ( result . status , 0 , result . stderr ) ;
235+ assert . equal ( result . stdout , '雲端計算' ) ;
236+ } ) ;
237+
238+ it ( 'loads jieba configs by mode name in the npm CLI' , function ( ) {
239+ const installRoot = createLocalInstalledShape ( ) ;
240+ if ( ! installRoot ) this . skip ( ) ;
241+
242+ const result = childProcess . spawnSync ( process . execPath , [
243+ path . join ( installRoot , 'node_modules' , 'opencc' , 'node' , 'cli.js' ) ,
244+ '-c' ,
245+ 's2twp_jieba' ,
246+ ] , {
247+ cwd : installRoot ,
248+ env : { ...process . env } ,
249+ input : '云计算' ,
250+ encoding : 'utf8' ,
251+ } ) ;
252+ assert . equal ( result . status , 0 , result . stderr ) ;
253+ assert . equal ( result . stdout , '雲端計算' ) ;
254+ } ) ;
255+ } ) ;
256+
197257describe ( 'Node prebuild assets' , function ( ) {
198258 it ( 'collects only runtime json and ocd2 assets' , function ( ) {
199259 const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'opencc-prebuild-assets-' ) ) ;
0 commit comments