<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using SharpEssentials;
using SharpEssentials.Collections;

namespace TestCaseAutomator.TeamFoundation
{
	/// <summary>
	/// Enables searching and browsing Team Foundation Server objects.
	/// </summary>
	public class TfsExplorer : DisposableBase, ITfsExplorer
	{
        /// <summary>
        /// Initializes a new <see cref="TfsExplorer"/>.
        /// </summary>
        /// <param name="serverFactory">Enables access to a TFS server</param>
        /// <param name="scheduler">Used to schedule background tasks</param>
        public TfsExplorer(Func<Uri, ITfsServer> serverFactory,
                           TaskScheduler scheduler)
		{
		    _serverFactory = serverFactory;
            _scheduler = scheduler;
		}

	    /// <summary>
	    /// Connects to a TFS server..
	    /// </summary>
	    public async Task ConnectAsync(Uri serverUri)
        {
            Server?.Dispose();

	        var server = await Task.Factory.StartNew(() => 
                _serverFactory(serverUri), 
                    CancellationToken.None, TaskCreationOptions.None, _scheduler).ConfigureAwait(false);

	        Server = server;
        }

        /// <summary>
        /// The Team Foundation Server.
        /// </summary>
        public ITfsServer Server { get; private set; }

        /// <summary>
        /// Retrieves the test cases of a given project.
        /// </summary>
        /// <param name="projectName">The TFS project to access</param>
        /// <param name="testCaseSink">An optional progress sink handler for each test case</param>
        /// <returns>The test cases in the given project</returns>
        public Task<IEnumerable<ITestCase>> GetTestCasesAsync(string projectName, IProgress<ITestCase> testCaseSink)
		{
            ServerGuard();

            return Task.Factory.StartNew(() => 
                        Server.TestManagement
                              .GetTeamProject(projectName)
                              .TestCases.Query(@"SELECT * 
                                                 FROM WorkItems 
                                                 WHERE [System.TeamProject] = @project
                                                   AND [System.WorkItemType] = 'Test Case' 
                                                 ORDER BY [System.Id]")
                              .Tee(tc => testCaseSink?.Report(tc)),
                    CancellationToken.None, TaskCreationOptions.None, _scheduler);
		}

	    /// <summary>
	    /// Retrieves the Team Projects for the given connection.
	    /// </summary>
	    public Task<IEnumerable<ICatalogNode>> GetTeamProjectsAsync()
	    {
            ServerGuard();

            return Server.CatalogRoot
                         .QueryChildrenAsync(CatalogResourceTypes.TeamProject.ToEnumerable(),
	                                         false,
	                                         CatalogQueryOptions.None);
	    }

	    /// <summary>
		/// Provides access to Visual Studio solutions in source control.<commit_msg>Use work item store for querying test cases.<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using SharpEssentials;
using SharpEssentials.Collections;

namespace TestCaseAutomator.TeamFoundation
{
	/// <summary>
	/// Enables searching and browsing Team Foundation Server objects.
	/// </summary>
	public class TfsExplorer : DisposableBase, ITfsExplorer
	{
        /// <summary>
        /// Initializes a new <see cref="TfsExplorer"/>.
        /// </summary>
        /// <param name="serverFactory">Enables access to a TFS server</param>
        /// <param name="scheduler">Used to schedule background tasks</param>
        public TfsExplorer(Func<Uri, ITfsServer> serverFactory,
                           TaskScheduler scheduler)
		{
		    _serverFactory = serverFactory;
            _scheduler = scheduler;
		}

	    /// <summary>
	    /// Connects to a TFS server..
	    /// </summary>
	    public async Task ConnectAsync(Uri serverUri)
        {
            Server?.Dispose();

	        var server = await Task.Factory.StartNew(() => 
                _serverFactory(serverUri), 
                    CancellationToken.None, TaskCreationOptions.None, _scheduler).ConfigureAwait(false);

	        Server = server;
        }

        /// <summary>
        /// The Team Foundation Server.
        /// </summary>
        public ITfsServer Server { get; private set; }

        /// <summary>
        /// Retrieves the test cases of a given project.
        /// </summary>
        /// <param name="projectName">The TFS project to access</param>
        /// <param name="testCaseSink">An optional progress sink handler for each test case</param>
        /// <returns>The test cases in the given project</returns>
        public async Task<IEnumerable<ITestCase>> GetTestCasesAsync(string projectName, IProgress<ITestCase> testCaseSink)
		{
            ServerGuard();

            var testProject = Server.TestManagement.GetTeamProject(projectName);
            return (await Server.WorkItemStore
                                .QueryAsync(@"SELECT * 
                                              FROM WorkItems 
                                              WHERE [System.TeamProject] = @project
                                              AND [System.WorkItemType] = 'Test Case' 
                                              ORDER BY [System.Id]",
                                              new Dictionary<string, string> { { "project", projectName }})
                                .ConfigureAwait(false))
                                .Select(wi => testProject.CreateFromWorkItem(wi))
                                .OfType<ITestCase>()
                                .Tee(tc => testCaseSink?.Report(tc));
		}

	    /// <summary>
	    /// Retrieves the Team Projects for the given connection.
	    /// </summary>
	    public Task<IEnumerable<ICatalogNode>> GetTeamProjectsAsync()
	    {
            ServerGuard();

            return Server.CatalogRoot
                         .QueryChildrenAsync(CatalogResourceTypes.TeamProject.ToEnumerable(),
	                                         false,
	                                         CatalogQueryOptions.None);
	    }

	    /// <summary>
		/// Provides access to Visual Studio solutions in source control.
{'fraction_non_alphanumeric': 0.09816581071166544, 'fraction_numerical': 0.0, 'mean_word_length': 1.70583564906709, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3305838', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1567, 'n_words': 401}