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 { quitCharacterHandler } from '../quit-character.js' ;
5
8
@@ -23,6 +26,12 @@ vi.mock('../../../game/game.instance.js', () => {
23
26
} ) ;
24
27
25
28
describe ( 'quit-character' , ( ) => {
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('quit-character', () => {
36
45
describe ( '#quitCharacterhandler' , async ( ) => {
37
46
it ( 'quits playing character 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,15 +55,54 @@ describe('quit-character', () => {
44
55
dispatch : mockIpcDispatcher ,
45
56
} ) ;
46
57
47
- await handler ( [ ] ) ;
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 ( [ ] ) ;
63
+ } ) ;
64
+
65
+ await vi . advanceTimersToNextTimerAsync ( ) ;
48
66
49
67
expect ( mockIpcDispatcher ) . toHaveBeenCalledWith ( 'game:command' , {
50
68
command : 'quit' ,
51
69
} ) ;
52
70
71
+ expect ( mockGameService . send ) . toHaveBeenCalledWith ( 'quit' ) ;
72
+
53
73
expect ( mockGameService . disconnect ) . toHaveBeenCalledTimes ( 1 ) ;
54
74
} ) ;
55
75
76
+ it ( 'skips sending quit command if game instance is disconnected' , async ( ) => {
77
+ const logInfoSpy = vi . spyOn ( logger , 'info' ) ;
78
+
79
+ const mockGameService = new GameServiceMockImpl ( ) ;
80
+ mockGameService . isConnected . mockReturnValueOnce ( false ) ;
81
+
82
+ mockGameInstance . getInstance . mockReturnValueOnce ( mockGameService ) ;
83
+
84
+ const mockIpcDispatcher = vi . fn ( ) ;
85
+
86
+ const handler = quitCharacterHandler ( {
87
+ dispatch : mockIpcDispatcher ,
88
+ } ) ;
89
+
90
+ await handler ( [ ] ) ;
91
+
92
+ expect ( logInfoSpy ) . toHaveBeenCalledWith (
93
+ 'game instance not connected, skipping send command' ,
94
+ {
95
+ command : 'quit' ,
96
+ }
97
+ ) ;
98
+
99
+ expect ( mockIpcDispatcher ) . not . toHaveBeenCalled ( ) ;
100
+
101
+ expect ( mockGameService . send ) . not . toHaveBeenCalled ( ) ;
102
+
103
+ expect ( mockGameService . disconnect ) . not . toHaveBeenCalled ( ) ;
104
+ } ) ;
105
+
56
106
it ( 'throws error if game instance not found' , async ( ) => {
57
107
mockGameInstance . getInstance . mockReturnValueOnce ( undefined ) ;
58
108
0 commit comments