1
+ import { describe , it , expect , vi , beforeEach } from 'vitest' ;
2
+ import { checkGitCli , GitCliCheckResult } from './gitCliCheck' ;
3
+
4
+ // Mock the child_process module
5
+ vi . mock ( 'child_process' , ( ) => ( {
6
+ exec : vi . fn ( ) ,
7
+ } ) ) ;
8
+
9
+ // Mock the util module
10
+ vi . mock ( 'util' , ( ) => ( {
11
+ promisify : vi . fn ( ( fn ) => {
12
+ return ( cmd : string ) => {
13
+ return new Promise ( ( resolve , reject ) => {
14
+ fn ( cmd , ( error : Error | null , result : { stdout : string } ) => {
15
+ if ( error ) {
16
+ reject ( error ) ;
17
+ } else {
18
+ resolve ( result ) ;
19
+ }
20
+ } ) ;
21
+ } ) ;
22
+ } ;
23
+ } ) ,
24
+ } ) ) ;
25
+
26
+ // Import the mocked modules
27
+ import { exec } from 'child_process' ;
28
+
29
+ describe ( 'gitCliCheck' , ( ) => {
30
+ const mockExec = exec as unknown as vi . Mock ;
31
+
32
+ beforeEach ( ( ) => {
33
+ mockExec . mockReset ( ) ;
34
+ } ) ;
35
+
36
+ it ( 'should return all true when git and gh are available and authenticated' , async ( ) => {
37
+ // Mock successful responses
38
+ mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
39
+ if ( cmd === 'git --version' ) {
40
+ callback ( null , { stdout : 'git version 2.30.1' } ) ;
41
+ } else if ( cmd === 'gh --version' ) {
42
+ callback ( null , { stdout : 'gh version 2.0.0' } ) ;
43
+ } else if ( cmd === 'gh auth status' ) {
44
+ callback ( null , { stdout : 'Logged in to github.com as username' } ) ;
45
+ }
46
+ } ) ;
47
+
48
+ const result = await checkGitCli ( ) ;
49
+
50
+ expect ( result . gitAvailable ) . toBe ( true ) ;
51
+ expect ( result . ghAvailable ) . toBe ( true ) ;
52
+ expect ( result . ghAuthenticated ) . toBe ( true ) ;
53
+ expect ( result . errors ) . toHaveLength ( 0 ) ;
54
+ } ) ;
55
+
56
+ it ( 'should detect when git is not available' , async ( ) => {
57
+ mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
58
+ if ( cmd === 'git --version' ) {
59
+ callback ( new Error ( 'Command not found' ) ) ;
60
+ } else if ( cmd === 'gh --version' ) {
61
+ callback ( null , { stdout : 'gh version 2.0.0' } ) ;
62
+ } else if ( cmd === 'gh auth status' ) {
63
+ callback ( null , { stdout : 'Logged in to github.com as username' } ) ;
64
+ }
65
+ } ) ;
66
+
67
+ const result = await checkGitCli ( ) ;
68
+
69
+ expect ( result . gitAvailable ) . toBe ( false ) ;
70
+ expect ( result . ghAvailable ) . toBe ( true ) ;
71
+ expect ( result . ghAuthenticated ) . toBe ( true ) ;
72
+ expect ( result . errors ) . toContain ( 'Git CLI is not available. Please install git.' ) ;
73
+ } ) ;
74
+
75
+ it ( 'should detect when gh is not available' , async ( ) => {
76
+ mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
77
+ if ( cmd === 'git --version' ) {
78
+ callback ( null , { stdout : 'git version 2.30.1' } ) ;
79
+ } else if ( cmd === 'gh --version' ) {
80
+ callback ( new Error ( 'Command not found' ) ) ;
81
+ }
82
+ } ) ;
83
+
84
+ const result = await checkGitCli ( ) ;
85
+
86
+ expect ( result . gitAvailable ) . toBe ( true ) ;
87
+ expect ( result . ghAvailable ) . toBe ( false ) ;
88
+ expect ( result . ghAuthenticated ) . toBe ( false ) ;
89
+ expect ( result . errors ) . toContain ( 'GitHub CLI is not available. Please install gh CLI.' ) ;
90
+ } ) ;
91
+
92
+ it ( 'should detect when gh is not authenticated' , async ( ) => {
93
+ mockExec . mockImplementation ( ( cmd : string , callback : Function ) => {
94
+ if ( cmd === 'git --version' ) {
95
+ callback ( null , { stdout : 'git version 2.30.1' } ) ;
96
+ } else if ( cmd === 'gh --version' ) {
97
+ callback ( null , { stdout : 'gh version 2.0.0' } ) ;
98
+ } else if ( cmd === 'gh auth status' ) {
99
+ callback ( new Error ( 'You are not logged into any GitHub hosts' ) ) ;
100
+ }
101
+ } ) ;
102
+
103
+ const result = await checkGitCli ( ) ;
104
+
105
+ expect ( result . gitAvailable ) . toBe ( true ) ;
106
+ expect ( result . ghAvailable ) . toBe ( true ) ;
107
+ expect ( result . ghAuthenticated ) . toBe ( false ) ;
108
+ expect ( result . errors ) . toContain ( 'GitHub CLI is not authenticated. Please run "gh auth login".' ) ;
109
+ } ) ;
110
+ } ) ;
0 commit comments