|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { default as assert } from 'assert'; |
| 7 | +import { createSandbox, SinonSandbox } from 'sinon'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import { RemoteOnlyRepository } from '../../../api/remoteOnlyRepository'; |
| 10 | +import { GitHubServerType } from '../../../common/authentication'; |
| 11 | +import { Protocol } from '../../../common/protocol'; |
| 12 | +import { GitHubRemote } from '../../../common/remote'; |
| 13 | +import { EXTENSION_ID } from '../../../constants'; |
| 14 | +import { CredentialStore } from '../../../github/credentials'; |
| 15 | +import { FolderRepositoryManager } from '../../../github/folderRepositoryManager'; |
| 16 | +import { FolderRepositoryManagerResolver } from '../../../github/folderRepositoryManagerResolver'; |
| 17 | +import { GitHubRepository } from '../../../github/githubRepository'; |
| 18 | +import { PullRequestModel } from '../../../github/pullRequestModel'; |
| 19 | +import { PullRequestOverviewPanel } from '../../../github/pullRequestOverview'; |
| 20 | +import { RepositoriesManager } from '../../../github/repositoriesManager'; |
| 21 | +import { convertRESTPullRequestToRawPullRequest } from '../../../github/utils'; |
| 22 | +import { OpenPullRequestWebviewTool } from '../../../lm/tools/openPullRequestWebviewTool'; |
| 23 | +import { PullRequestBuilder } from '../../builders/rest/pullRequestBuilder'; |
| 24 | +import { MockExtensionContext } from '../../mocks/mockExtensionContext'; |
| 25 | +import { MockTelemetry } from '../../mocks/mockTelemetry'; |
| 26 | + |
| 27 | +const EXTENSION_URI = vscode.extensions.getExtension(EXTENSION_ID)!.extensionUri; |
| 28 | + |
| 29 | +describe('OpenPullRequestWebviewTool', () => { |
| 30 | + let sandbox: SinonSandbox; |
| 31 | + let context: MockExtensionContext; |
| 32 | + let telemetry: MockTelemetry; |
| 33 | + let credentialStore: CredentialStore; |
| 34 | + let tool: OpenPullRequestWebviewTool; |
| 35 | + let cancellation: vscode.CancellationTokenSource; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + sandbox = createSandbox(); |
| 39 | + context = new MockExtensionContext(); |
| 40 | + telemetry = new MockTelemetry(); |
| 41 | + credentialStore = new CredentialStore(telemetry, context); |
| 42 | + sandbox.stub(credentialStore, 'isAnyAuthenticated').returns(true); |
| 43 | + // No repository is open, as can be the case in the Agents window. |
| 44 | + const repositoriesManager = new RepositoriesManager(credentialStore, telemetry); |
| 45 | + const resolver = new FolderRepositoryManagerResolver(context, repositoriesManager, telemetry); |
| 46 | + cancellation = new vscode.CancellationTokenSource(); |
| 47 | + context.subscriptions.push(credentialStore, repositoriesManager, resolver, cancellation); |
| 48 | + tool = new OpenPullRequestWebviewTool(credentialStore, repositoriesManager, resolver, telemetry, EXTENSION_URI); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + PullRequestOverviewPanel.findPanel('aaa', 'bbb', 1000)?.dispose(); |
| 53 | + context.dispose(); |
| 54 | + sandbox.restore(); |
| 55 | + }); |
| 56 | + |
| 57 | + function invoke(): Promise<vscode.LanguageModelToolResult> { |
| 58 | + return tool.invoke({ input: { pullRequestNumber: 1000, repo: { owner: 'aaa', name: 'bbb' } }, toolInvocationToken: undefined }, cancellation.token); |
| 59 | + } |
| 60 | + |
| 61 | + it('opens a pull request of a repository that is not open', async () => { |
| 62 | + const url = 'https://github.com/aaa/bbb'; |
| 63 | + const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom); |
| 64 | + const githubRepository = new GitHubRepository(1, remote, vscode.Uri.file('/aaa/bbb'), credentialStore, telemetry, true); |
| 65 | + const item = convertRESTPullRequestToRawPullRequest(new PullRequestBuilder().number(1000).build(), githubRepository); |
| 66 | + const pullRequest = new PullRequestModel(credentialStore, telemetry, githubRepository, remote, item); |
| 67 | + context.subscriptions.push(githubRepository, pullRequest); |
| 68 | + const resolvePullRequest = sandbox.stub(FolderRepositoryManager.prototype, 'resolvePullRequest').resolves(pullRequest); |
| 69 | + const createOrShow = sandbox.stub(PullRequestOverviewPanel, 'createOrShow').callsFake(async (_telemetry, _extensionUri, _folderManager, _identity, model) => { |
| 70 | + await model; |
| 71 | + }); |
| 72 | + |
| 73 | + const result = await invoke(); |
| 74 | + |
| 75 | + sandbox.assert.calledOnce(createOrShow); |
| 76 | + const [, extensionUri, folderManager, identity, model] = createOrShow.firstCall.args; |
| 77 | + assert.strictEqual(extensionUri, EXTENSION_URI); |
| 78 | + assert.ok(folderManager.repository instanceof RemoteOnlyRepository); |
| 79 | + assert.deepStrictEqual(identity, { owner: 'aaa', repo: 'bbb', number: 1000 }); |
| 80 | + assert.strictEqual(await model, pullRequest); |
| 81 | + assert.deepStrictEqual(resolvePullRequest.firstCall.args, ['aaa', 'bbb', 1000, false, 'overview']); |
| 82 | + assert.ok(result.content[0] instanceof vscode.LanguageModelTextPart); |
| 83 | + assert.ok(result.content[0].value.includes(`aaa/bbb#1000 "${pullRequest.title}" (${pullRequest.html_url})`)); |
| 84 | + }); |
| 85 | + |
| 86 | + it('reports a pull request that cannot be found and closes its webview', async () => { |
| 87 | + sandbox.stub(FolderRepositoryManager.prototype, 'resolvePullRequest').resolves(undefined); |
| 88 | + // The webview's preview cannot be loaded either. |
| 89 | + sandbox.stub(FolderRepositoryManager.prototype, 'createGitHubRepositoryFromOwnerName').resolves(undefined); |
| 90 | + const createWebviewPanel = sandbox.spy(vscode.window, 'createWebviewPanel'); |
| 91 | + |
| 92 | + await assert.rejects(invoke(), /No pull request found for aaa\/bbb#1000/); |
| 93 | + |
| 94 | + sandbox.assert.calledOnce(createWebviewPanel); |
| 95 | + assert.strictEqual(PullRequestOverviewPanel.findPanel('aaa', 'bbb', 1000), undefined); |
| 96 | + }); |
| 97 | + |
| 98 | + it('is available in the agents window', () => { |
| 99 | + const extension = vscode.extensions.getExtension(EXTENSION_ID); |
| 100 | + assert.ok(extension); |
| 101 | + const contribution = extension.packageJSON.contributes.languageModelTools.find( |
| 102 | + contributed => contributed.name === OpenPullRequestWebviewTool.toolId, |
| 103 | + ); |
| 104 | + |
| 105 | + assert.strictEqual(contribution.when, 'config.githubPullRequests.experimental.chat'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments