|
| 1 | +import { describe, expect, it, vi, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +import { RpcGMCallTypes } from '../core/rpc/types'; |
| 4 | +import { handleDevWorkerMessage } from './handleWorkerMessage'; |
| 5 | + |
| 6 | +describe('handleDevWorkerMessage', () => { |
| 7 | + afterEach(() => { |
| 8 | + vi.restoreAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('does not throw when EXIT arrives before module server is initialized', () => { |
| 12 | + const processExit = vi.fn(); |
| 13 | + |
| 14 | + expect(() => |
| 15 | + handleDevWorkerMessage( |
| 16 | + { |
| 17 | + type: RpcGMCallTypes.EXIT, |
| 18 | + id: 'exit-before-init', |
| 19 | + }, |
| 20 | + { processExit, log: vi.fn() }, |
| 21 | + ), |
| 22 | + ).not.toThrow(); |
| 23 | + |
| 24 | + expect(processExit).toHaveBeenCalledWith(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it('calls module server exit when present', () => { |
| 28 | + const processExit = vi.fn(); |
| 29 | + const moduleServer = { |
| 30 | + exit: vi.fn(), |
| 31 | + }; |
| 32 | + |
| 33 | + handleDevWorkerMessage( |
| 34 | + { |
| 35 | + type: RpcGMCallTypes.EXIT, |
| 36 | + id: 'exit-with-server', |
| 37 | + }, |
| 38 | + { moduleServer, processExit, log: vi.fn() }, |
| 39 | + ); |
| 40 | + |
| 41 | + expect(moduleServer.exit).toHaveBeenCalledTimes(1); |
| 42 | + expect(processExit).toHaveBeenCalledWith(0); |
| 43 | + }); |
| 44 | + |
| 45 | + it('ignores non-exit messages', () => { |
| 46 | + const processExit = vi.fn(); |
| 47 | + const moduleServer = { |
| 48 | + exit: vi.fn(), |
| 49 | + }; |
| 50 | + |
| 51 | + handleDevWorkerMessage( |
| 52 | + { |
| 53 | + type: RpcGMCallTypes.CALL, |
| 54 | + id: 'message', |
| 55 | + args: [], |
| 56 | + }, |
| 57 | + { moduleServer, processExit, log: vi.fn() }, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(moduleServer.exit).not.toHaveBeenCalled(); |
| 61 | + expect(processExit).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments