1+ const MdTail = require ( '../lib/mdtail' ) ;
2+ const fs = require ( 'fs' ) ;
3+ const readline = require ( 'readline' ) ;
4+
5+ // Mock modules
6+ jest . mock ( 'fs' ) ;
7+ jest . mock ( 'readline' ) ;
8+
9+ describe ( 'MdTail - 100% Branch Coverage' , ( ) => {
10+ let mdtail ;
11+ let consoleLogSpy ;
12+ let consoleClearSpy ;
13+ let stdoutWriteSpy ;
14+ let originalColumns ;
15+
16+ beforeEach ( ( ) => {
17+ mdtail = new MdTail ( ) ;
18+
19+ // Save original columns
20+ originalColumns = process . stdout . columns ;
21+
22+ // Mock console methods
23+ consoleLogSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
24+ consoleClearSpy = jest . spyOn ( console , 'clear' ) . mockImplementation ( ) ;
25+ stdoutWriteSpy = jest . spyOn ( process . stdout , 'write' ) . mockImplementation ( ) ;
26+
27+ // Mock process.stdin
28+ process . stdin . isTTY = true ;
29+ process . stdin . setRawMode = jest . fn ( ) ;
30+ process . stdin . resume = jest . fn ( ) ;
31+
32+ jest . clearAllMocks ( ) ;
33+ } ) ;
34+
35+ afterEach ( ( ) => {
36+ // Restore original columns
37+ process . stdout . columns = originalColumns ;
38+
39+ consoleLogSpy . mockRestore ( ) ;
40+ consoleClearSpy . mockRestore ( ) ;
41+ stdoutWriteSpy . mockRestore ( ) ;
42+ jest . clearAllMocks ( ) ;
43+ jest . clearAllTimers ( ) ;
44+ jest . useRealTimers ( ) ;
45+ } ) ;
46+
47+ describe ( 'displayCurrentFile - undefined columns fallback' , ( ) => {
48+ test ( 'should use default width of 80 when process.stdout.columns is undefined' , ( ) => {
49+ // Set columns to undefined to test the fallback
50+ process . stdout . columns = undefined ;
51+
52+ mdtail . files = [ '/test/file.md' ] ;
53+ mdtail . currentTabIndex = 0 ;
54+ fs . readFileSync . mockReturnValue ( '# Test Content' ) ;
55+
56+ mdtail . displayCurrentFile ( ) ;
57+
58+ // Should use default width of 80 for the borders
59+ expect ( consoleLogSpy ) . toHaveBeenCalledWith ( '\n' + '═' . repeat ( 80 ) ) ;
60+ expect ( consoleLogSpy ) . toHaveBeenCalledWith ( '═' . repeat ( 80 ) + '\n' ) ;
61+ } ) ;
62+ } ) ;
63+
64+ describe ( 'setupKeyboardNavigation - right arrow branch' , ( ) => {
65+ test ( 'should handle right arrow key with else-if branch' , ( ) => {
66+ mdtail . files = [ '/file1.md' , '/file2.md' , '/file3.md' ] ;
67+ mdtail . currentTabIndex = 0 ;
68+ mdtail . displayCurrentFile = jest . fn ( ) ;
69+
70+ let keypressCallback ;
71+ process . stdin . on = jest . fn ( ( event , callback ) => {
72+ if ( event === 'keypress' ) {
73+ keypressCallback = callback ;
74+ }
75+ } ) ;
76+ readline . emitKeypressEvents = jest . fn ( ) ;
77+
78+ mdtail . setupKeyboardNavigation ( ) ;
79+
80+ // First test left arrow
81+ keypressCallback ( '' , { name : 'left' } ) ;
82+ expect ( mdtail . currentTabIndex ) . toBe ( 2 ) ; // Wrapped to last
83+
84+ // Reset for right arrow test
85+ mdtail . currentTabIndex = 0 ;
86+ mdtail . displayCurrentFile . mockClear ( ) ;
87+
88+ // Now test right arrow (this will hit the else-if branch)
89+ keypressCallback ( '' , { name : 'right' } ) ;
90+ expect ( mdtail . currentTabIndex ) . toBe ( 1 ) ;
91+ expect ( mdtail . displayCurrentFile ) . toHaveBeenCalled ( ) ;
92+ } ) ;
93+
94+ test ( 'should not navigate when key is neither left nor right' , ( ) => {
95+ mdtail . files = [ '/file1.md' , '/file2.md' ] ;
96+ mdtail . currentTabIndex = 0 ;
97+ mdtail . displayCurrentFile = jest . fn ( ) ;
98+
99+ let keypressCallback ;
100+ process . stdin . on = jest . fn ( ( event , callback ) => {
101+ if ( event === 'keypress' ) {
102+ keypressCallback = callback ;
103+ }
104+ } ) ;
105+ readline . emitKeypressEvents = jest . fn ( ) ;
106+
107+ mdtail . setupKeyboardNavigation ( ) ;
108+
109+ // Test with up arrow (should not navigate)
110+ keypressCallback ( '' , { name : 'up' } ) ;
111+ expect ( mdtail . currentTabIndex ) . toBe ( 0 ) ; // Should not change
112+ expect ( mdtail . displayCurrentFile ) . not . toHaveBeenCalled ( ) ;
113+
114+ // Test with no key object
115+ keypressCallback ( 'a' , null ) ;
116+ expect ( mdtail . currentTabIndex ) . toBe ( 0 ) ; // Should not change
117+ expect ( mdtail . displayCurrentFile ) . not . toHaveBeenCalled ( ) ;
118+ } ) ;
119+ } ) ;
120+ } ) ;
0 commit comments