<commit_before>﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.ViewModels.Dialog.Clone;
using NSubstitute;
using NUnit.Framework;
using Rothko;

namespace GitHub.App.UnitTests.ViewModels.Dialog.Clone
{
    public class RepositoryCloneViewModelTests
    {
        [Test]
        public async Task GitHubPage_Is_Initialized()
        {
            var cm = CreateConnectionManager("https://github.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(null);

            target.GitHubTab.Received(1).Initialize(cm.Connections[0]);
            target.EnterpriseTab.DidNotReceiveWithAnyArgs().Initialize(null);
        }

        [TestCase("https://github.com", false, 0)]
        [TestCase("https://enterprise.com", false, 1)]
        [TestCase("https://github.com", true, 2, Description = "Show URL tab for GitHub connections")]
        [TestCase("https://enterprise.com", true, 2, Description = "Show URL tab for Enterprise connections")]
        public async Task Default_SelectedTabIndex_For_Group(string address, bool isGroupA, int expectTabIndex)
        {
            var cm = CreateConnectionManager(address);
            var connection = cm.Connections[0];
            var usageService = CreateUsageService(isGroupA);
            var target = CreateTarget(connectionManager: cm, usageService: usageService);

            await target.InitializeAsync(connection);

            Assert.That(target.SelectedTabIndex, Is.EqualTo(expectTabIndex));
        }

        [TestCase("https://github.com", false, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewGitHubTab))]
        [TestCase("https://enterprise.com", false, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewEnterpriseTab))]
        [TestCase("https://github.com", true, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewUrlTab))]
        [TestCase("https://enterprise.com", true, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewUrlTab))]
        public async Task IncrementCounter_Showing_Default_Tab(string address, bool isGroupA, int numberOfCalls, string counterName)
        {
            var cm = CreateConnectionManager(address);
            var connection = cm.Connections[0];
            var usageService = CreateUsageService(isGroupA);
            var usageTracker = Substitute.For<IUsageTracker>();
            var target = CreateTarget(connectionManager: cm, usageService: usageService, usageTracker: usageTracker);
            usageTracker.IncrementCounter(null).ReturnsForAnyArgs(Task.CompletedTask);

            await target.InitializeAsync(connection).ConfigureAwait(false);

            await usageTracker.Received(numberOfCalls).IncrementCounter(
                Arg.Is<Expression<Func<UsageModel.MeasuresModel, int>>>(x =>
                    ((MemberExpression)x.Body).Member.Name == counterName));
        }

        [Test]
        public async Task EnterprisePage_Is_Initialized()
        {
            var cm = CreateConnectionManager("https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(null);

            target.GitHubTab.DidNotReceiveWithAnyArgs().Initialize(null);
            target.EnterpriseTab.Received(1).Initialize(cm.Connections[0]);
        }

        [Test]
        public async Task GitHub_And_Enterprise_Pages_Are_Initialized()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(null);

            target.GitHubTab.Received(1).Initialize(cm.Connections[0]);
            target.EnterpriseTab.Received(1).Initialize(cm.Connections[1]);
        }

        [Test]
        public async Task GitHubPage_Is_Loaded()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[0]);

            await target.GitHubTab.Received(1).Activate();
            await target.EnterpriseTab.DidNotReceive().Activate();
        }

        [Test]
        public async Task Enterprise_Is_Loaded()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[1]);

            await target.GitHubTab.DidNotReceive().Activate();
            await target.EnterpriseTab.Received(1).Activate();
        }

        [Test]
        public async Task Switching_To_GitHubPage_Loads_It()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[1]);
            await target.GitHubTab.DidNotReceive().Activate();

            target.SelectedTabIndex = 0;

            await target.GitHubTab.Received(1).Activate();
        }

        [Test]
        public async Task Switching_To_EnterprisePage_Loads_It()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[0]);
            await target.EnterpriseTab.DidNotReceive().Activate();

            target.SelectedTabIndex = 1;

            await target.EnterpriseTab.Received(1).Activate();
        }

        [Test]
        public async Task Path_Is_Initialized()
        {
            var target = CreateTarget();

            Assert.That(target.Path, Is.EqualTo("d:\\efault\\path"));
        }

        [Test]
        public async Task Owner_And_Repository_Name_Is_Appended_To_Base_Path()
        {
            var target = CreateTarget();

            SetRepository(target.GitHubTab, CreateRepositoryModel("owner", "repo"));

            Assert.That(target.Path, Is.EqualTo("d:\\efault\\path\\owner\\repo"));
        }

        [Test]
        public async Task PathError_Is_Not_Set_When_No_Repository_Selected()
        {
            var target = CreateTarget();

            target.Path = "d:\\exists";

            Assert.That(target.PathError, Is.Null);
        }

        [Test]
        public async Task PathError_Is_Set_For_Existing_File_At_Destination()
        {
            var target = CreateTarget();
            SetRepository(target.GitHubTab, CreateRepositoryModel("owner", "repo"));
            target.Path = "d:\\exists";

            Assert.That(target.PathError, Is.EqualTo(Resources.DestinationAlreadyExists));
        }<commit_msg>Fixing test<commit_after>﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.ViewModels.Dialog.Clone;
using NSubstitute;
using NUnit.Framework;
using Rothko;

