@@ -4,16 +4,17 @@ import { SourceControlGitService } from '../source-control-git.service.ee';
4
4
import type { SourceControlPreferencesService } from '../source-control-preferences.service.ee' ;
5
5
import type { SourceControlPreferences } from '../types/source-control-preferences' ;
6
6
7
- // Mock simple-git
8
7
const mockSimpleGit = {
9
- env : jest . fn ( ) . mockReturnThis ( ) ,
8
+ env : jest . fn ( ) ,
10
9
init : jest . fn ( ) . mockResolvedValue ( undefined ) ,
11
10
addRemote : jest . fn ( ) . mockResolvedValue ( undefined ) ,
12
11
getRemotes : jest . fn ( ) . mockResolvedValue ( [ ] ) ,
13
12
} ;
14
13
14
+ mockSimpleGit . env . mockReturnValue ( mockSimpleGit ) ;
15
+
15
16
jest . mock ( 'simple-git' , ( ) => ( {
16
- simpleGit : jest . fn ( ) . mockImplementation ( ( ) => mockSimpleGit ) ,
17
+ simpleGit : jest . fn ( ) . mockReturnValue ( mockSimpleGit ) ,
17
18
} ) ) ;
18
19
19
20
describe ( 'SourceControlGitService - HTTPS functionality' , ( ) => {
@@ -37,34 +38,34 @@ describe('SourceControlGitService - HTTPS functionality', () => {
37
38
38
39
jest . spyOn ( sourceControlPreferencesService , 'getPreferences' ) . mockReturnValue ( mockPreferences ) ;
39
40
jest . clearAllMocks ( ) ;
41
+
42
+ mockSimpleGit . env . mockReturnValue ( mockSimpleGit ) ;
40
43
} ) ;
41
44
42
45
afterEach ( ( ) => {
43
- jest . resetAllMocks ( ) ;
46
+ mockSimpleGit . env . mockClear ( ) ;
47
+ mockSimpleGit . init . mockClear ( ) ;
48
+ mockSimpleGit . addRemote . mockClear ( ) ;
49
+ mockSimpleGit . getRemotes . mockClear ( ) ;
44
50
} ) ;
45
51
46
52
describe ( 'setGitSshCommand' , ( ) => {
47
53
it ( 'should configure git for HTTPS without SSH command' , async ( ) => {
48
- // Act
49
54
await sourceControlGitService . setGitSshCommand ( ) ;
50
55
51
- // Assert
52
56
expect ( mockSimpleGit . env ) . toHaveBeenCalledWith ( 'GIT_TERMINAL_PROMPT' , '0' ) ;
53
57
expect ( mockSimpleGit . env ) . not . toHaveBeenCalledWith ( 'GIT_SSH_COMMAND' , expect . any ( String ) ) ;
54
58
} ) ;
55
59
56
60
it ( 'should configure git for SSH with SSH command when connectionType is ssh' , async ( ) => {
57
- // Arrange
58
61
const sshPreferences = { ...mockPreferences , connectionType : 'ssh' as const } ;
59
62
jest . spyOn ( sourceControlPreferencesService , 'getPreferences' ) . mockReturnValue ( sshPreferences ) ;
60
63
jest
61
64
. spyOn ( sourceControlPreferencesService , 'getPrivateKeyPath' )
62
65
. mockResolvedValue ( '/path/to/key' ) ;
63
66
64
- // Act
65
67
await sourceControlGitService . setGitSshCommand ( '/git/folder' , '/ssh/folder' ) ;
66
68
67
- // Assert
68
69
expect ( sourceControlPreferencesService . getPrivateKeyPath ) . toHaveBeenCalled ( ) ;
69
70
expect ( mockSimpleGit . env ) . toHaveBeenCalledWith ( 'GIT_TERMINAL_PROMPT' , '0' ) ;
70
71
expect ( mockSimpleGit . env ) . toHaveBeenCalledWith (
@@ -76,11 +77,9 @@ describe('SourceControlGitService - HTTPS functionality', () => {
76
77
77
78
describe ( 'URL normalization logic' , ( ) => {
78
79
it ( 'should normalize HTTPS URLs for comparison' , ( ) => {
79
- // Arrange
80
80
const remoteWithCredentials = 'https://user:token@github.com/user/repo.git' ;
81
81
const inputWithoutCredentials = 'https://github.com/user/repo.git' ;
82
82
83
- // Act - simulate the URL normalization logic from hasRemoteConfigured
84
83
const normalizeUrl = ( url : string ) => {
85
84
try {
86
85
const urlObj = new URL ( url ) ;
@@ -95,15 +94,12 @@ describe('SourceControlGitService - HTTPS functionality', () => {
95
94
const normalizedRemote = normalizeUrl ( remoteWithCredentials ) ;
96
95
const normalizedInput = normalizeUrl ( inputWithoutCredentials ) ;
97
96
98
- // Assert
99
97
expect ( normalizedRemote ) . toBe ( normalizedInput ) ;
100
98
} ) ;
101
99
102
100
it ( 'should handle malformed URLs gracefully' , ( ) => {
103
- // Arrange
104
101
const malformedUrl = 'not-a-valid-url' ;
105
102
106
- // Act
107
103
const normalizeUrl = ( url : string ) => {
108
104
try {
109
105
const urlObj = new URL ( url ) ;
@@ -117,28 +113,24 @@ describe('SourceControlGitService - HTTPS functionality', () => {
117
113
118
114
const result = normalizeUrl ( malformedUrl ) ;
119
115
120
- // Assert
121
116
expect ( result ) . toBe ( malformedUrl ) ; // Should return original when URL parsing fails
122
117
} ) ;
123
118
} ) ;
124
119
125
120
describe ( 'URL encoding in repository initialization' , ( ) => {
126
121
it ( 'should properly encode credentials with special characters' , ( ) => {
127
- // Arrange
128
122
const mockCredentials = {
129
123
username : 'user@domain.com' ,
130
124
password : 'p@ssw0rd!' ,
131
125
} ;
132
126
133
127
const baseUrl = 'https://github.com/user/repo.git' ;
134
128
135
- // Act - simulate the URL encoding logic from initRepository
136
129
const urlObj = new URL ( baseUrl ) ;
137
130
urlObj . username = encodeURIComponent ( mockCredentials . username ) ;
138
131
urlObj . password = encodeURIComponent ( mockCredentials . password ) ;
139
132
const encodedUrl = urlObj . toString ( ) ;
140
133
141
- // Assert - URL constructor automatically encodes some characters but not all
142
134
expect ( encodedUrl ) . toContain ( 'user%40domain.com' ) ;
143
135
expect ( encodedUrl ) . toContain ( 'p%40ssw0rd' ) ;
144
136
expect ( encodedUrl ) . toMatch (
@@ -155,24 +147,20 @@ describe('SourceControlGitService - HTTPS functionality', () => {
155
147
156
148
const baseUrl = 'https://github.com/user/repo.git' ;
157
149
158
- // Act
159
150
const urlObj = new URL ( baseUrl ) ;
160
151
urlObj . username = encodeURIComponent ( mockCredentials . username ) ;
161
152
urlObj . password = encodeURIComponent ( mockCredentials . password ) ;
162
153
const encodedUrl = urlObj . toString ( ) ;
163
154
164
- // Assert
165
155
expect ( encodedUrl ) . toBe ( 'https://testuser:testtoken123@github.com/user/repo.git' ) ;
166
156
} ) ;
167
157
} ) ;
168
158
169
159
describe ( 'Connection type handling' , ( ) => {
170
160
it ( 'should differentiate between SSH and HTTPS configuration' , ( ) => {
171
- // Arrange
172
161
const httpsPrefs = { connectionType : 'https' as const } ;
173
162
const sshPrefs = { connectionType : 'ssh' as const } ;
174
163
175
- // Act & Assert
176
164
expect ( httpsPrefs . connectionType ) . toBe ( 'https' ) ;
177
165
expect ( sshPrefs . connectionType ) . toBe ( 'ssh' ) ;
178
166
expect ( httpsPrefs . connectionType !== sshPrefs . connectionType ) . toBe ( true ) ;
0 commit comments