@@ -4,6 +4,12 @@ import { join } from "node:path";
44import { afterEach , describe , expect , it , vi } from "vitest" ;
55import { runRestartHandoff , runUpdateWorker } from "./update-worker.js" ;
66
7+ type UpdateWorkerDeps = NonNullable < Parameters < typeof runUpdateWorker > [ 1 ] > ;
8+ type RestartHandoffDeps = NonNullable < Parameters < typeof runRestartHandoff > [ 1 ] > ;
9+ type RunCommandMock = NonNullable < UpdateWorkerDeps [ "runCommand" ] > ;
10+ type SpawnDetachedProcessMock = NonNullable < UpdateWorkerDeps [ "spawnDetachedProcess" ] > ;
11+ type WaitForProcessExitMock = NonNullable < RestartHandoffDeps [ "waitForProcessExit" ] > ;
12+
713describe ( "update-worker" , ( ) => {
814 const tempDirs : string [ ] = [ ] ;
915
@@ -31,8 +37,8 @@ describe("update-worker", () => {
3137
3238 it ( "writes restarting state and spawns a detached restart handoff after install success" , async ( ) => {
3339 const env = createEnv ( ) ;
34- const runCommand = vi . fn ( async ( ) => { } ) ;
35- const spawnDetachedProcess = vi . fn ( async ( ) => { } ) ;
40+ const runCommand = vi . fn < RunCommandMock > ( async ( ) => { } ) ;
41+ const spawnDetachedProcess = vi . fn < SpawnDetachedProcessMock > ( async ( ) => { } ) ;
3642
3743 await runUpdateWorker ( env , {
3844 runCommand,
@@ -61,7 +67,7 @@ describe("update-worker", () => {
6167
6268 it ( "falls back to manual_required on permission-related install errors" , async ( ) => {
6369 const env = createEnv ( ) ;
64- const runCommand = vi . fn ( async ( ) => {
70+ const runCommand = vi . fn < RunCommandMock > ( async ( ) => {
6571 throw new Error ( "npm install failed with EACCES" ) ;
6672 } ) ;
6773
@@ -82,8 +88,10 @@ describe("update-worker", () => {
8288
8389 it ( "marks restart failures with manual restart guidance" , async ( ) => {
8490 const env = createEnv ( ) ;
85- const runCommand = vi . fn ( ) . mockRejectedValueOnce ( new Error ( "pm2 restart failed" ) ) ;
86- const waitForProcessExit = vi . fn ( async ( ) => { } ) ;
91+ const runCommand = vi
92+ . fn < RunCommandMock > ( )
93+ . mockRejectedValueOnce ( new Error ( "pm2 restart failed" ) ) ;
94+ const waitForProcessExit = vi . fn < WaitForProcessExitMock > ( async ( ) => { } ) ;
8795
8896 await runRestartHandoff ( env , {
8997 runCommand,
@@ -105,8 +113,8 @@ describe("update-worker", () => {
105113
106114 it ( "sanitizes pm2 and runtime override env before invoking install and restart commands" , async ( ) => {
107115 const env = createEnv ( ) ;
108- const runCommand = vi . fn ( async ( ) => { } ) ;
109- const spawnDetachedProcess = vi . fn ( async ( ) => { } ) ;
116+ const runCommand = vi . fn < RunCommandMock > ( async ( ) => { } ) ;
117+ const spawnDetachedProcess = vi . fn < SpawnDetachedProcessMock > ( async ( ) => { } ) ;
110118 const originalEnv = {
111119 PM2_HOME : process . env . PM2_HOME ,
112120 PM2_PROGRAMMATIC : process . env . PM2_PROGRAMMATIC ,
@@ -150,8 +158,12 @@ describe("update-worker", () => {
150158 }
151159 }
152160
153- for ( const call of runCommand . mock . calls ) {
154- const options = call [ 2 ] as { env ?: NodeJS . ProcessEnv } ;
161+ for ( const [ , , options ] of runCommand . mock . calls ) {
162+ expect ( options ) . toBeDefined ( ) ;
163+ if ( ! options ) {
164+ throw new Error ( "Expected runCommand to receive an options object" ) ;
165+ }
166+
155167 expect ( options . env ?. PM2_HOME ) . toBe ( "/tmp/custom-pm2-home" ) ;
156168 expect ( options . env ?. PM2_PROGRAMMATIC ) . toBeUndefined ( ) ;
157169 expect ( options . env ?. PM2_JSON_PROCESSING ) . toBeUndefined ( ) ;
@@ -165,7 +177,8 @@ describe("update-worker", () => {
165177 expect ( options . env ?. pm_id ) . toBeUndefined ( ) ;
166178 }
167179
168- const handoffEnv = spawnDetachedProcess . mock . calls [ 0 ] ?. [ 2 ] as NodeJS . ProcessEnv | undefined ;
180+ const handoffCall = spawnDetachedProcess . mock . calls [ 0 ] ;
181+ const handoffEnv = handoffCall ?. [ 2 ] ;
169182 expect ( handoffEnv ?. PM2_HOME ) . toBe ( "/tmp/custom-pm2-home" ) ;
170183 expect ( handoffEnv ?. PM2_PROGRAMMATIC ) . toBeUndefined ( ) ;
171184 expect ( handoffEnv ?. PM2_JSON_PROCESSING ) . toBeUndefined ( ) ;
@@ -180,8 +193,8 @@ describe("update-worker", () => {
180193
181194 it ( "waits for the install worker to exit before running the restart command" , async ( ) => {
182195 const env = createEnv ( ) ;
183- const waitForProcessExit = vi . fn ( async ( ) => { } ) ;
184- const runCommand = vi . fn ( async ( ) => { } ) ;
196+ const waitForProcessExit = vi . fn < WaitForProcessExitMock > ( async ( ) => { } ) ;
197+ const runCommand = vi . fn < RunCommandMock > ( async ( ) => { } ) ;
185198
186199 await runRestartHandoff ( env , {
187200 runCommand,
0 commit comments