Skip to content

Commit c41ac61

Browse files
committed
test: Improve tests
1 parent 8395f42 commit c41ac61

File tree

8 files changed

+293
-498
lines changed

8 files changed

+293
-498
lines changed

packages/cli/src/environments.ee/source-control/__tests__/source-control-git.service.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ describe('SourceControlGitService', () => {
124124

125125
// Mock the getPrivateKeyPath to return a Windows path
126126
mockPreferencesService.getPrivateKeyPath.mockResolvedValue(windowsPath);
127+
// Mock getPreferences to return SSH connection type (required for new functionality)
128+
mockPreferencesService.getPreferences.mockReturnValue({
129+
connectionType: 'ssh',
130+
connected: true,
131+
repositoryUrl: 'git@github.com:user/repo.git',
132+
branchName: 'main',
133+
branchReadOnly: false,
134+
branchColor: '#5296D6',
135+
initRepo: false,
136+
keyGeneratorType: 'ed25519',
137+
});
127138

128139
const gitService = new SourceControlGitService(mock(), mock(), mockPreferencesService);
129140

@@ -154,6 +165,17 @@ describe('SourceControlGitService', () => {
154165

155166
// Mock the getPrivateKeyPath to return a path with spaces
156167
mockPreferencesService.getPrivateKeyPath.mockResolvedValue(privateKeyPath);
168+
// Mock getPreferences to return SSH connection type
169+
mockPreferencesService.getPreferences.mockReturnValue({
170+
connectionType: 'ssh',
171+
connected: true,
172+
repositoryUrl: 'git@github.com:user/repo.git',
173+
branchName: 'main',
174+
branchReadOnly: false,
175+
branchColor: '#5296D6',
176+
initRepo: false,
177+
keyGeneratorType: 'ed25519',
178+
});
157179

158180
const gitService = new SourceControlGitService(mock(), mock(), mockPreferencesService);
159181

@@ -187,6 +209,17 @@ describe('SourceControlGitService', () => {
187209

188210
// Mock the getPrivateKeyPath to return a path with quotes
189211
mockPreferencesService.getPrivateKeyPath.mockResolvedValue(pathWithQuotes);
212+
// Mock getPreferences to return SSH connection type
213+
mockPreferencesService.getPreferences.mockReturnValue({
214+
connectionType: 'ssh',
215+
connected: true,
216+
repositoryUrl: 'git@github.com:user/repo.git',
217+
branchName: 'main',
218+
branchReadOnly: false,
219+
branchColor: '#5296D6',
220+
initRepo: false,
221+
keyGeneratorType: 'ed25519',
222+
});
190223

191224
const gitService = new SourceControlGitService(mock(), mock(), mockPreferencesService);
192225

packages/cli/src/environments.ee/source-control/__tests__/source-control-https.service.ee.test.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import { SourceControlGitService } from '../source-control-git.service.ee';
44
import type { SourceControlPreferencesService } from '../source-control-preferences.service.ee';
55
import type { SourceControlPreferences } from '../types/source-control-preferences';
66

7-
// Mock simple-git
87
const mockSimpleGit = {
9-
env: jest.fn().mockReturnThis(),
8+
env: jest.fn(),
109
init: jest.fn().mockResolvedValue(undefined),
1110
addRemote: jest.fn().mockResolvedValue(undefined),
1211
getRemotes: jest.fn().mockResolvedValue([]),
1312
};
1413

14+
mockSimpleGit.env.mockReturnValue(mockSimpleGit);
15+
1516
jest.mock('simple-git', () => ({
16-
simpleGit: jest.fn().mockImplementation(() => mockSimpleGit),
17+
simpleGit: jest.fn().mockReturnValue(mockSimpleGit),
1718
}));
1819

1920
describe('SourceControlGitService - HTTPS functionality', () => {
@@ -37,34 +38,34 @@ describe('SourceControlGitService - HTTPS functionality', () => {
3738

3839
jest.spyOn(sourceControlPreferencesService, 'getPreferences').mockReturnValue(mockPreferences);
3940
jest.clearAllMocks();
41+
42+
mockSimpleGit.env.mockReturnValue(mockSimpleGit);
4043
});
4144

4245
afterEach(() => {
43-
jest.resetAllMocks();
46+
mockSimpleGit.env.mockClear();
47+
mockSimpleGit.init.mockClear();
48+
mockSimpleGit.addRemote.mockClear();
49+
mockSimpleGit.getRemotes.mockClear();
4450
});
4551

4652
describe('setGitSshCommand', () => {
4753
it('should configure git for HTTPS without SSH command', async () => {
48-
// Act
4954
await sourceControlGitService.setGitSshCommand();
5055

51-
// Assert
5256
expect(mockSimpleGit.env).toHaveBeenCalledWith('GIT_TERMINAL_PROMPT', '0');
5357
expect(mockSimpleGit.env).not.toHaveBeenCalledWith('GIT_SSH_COMMAND', expect.any(String));
5458
});
5559

5660
it('should configure git for SSH with SSH command when connectionType is ssh', async () => {
57-
// Arrange
5861
const sshPreferences = { ...mockPreferences, connectionType: 'ssh' as const };
5962
jest.spyOn(sourceControlPreferencesService, 'getPreferences').mockReturnValue(sshPreferences);
6063
jest
6164
.spyOn(sourceControlPreferencesService, 'getPrivateKeyPath')
6265
.mockResolvedValue('/path/to/key');
6366

64-
// Act
6567
await sourceControlGitService.setGitSshCommand('/git/folder', '/ssh/folder');
6668

67-
// Assert
6869
expect(sourceControlPreferencesService.getPrivateKeyPath).toHaveBeenCalled();
6970
expect(mockSimpleGit.env).toHaveBeenCalledWith('GIT_TERMINAL_PROMPT', '0');
7071
expect(mockSimpleGit.env).toHaveBeenCalledWith(
@@ -76,11 +77,9 @@ describe('SourceControlGitService - HTTPS functionality', () => {
7677

7778
describe('URL normalization logic', () => {
7879
it('should normalize HTTPS URLs for comparison', () => {
79-
// Arrange
8080
const remoteWithCredentials = 'https://user:token@github.com/user/repo.git';
8181
const inputWithoutCredentials = 'https://github.com/user/repo.git';
8282

83-
// Act - simulate the URL normalization logic from hasRemoteConfigured
8483
const normalizeUrl = (url: string) => {
8584
try {
8685
const urlObj = new URL(url);
@@ -95,15 +94,12 @@ describe('SourceControlGitService - HTTPS functionality', () => {
9594
const normalizedRemote = normalizeUrl(remoteWithCredentials);
9695
const normalizedInput = normalizeUrl(inputWithoutCredentials);
9796

98-
// Assert
9997
expect(normalizedRemote).toBe(normalizedInput);
10098
});
10199

102100
it('should handle malformed URLs gracefully', () => {
103-
// Arrange
104101
const malformedUrl = 'not-a-valid-url';
105102

106-
// Act
107103
const normalizeUrl = (url: string) => {
108104
try {
109105
const urlObj = new URL(url);
@@ -117,28 +113,24 @@ describe('SourceControlGitService - HTTPS functionality', () => {
117113

118114
const result = normalizeUrl(malformedUrl);
119115

120-
// Assert
121116
expect(result).toBe(malformedUrl); // Should return original when URL parsing fails
122117
});
123118
});
124119

125120
describe('URL encoding in repository initialization', () => {
126121
it('should properly encode credentials with special characters', () => {
127-
// Arrange
128122
const mockCredentials = {
129123
username: 'user@domain.com',
130124
password: 'p@ssw0rd!',
131125
};
132126

133127
const baseUrl = 'https://github.com/user/repo.git';
134128

135-
// Act - simulate the URL encoding logic from initRepository
136129
const urlObj = new URL(baseUrl);
137130
urlObj.username = encodeURIComponent(mockCredentials.username);
138131
urlObj.password = encodeURIComponent(mockCredentials.password);
139132
const encodedUrl = urlObj.toString();
140133

141-
// Assert - URL constructor automatically encodes some characters but not all
142134
expect(encodedUrl).toContain('user%40domain.com');
143135
expect(encodedUrl).toContain('p%40ssw0rd');
144136
expect(encodedUrl).toMatch(
@@ -155,24 +147,20 @@ describe('SourceControlGitService - HTTPS functionality', () => {
155147

156148
const baseUrl = 'https://github.com/user/repo.git';
157149

158-
// Act
159150
const urlObj = new URL(baseUrl);
160151
urlObj.username = encodeURIComponent(mockCredentials.username);
161152
urlObj.password = encodeURIComponent(mockCredentials.password);
162153
const encodedUrl = urlObj.toString();
163154

164-
// Assert
165155
expect(encodedUrl).toBe('https://testuser:testtoken123@github.com/user/repo.git');
166156
});
167157
});
168158

169159
describe('Connection type handling', () => {
170160
it('should differentiate between SSH and HTTPS configuration', () => {
171-
// Arrange
172161
const httpsPrefs = { connectionType: 'https' as const };
173162
const sshPrefs = { connectionType: 'ssh' as const };
174163

175-
// Act & Assert
176164
expect(httpsPrefs.connectionType).toBe('https');
177165
expect(sshPrefs.connectionType).toBe('ssh');
178166
expect(httpsPrefs.connectionType !== sshPrefs.connectionType).toBe(true);

0 commit comments

Comments
 (0)