<commit_before>﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using LibGit2Sharp;
using NSubstitute;
using Octokit;
using Rothko;
using UnitTests;
using Xunit;

public class PullRequestServiceTests : TestBaseClass
{
    public class TheExtractDiffFilesMethod
    {
        [Fact]
        public async Task NotCheckedOut_ExtractFilesFromLocalRepo()
        {
            var serviceProvider = Substitutes.ServiceProvider;
            var gitClient = MockGitClient();
            var service = new PullRequestService(gitClient, serviceProvider.GetGitService(), serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());

            var baseFile = "baseFile";
            var headFile = "headFile";
            var fileName = "fileName";
            var baseSha = "baseSha";
            var headSha = "headSha";
            var baseRef = new GitReferenceModel("ref", "label", baseSha, "uri");
            var headRef = new GitReferenceModel("ref", "label", headSha, "uri");
            var headFileSha = "???";
            var checkedOut = false;

            var repo = Substitute.For<ILocalRepositoryModel>();
            var modelService = Substitute.For<IModelService>();
            var pullRequest = Substitute.For<IPullRequestModel>();
            pullRequest.Base.Returns(baseRef);
            pullRequest.Head.Returns(headRef);

            gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(Task.FromResult(baseFile));
            gitClient.ExtractFile(Arg.Any<IRepository>(), headSha, fileName).Returns(Task.FromResult(headFile));

            var files = await service.ExtractDiffFiles(repo, modelService, pullRequest, fileName, headFileSha, checkedOut);

            Assert.Equal(baseFile, files.Item1);
            Assert.Equal(headFile, files.Item2);
        }

        [Fact]
        public async Task CheckedOut_BaseFromWorkingFile()
        {
            var serviceProvider = Substitutes.ServiceProvider;
            var gitClient = MockGitClient();
            var service = new PullRequestService(gitClient, serviceProvider.GetGitService(), serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());

            var repoDir = "repoDir";
            var baseFile = "baseFile";
            var fileName = "fileName";
            var baseSha = "baseSha";
            var baseRef = new GitReferenceModel("ref", "label", baseSha, "uri");
            var headFileSha = "headFileSha";
            var checkedOut = true;
            var workingFile = Path.Combine(repoDir, fileName);

            var repo = Substitute.For<ILocalRepositoryModel>();
            repo.LocalPath.Returns(repoDir);
            var modelService = Substitute.For<IModelService>();
            var pullRequest = Substitute.For<IPullRequestModel>();
            pullRequest.Base.Returns(baseRef);

            gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(Task.FromResult(baseFile));

            var files = await service.ExtractDiffFiles(repo, modelService, pullRequest, fileName, headFileSha, checkedOut);

            Assert.Equal(baseFile, files.Item1);
            Assert.Equal(workingFile, files.Item2);
        }

        [Fact]
        public async Task HeadBranchNotAvailable_GetFileFromModelService()
        {
            var serviceProvider = Substitutes.ServiceProvider;
            var gitClient = MockGitClient();
            var service = new PullRequestService(gitClient, serviceProvider.GetGitService(), serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());

            var baseFile = "baseFile";
            var headFile = "headFile";
            var fileName = "fileName";
            var baseSha = "baseSha";
            var headSha = "headSha";
            var baseRef = new GitReferenceModel("ref", "label", baseSha, "uri");
            var headRef = new GitReferenceModel("ref", "label", headSha, "uri");
            var headFileSha = "headFileSha";
            var checkedOut = false;

            var repo = Substitute.For<ILocalRepositoryModel>();
            var modelService = Substitute.For<IModelService>();
            var pullRequest = Substitute.For<IPullRequestModel>();
            pullRequest.Base.Returns(baseRef);
            pullRequest.Head.Returns(headRef);

            gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(Task.FromResult(baseFile));
            gitClient.ExtractFile(Arg.Any<IRepository>(), headSha, fileName).Returns(Task.FromResult<string>(null));
            modelService.GetFileContents(Arg.Any<IRepositoryModel>(), headSha, fileName, headFileSha).Returns(Observable.Return(headFile));

            var files = await service.ExtractDiffFiles(repo, modelService, pullRequest, fileName, headFileSha, checkedOut);

            Assert.Equal(baseFile, files.Item1);
            Assert.Equal(headFile, files.Item2);
        }
    }<commit_msg>Update tests now `ExtractFile` returns content rather than a file path<commit_after>﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using LibGit2Sharp;
using NSubstitute;
using Octokit;
using Rothko;
using UnitTests;
using Xunit;

public class PullRequestServiceTests : TestBaseClass
{
    public class TheExtractDiffFilesMethod
    {
        [Fact]
        public async Task NotCheckedOut_ExtractFilesFromLocalRepo()
        {
            var serviceProvider = Substitutes.ServiceProvider;
            var gitClient = MockGitClient();
            var service = new PullRequestService(gitClient, serviceProvider.GetGitService(), serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());

            var baseFileContent = "baseFileContent";
            var headFileContent = "headFileContent";
            var fileName = "fileName";
            var baseSha = "baseSha";
            var headSha = "headSha";
            var baseRef = new GitReferenceModel("ref", "label", baseSha, "uri");
            var headRef = new GitReferenceModel("ref", "label", headSha, "uri");
            var headFileSha = "???";
            var checkedOut = false;

            var repo = Substitute.For<ILocalRepositoryModel>();
            var modelService = Substitute.For<IModelService>();
            var pullRequest = Substitute.For<IPullRequestModel>();
            pullRequest.Base.Returns(baseRef);
            pullRequest.Head.Returns(headRef);

            gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(Task.FromResult(baseFileContent));
            gitClient.ExtractFile(Arg.Any<IRepository>(), headSha, fileName).Returns(Task.FromResult(headFileContent));

            var files = await service.ExtractDiffFiles(repo, modelService, pullRequest, fileName, headFileSha, checkedOut);

            Assert.Equal(baseFileContent, File.ReadAllText(files.Item1));
            Assert.Equal(headFileContent, File.ReadAllText(files.Item2));
        }

