<commit_before><?php

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Quassel\Factory;
use Clue\React\Block;
use Clue\React\Quassel\Client;
use React\Promise\Promise;
use Clue\React\Quassel\Io\Protocol;

class FunctionalTest extends TestCase
{
    private static $host;
    private static $username;
    private static $password;

    private static $loop;
    private static $blocker;

    public static function setUpBeforeClass()
    {
        if (!getenv('QUASSEL_HOST')) {
            return;
        }

        self::$host = getenv('QUASSEL_HOST');
        self::$username = getenv('QUASSEL_USER');
        if (!self::$username) {
            self::$username = 'quassel';
        }

        self::$password = getenv('QUASSEL_PASS');
        if (!self::$password) {
            self::$password = '<PASSWORD>';
        }

        self::$loop = LoopFactory::create();
    }

    public function setUp()
    {
        if (!self::$host) {
            $this->markTestSkipped('No ENV QUASSEL_HOST (plus optionally QUASSEL_USER and QUASSEL_PASS) given');
        }
    }

    public function testCreateClient()
    {
        $factory = new Factory(self::$loop);
        $promise = $factory->createClient(self::$host);

        $client = Block\await($promise, self::$loop, 10.0);

        return $client;
    }

    /**
     * @depends testCreateClient
     * @param Client $client
     */
    public function testSendClientInit(Client $client)
    {
        $client->sendClientInit();

        $message = $this->awaitMessage($client);
        $this->assertEquals('ClientInitAck', $message['MsgType']);

        return $message;
    }

    /**
     * @depends testCreateClient
     * @depends testSendClientInit
     *
     * @param Client $client
     * @param array  $message
     */
    public function testSendCoreSetupData(Client $client, $message)
    {
        if ($message['Configured']) {
            $this->markTestSkipped('Given core already configured, can not set-up');
        }

        $client->sendCoreSetupData(self::$username, self::$password);

        $message = $this->awaitMessage($client);
        $this->assertEquals('CoreSetupAck', $message['MsgType']);

        return $message;
    }

    /**
     * @depends testCreateClient
     * @depends testSendClientInit
     *
     * @param Client $client
     * @param array  $message
     */
    public function testSendClientLogin(Client $client, $message)
    {
        $client->sendClientLogin(self::$username, self::$password);

        $message = $this->awaitMessage($client);
        $this->assertEquals('ClientLoginAck', $message['MsgType']);

        return $message;
    }

    /**
     * @depends testCreateClient
     *
     * @param Client $client
     */
    public function testSendHeartBeat(Client $client)
    {
        $time = new \DateTime();

        $promise = new Promise(function ($resolve) use ($client) {
            $callback = function ($message) use ($resolve, &$callback, $client) {
                if (isset($message[0]) && $message[0] === Protocol::REQUEST_HEARTBEATREPLY) {
                    $client->removeListener('message', $callback);
                    $resolve($message[1]);
                }
            };

            $client->on('message', $callback);
        });

        $client->sendHeartBeatRequest($time);
<commit_msg>Await SessionInit message after ClientLoginAck before sending heartbeat<commit_after><?php

use React\EventLoop\Factory as LoopFactory;
use Clue\React\Quassel\Factory;
use Clue\React\Block;
use Clue\React\Quassel\Client;
use React\Promise\Promise;
use Clue\React\Quassel\Io\Protocol;

class FunctionalTest extends TestCase
{
    private static $host;
    private static $username;
    private static $password;

    private static $loop;
    private static $blocker;

    public static function setUpBeforeClass()
    {
        if (!getenv('QUASSEL_HOST')) {
            return;
        }

        self::$host = getenv('QUASSEL_HOST');
        self::$username = getenv('QUASSEL_USER');
        if (!self::$username) {
            self::$username = 'quassel';
        }

        self::$password = getenv('QUASSEL_PASS');
        if (!self::$password) {
            self::$password = '<PASSWORD>';
        }

        self::$loop = LoopFactory::create();
    }

    public function setUp()
    {
        if (!self::$host) {
            $this->markTestSkipped('No ENV QUASSEL_HOST (plus optionally QUASSEL_USER and QUASSEL_PASS) given');
        }
    }

    public function testCreateClient()
    {
        $factory = new Factory(self::$loop);
        $promise = $factory->createClient(self::$host);

        $client = Block\await($promise, self::$loop, 10.0);

        return $client;
    }

    /**
     * @depends testCreateClient
     * @param Client $client
     */
    public function testSendClientInit(Client $client)
    {
        $client->sendClientInit();

        $message = $this->awaitMessage($client);
        $this->assertEquals('ClientInitAck', $message['MsgType']);

        return $message;
    }

    /**
     * @depends testCreateClient
     * @depends testSendClientInit
     *
     * @param Client $client
     * @param array  $message
     */
    public function testSendCoreSetupData(Client $client, $message)
    {
        if ($message['Configured']) {
            $this->markTestSkipped('Given core already configured, can not set-up');
        }

        $client->sendCoreSetupData(self::$username, self::$password);

        $message = $this->awaitMessage($client);
        $this->assertEquals('CoreSetupAck', $message['MsgType']);

        return $message;
    }

    /**
     * @depends testCreateClient
     * @depends testSendClientInit
     *
     * @param Client $client
     * @param array  $message
     */
    public function testSendClientLogin(Client $client, $message)
    {
        $client->sendClientLogin(self::$username, self::$password);

        $message = $this->awaitMessage($client);
        $this->assertEquals('ClientLoginAck', $message['MsgType']);

        $message = $this->awaitMessage($client);
        $this->assertEquals('SessionInit', $message['MsgType']);

        return $message;
    }

    /**
     * @depends testCreateClient
     *
     * @param Client $client
     */
    public function testSendHeartBeat(Client $client)
    {
        $time = new \DateTime();

        $promise = new Promise(function ($resolve) use ($client) {
            $callback = function ($message) use ($resolve, &$callback, $client) {
                if (isset($message[0]) && $message[0] === Protocol::REQUEST_HEARTBEATREPLY) {
                    $client->removeListener('message', $callback);
                    $resolve($message[1]);
                }
            };

            $client->on('message', $callback);
        });

        $client->sendHeartBeatRequest($time);

{'fraction_non_alphanumeric': 0.14027676620538965, 'fraction_numerical': 0.0017479970866715222, 'mean_word_length': 2.4398797595190382, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '3362061', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1962, 'n_words': 393}