<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Game.Beatmaps;
using osu.Game.Online.Spectator;
using osu.Game.Replays.Legacy;
using osu.Game.Screens.Play;
using osu.Game.Users;

namespace osu.Game.Tests.Visual.Gameplay
{
    public class TestSceneSpectator : ScreenTestScene
    {
        [Cached(typeof(SpectatorStreamingClient))]
        private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient();

        [Resolved]
        private OsuGameBase game { get; set; }

        [Test]
        public void TestBasicSpectatingFlow()
        {
            AddStep("add streaming client", () => Add(testSpectatorStreamingClient));
            AddStep("load screen", () => LoadScreen(new Spectator(testSpectatorStreamingClient.StreamingUser)));
            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
        }

        [Test]
        public void TestSpectatingDuringGameplay()
        {
            // should seek immediately to available frames
        }

        [Test]
        public void TestHostStartsPlayingWhileAlreadyWatching()
        {
            // should restart either immediately or after running out of frames
        }

        [Test]
        public void TestHostFails()
        {
            // should replay until running out of frames then fail
        }

        [Test]
        public void TestStopWatchingDuringPlay()
        {
            // should immediately exit and unbind from streaming client
        }

        internal class TestSpectatorStreamingClient : SpectatorStreamingClient
        {
            [Resolved]
            private BeatmapManager beatmaps { get; set; }

            public readonly User StreamingUser = new User { Id = 1234, Username = "Test user" };

            public void StartPlay()
            {
                ((ISpectatorClient)this).UserBeganPlaying((int)StreamingUser.Id, new SpectatorState
                {
                    BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First().OnlineBeatmapID,
                    RulesetID = 0,
                });
            }

            public void SendFrames()
            {
                ((ISpectatorClient)this).UserSentFrames((int)StreamingUser.Id, new FrameDataBundle(new[]
                {
                    // todo: populate more frames
                    new LegacyReplayFrame(0, 0, 0, ReplayButtonState.Left1)
                }));
            }
        }
    }<commit_msg>Setup tests to run headless, add basic pass support<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Online.Spectator;
using osu.Game.Replays.Legacy;
using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps.IO;
using osu.Game.Users;

namespace osu.Game.Tests.Visual.Gameplay
{
    public class TestSceneSpectator : ScreenTestScene
    {
        [Cached(typeof(SpectatorStreamingClient))]
        private TestSpectatorStreamingClient testSpectatorStreamingClient = new TestSpectatorStreamingClient();

        private Spectator spectatorScreen;

        [Resolved]
        private OsuGameBase game { get; set; }

        public override void SetUpSteps()
        {
            base.SetUpSteps();

            AddStep("import beatmap", () => ImportBeatmapTest.LoadOszIntoOsu(game, virtualTrack: true).Wait());

            AddStep("add streaming client", () =>
            {
                Remove(testSpectatorStreamingClient);
                Add(testSpectatorStreamingClient);
            });
        }

        [Test]
        public void TestBasicSpectatingFlow()
        {
            beginSpectating();
            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
        }

        [Test]
        public void TestSpectatingDuringGameplay()
        {
            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());

            // should seek immediately to available frames
            beginSpectating();
        }

        [Test]
        public void TestHostStartsPlayingWhileAlreadyWatching()
        {
            beginSpectating();

            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());

            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
            // should restart either immediately or after running out of frames
        }

        [Test]
        public void TestHostFails()
        {
            beginSpectating();

            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());
            // todo: send fail state

            // should replay until running out of frames then fail
        }

        [Test]
        public void TestStopWatchingDuringPlay()
        {
            beginSpectating();

            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());

            AddUntilStep("wait for player", () => Stack.CurrentScreen is Player);

            // should immediately exit and unbind from streaming client
            AddStep("stop spectating", () => (Stack.CurrentScreen as Player)?.Exit());

            AddUntilStep("spectating stopped", () => spectatorScreen.GetParentScreen() == null);
        }

        [Test]
        public void TestWatchingBeatmapThatDoesntExistLocally()
        {
            beginSpectating();

            AddStep("start play", () => testSpectatorStreamingClient.StartPlay());
            AddStep("send frames", () => testSpectatorStreamingClient.SendFrames());

            // player should never arrive.
        }

        private void beginSpectating() =>
            AddStep("load screen", () => LoadScreen(spectatorScreen = new Spectator(testSpectatorStreamingClient.StreamingUser)));

        internal class TestSpectatorStreamingClient : SpectatorStreamingClient
        {
            [Resolved]
            private BeatmapManager beatmaps { get; set; }

            public readonly User StreamingUser = new User { Id = 1234, Username = "Test user" };

            public void StartPlay()
            {
                ((ISpectatorClient)this).UserBeganPlaying((int)StreamingUser.Id, new SpectatorState
                {
                    BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
                    RulesetID = 0,
                });
            }

            public void EndPlay()
            {
                ((ISpectatorClient)this).UserFinishedPlaying((int)StreamingUser.Id, new SpectatorState
                {
                    BeatmapID = beatmaps.GetAllUsableBeatmapSets().First().Beatmaps.First(b => b.RulesetID == 0).OnlineBeatmapID,
                    RulesetID = 0,
                });
            }

            public void SendFrames()
            {
                ((ISpectatorClient)this).UserSentFrames((int)StreamingUser.Id, new FrameDataBundle(new[]
                {
                    // todo: populate more frames
                    new LegacyReplayFrame(0, 0, 0, ReplayButtonState.Left1)
                }));
            }
        }
    }
{'fraction_non_alphanumeric': 0.09857936998147004, 'fraction_numerical': 0.0025941939468807906, 'mean_word_length': 2.157566302652106, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, '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': '5102501', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2015, 'n_words': 457}