1
1
import type { Mocked } from 'vitest' ;
2
2
import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
3
+ import { mockCreateLogger } from '../../../../common/__mocks__/create-logger.mock.js' ;
4
+ import type { Logger } from '../../../../common/logger/types.js' ;
5
+ import { runInBackground } from '../../../async/run-in-background.js' ;
3
6
import { GameServiceMockImpl } from '../../../game/__mocks__/game-service.mock.js' ;
4
7
import { sendCommandHandler } from '../send-command.js' ;
5
8
@@ -23,6 +26,12 @@ vi.mock('../../../game/game.instance.js', () => {
23
26
} ) ;
24
27
25
28
describe ( 'send-command' , ( ) => {
29
+ let logger : Logger ;
30
+
31
+ beforeEach ( ( ) => {
32
+ logger = mockCreateLogger ( ) ;
33
+ } ) ;
34
+
26
35
beforeEach ( ( ) => {
27
36
vi . useFakeTimers ( { shouldAdvanceTime : true } ) ;
28
37
} ) ;
@@ -36,6 +45,8 @@ describe('send-command', () => {
36
45
describe ( '#sendCommandHandler' , async ( ) => {
37
46
it ( 'sends a command with the game instance' , async ( ) => {
38
47
const mockGameService = new GameServiceMockImpl ( ) ;
48
+ mockGameService . isConnected . mockReturnValueOnce ( true ) ;
49
+
39
50
mockGameInstance . getInstance . mockReturnValueOnce ( mockGameService ) ;
40
51
41
52
const mockIpcDispatcher = vi . fn ( ) ;
@@ -44,13 +55,50 @@ describe('send-command', () => {
44
55
dispatch : mockIpcDispatcher ,
45
56
} ) ;
46
57
47
- await handler ( [ 'test-command' ] ) ;
58
+ // Run the handler in the background so that we can
59
+ // advance the mock timers for a speedier test.
60
+ // Normally, this handler waits a second between its actions.
61
+ runInBackground ( async ( ) => {
62
+ await handler ( [ 'test-command' ] ) ;
63
+ } ) ;
64
+
65
+ await vi . advanceTimersToNextTimerAsync ( ) ;
48
66
49
67
expect ( mockIpcDispatcher ) . toHaveBeenCalledWith ( 'game:command' , {
50
68
command : 'test-command' ,
51
69
} ) ;
52
70
} ) ;
53
71
72
+ it ( 'skips sending command if game instance is disconnected' , async ( ) => {
73
+ const logInfoSpy = vi . spyOn ( logger , 'info' ) ;
74
+
75
+ const mockGameService = new GameServiceMockImpl ( ) ;
76
+ mockGameService . isConnected . mockReturnValueOnce ( false ) ;
77
+
78
+ mockGameInstance . getInstance . mockReturnValueOnce ( mockGameService ) ;
79
+
80
+ const mockIpcDispatcher = vi . fn ( ) ;
81
+
82
+ const handler = sendCommandHandler ( {
83
+ dispatch : mockIpcDispatcher ,
84
+ } ) ;
85
+
86
+ await handler ( [ 'test-command' ] ) ;
87
+
88
+ expect ( logInfoSpy ) . toHaveBeenCalledWith (
89
+ 'game instance not connected, skipping send command' ,
90
+ {
91
+ command : 'test-command' ,
92
+ }
93
+ ) ;
94
+
95
+ expect ( mockIpcDispatcher ) . not . toHaveBeenCalled ( ) ;
96
+
97
+ expect ( mockGameService . send ) . not . toHaveBeenCalled ( ) ;
98
+
99
+ expect ( mockGameService . disconnect ) . not . toHaveBeenCalled ( ) ;
100
+ } ) ;
101
+
54
102
it ( 'throws error if game instance not found' , async ( ) => {
55
103
mockGameInstance . getInstance . mockReturnValueOnce ( undefined ) ;
56
104
0 commit comments