Path: blob/main/src/vs/workbench/contrib/chat/test/electron-browser/pluginGitCommandService.test.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { CancellationTokenSource } from '../../../../../base/common/cancellation.js';7import { URI } from '../../../../../base/common/uri.js';8import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';9import { ILocalGitService } from '../../../../../platform/git/common/localGitService.js';10import { NativePluginGitCommandService } from '../../electron-browser/pluginGitCommandService.js';1112suite('NativePluginGitCommandService', () => {13const store = ensureNoDisposablesAreLeakedInTestSuite();1415function createLocalGitStub(overrides?: Partial<ILocalGitService>): ILocalGitService {16return {17_serviceBrand: undefined,18clone: async () => { },19pull: async () => false,20checkout: async () => { },21revParse: async () => '',22fetch: async () => { },23revListCount: async () => 0,24cancel: async () => { },25...overrides,26} as ILocalGitService;27}2829test('cloneRepository delegates to ILocalGitService', async () => {30const calls: string[] = [];31const service = new NativePluginGitCommandService(createLocalGitStub({32clone: async (_operationId, url, path, ref) => { calls.push(`clone:${url}:${path}:${ref}`); },33}));3435const targetDir = URI.file('/tmp/repo');36await service.cloneRepository('https://github.com/test/repo.git', targetDir, 'main');37assert.deepStrictEqual(calls, [`clone:https://github.com/test/repo.git:${targetDir.fsPath}:main`]);38});3940test('pull delegates to ILocalGitService and returns result', async () => {41const service = new NativePluginGitCommandService(createLocalGitStub({42pull: async () => true,43}));4445const result = await service.pull(URI.file('/tmp/repo'));46assert.strictEqual(result, true);47});4849test('checkout delegates to ILocalGitService with detached flag', async () => {50const calls: string[] = [];51const service = new NativePluginGitCommandService(createLocalGitStub({52checkout: async (_operationId, _path, treeish, detached) => { calls.push(`checkout:${treeish}:${detached}`); },53}));5455await service.checkout(URI.file('/tmp/repo'), 'abc123', true);56assert.deepStrictEqual(calls, ['checkout:abc123:true']);57});5859test('revParse delegates to ILocalGitService', async () => {60const service = new NativePluginGitCommandService(createLocalGitStub({61revParse: async () => 'abc123',62}));6364const result = await service.revParse(URI.file('/tmp/repo'), 'HEAD');65assert.strictEqual(result, 'abc123');66});6768test('fetch delegates to ILocalGitService', async () => {69const calls: string[] = [];70const service = new NativePluginGitCommandService(createLocalGitStub({71fetch: async (_operationId, path) => { calls.push(`fetch:${path}`); },72}));7374const repoDir = URI.file('/tmp/repo');75await service.fetch(repoDir);76assert.deepStrictEqual(calls, [`fetch:${repoDir.fsPath}`]);77});7879test('fetchRepository delegates to ILocalGitService.fetch', async () => {80const calls: string[] = [];81const service = new NativePluginGitCommandService(createLocalGitStub({82fetch: async (_operationId, path) => { calls.push(`fetch:${path}`); },83}));8485const repoDir = URI.file('/tmp/repo');86await service.fetchRepository(repoDir);87assert.deepStrictEqual(calls, [`fetch:${repoDir.fsPath}`]);88});8990test('revListCount delegates to ILocalGitService', async () => {91const service = new NativePluginGitCommandService(createLocalGitStub({92revListCount: async () => 5,93}));9495const result = await service.revListCount(URI.file('/tmp/repo'), 'HEAD', '@{u}');96assert.strictEqual(result, 5);97});9899test('cancellation token triggers cancel on local git service', async () => {100const cts = store.add(new CancellationTokenSource());101const cancelledIds: string[] = [];102let cloneResolve: (() => void) | undefined;103const service = new NativePluginGitCommandService(createLocalGitStub({104clone: () => new Promise(resolve => { cloneResolve = resolve; }),105cancel: async (id) => { cancelledIds.push(id); },106}));107108const p = service.cloneRepository('https://github.com/test/repo.git', URI.file('/tmp/repo'), undefined, cts.token);109cts.cancel();110assert.strictEqual(cancelledIds.length, 1);111cloneResolve!();112await p;113});114});115116117