namespace GitHub.App.UnitTests.ViewModels.Dialog.Clone
{
    public class RepositoryCloneViewModelTests
    {
        [Test]
        public async Task GitHubPage_Is_Initialized()
        {
            var cm = CreateConnectionManager("https://github.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(null);

            target.GitHubTab.Received(1).Initialize(cm.Connections[0]);
            target.EnterpriseTab.DidNotReceiveWithAnyArgs().Initialize(null);
        }

        [TestCase("https://github.com", false, 0)]
        [TestCase("https://enterprise.com", false, 1)]
        [TestCase("https://github.com", true, 2, Description = "Show URL tab for GitHub connections")]
        [TestCase("https://enterprise.com", true, 2, Description = "Show URL tab for Enterprise connections")]
        public async Task Default_SelectedTabIndex_For_Group(string address, bool isGroupA, int expectTabIndex)
        {
            var cm = CreateConnectionManager(address);
            var connection = cm.Connections[0];
            var usageService = CreateUsageService(isGroupA);
            var target = CreateTarget(connectionManager: cm, usageService: usageService);

            await target.InitializeAsync(connection);

            Assert.That(target.SelectedTabIndex, Is.EqualTo(expectTabIndex));
        }

        [TestCase("https://github.com", false, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewGitHubTab))]
        [TestCase("https://enterprise.com", false, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewEnterpriseTab))]
        [TestCase("https://github.com", true, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewUrlTab))]
        [TestCase("https://enterprise.com", true, 1, nameof(UsageModel.MeasuresModel.NumberOfCloneViewUrlTab))]
        public async Task IncrementCounter_Showing_Default_Tab(string address, bool isGroupA, int numberOfCalls, string counterName)
        {
            var cm = CreateConnectionManager(address);
            var connection = cm.Connections[0];
            var usageService = CreateUsageService(isGroupA);
            var usageTracker = Substitute.For<IUsageTracker>();
            var target = CreateTarget(connectionManager: cm, usageService: usageService, usageTracker: usageTracker);
            usageTracker.IncrementCounter(null).ReturnsForAnyArgs(Task.CompletedTask);

            await target.InitializeAsync(connection).ConfigureAwait(false);

            await usageTracker.Received(numberOfCalls).IncrementCounter(
                Arg.Is<Expression<Func<UsageModel.MeasuresModel, int>>>(x =>
                    ((MemberExpression)x.Body).Member.Name == counterName));
        }

        [Test]
        public async Task EnterprisePage_Is_Initialized()
        {
            var cm = CreateConnectionManager("https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(null);

            target.GitHubTab.DidNotReceiveWithAnyArgs().Initialize(null);
            target.EnterpriseTab.Received(1).Initialize(cm.Connections[0]);
        }

        [Test]
        public async Task GitHub_And_Enterprise_Pages_Are_Initialized()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(null);

            target.GitHubTab.Received(1).Initialize(cm.Connections[0]);
            target.EnterpriseTab.Received(1).Initialize(cm.Connections[1]);
        }

        [Test]
        public async Task GitHubPage_Is_Loaded()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[0]);

            await target.GitHubTab.Received(1).Activate();
            await target.EnterpriseTab.DidNotReceive().Activate();
        }

        [Test]
        public async Task Enterprise_Is_Loaded()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[1]);

            await target.GitHubTab.DidNotReceive().Activate();
            await target.EnterpriseTab.Received(1).Activate();
        }

        [Test]
        public async Task Switching_To_GitHubPage_Loads_It()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[1]);
            await target.GitHubTab.DidNotReceive().Activate();

            target.SelectedTabIndex = 0;

            await target.GitHubTab.Received(1).Activate();
        }

        [Test]
        public async Task Switching_To_EnterprisePage_Loads_It()
        {
            var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
            var target = CreateTarget(connectionManager: cm);

            await target.InitializeAsync(cm.Connections[0]);
            await target.EnterpriseTab.DidNotReceive().Activate();

            target.SelectedTabIndex = 1;

            await target.EnterpriseTab.Received(1).Activate();
        }

        [Test]
        public async Task Path_Is_Initialized()
        {
            var target = CreateTarget();

            Assert.That(target.Path, Is.EqualTo("d:\\efault\\path"));
        }

        [Test]
        public async Task Owner_And_Repository_Name_Is_Appended_To_Base_Path()
        {
            var target = CreateTarget();

            SetRepository(target.GitHubTab, CreateRepositoryModel("owner", "repo"));

            Assert.That(target.Path, Is.EqualTo("d:\\efault\\path\\owner\\repo"));
        }

        [Test]
        public async Task PathError_Is_Not_Set_When_No_Repository_Selected()
        {
            var target = CreateTarget();

            target.Path = "d:\\exists";

            Assert.That(target.PathWarning, Is.Null);
        }

        [Test]
        public async Task PathError_Is_Set_For_Existing_File_At_Destination()
        {
            var target = CreateTarget();
            SetRepository(target.GitHubTab, CreateRepositoryModel("owner", "repo"));
            target.Path = "d:\\exists";

            Assert.That(target.PathWarning, Is.EqualTo(Resources.DestinationAlreadyExists));
        }
{'fraction_non_alphanumeric': 0.11064688085290304, 'fraction_numerical': 0.004034000864428756, 'mean_word_length': 2.678590355060943, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 40, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4336411', 'n_tokens_mistral': 3683, 'n_tokens_neox': 3633, 'n_words': 645}