|
| 1 | +import type { ChildProcess } from 'node:child_process'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { GameCode } from '../../../common/game/types.js'; |
| 4 | +import { PreferenceKey } from '../../preference/types.js'; |
| 5 | +import { startLichProcess } from '../start-process.js'; |
| 6 | + |
| 7 | +const { mockPreferenceService, mockSpawn } = await vi.hoisted(async () => { |
| 8 | + const preferenceServiceMockModule = await import( |
| 9 | + '../../preference/__mocks__/preference-service.mock.js' |
| 10 | + ); |
| 11 | + |
| 12 | + const mockPreferenceService = |
| 13 | + new preferenceServiceMockModule.PreferenceServiceMockImpl(); |
| 14 | + |
| 15 | + return { |
| 16 | + mockPreferenceService, |
| 17 | + mockSpawn: vi.fn(), |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +vi.mock('node:child_process', () => ({ |
| 22 | + spawn: mockSpawn, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../../preference/preference.instance.js', () => { |
| 26 | + return { |
| 27 | + Preferences: mockPreferenceService, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +vi.mock('../../logger/logger.factory.ts'); |
| 32 | + |
| 33 | +describe('start-process', () => { |
| 34 | + let mockChildProcess: Partial<ChildProcess>; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + vi.useFakeTimers({ shouldAdvanceTime: true }); |
| 38 | + |
| 39 | + mockChildProcess = { |
| 40 | + pid: 1234, |
| 41 | + once: vi.fn(), |
| 42 | + }; |
| 43 | + |
| 44 | + mockSpawn.mockReturnValue(mockChildProcess); |
| 45 | + |
| 46 | + mockPreferenceService.get.mockImplementation((key) => { |
| 47 | + switch (key) { |
| 48 | + case PreferenceKey.LICH_RUBY_PATH: |
| 49 | + return '/path/to/ruby'; |
| 50 | + case PreferenceKey.LICH_PATH: |
| 51 | + return '/path/to/lich.rb'; |
| 52 | + case PreferenceKey.LICH_HOST: |
| 53 | + return 'localhost'; |
| 54 | + case PreferenceKey.LICH_PORT: |
| 55 | + return 4242; |
| 56 | + case PreferenceKey.LICH_START_WAIT: |
| 57 | + return 0; |
| 58 | + } |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + vi.clearAllMocks(); |
| 64 | + vi.clearAllTimers(); |
| 65 | + vi.useRealTimers(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('#startLichProcess', () => { |
| 69 | + it('should start the process for dragonrealms prime', async () => { |
| 70 | + const result = await startLichProcess({ |
| 71 | + gameCode: GameCode.PRIME, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(mockSpawn).toHaveBeenCalledWith('/path/to/ruby', [ |
| 75 | + '/path/to/lich.rb', |
| 76 | + '--dragonrealms', |
| 77 | + '--genie', |
| 78 | + ]); |
| 79 | + |
| 80 | + expect(result).toEqual({ |
| 81 | + pid: mockChildProcess.pid, |
| 82 | + host: 'localhost', |
| 83 | + port: 4242, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should start the process for dragonrealms platinum', async () => { |
| 88 | + const result = await startLichProcess({ |
| 89 | + gameCode: GameCode.PLATINUM, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(mockSpawn).toHaveBeenCalledWith('/path/to/ruby', [ |
| 93 | + '/path/to/lich.rb', |
| 94 | + '--dragonrealms', |
| 95 | + '--genie', |
| 96 | + '--platinum', |
| 97 | + ]); |
| 98 | + |
| 99 | + expect(result).toEqual({ |
| 100 | + pid: mockChildProcess.pid, |
| 101 | + host: 'localhost', |
| 102 | + port: 4242, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should start the process for dragonrealms fallen', async () => { |
| 107 | + const result = await startLichProcess({ |
| 108 | + gameCode: GameCode.FALLEN, |
| 109 | + }); |
| 110 | + |
| 111 | + expect(mockSpawn).toHaveBeenCalledWith('/path/to/ruby', [ |
| 112 | + '/path/to/lich.rb', |
| 113 | + '--dragonrealms', |
| 114 | + '--genie', |
| 115 | + '--fallen', |
| 116 | + ]); |
| 117 | + |
| 118 | + expect(result).toEqual({ |
| 119 | + pid: mockChildProcess.pid, |
| 120 | + host: 'localhost', |
| 121 | + port: 4242, |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should start the process for dragonrealms test', async () => { |
| 126 | + const result = await startLichProcess({ |
| 127 | + gameCode: GameCode.TEST, |
| 128 | + }); |
| 129 | + |
| 130 | + expect(mockSpawn).toHaveBeenCalledWith('/path/to/ruby', [ |
| 131 | + '/path/to/lich.rb', |
| 132 | + '--dragonrealms', |
| 133 | + '--genie', |
| 134 | + '--test', |
| 135 | + ]); |
| 136 | + |
| 137 | + expect(result).toEqual({ |
| 138 | + pid: mockChildProcess.pid, |
| 139 | + host: 'localhost', |
| 140 | + port: 4242, |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments