@@ -34,12 +34,49 @@ describe('loadTransformers install prompt', () => {
3434 vi . restoreAllMocks ( ) ;
3535 } ) ;
3636
37- test ( 'non-TTY: throws EngineError without prompting' , async ( ) => {
37+ test ( 'non-TTY: auto-installs without prompting' , async ( ) => {
3838 process . stdin . isTTY = undefined ;
3939
40+ let importCount = 0 ;
4041 const rlFactory = vi . fn ( ) ;
42+ const execMock = vi . fn ( ) ;
4143 vi . doMock ( 'node:readline' , ( ) => ( { createInterface : rlFactory } ) ) ;
42- vi . doMock ( 'node:child_process' , ( ) => ( { execFileSync : vi . fn ( ) } ) ) ;
44+ vi . doMock ( 'node:child_process' , ( ) => ( { execFileSync : execMock } ) ) ;
45+ vi . doMock ( '@huggingface/transformers' , ( ) => {
46+ importCount ++ ;
47+ if ( importCount <= 1 ) throw new Error ( 'Cannot find package' ) ;
48+ return {
49+ pipeline : async ( ) => async ( batch : string [ ] ) => ( {
50+ data : new Float32Array ( 384 * batch . length ) ,
51+ } ) ,
52+ cos_sim : ( ) => 0 ,
53+ } ;
54+ } ) ;
55+
56+ const { embed } = await import ( '../../src/domain/search/index.js' ) ;
57+
58+ const result = await embed ( [ 'test text' ] , 'minilm' ) ;
59+ expect ( result . vectors ) . toHaveLength ( 1 ) ;
60+ expect ( result . dim ) . toBe ( 384 ) ;
61+ // readline should NOT have been called — no prompt in non-TTY
62+ expect ( rlFactory ) . not . toHaveBeenCalled ( ) ;
63+ // npm install should have been called automatically
64+ expect ( execMock ) . toHaveBeenCalledWith (
65+ 'npm' ,
66+ [ 'install' , '--no-save' , '@huggingface/transformers' ] ,
67+ expect . objectContaining ( { stdio : 'inherit' , timeout : 300_000 } ) ,
68+ ) ;
69+ } ) ;
70+
71+ test ( 'non-TTY: throws EngineError when auto-install fails' , async ( ) => {
72+ process . stdin . isTTY = undefined ;
73+
74+ const rlFactory = vi . fn ( ) ;
75+ const execMock = vi . fn ( ( ) => {
76+ throw new Error ( 'npm ERR!' ) ;
77+ } ) ;
78+ vi . doMock ( 'node:readline' , ( ) => ( { createInterface : rlFactory } ) ) ;
79+ vi . doMock ( 'node:child_process' , ( ) => ( { execFileSync : execMock } ) ) ;
4380 vi . doMock ( '@huggingface/transformers' , ( ) => {
4481 throw new Error ( 'Cannot find package' ) ;
4582 } ) ;
@@ -55,6 +92,8 @@ describe('loadTransformers install prompt', () => {
5592 } ) ;
5693 // readline should NOT have been called — no prompt in non-TTY
5794 expect ( rlFactory ) . not . toHaveBeenCalled ( ) ;
95+ // npm install was attempted
96+ expect ( execMock ) . toHaveBeenCalled ( ) ;
5897 } ) ;
5998
6099 test ( 'TTY + user declines: throws EngineError' , async ( ) => {
0 commit comments