@@ -407,7 +407,7 @@ describe("GUI update execution decisions", () => {
407407 ) ) . toBe ( true ) ;
408408 } ) ;
409409
410- test ( "npm finish does not skip when a healthy proxy is still the pre-update PID " , async ( ) => {
410+ test ( "npm finish fails when stale PID survives a no-op explicit restart " , async ( ) => {
411411 let now = 0 ;
412412 let restartCalls = 0 ;
413413 const job : UpdateJobState = {
@@ -431,7 +431,7 @@ describe("GUI update execution decisions", () => {
431431 "npm" ,
432432 {
433433 serviceInstalledFn : ( ) => true ,
434- // Soft probe stays healthy (old process). Explicit restart then confirms .
434+ // Soft probe stays healthy (old process). Explicit restart is a no-op .
435435 probeProxy : async ( ) => true ,
436436 probeProxyIdentity : async ( ) => ( { pid : 111 , version : "2.7.40" } ) ,
437437 now : ( ) => now ,
@@ -442,13 +442,109 @@ describe("GUI update execution decisions", () => {
442442 } ,
443443 } ,
444444 ) ;
445- expect ( ok ) . toBe ( true ) ;
445+ expect ( ok ) . toBe ( false ) ;
446446 expect ( restartCalls ) . toBe ( 1 ) ;
447+ expect ( readUpdateJob ( job . id ) ) . toMatchObject ( {
448+ status : "failed" ,
449+ restarted : false ,
450+ } ) ;
451+ expect ( readUpdateJob ( job . id ) ?. error ) . toContain ( "still the pre-update PID" ) ;
447452 expect ( readUpdateJob ( job . id ) ?. log . some ( line =>
448453 line . includes ( "still the pre-update PID" ) && line . includes ( "performing explicit restart" ) ,
449454 ) ) . toBe ( true ) ;
450455 } ) ;
451456
457+ test ( "npm finish succeeds when explicit restart yields a new PID at the target version" , async ( ) => {
458+ let now = 0 ;
459+ let restartCalls = 0 ;
460+ let livePid = 111 ;
461+ let liveVersion = "2.7.40" ;
462+ const job : UpdateJobState = {
463+ id : "npm-explicit-replaced" ,
464+ status : "restarting" ,
465+ startedAt : new Date ( ) . toISOString ( ) ,
466+ updatedAt : new Date ( ) . toISOString ( ) ,
467+ currentVersion : "2.7.40" ,
468+ latestVersion : "2.7.41" ,
469+ channel : "latest" ,
470+ installer : "npm" ,
471+ restart : true ,
472+ command : "" ,
473+ releaseNotesUrl : "" ,
474+ log : [ ] ,
475+ } ;
476+ writeFileSync ( updateJobPath ( job . id ) , JSON . stringify ( job ) ) ;
477+ const ok = await finishGuiUpdateRestart (
478+ job ,
479+ { port : 10100 , hostname : "127.0.0.1" , oldPid : 111 } ,
480+ "npm" ,
481+ {
482+ serviceInstalledFn : ( ) => true ,
483+ probeProxy : async ( ) => true ,
484+ probeProxyIdentity : async ( ) => ( { pid : livePid , version : liveVersion } ) ,
485+ now : ( ) => now ,
486+ sleepMs : async ( ms ) => { now += ms ; } ,
487+ restartAfterUpdateFn : async ( ) => {
488+ restartCalls += 1 ;
489+ livePid = 222 ;
490+ liveVersion = "2.7.41" ;
491+ now = 0 ;
492+ } ,
493+ } ,
494+ ) ;
495+ expect ( ok ) . toBe ( true ) ;
496+ expect ( restartCalls ) . toBe ( 1 ) ;
497+ expect ( readUpdateJob ( job . id ) ?. status ) . not . toBe ( "failed" ) ;
498+ expect ( readUpdateJob ( job . id ) ?. log . some ( line =>
499+ line . includes ( "Proxy restart confirmed" ) && line . includes ( "pid changed" ) ,
500+ ) ) . toBe ( true ) ;
501+ } ) ;
502+
503+ test ( "npm finish fails when port reclaim leaves the pre-update proxy healthy" , async ( ) => {
504+ let now = 0 ;
505+ const job : UpdateJobState = {
506+ id : "npm-reclaim-stale" ,
507+ status : "restarting" ,
508+ startedAt : new Date ( ) . toISOString ( ) ,
509+ updatedAt : new Date ( ) . toISOString ( ) ,
510+ currentVersion : "2.7.40" ,
511+ latestVersion : "2.7.41" ,
512+ channel : "latest" ,
513+ installer : "npm" ,
514+ restart : true ,
515+ command : "" ,
516+ releaseNotesUrl : "" ,
517+ log : [ ] ,
518+ } ;
519+ writeFileSync ( updateJobPath ( job . id ) , JSON . stringify ( job ) ) ;
520+ const ok = await finishGuiUpdateRestart (
521+ job ,
522+ { port : 10100 , hostname : "127.0.0.1" , oldPid : 111 } ,
523+ "npm" ,
524+ {
525+ // Direct path: reclaim failure returns without spawning a replacement.
526+ serviceInstalledFn : ( ) => false ,
527+ waitForPort : async ( ) => false ,
528+ spawnStart : ( ) => {
529+ throw new Error ( "must not spawn when reclaim failed" ) ;
530+ } ,
531+ probeProxy : async ( ) => true ,
532+ probeProxyIdentity : async ( ) => ( { pid : 111 , version : "2.7.40" } ) ,
533+ now : ( ) => now ,
534+ sleepMs : async ( ms ) => { now += ms ; } ,
535+ } ,
536+ ) ;
537+ expect ( ok ) . toBe ( false ) ;
538+ expect ( readUpdateJob ( job . id ) ) . toMatchObject ( {
539+ status : "failed" ,
540+ restarted : false ,
541+ } ) ;
542+ expect ( readUpdateJob ( job . id ) ?. error ) . toContain ( "still the pre-update PID" ) ;
543+ expect ( readUpdateJob ( job . id ) ?. log . some ( line =>
544+ line . includes ( "still busy" ) && line . includes ( "not starting on another port" ) ,
545+ ) ) . toBe ( true ) ;
546+ } ) ;
547+
452548 test ( "npm finish skips the soft probe for direct installs and restarts immediately" , async ( ) => {
453549 let now = 0 ;
454550 let restartCalls = 0 ;
@@ -474,6 +570,9 @@ describe("GUI update execution decisions", () => {
474570 // Only becomes healthy after the explicit restart (launcher printed `ocx start` only).
475571 return restartCalls > 0 ;
476572 } ,
573+ probeProxyIdentity : async ( ) => (
574+ restartCalls > 0 ? { pid : 333 , version : "2.7.41" } : null
575+ ) ,
477576 now : ( ) => now ,
478577 sleepMs : async ( ms ) => { now += ms ; } ,
479578 restartAfterUpdateFn : async ( ) => {
@@ -512,6 +611,9 @@ describe("GUI update execution decisions", () => {
512611 serviceInstalledFn : ( ) => true ,
513612 // Soft probe times out (proxy down after npm update); confirm after explicit restart succeeds.
514613 probeProxy : async ( ) => restartCalls > 0 ,
614+ probeProxyIdentity : async ( ) => (
615+ restartCalls > 0 ? { pid : 444 , version : "2.7.41" } : null
616+ ) ,
515617 now : ( ) => now ,
516618 sleepMs : async ( ms ) => { now += ms ; } ,
517619 restartAfterUpdateFn : async ( ) => {
0 commit comments