|
| 1 | +/** |
| 2 | + * @fileoverview Tests for startCustomL1ParentSync job. |
| 3 | + */ |
| 4 | +require('../mocks/lib/queue'); |
| 5 | +require('../mocks/lib/logger'); |
| 6 | +require('../mocks/models'); |
| 7 | + |
| 8 | +const { Workspace, OrbitChainConfig, OpChainConfig } = require('../mocks/models'); |
| 9 | +const startCustomL1ParentSync = require('../../jobs/startCustomL1ParentSync'); |
| 10 | + |
| 11 | +jest.mock('../../lib/queue'); |
| 12 | +jest.mock('../../lib/logger'); |
| 13 | +jest.mock('../../lib/pm2'); |
| 14 | +jest.mock('../../lib/env'); |
| 15 | + |
| 16 | +const PM2 = require('../../lib/pm2'); |
| 17 | +const { getPm2Host, getPm2Secret } = require('../../lib/env'); |
| 18 | + |
| 19 | +beforeEach(() => jest.clearAllMocks()); |
| 20 | + |
| 21 | +describe('startCustomL1ParentSync', () => { |
| 22 | + let mockWorkspace; |
| 23 | + let mockPm2Instance; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + |
| 28 | + getPm2Host.mockReturnValue('http://localhost:3000'); |
| 29 | + getPm2Secret.mockReturnValue('test-secret'); |
| 30 | + |
| 31 | + mockWorkspace = { |
| 32 | + id: 113, |
| 33 | + name: 'My Custom L1', |
| 34 | + rpcServer: 'https://my-custom-l1.com/rpc', |
| 35 | + isCustomL1Parent: true |
| 36 | + }; |
| 37 | + |
| 38 | + mockPm2Instance = { |
| 39 | + find: jest.fn(), |
| 40 | + start: jest.fn() |
| 41 | + }; |
| 42 | + |
| 43 | + PM2.mockImplementation(() => mockPm2Instance); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('when workspace is a valid custom L1 parent', () => { |
| 47 | + it('should start sync when workspace has OP children', async () => { |
| 48 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 49 | + OrbitChainConfig.count.mockResolvedValue(0); |
| 50 | + OpChainConfig.count.mockResolvedValue(1); |
| 51 | + mockPm2Instance.find.mockResolvedValue({ data: null }); |
| 52 | + mockPm2Instance.start.mockResolvedValue({}); |
| 53 | + |
| 54 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 55 | + |
| 56 | + expect(Workspace.findByPk).toHaveBeenCalledWith(113); |
| 57 | + expect(OpChainConfig.count).toHaveBeenCalledWith({ where: { parentWorkspaceId: 113 } }); |
| 58 | + expect(mockPm2Instance.start).toHaveBeenCalledWith('custom-l1-113', 113); |
| 59 | + expect(result).toBe('Started sync for workspace 113'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should start sync when workspace has Orbit children', async () => { |
| 63 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 64 | + OrbitChainConfig.count.mockResolvedValue(2); |
| 65 | + OpChainConfig.count.mockResolvedValue(0); |
| 66 | + mockPm2Instance.find.mockResolvedValue({ data: null }); |
| 67 | + mockPm2Instance.start.mockResolvedValue({}); |
| 68 | + |
| 69 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 70 | + |
| 71 | + expect(OrbitChainConfig.count).toHaveBeenCalledWith({ where: { parentWorkspaceId: 113 } }); |
| 72 | + expect(mockPm2Instance.start).toHaveBeenCalledWith('custom-l1-113', 113); |
| 73 | + expect(result).toBe('Started sync for workspace 113'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should skip if sync is already running', async () => { |
| 77 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 78 | + OrbitChainConfig.count.mockResolvedValue(1); |
| 79 | + OpChainConfig.count.mockResolvedValue(0); |
| 80 | + mockPm2Instance.find.mockResolvedValue({ data: { pm2_env: { status: 'online' } } }); |
| 81 | + |
| 82 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 83 | + |
| 84 | + expect(mockPm2Instance.start).not.toHaveBeenCalled(); |
| 85 | + expect(result).toBe('Sync already running for workspace 113'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('when workspace should not be synced', () => { |
| 90 | + it('should skip if workspace not found', async () => { |
| 91 | + Workspace.findByPk.mockResolvedValue(null); |
| 92 | + |
| 93 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 999 } }); |
| 94 | + |
| 95 | + expect(result).toBe('Workspace 999 not found'); |
| 96 | + expect(mockPm2Instance.start).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should skip if workspace is not a custom L1 parent', async () => { |
| 100 | + Workspace.findByPk.mockResolvedValue({ ...mockWorkspace, isCustomL1Parent: false }); |
| 101 | + |
| 102 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 103 | + |
| 104 | + expect(result).toBe('Workspace 113 is not a custom L1 parent'); |
| 105 | + expect(mockPm2Instance.start).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should skip if workspace has no L2 children', async () => { |
| 109 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 110 | + OrbitChainConfig.count.mockResolvedValue(0); |
| 111 | + OpChainConfig.count.mockResolvedValue(0); |
| 112 | + |
| 113 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 114 | + |
| 115 | + expect(result).toBe('No L2 children for workspace 113'); |
| 116 | + expect(mockPm2Instance.start).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('when PM2 is not configured', () => { |
| 121 | + it('should skip if PM2 host is not set', async () => { |
| 122 | + getPm2Host.mockReturnValue(null); |
| 123 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 124 | + OrbitChainConfig.count.mockResolvedValue(1); |
| 125 | + OpChainConfig.count.mockResolvedValue(0); |
| 126 | + |
| 127 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 128 | + |
| 129 | + expect(result).toBe('PM2 not configured'); |
| 130 | + expect(mockPm2Instance.start).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should skip if PM2 secret is not set', async () => { |
| 134 | + getPm2Secret.mockReturnValue(null); |
| 135 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 136 | + OrbitChainConfig.count.mockResolvedValue(1); |
| 137 | + OpChainConfig.count.mockResolvedValue(0); |
| 138 | + |
| 139 | + const result = await startCustomL1ParentSync({ data: { workspaceId: 113 } }); |
| 140 | + |
| 141 | + expect(result).toBe('PM2 not configured'); |
| 142 | + expect(mockPm2Instance.start).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('error handling', () => { |
| 147 | + it('should throw if workspaceId is missing', async () => { |
| 148 | + await expect(startCustomL1ParentSync({ data: {} })) |
| 149 | + .rejects.toThrow('Missing workspaceId parameter'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should throw if PM2 start fails', async () => { |
| 153 | + Workspace.findByPk.mockResolvedValue(mockWorkspace); |
| 154 | + OrbitChainConfig.count.mockResolvedValue(1); |
| 155 | + OpChainConfig.count.mockResolvedValue(0); |
| 156 | + mockPm2Instance.find.mockResolvedValue({ data: null }); |
| 157 | + mockPm2Instance.start.mockRejectedValue(new Error('PM2 connection failed')); |
| 158 | + |
| 159 | + await expect(startCustomL1ParentSync({ data: { workspaceId: 113 } })) |
| 160 | + .rejects.toThrow('PM2 connection failed'); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments