|
| 1 | +import { SiteData, clearSiteLatestCliPid, readAppdata } from 'cli/lib/appdata'; |
| 2 | +import { connect, disconnect } from 'cli/lib/pm2-manager'; |
| 3 | +import { isServerRunning, stopWordPressServer } from 'cli/lib/wordpress-server-manager'; |
| 4 | +import { Logger, LoggerError } from 'cli/logger'; |
| 5 | + |
| 6 | +jest.mock( 'cli/lib/appdata', () => ( { |
| 7 | + ...jest.requireActual( 'cli/lib/appdata' ), |
| 8 | + getAppdataDirectory: jest.fn().mockReturnValue( '/test/appdata' ), |
| 9 | + readAppdata: jest.fn(), |
| 10 | + clearSiteLatestCliPid: jest.fn(), |
| 11 | + getSiteUrl: jest.fn( ( site ) => `http://localhost:${ site.port }` ), |
| 12 | +} ) ); |
| 13 | +jest.mock( 'common/lib/fs-utils', () => ( { |
| 14 | + ...jest.requireActual( 'common/lib/fs-utils' ), |
| 15 | + arePathsEqual: jest.fn( ( a: string, b: string ) => a === b ), |
| 16 | +} ) ); |
| 17 | +jest.mock( 'cli/lib/pm2-manager' ); |
| 18 | +jest.mock( 'cli/lib/wordpress-server-manager' ); |
| 19 | +jest.mock( 'cli/logger' ); |
| 20 | + |
| 21 | +describe( 'Site Stop Command', () => { |
| 22 | + const mockSiteFolder = '/test/site/path'; |
| 23 | + const mockSiteData: SiteData = { |
| 24 | + id: 'test-site-id', |
| 25 | + name: 'Test Site', |
| 26 | + path: mockSiteFolder, |
| 27 | + port: 8881, |
| 28 | + adminUsername: 'admin', |
| 29 | + adminPassword: 'password123', |
| 30 | + running: true, |
| 31 | + phpVersion: '8.0', |
| 32 | + url: `http://localhost:8881`, |
| 33 | + }; |
| 34 | + |
| 35 | + const mockProcessDescription = { |
| 36 | + name: 'test-site-id', |
| 37 | + pmId: 0, |
| 38 | + status: 'online', |
| 39 | + pid: 12345, |
| 40 | + }; |
| 41 | + |
| 42 | + let mockLogger: { |
| 43 | + reportStart: jest.Mock; |
| 44 | + reportSuccess: jest.Mock; |
| 45 | + reportError: jest.Mock; |
| 46 | + }; |
| 47 | + |
| 48 | + beforeEach( () => { |
| 49 | + jest.clearAllMocks(); |
| 50 | + |
| 51 | + mockLogger = { |
| 52 | + reportStart: jest.fn(), |
| 53 | + reportSuccess: jest.fn(), |
| 54 | + reportError: jest.fn(), |
| 55 | + }; |
| 56 | + |
| 57 | + ( Logger as jest.Mock ).mockReturnValue( mockLogger ); |
| 58 | + ( connect as jest.Mock ).mockResolvedValue( undefined ); |
| 59 | + ( disconnect as jest.Mock ).mockReturnValue( undefined ); |
| 60 | + ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 61 | + ( stopWordPressServer as jest.Mock ).mockResolvedValue( undefined ); |
| 62 | + ( clearSiteLatestCliPid as jest.Mock ).mockResolvedValue( undefined ); |
| 63 | + } ); |
| 64 | + |
| 65 | + afterEach( () => { |
| 66 | + jest.restoreAllMocks(); |
| 67 | + } ); |
| 68 | + |
| 69 | + describe( 'Error Handling', () => { |
| 70 | + it( 'should handle site not found error', async () => { |
| 71 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 72 | + sites: [], |
| 73 | + snapshots: [], |
| 74 | + } ); |
| 75 | + |
| 76 | + const { runCommand } = await import( '../stop' ); |
| 77 | + await runCommand( mockSiteFolder ); |
| 78 | + |
| 79 | + expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); |
| 80 | + expect( disconnect ).toHaveBeenCalled(); |
| 81 | + } ); |
| 82 | + |
| 83 | + it( 'should handle PM2 connection failure', async () => { |
| 84 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 85 | + sites: [ mockSiteData ], |
| 86 | + snapshots: [], |
| 87 | + } ); |
| 88 | + ( connect as jest.Mock ).mockRejectedValue( new Error( 'PM2 connection failed' ) ); |
| 89 | + |
| 90 | + const { runCommand } = await import( '../stop' ); |
| 91 | + await runCommand( mockSiteFolder ); |
| 92 | + |
| 93 | + expect( mockLogger.reportError ).toHaveBeenCalled(); |
| 94 | + expect( disconnect ).toHaveBeenCalled(); |
| 95 | + } ); |
| 96 | + |
| 97 | + it( 'should handle WordPress server stop failure', async () => { |
| 98 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 99 | + sites: [ mockSiteData ], |
| 100 | + snapshots: [], |
| 101 | + } ); |
| 102 | + ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 103 | + ( stopWordPressServer as jest.Mock ).mockRejectedValue( new Error( 'Server stop failed' ) ); |
| 104 | + |
| 105 | + const { runCommand } = await import( '../stop' ); |
| 106 | + await runCommand( mockSiteFolder ); |
| 107 | + |
| 108 | + expect( mockLogger.reportStart ).toHaveBeenCalledWith( |
| 109 | + 'stopSite', |
| 110 | + 'Stopping WordPress site...' |
| 111 | + ); |
| 112 | + expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); |
| 113 | + expect( disconnect ).toHaveBeenCalled(); |
| 114 | + } ); |
| 115 | + } ); |
| 116 | + |
| 117 | + describe( 'Success Cases', () => { |
| 118 | + it( 'should report that site is not running if server is not running', async () => { |
| 119 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 120 | + sites: [ mockSiteData ], |
| 121 | + snapshots: [], |
| 122 | + } ); |
| 123 | + ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 124 | + |
| 125 | + const { runCommand } = await import( '../stop' ); |
| 126 | + await runCommand( mockSiteFolder ); |
| 127 | + |
| 128 | + expect( mockLogger.reportSuccess ).toHaveBeenCalledWith( 'WordPress site is not running' ); |
| 129 | + expect( stopWordPressServer ).not.toHaveBeenCalled(); |
| 130 | + expect( clearSiteLatestCliPid ).not.toHaveBeenCalled(); |
| 131 | + expect( disconnect ).toHaveBeenCalled(); |
| 132 | + } ); |
| 133 | + |
| 134 | + it( 'should stop a running site', async () => { |
| 135 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 136 | + sites: [ mockSiteData ], |
| 137 | + snapshots: [], |
| 138 | + } ); |
| 139 | + ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 140 | + |
| 141 | + const { runCommand } = await import( '../stop' ); |
| 142 | + await runCommand( mockSiteFolder ); |
| 143 | + |
| 144 | + expect( connect ).toHaveBeenCalled(); |
| 145 | + expect( isServerRunning ).toHaveBeenCalledWith( mockSiteData.id ); |
| 146 | + expect( mockLogger.reportStart ).toHaveBeenCalledWith( |
| 147 | + 'stopSite', |
| 148 | + 'Stopping WordPress site...' |
| 149 | + ); |
| 150 | + expect( stopWordPressServer ).toHaveBeenCalledWith( mockSiteData.id ); |
| 151 | + expect( clearSiteLatestCliPid ).toHaveBeenCalledWith( mockSiteData.id ); |
| 152 | + expect( mockLogger.reportSuccess ).toHaveBeenCalledWith( 'WordPress site stopped' ); |
| 153 | + expect( disconnect ).toHaveBeenCalled(); |
| 154 | + } ); |
| 155 | + } ); |
| 156 | + |
| 157 | + describe( 'Cleanup', () => { |
| 158 | + it( 'should disconnect from PM2 even on error', async () => { |
| 159 | + ( readAppdata as jest.Mock ).mockRejectedValue( new Error( 'Appdata error' ) ); |
| 160 | + |
| 161 | + const { runCommand } = await import( '../stop' ); |
| 162 | + await runCommand( mockSiteFolder ); |
| 163 | + |
| 164 | + expect( disconnect ).toHaveBeenCalled(); |
| 165 | + } ); |
| 166 | + |
| 167 | + it( 'should disconnect from PM2 on success', async () => { |
| 168 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 169 | + sites: [ mockSiteData ], |
| 170 | + snapshots: [], |
| 171 | + } ); |
| 172 | + ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 173 | + |
| 174 | + const { runCommand } = await import( '../stop' ); |
| 175 | + await runCommand( mockSiteFolder ); |
| 176 | + |
| 177 | + expect( disconnect ).toHaveBeenCalled(); |
| 178 | + } ); |
| 179 | + |
| 180 | + it( 'should disconnect from PM2 even if server was not running', async () => { |
| 181 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 182 | + sites: [ mockSiteData ], |
| 183 | + snapshots: [], |
| 184 | + } ); |
| 185 | + ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 186 | + |
| 187 | + const { runCommand } = await import( '../stop' ); |
| 188 | + await runCommand( mockSiteFolder ); |
| 189 | + |
| 190 | + expect( disconnect ).toHaveBeenCalled(); |
| 191 | + } ); |
| 192 | + } ); |
| 193 | +} ); |
0 commit comments