|
| 1 | +import { existsSync } from 'node:fs'; |
| 2 | +import { homedir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +import { execa } from 'execa'; |
| 7 | +import pm2 from 'pm2'; |
| 8 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'; |
| 9 | + |
| 10 | +import { isUnraidApiRunning } from '@app/core/utils/pm2/unraid-api-running.js'; |
| 11 | + |
| 12 | +const __dirname = fileURLToPath(new URL('.', import.meta.url)); |
| 13 | +const PROJECT_ROOT = join(__dirname, '../../../../..'); |
| 14 | +const DUMMY_PROCESS_PATH = join(__dirname, 'dummy-process.js'); |
| 15 | +const CLI_PATH = join(PROJECT_ROOT, 'dist/cli.js'); |
| 16 | +const TEST_PROCESS_NAME = 'test-unraid-api'; |
| 17 | + |
| 18 | +// Shared PM2 connection state |
| 19 | +let pm2Connected = false; |
| 20 | + |
| 21 | +// Helper to ensure PM2 connection is established |
| 22 | +async function ensurePM2Connection() { |
| 23 | + if (pm2Connected) return; |
| 24 | + |
| 25 | + return new Promise<void>((resolve, reject) => { |
| 26 | + pm2.connect((err) => { |
| 27 | + if (err) { |
| 28 | + reject(err); |
| 29 | + return; |
| 30 | + } |
| 31 | + pm2Connected = true; |
| 32 | + resolve(); |
| 33 | + }); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +// Helper to delete specific test processes (lightweight, reuses connection) |
| 38 | +async function deleteTestProcesses() { |
| 39 | + if (!pm2Connected) { |
| 40 | + // No connection, nothing to clean up |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const deletePromise = new Promise<void>((resolve) => { |
| 45 | + // Delete specific processes we might have created |
| 46 | + const processNames = ['unraid-api', TEST_PROCESS_NAME]; |
| 47 | + let deletedCount = 0; |
| 48 | + |
| 49 | + const deleteNext = () => { |
| 50 | + if (deletedCount >= processNames.length) { |
| 51 | + resolve(); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const processName = processNames[deletedCount]; |
| 56 | + pm2.delete(processName, () => { |
| 57 | + // Ignore errors, process might not exist |
| 58 | + deletedCount++; |
| 59 | + deleteNext(); |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + deleteNext(); |
| 64 | + }); |
| 65 | + |
| 66 | + const timeoutPromise = new Promise<void>((resolve) => { |
| 67 | + setTimeout(() => resolve(), 3000); // 3 second timeout |
| 68 | + }); |
| 69 | + |
| 70 | + return Promise.race([deletePromise, timeoutPromise]); |
| 71 | +} |
| 72 | + |
| 73 | +// Helper to ensure PM2 is completely clean (heavy cleanup with daemon kill) |
| 74 | +async function cleanupAllPM2Processes() { |
| 75 | + // First delete test processes if we have a connection |
| 76 | + if (pm2Connected) { |
| 77 | + await deleteTestProcesses(); |
| 78 | + } |
| 79 | + |
| 80 | + return new Promise<void>((resolve) => { |
| 81 | + // Always connect fresh for daemon kill (in case we weren't connected) |
| 82 | + pm2.connect((err) => { |
| 83 | + if (err) { |
| 84 | + // If we can't connect, assume PM2 is not running |
| 85 | + pm2Connected = false; |
| 86 | + resolve(); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Kill the daemon to ensure fresh state |
| 91 | + pm2.killDaemon(() => { |
| 92 | + pm2.disconnect(); |
| 93 | + pm2Connected = false; |
| 94 | + // Small delay to let PM2 fully shutdown |
| 95 | + setTimeout(resolve, 500); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +describe.skipIf(!!process.env.CI)('PM2 integration tests', () => { |
| 102 | + beforeAll(async () => { |
| 103 | + // Set PM2_HOME to use home directory for testing (not /var/log) |
| 104 | + process.env.PM2_HOME = join(homedir(), '.pm2'); |
| 105 | + |
| 106 | + // Build the CLI if it doesn't exist (only for CLI tests) |
| 107 | + if (!existsSync(CLI_PATH)) { |
| 108 | + console.log('Building CLI for integration tests...'); |
| 109 | + try { |
| 110 | + await execa('pnpm', ['build'], { |
| 111 | + cwd: PROJECT_ROOT, |
| 112 | + stdio: 'inherit', |
| 113 | + timeout: 120000, // 2 minute timeout for build |
| 114 | + }); |
| 115 | + } catch (error) { |
| 116 | + console.error('Failed to build CLI:', error); |
| 117 | + throw new Error( |
| 118 | + 'Cannot run CLI integration tests without built CLI. Run `pnpm build` first.' |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Only do a full cleanup once at the beginning |
| 124 | + await cleanupAllPM2Processes(); |
| 125 | + }, 150000); // 2.5 minute timeout for setup |
| 126 | + |
| 127 | + afterAll(async () => { |
| 128 | + // Only do a full cleanup once at the end |
| 129 | + await cleanupAllPM2Processes(); |
| 130 | + }); |
| 131 | + |
| 132 | + afterEach(async () => { |
| 133 | + // Lightweight cleanup after each test - just delete our test processes |
| 134 | + await deleteTestProcesses(); |
| 135 | + }, 5000); // 5 second timeout for cleanup |
| 136 | + |
| 137 | + describe('isUnraidApiRunning function', () => { |
| 138 | + it('should return false when PM2 is not running the unraid-api process', async () => { |
| 139 | + const result = await isUnraidApiRunning(); |
| 140 | + expect(result).toBe(false); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should return true when PM2 has unraid-api process running', async () => { |
| 144 | + // Ensure PM2 connection |
| 145 | + await ensurePM2Connection(); |
| 146 | + |
| 147 | + // Start a dummy process with the name 'unraid-api' |
| 148 | + await new Promise<void>((resolve, reject) => { |
| 149 | + pm2.start( |
| 150 | + { |
| 151 | + script: DUMMY_PROCESS_PATH, |
| 152 | + name: 'unraid-api', |
| 153 | + }, |
| 154 | + (startErr) => { |
| 155 | + if (startErr) return reject(startErr); |
| 156 | + resolve(); |
| 157 | + } |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + // Give PM2 time to start the process |
| 162 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 163 | + |
| 164 | + const result = await isUnraidApiRunning(); |
| 165 | + expect(result).toBe(true); |
| 166 | + }, 30000); |
| 167 | + |
| 168 | + it('should return false when unraid-api process is stopped', async () => { |
| 169 | + // Ensure PM2 connection |
| 170 | + await ensurePM2Connection(); |
| 171 | + |
| 172 | + // Start and then stop the process |
| 173 | + await new Promise<void>((resolve, reject) => { |
| 174 | + pm2.start( |
| 175 | + { |
| 176 | + script: DUMMY_PROCESS_PATH, |
| 177 | + name: 'unraid-api', |
| 178 | + }, |
| 179 | + (startErr) => { |
| 180 | + if (startErr) return reject(startErr); |
| 181 | + |
| 182 | + // Stop the process after starting |
| 183 | + setTimeout(() => { |
| 184 | + pm2.stop('unraid-api', (stopErr) => { |
| 185 | + if (stopErr) return reject(stopErr); |
| 186 | + resolve(); |
| 187 | + }); |
| 188 | + }, 1000); |
| 189 | + } |
| 190 | + ); |
| 191 | + }); |
| 192 | + |
| 193 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 194 | + |
| 195 | + const result = await isUnraidApiRunning(); |
| 196 | + expect(result).toBe(false); |
| 197 | + }, 30000); |
| 198 | + |
| 199 | + it('should handle PM2 connection errors gracefully', async () => { |
| 200 | + // Disconnect PM2 first to ensure we're testing fresh connection |
| 201 | + await new Promise<void>((resolve) => { |
| 202 | + pm2.disconnect(); |
| 203 | + pm2Connected = false; |
| 204 | + setTimeout(resolve, 100); |
| 205 | + }); |
| 206 | + |
| 207 | + // Set an invalid PM2_HOME to force connection failure |
| 208 | + const originalPM2Home = process.env.PM2_HOME; |
| 209 | + process.env.PM2_HOME = '/invalid/path/that/does/not/exist'; |
| 210 | + |
| 211 | + const result = await isUnraidApiRunning(); |
| 212 | + expect(result).toBe(false); |
| 213 | + |
| 214 | + // Restore original PM2_HOME |
| 215 | + if (originalPM2Home) { |
| 216 | + process.env.PM2_HOME = originalPM2Home; |
| 217 | + } else { |
| 218 | + delete process.env.PM2_HOME; |
| 219 | + } |
| 220 | + }, 15000); // 15 second timeout to allow for the Promise.race timeout |
| 221 | + }); |
| 222 | +}); |
0 commit comments