<commit_before>namespace EyeInTheSky.Tests.Services.RecentChanges.Irc
{
    using System;
    using System.Collections.Generic;
    using EyeInTheSky.Exceptions;
    using EyeInTheSky.Model;
    using EyeInTheSky.Model.Interfaces;
    using EyeInTheSky.Services.Interfaces;
    using EyeInTheSky.Services.RecentChanges.Irc;
    using Moq;
    using NUnit.Framework;
    using Stwalkerster.Bot.MediaWikiLib.Services.Interfaces;

    [TestFixture]
    public class IrcRecentChangeParserTests : TestBase
    {
        private IrcRecentChangeParser rcparser;

        [SetUp]
        public void LocalSetup()
        {
            var helperMock = new Mock<IMediaWikiApiHelper>();
            helperMock.Setup(x => x.GetApiForChannel(It.IsAny<string>())).Returns((IMediaWikiApi)null);
            
            this.rcparser = new IrcRecentChangeParser(this.LoggerMock.Object, helperMock.Object);
        }

        [Test, TestCaseSource(typeof(IrcRecentChangeParserTests), "ParseTestData")]
        public IRecentChange ShouldParseCorrectly(string data)
        {
            var shouldParseCorrectly = this.rcparser.Parse(data, "");
            this.LoggerMock.Verify(x => x.ErrorFormat(It.IsAny<string>(), It.IsAny<object[]>()), Times.Never);
            return shouldParseCorrectly;
        }

        [Test, TestCaseSource(typeof(IrcRecentChangeParserTests), "ParseLogTestData")]
        public IRecentChange ShouldParseLogCorrectly(string data)
        {
            var shouldParseLogCorrectly = this.rcparser.Parse(data, "");
            this.LoggerMock.Verify(x => x.ErrorFormat(It.IsAny<string>(), It.IsAny<object[]>()), Times.Never, "Failed to parse log entry");
            return shouldParseLogCorrectly;
        }

        [Test]
        public void ShouldErrorIfUnknownLogType()
        {
            // arrange
            string data = "14[[07Special:Log/fakelog14]]4 sdfsdf10 02 5* 03Jimbo 5*  10Loggy message";

            // act
            Assert.Throws<BugException>(() => this.rcparser.Parse(data, ""));
        }

        public static IEnumerable<TestCaseData> ParseTestData
        {
            get
            {
                yield return new TestCaseData(
                        "14[[07List of Sites of Special Scientific Interest in Kent14]]4 10 02https://en.wikipedia.org/w/index.php?diff=831707969&oldid=831707719 5* 03Dudley Miles 5* (+2) 10")
                    .Returns(
                        new RecentChange("<NAME>")
                        {
                            Page = "List of Sites of Special Scientific Interest in Kent",
                            Url = "https://en.wikipedia.org/w/index.php?diff=831707969&oldid=831707719",
                            SizeDiff = 2
                        });

                yield return new TestCaseData(
                        "14[[07<NAME>14]]4 M10 02https://en.wikipedia.org/w/index.php?diff=831708179&oldid=831696958 5* 03Doctorhawkes 5* (+45) 10Reverted edits by [[Special:Contribs/122.108.247.131|192.168.3.11]] ([[User talk:192.168.3.11|talk]]) to last version by Fleets")
                    .Returns(
                        new RecentChange("Doctorhawkes")
                        {
                            Page = "<NAME>",
                            Url = "https://en.wikipedia.org/w/index.php?diff=831708179&oldid=831696958",
                            EditSummary = "Reverted edits by [[Special:Contribs/122.108.247.131|122.108.247.131]] ([[User talk:192.168.3.11|talk]]) to last version by Fleets",
                            EditFlags = "M",
                            SizeDiff = 45
                        });

                yield return new TestCaseData(
                        "14[[07Myrmecia gratiosa14]]4 MB10 02https://en.wikipedia.org/w/index.php?diff=831707968&oldid=804753497 5* 03Tom.Bot 5* (+29) 10[[User:Tom.Bot/Task3|Task 3]]: +{{Taxonbar|[[:Category:Taxonbar templates without from parameter|from]]=[[d:Special:EntityPage/Q13868803|Q13868803]]}} ([[WT:TREE#Taxonbar addition requirements|2 sig. taxon IDs]]); WP:GenFix using [[Project:AWB|AWB]]")
                    .Returns(
                        new RecentChange("Tom.Bot")
                        {
                            Page = "Myrmecia gratiosa",
                            Url = "https://en.wikipedia.org/w/index.php?diff=831707968&oldid=804753497",
                            EditSummary = "[[User:Tom.Bot/Task3|Task 3]]: +{{Taxonbar|[[:Category:Taxonbar templates without from parameter|from]]=[[d:Special:EntityPage/Q13868803|Q13868803]]}} ([[WT:TREE#Taxonbar addition requirements|2 sig. taxon IDs]]); WP:GenFix using [[Project:AWB|AWB]]",
                            EditFlags = "MB",
                            SizeDiff = 29
                        });

                yield return new TestCaseData(
                        "14[[07User:Erodr017/be bold14]]4 !N10 02https://en.wikipedia.org/w/index.php?oldid=831708535&rcid=1038874003 5* 03Erodr017 5* (+38) 10automatic post as part of sandbox guided tour")
                    .Returns(
                        new RecentChange("Erodr017")
                        {
                            Page = "User:Erodr017/be bold",
                            Url = "https://en.wikipedia.org/w/index.php?oldid=831708535&rcid=1038874003",
                            EditSummary = "automatic post as part of sandbox guided tour",
                            EditFlags = "!N",
                            SizeDiff = 38
                        });
            }
        }

        public static IEnumerable<TestCaseData> ParseLogTestData
        {
            get
            {
                #region abusefilter
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 hit10 02 5* 03GriffinMorganHuff 5*  10GriffinMorganHuff triggered [[Special:AbuseFilter/550|filter 550]], performing the action \"edit\" on [[02The Boy in Blue (1986 film)10]]. Actions taken: Tag ([[Special:AbuseLog/20813489|details]])")
                    .Returns(
                        new RecentChange("GriffinMorganHuff")
                        {
                            Page = "Special:AbuseFilter/550",
                            TargetPage = "The Boy in Blue (1986 film)",
                            EditFlags = "hit; edit",
                            Log = "abusefilter",
                            AdditionalData = "Tag"
                        });
                
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 hit10 02 5* 03Ceanneisenhammer 5*  10Ceanneisenhammer triggered [[Special:AbuseFilter/527|filter 527]], performing the action \"createaccount\" on [[02Special:UserLogin10]]. Actions taken: none ([[Special:AbuseLog/20724740|details]])")
                    .Returns(
                        new RecentChange("Ceanneisenhammer")
                        {
                            Page = "Special:AbuseFilter/527",
                            TargetPage = "Special:UserLogin",
                            EditFlags = "hit; createaccount",
                            Log = "abusefilter",
                            AdditionalData = "none"
                        });
                
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 create10 02 5* 03Beetstra 5*  10Beetstra created [[02Special:AbuseFilter/91010]] ([[Special:AbuseFilter/history/910/diff/prev/18570]])")
                    .Returns(
                        new RecentChange("Beetstra")
                        {
                            Page = "Special:AbuseFilter/910",
                            EditFlags = "create",
                            Log = "abusefilter"
                        });
                
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 modify10 02 5* 03Cyp 5*  10Cyp modified [[02Special:AbuseFilter/89810]] ([[Special:AbuseFilter/history/898/diff/prev/18571]])")
                    .Returns(
                        new RecentChange("Cyp")
                        {
                            Page = "Special:AbuseFilter/898",
                            EditFlags = "modify",
                            Log = "abusefilter"
                        });
                #endregion
                #region block
                yield return new TestCaseData(
                        "14[[07Special:Log/block14]]4 block10 02 5* 03MSGJ 5*  10blocked User:172.16.17.32 (account creation blocked) with an expiry time of 31 hours: [[WP:Edit warring|Edit warring]]")
                    .Returns(
                        new RecentChange("MSGJ")<commit_msg>Ignore tests<commit_after>namespace EyeInTheSky.Tests.Services.RecentChanges.Irc
{
    using System;
    using System.Collections.Generic;
    using EyeInTheSky.Exceptions;
    using EyeInTheSky.Model;
    using EyeInTheSky.Model.Interfaces;
    using EyeInTheSky.Services.Interfaces;
    using EyeInTheSky.Services.RecentChanges.Irc;
    using Moq;
    using NUnit.Framework;
    using Stwalkerster.Bot.MediaWikiLib.Services.Interfaces;

    [TestFixture]
    public class IrcRecentChangeParserTests : TestBase
    {
        private IrcRecentChangeParser rcparser;

        [SetUp]
        public void LocalSetup()
        {
            var helperMock = new Mock<IMediaWikiApiHelper>();
            helperMock.Setup(x => x.GetApiForChannel(It.IsAny<string>())).Returns((IMediaWikiApi)null);
            
            this.rcparser = new IrcRecentChangeParser(this.LoggerMock.Object, helperMock.Object);
        }

        [Test, TestCaseSource(typeof(IrcRecentChangeParserTests), "ParseTestData")]
        public IRecentChange ShouldParseCorrectly(string data)
        {
            var shouldParseCorrectly = this.rcparser.Parse(data, "");
            this.LoggerMock.Verify(x => x.ErrorFormat(It.IsAny<string>(), It.IsAny<object[]>()), Times.Never);
            return shouldParseCorrectly;
        }

        [Test, TestCaseSource(typeof(IrcRecentChangeParserTests), "ParseLogTestData")]
        public IRecentChange ShouldParseLogCorrectly(string data)
        {
            var shouldParseLogCorrectly = this.rcparser.Parse(data, "");
            this.LoggerMock.Verify(x => x.ErrorFormat(It.IsAny<string>(), It.IsAny<object[]>()), Times.Never, "Failed to parse log entry");
            return shouldParseLogCorrectly;
        }

        [Test]
        public void ShouldErrorIfUnknownLogType()
        {
            // arrange
            string data = "14[[07Special:Log/fakelog14]]4 sdfsdf10 02 5* 03Jimbo 5*  10Loggy message";

            // act
            Assert.Throws<BugException>(() => this.rcparser.Parse(data, ""));
        }

        public static IEnumerable<TestCaseData> ParseTestData
        {
            get
            {
                yield return new TestCaseData(
                        "14[[07List of Sites of Special Scientific Interest in Kent14]]4 10 02https://en.wikipedia.org/w/index.php?diff=831707969&oldid=831707719 5* 03Dudley Miles 5* (+2) 10")
                    .Returns(
                        new RecentChange("<NAME>")
                        {
                            Page = "List of Sites of Special Scientific Interest in Kent",
                            Url = "https://en.wikipedia.org/w/index.php?diff=831707969&oldid=831707719",
                            SizeDiff = 2
                        });

                yield return new TestCaseData(
                        "14[[07<NAME>14]]4 M10 02https://en.wikipedia.org/w/index.php?diff=831708179&oldid=831696958 5* 03Doctorhawkes 5* (+45) 10Reverted edits by [[Special:Contribs/122.108.247.131|192.168.3.11]] ([[User talk:192.168.3.11|talk]]) to last version by Fleets")
                    .Returns(
                        new RecentChange("Doctorhawkes")
                        {
                            Page = "<NAME>",
                            Url = "https://en.wikipedia.org/w/index.php?diff=831708179&oldid=831696958",
                            EditSummary = "Reverted edits by [[Special:Contribs/122.108.247.131|192.168.3.11]] ([[User talk:192.168.3.11|talk]]) to last version by Fleets",
                            EditFlags = "M",
                            SizeDiff = 45
                        });

                yield return new TestCaseData(
                        "14[[07Myrmecia gratiosa14]]4 MB10 02https://en.wikipedia.org/w/index.php?diff=831707968&oldid=804753497 5* 03Tom.Bot 5* (+29) 10[[User:Tom.Bot/Task3|Task 3]]: +{{Taxonbar|[[:Category:Taxonbar templates without from parameter|from]]=[[d:Special:EntityPage/Q13868803|Q13868803]]}} ([[WT:TREE#Taxonbar addition requirements|2 sig. taxon IDs]]); WP:GenFix using [[Project:AWB|AWB]]")
                    .Returns(
                        new RecentChange("Tom.Bot")
                        {
                            Page = "Myrmecia gratiosa",
                            Url = "https://en.wikipedia.org/w/index.php?diff=831707968&oldid=804753497",
                            EditSummary = "[[User:Tom.Bot/Task3|Task 3]]: +{{Taxonbar|[[:Category:Taxonbar templates without from parameter|from]]=[[d:Special:EntityPage/Q13868803|Q13868803]]}} ([[WT:TREE#Taxonbar addition requirements|2 sig. taxon IDs]]); WP:GenFix using [[Project:AWB|AWB]]",
                            EditFlags = "MB",
                            SizeDiff = 29
                        });

                yield return new TestCaseData(
                        "14[[07User:Erodr017/be bold14]]4 !N10 02https://en.wikipedia.org/w/index.php?oldid=831708535&rcid=1038874003 5* 03Erodr017 5* (+38) 10automatic post as part of sandbox guided tour")
                    .Returns(
                        new RecentChange("Erodr017")
                        {
                            Page = "User:Erodr017/be bold",
                            Url = "https://en.wikipedia.org/w/index.php?oldid=831708535&rcid=1038874003",
                            EditSummary = "automatic post as part of sandbox guided tour",
                            EditFlags = "!N",
                            SizeDiff = 38
                        });
            }
        }

        public static IEnumerable<TestCaseData> ParseLogTestData
        {
            get
            {
                #region abusefilter
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 hit10 02 5* 03GriffinMorganHuff 5*  10GriffinMorganHuff triggered [[Special:AbuseFilter/550|filter 550]], performing the action \"edit\" on [[02The Boy in Blue (1986 film)10]]. Actions taken: Tag ([[Special:AbuseLog/20813489|details]])")
                    .Returns(
                        new RecentChange("GriffinMorganHuff")
                        {
                            Page = "Special:AbuseFilter/550",
                            TargetPage = "The Boy in Blue (1986 film)",
                            EditFlags = "hit; edit",
                            Log = "abusefilter",
                            AdditionalData = "Tag"
                        }).Ignore("URL is dynamic.");
                
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 hit10 02 5* 03Ceanneisenhammer 5*  10Ceanneisenhammer triggered [[Special:AbuseFilter/527|filter 527]], performing the action \"createaccount\" on [[02Special:UserLogin10]]. Actions taken: none ([[Special:AbuseLog/20724740|details]])")
                    .Returns(
                        new RecentChange("Ceanneisenhammer")
                        {
                            Page = "Special:AbuseFilter/527",
                            TargetPage = "Special:UserLogin",
                            EditFlags = "hit; createaccount",
                            Log = "abusefilter",
                            AdditionalData = "none"
                        }).Ignore("URL is dynamic.");
                
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 create10 02 5* 03Beetstra 5*  10Beetstra created [[02Special:AbuseFilter/91010]] ([[Special:AbuseFilter/history/910/diff/prev/18570]])")
                    .Returns(
                        new RecentChange("Beetstra")
                        {
                            Page = "Special:AbuseFilter/910",
                            EditFlags = "create",
                            Log = "abusefilter"
                        }).Ignore("URL is dynamic.");
                
                yield return new TestCaseData(
                        "14[[07Special:Log/abusefilter14]]4 modify10 02 5* 03Cyp 5*  10Cyp modified [[02Special:AbuseFilter/89810]] ([[Special:AbuseFilter/history/898/diff/prev/18571]])")
                    .Returns(
                        new RecentChange("Cyp")
                        {
                            Page = "Special:AbuseFilter/898",
                            EditFlags = "modify",
                            Log = "abusefilter"
                        }).Ignore("URL is dynamic.");
                #endregion
                #region block
                yield return new TestCaseData(
                        "14[[07Special:Log/block14]]4 block10 02 5* 03MSGJ 5*  10blocked User:172.16.17.32 (account creation blocked) with an expiry time of 31 hours: [[WP:Edit warring|Edit warring]]")
                    .Returns(
                        new RecentChange("MSGJ")
{'fraction_non_alphanumeric': 0.12251062877601253, 'fraction_numerical': 0.06024837771313493, 'mean_word_length': 1.6351709905660377, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, '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': '5219415', 'n_tokens_mistral': 5803, 'n_tokens_neox': 5055, 'n_words': 922}