@@ -404,16 +404,77 @@ describe('update-check', () => {
404404 it ( 'should detect source when running from git repo' , ( ) => {
405405 delete process . env . npm_command ;
406406 delete process . env . npm_execpath ;
407- const { detectInstallMethod } = require ( '../../src/utils/update-check' ) ;
408- const result = detectInstallMethod ( ) ;
409- // Running tests from the repo — .git exists and not in node_modules.
410- // In Docker/CI containers, may report 'docker' instead — both are non-auto-updatable.
407+ // Clear PM2 env vars to test the non-PM2 source path
408+ const origPm2Home = process . env . PM2_HOME ;
409+ const origPmId = process . env . pm_id ;
410+ const origPm2Usage = process . env . PM2_USAGE ;
411+ delete process . env . PM2_HOME ;
412+ delete process . env . pm_id ;
413+ delete process . env . PM2_USAGE ;
414+ delete require . cache [ require . resolve ( '../../src/utils/update-check' ) ] ;
415+ try {
416+ const { detectInstallMethod } = require ( '../../src/utils/update-check' ) ;
417+ const result = detectInstallMethod ( ) ;
418+ // Running tests from the repo — .git exists and not in node_modules.
419+ // In Docker/CI containers, may report 'docker' instead — both are non-auto-updatable.
420+ assert . ok (
421+ [ 'source' , 'docker' ] . includes ( result . method ) ,
422+ `expected 'source' or 'docker', got '${ result . method } '` ,
423+ ) ;
424+ assert . equal ( result . canAutoUpdate , false ) ;
425+ assert . equal ( result . restartStrategy , 'none' ) ;
426+ if ( result . method === 'source' ) {
427+ assert . ok ( result . cwd , 'source method should include cwd' ) ;
428+ }
429+ } finally {
430+ if ( origPm2Home !== undefined ) process . env . PM2_HOME = origPm2Home ;
431+ else delete process . env . PM2_HOME ;
432+ if ( origPmId !== undefined ) process . env . pm_id = origPmId ;
433+ else delete process . env . pm_id ;
434+ if ( origPm2Usage !== undefined ) process . env . PM2_USAGE = origPm2Usage ;
435+ else delete process . env . PM2_USAGE ;
436+ }
437+ } ) ;
438+
439+ it ( 'should enable auto-update for source under PM2' , ( ) => {
440+ delete process . env . npm_command ;
441+ delete process . env . npm_execpath ;
442+ const origPm2Home = process . env . PM2_HOME ;
443+ const origPmId = process . env . pm_id ;
444+ process . env . PM2_HOME = '/home/user/.pm2' ;
445+ process . env . pm_id = '0' ;
446+ delete require . cache [ require . resolve ( '../../src/utils/update-check' ) ] ;
447+ try {
448+ const {
449+ detectInstallMethod,
450+ isRunningFromSource,
451+ } = require ( '../../src/utils/update-check' ) ;
452+ if ( ! isRunningFromSource ( ) ) return ; // Skip in Docker/CI
453+ const result = detectInstallMethod ( ) ;
454+ assert . equal ( result . method , 'source' ) ;
455+ assert . equal ( result . canAutoUpdate , true ) ;
456+ assert . equal ( result . restartStrategy , 'pm2' ) ;
457+ assert . ok ( result . command . includes ( 'pm2 restart' ) , 'command should include pm2 restart' ) ;
458+ assert . ok ( result . installCmd , 'should have installCmd for auto-update' ) ;
459+ assert . ok ( result . installArgs , 'should have installArgs for auto-update' ) ;
460+ assert . ok ( result . cwd , 'should include cwd for source install' ) ;
461+ } finally {
462+ if ( origPm2Home !== undefined ) process . env . PM2_HOME = origPm2Home ;
463+ else delete process . env . PM2_HOME ;
464+ if ( origPmId !== undefined ) process . env . pm_id = origPmId ;
465+ else delete process . env . pm_id ;
466+ }
467+ } ) ;
468+
469+ it ( 'getSourceRoot should return repo root directory' , ( ) => {
470+ const { getSourceRoot } = require ( '../../src/utils/update-check' ) ;
471+ const root = getSourceRoot ( ) ;
472+ assert . ok ( root , 'should find source root' ) ;
473+ assert . ok ( fs . existsSync ( path . join ( root , '.git' ) ) , 'source root should contain .git' ) ;
411474 assert . ok (
412- [ 'source' , 'docker' ] . includes ( result . method ) ,
413- `expected 'source' or 'docker', got ' ${ result . method } '` ,
475+ fs . existsSync ( path . join ( root , 'package.json' ) ) ,
476+ 'source root should contain package.json' ,
414477 ) ;
415- assert . equal ( result . canAutoUpdate , false ) ;
416- assert . equal ( result . restartStrategy , 'none' ) ;
417478 } ) ;
418479
419480 it ( 'should return canAutoUpdate and restartStrategy fields' , ( ) => {
0 commit comments