        [Fact]
        public async Task CheckedOut_BaseFromWorkingFile()
        {
            var serviceProvider = Substitutes.ServiceProvider;
            var gitClient = MockGitClient();
            var service = new PullRequestService(gitClient, serviceProvider.GetGitService(), serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());

            var repoDir = "repoDir";
            var baseFileContent = "baseFileContent";
            var fileName = "fileName";
            var baseSha = "baseSha";
            var baseRef = new GitReferenceModel("ref", "label", baseSha, "uri");
            var headFileSha = "headFileSha";
            var checkedOut = true;
            var workingFile = Path.Combine(repoDir, fileName);

            var repo = Substitute.For<ILocalRepositoryModel>();
            repo.LocalPath.Returns(repoDir);
            var modelService = Substitute.For<IModelService>();
            var pullRequest = Substitute.For<IPullRequestModel>();
            pullRequest.Base.Returns(baseRef);

            gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(Task.FromResult(baseFileContent));

            var files = await service.ExtractDiffFiles(repo, modelService, pullRequest, fileName, headFileSha, checkedOut);

            Assert.Equal(baseFileContent, File.ReadAllText(files.Item1));
            Assert.Equal(workingFile, files.Item2);
        }

        [Fact]
        public async Task HeadBranchNotAvailable_GetFileFromModelService()
        {
            var serviceProvider = Substitutes.ServiceProvider;
            var gitClient = MockGitClient();
            var service = new PullRequestService(gitClient, serviceProvider.GetGitService(), serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());

            var baseFileContent = "baseFileContent";
            var headFileContent = "headFileContent";
            var fileName = "fileName";
            var baseSha = "baseSha";
            var headSha = "headSha";
            var baseRef = new GitReferenceModel("ref", "label", baseSha, "uri");
            var headRef = new GitReferenceModel("ref", "label", headSha, "uri");
            var headFileSha = "headFileSha";
            var checkedOut = false;

            var repo = Substitute.For<ILocalRepositoryModel>();
            var modelService = Substitute.For<IModelService>();
            var pullRequest = Substitute.For<IPullRequestModel>();
            pullRequest.Base.Returns(baseRef);
            pullRequest.Head.Returns(headRef);

            gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(Task.FromResult(baseFileContent));
            gitClient.ExtractFile(Arg.Any<IRepository>(), headSha, fileName).Returns(Task.FromResult<string>(null));
            modelService.GetFileContents(Arg.Any<IRepositoryModel>(), headSha, fileName, headFileSha).Returns(Observable.Return(headFileContent));

            var files = await service.ExtractDiffFiles(repo, modelService, pullRequest, fileName, headFileSha, checkedOut);

            Assert.Equal(baseFileContent, File.ReadAllText(files.Item1));
            Assert.Equal(headFileContent, File.ReadAllText(files.Item2));
        }
    }
{'fraction_non_alphanumeric': 0.1049164677804296, 'fraction_numerical': 0.0013365155131264917, 'mean_word_length': 2.914798206278027, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '13464', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2773, 'n_words': 574}