-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathfolderRepositoryManager.test.ts
More file actions
130 lines (106 loc) · 5.66 KB
/
folderRepositoryManager.test.ts
File metadata and controls
130 lines (106 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import { createSandbox, SinonSandbox } from 'sinon';
import { FolderRepositoryManager, titleAndBodyFrom } from '../../github/folderRepositoryManager';
import { MockRepository } from '../mocks/mockRepository';
import { MockTelemetry } from '../mocks/mockTelemetry';
import { MockCommandRegistry } from '../mocks/mockCommandRegistry';
import { PullRequestModel } from '../../github/pullRequestModel';
import { GitHubRemote, Remote } from '../../common/remote';
import { Protocol } from '../../common/protocol';
import { GitHubRepository } from '../../github/githubRepository';
import { PullRequestBuilder } from '../builders/rest/pullRequestBuilder';
import { convertRESTPullRequestToRawPullRequest } from '../../github/utils';
import { GitApiImpl } from '../../api/api1';
import { CredentialStore } from '../../github/credentials';
import { MockExtensionContext } from '../mocks/mockExtensionContext';
import { Uri } from 'vscode';
import { GitHubServerType } from '../../common/authentication';
import { CreatePullRequestHelper } from '../../view/createPullRequestHelper';
import { RepositoriesManager } from '../../github/repositoriesManager';
import { MockThemeWatcher } from '../mocks/mockThemeWatcher';
describe('PullRequestManager', function () {
let sinon: SinonSandbox;
let manager: FolderRepositoryManager;
let telemetry: MockTelemetry;
let mockThemeWatcher: MockThemeWatcher;
let mockRepository: MockRepository;
beforeEach(function () {
sinon = createSandbox();
MockCommandRegistry.install(sinon);
telemetry = new MockTelemetry();
mockThemeWatcher = new MockThemeWatcher();
mockRepository = new MockRepository();
const context = new MockExtensionContext();
const credentialStore = new CredentialStore(telemetry, context);
const repositoriesManager = new RepositoriesManager(credentialStore, telemetry);
manager = new FolderRepositoryManager(0, context, mockRepository, telemetry, new GitApiImpl(repositoriesManager), credentialStore, new CreatePullRequestHelper(), mockThemeWatcher);
});
afterEach(function () {
sinon.restore();
});
describe('activePullRequest', function () {
it('gets and sets the active pull request', function () {
assert.strictEqual(manager.activePullRequest, undefined);
const changeFired = sinon.spy();
manager.onDidChangeActivePullRequest(changeFired);
const url = 'https://github.com/aaa/bbb.git';
const protocol = new Protocol(url);
const remote = new GitHubRemote('origin', url, protocol, GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
const repository = new GitHubRepository(1, remote, rootUri, manager.credentialStore, telemetry);
const prItem = convertRESTPullRequestToRawPullRequest(new PullRequestBuilder().build(), repository);
const pr = new PullRequestModel(manager.credentialStore, telemetry, repository, remote, prItem);
manager.activePullRequest = pr;
assert(changeFired.called);
assert.deepStrictEqual(manager.activePullRequest, pr);
});
});
describe('getOrigin', function () {
it('falls back to configured GitHub remote when upstream is not GitHub', async function () {
// Setup: Add a GitHub remote
const githubUrl = 'https://github.com/test/repo.git';
await mockRepository.addRemote('github', githubUrl);
const githubProtocol = new Protocol(githubUrl);
const githubRemote = new GitHubRemote('github', githubUrl, githubProtocol, GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('/test/repo');
const githubRepository = new GitHubRepository(1, githubRemote, rootUri, manager.credentialStore, telemetry);
// Manually set up the GitHub repository in the manager (simulating successful initialization)
(manager as any)._githubRepositories = [githubRepository];
// Add a non-GitHub remote (simulating the scenario from the bug)
await mockRepository.addRemote('origin', 'https://example.com/git/repo.git');
// Create a branch with upstream set to non-GitHub remote
await mockRepository.createBranch('test-branch', true);
await mockRepository.setBranchUpstream('test-branch', 'refs/remotes/origin/main');
// Mock getAllGitHubRemotes to return only the github remote
sinon.stub(manager, 'getAllGitHubRemotes').resolves([githubRemote]);
// Act: getOrigin should fall back to the configured GitHub remote instead of throwing
const result = await manager.getOrigin();
// Assert: Should return the GitHub repository, not throw an error
assert.strictEqual(result, githubRepository);
});
});
});
describe('titleAndBodyFrom', function () {
it('separates title and body', async function () {
const message = Promise.resolve('title\n\ndescription 1\n\ndescription 2\n');
const result = await titleAndBodyFrom(message);
assert.strictEqual(result?.title, 'title');
assert.strictEqual(result?.body, 'description 1\n\ndescription 2');
});
it('returns only title with no body', async function () {
const message = Promise.resolve('title');
const result = await titleAndBodyFrom(message);
assert.strictEqual(result?.title, 'title');
assert.strictEqual(result?.body, '');
});
it('returns only title when body contains only whitespace', async function () {
const message = Promise.resolve('title\n\n');
const result = await titleAndBodyFrom(message);
assert.strictEqual(result?.title, 'title');
assert.strictEqual(result?.body, '');
});
});