|
| 1 | +import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions'; |
| 2 | +import { SiteData, readAppdata } from 'cli/lib/appdata'; |
| 3 | +import { isProxyProcessRunning, stopProxyProcess } from 'cli/lib/pm2-manager'; |
| 4 | +import { stopProxyIfNoSitesNeedIt } from 'cli/lib/site-utils'; |
| 5 | +import { isServerRunning } from 'cli/lib/wordpress-server-manager'; |
| 6 | +import { Logger } from 'cli/logger'; |
| 7 | + |
| 8 | +jest.mock( 'cli/lib/appdata', () => ( { |
| 9 | + ...jest.requireActual( 'cli/lib/appdata' ), |
| 10 | + getAppdataDirectory: jest.fn().mockReturnValue( '/test/appdata' ), |
| 11 | + readAppdata: jest.fn(), |
| 12 | +} ) ); |
| 13 | +jest.mock( 'cli/lib/pm2-manager' ); |
| 14 | +jest.mock( 'cli/lib/wordpress-server-manager' ); |
| 15 | + |
| 16 | +describe( 'stopProxyIfNoSitesNeedIt', () => { |
| 17 | + const mockProcessDescription = { |
| 18 | + name: 'studio-proxy', |
| 19 | + pmId: 0, |
| 20 | + status: 'online', |
| 21 | + pid: 12345, |
| 22 | + }; |
| 23 | + |
| 24 | + let mockLogger: Logger< LoggerAction >; |
| 25 | + |
| 26 | + const createSiteData = ( overrides: Partial< SiteData > = {} ): SiteData => ( { |
| 27 | + id: 'site-1', |
| 28 | + name: 'Test Site', |
| 29 | + path: '/test/site', |
| 30 | + port: 8881, |
| 31 | + phpVersion: '8.0', |
| 32 | + ...overrides, |
| 33 | + } ); |
| 34 | + |
| 35 | + beforeEach( () => { |
| 36 | + jest.clearAllMocks(); |
| 37 | + |
| 38 | + mockLogger = { |
| 39 | + reportStart: jest.fn(), |
| 40 | + reportSuccess: jest.fn(), |
| 41 | + reportError: jest.fn(), |
| 42 | + } as unknown as Logger< LoggerAction >; |
| 43 | + |
| 44 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 45 | + ( stopProxyProcess as jest.Mock ).mockResolvedValue( undefined ); |
| 46 | + ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 47 | + ( readAppdata as jest.Mock ).mockResolvedValue( { sites: [], snapshots: [] } ); |
| 48 | + } ); |
| 49 | + |
| 50 | + afterEach( () => { |
| 51 | + jest.restoreAllMocks(); |
| 52 | + } ); |
| 53 | + |
| 54 | + it( 'should do nothing if proxy is not running', async () => { |
| 55 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 56 | + |
| 57 | + await stopProxyIfNoSitesNeedIt( 'site-1', mockLogger ); |
| 58 | + |
| 59 | + expect( readAppdata ).not.toHaveBeenCalled(); |
| 60 | + expect( stopProxyProcess ).not.toHaveBeenCalled(); |
| 61 | + } ); |
| 62 | + |
| 63 | + it( 'should stop proxy if no other sites exist', async () => { |
| 64 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 65 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 66 | + sites: [ createSiteData( { id: 'stopped-site' } ) ], |
| 67 | + snapshots: [], |
| 68 | + } ); |
| 69 | + |
| 70 | + await stopProxyIfNoSitesNeedIt( 'stopped-site', mockLogger ); |
| 71 | + |
| 72 | + expect( mockLogger.reportStart ).toHaveBeenCalledWith( |
| 73 | + 'stopProxy', |
| 74 | + 'Stopping HTTP proxy server...' |
| 75 | + ); |
| 76 | + expect( stopProxyProcess ).toHaveBeenCalled(); |
| 77 | + expect( mockLogger.reportSuccess ).toHaveBeenCalledWith( 'HTTP proxy server stopped' ); |
| 78 | + } ); |
| 79 | + |
| 80 | + it( 'should stop proxy if other sites exist but none have custom domains', async () => { |
| 81 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 82 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 83 | + sites: [ |
| 84 | + createSiteData( { id: 'stopped-site', customDomain: 'stopped.local' } ), |
| 85 | + createSiteData( { id: 'other-site-1' } ), |
| 86 | + createSiteData( { id: 'other-site-2' } ), |
| 87 | + ], |
| 88 | + snapshots: [], |
| 89 | + } ); |
| 90 | + |
| 91 | + await stopProxyIfNoSitesNeedIt( 'stopped-site', mockLogger ); |
| 92 | + |
| 93 | + expect( stopProxyProcess ).toHaveBeenCalled(); |
| 94 | + } ); |
| 95 | + |
| 96 | + it( 'should stop proxy if other sites have custom domains but are not running', async () => { |
| 97 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 98 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 99 | + sites: [ |
| 100 | + createSiteData( { id: 'stopped-site', customDomain: 'stopped.local' } ), |
| 101 | + createSiteData( { id: 'other-site', customDomain: 'other.local' } ), |
| 102 | + ], |
| 103 | + snapshots: [], |
| 104 | + } ); |
| 105 | + ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); |
| 106 | + |
| 107 | + await stopProxyIfNoSitesNeedIt( 'stopped-site', mockLogger ); |
| 108 | + |
| 109 | + expect( isServerRunning ).toHaveBeenCalledWith( 'other-site' ); |
| 110 | + expect( stopProxyProcess ).toHaveBeenCalled(); |
| 111 | + } ); |
| 112 | + |
| 113 | + it( 'should not stop proxy if another site with custom domain is running', async () => { |
| 114 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 115 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 116 | + sites: [ |
| 117 | + createSiteData( { id: 'stopped-site', customDomain: 'stopped.local' } ), |
| 118 | + createSiteData( { id: 'running-site', customDomain: 'running.local' } ), |
| 119 | + ], |
| 120 | + snapshots: [], |
| 121 | + } ); |
| 122 | + ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 123 | + |
| 124 | + await stopProxyIfNoSitesNeedIt( 'stopped-site', mockLogger ); |
| 125 | + |
| 126 | + expect( isServerRunning ).toHaveBeenCalledWith( 'running-site' ); |
| 127 | + expect( stopProxyProcess ).not.toHaveBeenCalled(); |
| 128 | + } ); |
| 129 | + |
| 130 | + it( 'should not check if the stopped site is running', async () => { |
| 131 | + ( isProxyProcessRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); |
| 132 | + ( readAppdata as jest.Mock ).mockResolvedValue( { |
| 133 | + sites: [ createSiteData( { id: 'stopped-site', customDomain: 'stopped.local' } ) ], |
| 134 | + snapshots: [], |
| 135 | + } ); |
| 136 | + |
| 137 | + await stopProxyIfNoSitesNeedIt( 'stopped-site', mockLogger ); |
| 138 | + |
| 139 | + expect( isServerRunning ).not.toHaveBeenCalledWith( 'stopped-site' ); |
| 140 | + expect( stopProxyProcess ).toHaveBeenCalled(); |
| 141 | + } ); |
| 142 | +} ); |
0 commit comments