@@ -17,7 +17,8 @@ import {
1717 installSherpaRuntime ,
1818 SHERPA_RUNTIME_DIR ,
1919 sherpaPlatformPackage ,
20- sherpaRuntimeLoads ,
20+ DecodeCancelled ,
21+ sherpaRuntimeLoadError ,
2122 sherpaUnsupportedReason ,
2223 type ModelFile ,
2324} from "./sherpa.js" ;
@@ -66,28 +67,27 @@ describe("installSherpaRuntime", () => {
6667 } ) ;
6768
6869 /** A stand-in runtime in `root` whose entry point loads, or throws as a missing library does. */
69- function fakeRuntime ( root : string , loads : boolean ) {
70+ const LOADS = "module.exports = {};" ;
71+ const BROKEN = 'throw new Error("libonnxruntime.so: cannot open\\n shared object");' ;
72+ function fakeRuntime ( root : string , body : string ) {
7073 const pkg = join ( root , "node_modules" , "sherpa-onnx-node" ) ;
7174 mkdirSync ( pkg , { recursive : true } ) ;
7275 writeFileSync ( join ( pkg , "package.json" ) , '{"name":"sherpa-onnx-node","main":"index.js"}' ) ;
73- const body = loads
74- ? "module.exports = {};"
75- : 'throw new Error("libonnxruntime.so: cannot open");' ;
7676 writeFileSync ( join ( pkg , "index.js" ) , body ) ;
7777 }
7878
7979 it ( "reinstalls a runtime that is present but does not load" , async ( ) => {
8080 const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
8181 try {
82- fakeRuntime ( dir , false ) ;
83- expect ( sherpaRuntimeLoads ( dir ) ) . toBe ( false ) ;
82+ fakeRuntime ( dir , BROKEN ) ;
83+ expect ( sherpaRuntimeLoadError ( dir ) ) . toBe ( "libonnxruntime.so: cannot open shared object" ) ;
8484 const run = vi . fn ( async ( args : string [ ] ) =>
85- fakeRuntime ( args [ args . indexOf ( "--prefix" ) + 1 ] ! , true ) ,
85+ fakeRuntime ( args [ args . indexOf ( "--prefix" ) + 1 ] ! , LOADS ) ,
8686 ) ;
8787
8888 expect ( await installSherpaRuntime ( { run, dir } ) ) . toBe ( true ) ;
8989 expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
90- expect ( sherpaRuntimeLoads ( dir ) ) . toBe ( true ) ;
90+ expect ( sherpaRuntimeLoadError ( dir ) ) . toBeNull ( ) ;
9191 } finally {
9292 rmSync ( dir , { recursive : true , force : true } ) ;
9393 }
@@ -96,7 +96,7 @@ describe("installSherpaRuntime", () => {
9696 it ( "leaves a runtime that loads alone" , async ( ) => {
9797 const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
9898 try {
99- fakeRuntime ( dir , true ) ;
99+ fakeRuntime ( dir , LOADS ) ;
100100 const run = vi . fn ( ) ;
101101 expect ( await installSherpaRuntime ( { run, dir } ) ) . toBe ( false ) ;
102102 expect ( run ) . not . toHaveBeenCalled ( ) ;
@@ -105,6 +105,36 @@ describe("installSherpaRuntime", () => {
105105 }
106106 } ) ;
107107
108+ it ( "fails with the loader's error when the reinstalled runtime still does not load" , async ( ) => {
109+ const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
110+ try {
111+ const run = vi . fn ( async ( args : string [ ] ) =>
112+ fakeRuntime ( args [ args . indexOf ( "--prefix" ) + 1 ] ! , BROKEN ) ,
113+ ) ;
114+ await expect ( installSherpaRuntime ( { run, dir } ) ) . rejects . toThrow (
115+ "The sherpa-onnx runtime installed but does not load: libonnxruntime.so: cannot open shared object" ,
116+ ) ;
117+ } finally {
118+ rmSync ( dir , { recursive : true , force : true } ) ;
119+ }
120+ } ) ;
121+
122+ it . skipIf ( process . platform === "win32" ) (
123+ "treats a probe stopped by Ctrl-C as a cancel and keeps the runtime" ,
124+ async ( ) => {
125+ const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
126+ try {
127+ fakeRuntime ( dir , 'process.kill(process.pid, "SIGINT");' ) ;
128+ const run = vi . fn ( ) ;
129+ await expect ( installSherpaRuntime ( { run, dir } ) ) . rejects . toBeInstanceOf ( DecodeCancelled ) ;
130+ expect ( run ) . not . toHaveBeenCalled ( ) ;
131+ expect ( existsSync ( join ( dir , "node_modules" , "sherpa-onnx-node" , "index.js" ) ) ) . toBe ( true ) ;
132+ } finally {
133+ rmSync ( dir , { recursive : true , force : true } ) ;
134+ }
135+ } ,
136+ ) ;
137+
108138 it ( "keeps each platform and arch in its own runtime dir" , ( ) => {
109139 expect ( SHERPA_RUNTIME_DIR ) . toMatch (
110140 new RegExp ( `sherpa-onnx-node@1\\.13\\.8-${ process . platform } -${ process . arch } $` ) ,
@@ -132,7 +162,7 @@ describe("sherpaParakeetInstalled", () => {
132162 writeFileSync ( join ( sherpa . PARAKEET_MODEL_DIR , name ) , "" ) ;
133163 truncateSync ( join ( sherpa . PARAKEET_MODEL_DIR , name ) , bytes ) ;
134164 }
135- expect ( sherpa . sherpaRuntimeLoads ( ) ) . toBe ( false ) ;
165+ expect ( sherpa . sherpaRuntimeLoadError ( ) ) . not . toBeNull ( ) ;
136166 expect ( sherpa . sherpaParakeetInstalled ( ) ) . toBe ( true ) ;
137167 } finally {
138168 vi . unstubAllEnvs ( ) ;
0 commit comments