<commit_before>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xunit;

namespace Nether.Web.IntegrationTests.Leaderboard
{
    //todo: come up with better naming
    public class LeaderboardTests : WebTestBase
    {
        private const string BaseUri = "/api/leaderboard";
        private HttpClient _client;

        public LeaderboardTests()
        {
            _client = GetClient();
        }

        [Fact]
        public async Task Get_leaderboard_call_succeeds()
        {
            HttpResponseMessage response = await _client.GetAsync(BaseUri + "/Default");
            Assert.True(response.IsSuccessStatusCode);
        }

        [Fact]
        public async Task Post_leaderboard_call_succeeds()
        {
            await PostScore(1);
        }

        [Fact]
        public async Task Posting_new_score_updates_default_leaderboard()
        {
            LeaderboardGetResponse leaderboardBefore = await GetLeaderboard();

            List<LeaderboardGetResponse.LeaderboardEntry> entries =
                leaderboardBefore.Entries.Where(e => e.Gamertag == _gamertag).ToList();

            //check that there is only one or less (if score wasn't posted yet) entry per user
            Assert.True(entries.Count <= 1);
            int oldScore = entries.Count == 0 ? 0 : entries.First().Score;

            //update the score posting a different (higher) result
            int newScore = oldScore + 1;
            await PostScore(newScore);

            //check that leaderboard has the updated score
            LeaderboardGetResponse leaderboardAfter = await GetLeaderboard();
            int newFreshScore = leaderboardAfter.Entries.Where(e => e.Gamertag == _gamertag).Select(e => e.Score).First();
            Assert.Equal(newFreshScore, newScore);
        }

        [Fact]
        public async Task Posting_similar_score_gets_around_me()
        {
            //note: radius is 2 at the moment, meaning you get 2 players above and 2 below (4 elements in response in general)

            //check there are at least 5 users
            LeaderboardGetResponse response = await GetLeaderboard();
            if (response.Entries.Length < 5)
            {
                throw new NotImplementedException();    //todo: post scores to get at least 5
            }

            //todo: delete score entries before testing, this requires a separate http method

            //put me somewhere in the middle and push the other user in the bottom so he is not around me
            await DeleteMyScores();
            await PostScore(int.MaxValue / 2);
            string myGamertag = _gamertag;
            _client = GetClient("testuser1");
            string hisGamertag = _gamertag;
            await DeleteMyScores();
            await PostScore(1);

            //check he is not around me
            _client = GetClient();
            response = await GetLeaderboard("5-AroundMe");
            Assert.False(response.Entries.Any(e => e.Gamertag == hisGamertag));

            //make his score similar to mine and check he is around me
            _client = GetClient("testuser1");
            await PostScore(int.MaxValue / 2 + 1);
            _client = GetClient();
            response = await GetLeaderboard("5-AroundMe");
            Assert.True(response.Entries.Any(e => e.Gamertag == hisGamertag));
        }

        [Fact]
        public async Task Limiting_top_scores_returns_limited_numer_of_rows()
        {
            LeaderboardGetResponse response = await GetLeaderboard("Top-5", HttpStatusCode.OK);

            Assert.True(response.Entries.Length <= 5);
        }

        [Fact]
        public async Task Posting_negative_score_causes_bad_request()<commit_msg>de-gender the comments<commit_after>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xunit;

namespace Nether.Web.IntegrationTests.Leaderboard
{
    //todo: come up with better naming
    public class LeaderboardTests : WebTestBase
    {
        private const string BaseUri = "/api/leaderboard";
        private HttpClient _client;

        public LeaderboardTests()
        {
            _client = GetClient();
        }

        [Fact]
        public async Task Get_leaderboard_call_succeeds()
        {
            HttpResponseMessage response = await _client.GetAsync(BaseUri + "/Default");
            Assert.True(response.IsSuccessStatusCode);
        }

        [Fact]
        public async Task Post_leaderboard_call_succeeds()
        {
            await PostScore(1);
        }

        [Fact]
        public async Task Posting_new_score_updates_default_leaderboard()
        {
            LeaderboardGetResponse leaderboardBefore = await GetLeaderboard();

            List<LeaderboardGetResponse.LeaderboardEntry> entries =
                leaderboardBefore.Entries.Where(e => e.Gamertag == _gamertag).ToList();

            //check that there is only one or less (if score wasn't posted yet) entry per user
            Assert.True(entries.Count <= 1);
            int oldScore = entries.Count == 0 ? 0 : entries.First().Score;

            //update the score posting a different (higher) result
            int newScore = oldScore + 1;
            await PostScore(newScore);

            //check that leaderboard has the updated score
            LeaderboardGetResponse leaderboardAfter = await GetLeaderboard();
            int newFreshScore = leaderboardAfter.Entries.Where(e => e.Gamertag == _gamertag).Select(e => e.Score).First();
            Assert.Equal(newFreshScore, newScore);
        }

        [Fact]
        public async Task Posting_similar_score_gets_around_me()
        {
            //note: radius is 2 at the moment, meaning you get 2 players above and 2 below (4 elements in response in general)

            //check there are at least 5 users
            LeaderboardGetResponse response = await GetLeaderboard();
            if (response.Entries.Length < 5)
            {
                throw new NotImplementedException();    //todo: post scores to get at least 5
            }

            //todo: delete score entries before testing, this requires a separate http method

            //put me somewhere in the middle and push the other user in the bottom so they are not around me
            await DeleteMyScores();
            await PostScore(int.MaxValue / 2);
            string myGamertag = _gamertag;
            _client = GetClient("testuser1");
            string hisGamertag = _gamertag;
            await DeleteMyScores();
            await PostScore(1);

            //check they are not around me
            _client = GetClient();
            response = await GetLeaderboard("5-AroundMe");
            Assert.False(response.Entries.Any(e => e.Gamertag == hisGamertag));

            //make their score similar to mine and check they are around me
            _client = GetClient("testuser1");
            await PostScore(int.MaxValue / 2 + 1);
            _client = GetClient();
            response = await GetLeaderboard("5-AroundMe");
            Assert.True(response.Entries.Any(e => e.Gamertag == hisGamertag));
        }

        [Fact]
        public async Task Limiting_top_scores_returns_limited_numer_of_rows()
        {
            LeaderboardGetResponse response = await GetLeaderboard("Top-5", HttpStatusCode.OK);

            Assert.True(response.Entries.Length <= 5);
        }

        [Fact]
        public async Task Posting_negative_score_causes_bad_request()
{'fraction_non_alphanumeric': 0.0821007771371271, 'fraction_numerical': 0.0055151667084482325, 'mean_word_length': 2.1865015974440896, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '7660169', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2066, 'n_words': 621}