@@ -17,7 +17,7 @@ import {
1717 installSherpaRuntime ,
1818 SHERPA_RUNTIME_DIR ,
1919 sherpaPlatformPackage ,
20- sherpaRuntimeInstalled ,
20+ sherpaRuntimeLoads ,
2121 sherpaUnsupportedReason ,
2222 type ModelFile ,
2323} from "./sherpa.js" ;
@@ -56,35 +56,50 @@ describe("installSherpaRuntime", () => {
5656 throw new Error ( "stop before touching the cache" ) ;
5757 } ) ;
5858 const cancel = new AbortController ( ) ;
59- await installSherpaRuntime ( { run, signal : cancel . signal } ) . catch ( ( ) => undefined ) ;
59+ const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
60+ await installSherpaRuntime ( { run, signal : cancel . signal , dir } ) . catch ( ( ) => undefined ) ;
61+ rmSync ( dir , { recursive : true , force : true } ) ;
6062 expect ( ( run . mock . calls [ 0 ] as unknown [ ] | undefined ) ?. [ 1 ] ) . toBe ( cancel . signal ) ;
6163 const args = run . mock . calls [ 0 ] ?. [ 0 ] as string [ ] | undefined ;
6264 expect ( args ) . toContain ( "[email protected] " ) ; 6365 expect ( args ) . toContain ( sherpaPlatformPackage ( ) ) ;
6466 } ) ;
6567
66- it ( "repairs a runtime whose native binary is missing instead of calling it installed" , async ( ) => {
68+ /** 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 pkg = join ( root , "node_modules" , "sherpa-onnx-node" ) ;
71+ mkdirSync ( pkg , { recursive : true } ) ;
72+ 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");' ;
76+ writeFileSync ( join ( pkg , "index.js" ) , body ) ;
77+ }
78+
79+ it ( "reinstalls a runtime that is present but does not load" , async ( ) => {
6780 const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
68- const native = sherpaPlatformPackage ( ) . replace ( / @ [ ^ @ ] + $ / , "" ) ;
69- const manifest = ( root : string , name : string ) => {
70- mkdirSync ( join ( root , "node_modules" , name ) , { recursive : true } ) ;
71- writeFileSync ( join ( root , "node_modules" , name , "package.json" ) , `{"name":"${ name } "}` ) ;
72- } ;
7381 try {
74- manifest ( dir , "sherpa-onnx-node" ) ;
75- manifest ( dir , native ) ;
76- expect ( sherpaRuntimeInstalled ( dir ) ) . toBe ( false ) ;
77- const run = vi . fn ( async ( args : string [ ] ) => {
78- const staging = args [ args . indexOf ( "--prefix" ) + 1 ] ! ;
79- manifest ( staging , "sherpa-onnx-node" ) ;
80- manifest ( staging , native ) ;
81- writeFileSync ( join ( staging , "node_modules" , native , "sherpa-onnx.node" ) , "binary" ) ;
82- } ) ;
83-
84- await installSherpaRuntime ( { run, dir } ) ;
82+ fakeRuntime ( dir , false ) ;
83+ expect ( sherpaRuntimeLoads ( dir ) ) . toBe ( false ) ;
84+ const run = vi . fn ( async ( args : string [ ] ) =>
85+ fakeRuntime ( args [ args . indexOf ( "--prefix" ) + 1 ] ! , true ) ,
86+ ) ;
87+
88+ expect ( await installSherpaRuntime ( { run, dir } ) ) . toBe ( true ) ;
8589 expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
86- expect ( existsSync ( join ( dir , "node_modules" , native , "sherpa-onnx.node" ) ) ) . toBe ( true ) ;
87- expect ( sherpaRuntimeInstalled ( dir ) ) . toBe ( true ) ;
90+ expect ( sherpaRuntimeLoads ( dir ) ) . toBe ( true ) ;
91+ } finally {
92+ rmSync ( dir , { recursive : true , force : true } ) ;
93+ }
94+ } ) ;
95+
96+ it ( "leaves a runtime that loads alone" , async ( ) => {
97+ const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-runtime-" ) ) ;
98+ try {
99+ fakeRuntime ( dir , true ) ;
100+ const run = vi . fn ( ) ;
101+ expect ( await installSherpaRuntime ( { run, dir } ) ) . toBe ( false ) ;
102+ expect ( run ) . not . toHaveBeenCalled ( ) ;
88103 } finally {
89104 rmSync ( dir , { recursive : true , force : true } ) ;
90105 }
@@ -98,7 +113,7 @@ describe("installSherpaRuntime", () => {
98113} ) ;
99114
100115describe ( "sherpaParakeetInstalled" , ( ) => {
101- it ( "counts a runtime missing its native binary , so transcribe reports it instead of skipping it" , async ( ) => {
116+ it ( "counts a runtime that does not load , so transcribe reports it instead of skipping it" , async ( ) => {
102117 const home = mkdtempSync ( join ( tmpdir ( ) , "hf-sherpa-home-" ) ) ;
103118 vi . stubEnv ( "HOME" , home ) ;
104119 vi . resetModules ( ) ;
@@ -117,7 +132,7 @@ describe("sherpaParakeetInstalled", () => {
117132 writeFileSync ( join ( sherpa . PARAKEET_MODEL_DIR , name ) , "" ) ;
118133 truncateSync ( join ( sherpa . PARAKEET_MODEL_DIR , name ) , bytes ) ;
119134 }
120- expect ( sherpa . sherpaRuntimeInstalled ( ) ) . toBe ( false ) ;
135+ expect ( sherpa . sherpaRuntimeLoads ( ) ) . toBe ( false ) ;
121136 expect ( sherpa . sherpaParakeetInstalled ( ) ) . toBe ( true ) ;
122137 } finally {
123138 vi . unstubAllEnvs ( ) ;
0 commit comments