<commit_before><?php

namespace OpenDominion\Tests\Unit\Services;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use OpenDominion\Models\Pack;
use OpenDominion\Models\Race;
use OpenDominion\Models\Realm;
use OpenDominion\Models\Round;
use OpenDominion\Services\RealmFinderService;
use OpenDominion\Tests\AbstractBrowserKitTestCase;

class RealmFinderServiceTest extends AbstractBrowserKitTestCase
{
    use DatabaseMigrations;

    /** @var Round */
    protected $round;

    /** @var Race */
    protected $goodRace;

    /** @var RealmFinderService */
    protected $realmFinderService;

    protected function setUp()
    {
        parent::setUp();

        $this->seedDatabase();

        $this->round = $this->createRound();
        $this->goodRace = Race::where('alignment', 'good')->firstOrFail();

        $this->realmFinderService = $this->app->make(RealmFinderService::class);
    }

    public function testFindRandomReturnsVacantRealmBasedOnRaceAlignment()
    {
        $goodRealm = $this->createRealm($this->round, $this->goodRace->alignment);

        $evilRace = Race::where('alignment', 'evil')->firstOrFail();
        $evilRealm = $this->createRealm($this->round, $evilRace->alignment);

        $testRealm = $this->realmFinderService->findRandomRealm($this->round, $this->goodRace);

        $this->assertNotNull($testRealm);
        $this->assertEquals($goodRealm->id, $testRealm->id);

        $testRealm = $this->realmFinderService->findRandomRealm($this->round, $evilRace);

        $this->assertNotNull($testRealm);
        $this->assertEquals($evilRealm->id, $testRealm->id);
    }

    public function testFindRandomReturnsNullWhenNoValidRealmExists()
    {
        $evilRace = Race::where('alignment', 'evil')->firstOrFail();

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));

        $this->createRealm($this->round, $evilRace->alignment);

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));
    }

    public function testFindRandomReturnsNullIfAllValidRealmsAreFull()
    {
        // Create 3 realms full of dominions
        for ($i = 0; $i < 3; $i++) {
            for ($dominionCounter = 0; $dominionCounter < 12; $dominionCounter++) {
                $user = $this->createUser(null, ['email' => "<EMAIL>"]); // todo: why this email?
                $this->createDominion($user, $this->round, $this->goodRace) ;
            }
        }

        $this->assertEquals(3, Realm::count());

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));
    }

    public function testFindRandomRealmRespectsReservedPackSlots()
    {
        // Dominion slot 1
        $user = $this->createUser();
        $dominion = $this->createDominion($user, $this->round, $this->goodRace);
        $realm = $dominion->realm;

        // Dominion slots 2-10
        for ($i = 0; $i < 9; $i++) {
            $this->createDominion($this->createUser(), $this->round, $this->goodRace);
        }

        $this->assertEquals(10, $realm->dominions()->count());
        $this->assertEquals($realm->id, $this->realmFinderService->findRandomRealm($this->round, $this->goodRace)->id);

        // Last 2 spots reserved for pack from user in spot 1
        $pack = Pack::create([
            'round_id' => $this->round->id,
            'user_id' => $user->id,
            'realm_id' => $realm->id,
            'name' => 'test pack name',
            'password' => '<PASSWORD>',
            'size' => 3,
        ]);

        $dominion->pack_id = $pack->id;
        $dominion->save();
        $dominion->refresh();

        $realm->has_pack = true;
        $realm->reserved_slots = 2;
        $realm->save();
        $realm->refresh();

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));
    }

    public function testFindRandomRealmReturnsRealmWithClosedPacksSlots()
    {
        // Dominion slot 1
        $user = $this->createUser();
        $dominion = $this->createDominion($user, $this->round, $this->goodRace);
        $realm = $dominion->realm;

        // Dominion slots 2-10
        for ($i = 0; $i < 9; $i++) {
            $this->createDominion($this->createUser(), $this->round, $this->goodRace);
        }

        $this->assertEquals(10, $realm->dominions()->count());
        $this->assertEquals($realm->id, $this->realmFinderService->findRandomRealm($this->round, $this->goodRace)->id);

        // Last 2 spots reserved for pack from user in spot 1
        $pack = Pack::create([
            'round_id' => $this->round->id,
            'user_id' => $user->id,
            'realm_id' => $realm->id,
            'name' => 'test pack name',
            'password' => '<PASSWORD>',
            'size' => 3,
            'closed_at' => now(),
        ]);

        $dominion->pack_id = $pack->id;
        $dominion->save();
        $dominion->refresh();

        $realm->has_pack = true;
        $realm->reserved_slots = 2;
        $realm->save();
        $realm->refresh();

        $this->assertEquals($realm->id, $this->realmFinderService->findRandomRealm($this->round, $this->goodRace)->id);
    }

}<commit_msg>Apply fixes from StyleCI<commit_after><?php

namespace OpenDominion\Tests\Unit\Services;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use OpenDominion\Models\Pack;
use OpenDominion\Models\Race;
use OpenDominion\Models\Realm;
use OpenDominion\Models\Round;
use OpenDominion\Services\RealmFinderService;
use OpenDominion\Tests\AbstractBrowserKitTestCase;

class RealmFinderServiceTest extends AbstractBrowserKitTestCase
{
    use DatabaseMigrations;

    /** @var Round */
    protected $round;

    /** @var Race */
    protected $goodRace;

    /** @var RealmFinderService */
    protected $realmFinderService;

    protected function setUp()
    {
        parent::setUp();

        $this->seedDatabase();

        $this->round = $this->createRound();
        $this->goodRace = Race::where('alignment', 'good')->firstOrFail();

        $this->realmFinderService = $this->app->make(RealmFinderService::class);
    }

    public function testFindRandomReturnsVacantRealmBasedOnRaceAlignment()
    {
        $goodRealm = $this->createRealm($this->round, $this->goodRace->alignment);

        $evilRace = Race::where('alignment', 'evil')->firstOrFail();
        $evilRealm = $this->createRealm($this->round, $evilRace->alignment);

        $testRealm = $this->realmFinderService->findRandomRealm($this->round, $this->goodRace);

        $this->assertNotNull($testRealm);
        $this->assertEquals($goodRealm->id, $testRealm->id);

        $testRealm = $this->realmFinderService->findRandomRealm($this->round, $evilRace);

        $this->assertNotNull($testRealm);
        $this->assertEquals($evilRealm->id, $testRealm->id);
    }

    public function testFindRandomReturnsNullWhenNoValidRealmExists()
    {
        $evilRace = Race::where('alignment', 'evil')->firstOrFail();

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));

        $this->createRealm($this->round, $evilRace->alignment);

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));
    }

    public function testFindRandomReturnsNullIfAllValidRealmsAreFull()
    {
        // Create 3 realms full of dominions
        for ($i = 0; $i < 3; $i++) {
            for ($dominionCounter = 0; $dominionCounter < 12; $dominionCounter++) {
                $user = $this->createUser(null, ['email' => "<EMAIL>"]); // todo: why this email?
                $this->createDominion($user, $this->round, $this->goodRace) ;
            }
        }

        $this->assertEquals(3, Realm::count());

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));
    }

    public function testFindRandomRealmRespectsReservedPackSlots()
    {
        // Dominion slot 1
        $user = $this->createUser();
        $dominion = $this->createDominion($user, $this->round, $this->goodRace);
        $realm = $dominion->realm;

        // Dominion slots 2-10
        for ($i = 0; $i < 9; $i++) {
            $this->createDominion($this->createUser(), $this->round, $this->goodRace);
        }

        $this->assertEquals(10, $realm->dominions()->count());
        $this->assertEquals($realm->id, $this->realmFinderService->findRandomRealm($this->round, $this->goodRace)->id);

        // Last 2 spots reserved for pack from user in spot 1
        $pack = Pack::create([
            'round_id' => $this->round->id,
            'user_id' => $user->id,
            'realm_id' => $realm->id,
            'name' => 'test pack name',
            'password' => '<PASSWORD>',
            'size' => 3,
        ]);

        $dominion->pack_id = $pack->id;
        $dominion->save();
        $dominion->refresh();

        $realm->has_pack = true;
        $realm->reserved_slots = 2;
        $realm->save();
        $realm->refresh();

        $this->assertNull($this->realmFinderService->findRandomRealm($this->round, $this->goodRace));
    }

    public function testFindRandomRealmReturnsRealmWithClosedPacksSlots()
    {
        // Dominion slot 1
        $user = $this->createUser();
        $dominion = $this->createDominion($user, $this->round, $this->goodRace);
        $realm = $dominion->realm;

        // Dominion slots 2-10
        for ($i = 0; $i < 9; $i++) {
            $this->createDominion($this->createUser(), $this->round, $this->goodRace);
        }

        $this->assertEquals(10, $realm->dominions()->count());
        $this->assertEquals($realm->id, $this->realmFinderService->findRandomRealm($this->round, $this->goodRace)->id);

        // Last 2 spots reserved for pack from user in spot 1
        $pack = Pack::create([
            'round_id' => $this->round->id,
            'user_id' => $user->id,
            'realm_id' => $realm->id,
            'name' => 'test pack name',
            'password' => '<PASSWORD>',
            'size' => 3,
            'closed_at' => now(),
        ]);

        $dominion->pack_id = $pack->id;
        $dominion->save();
        $dominion->refresh();

        $realm->has_pack = true;
        $realm->reserved_slots = 2;
        $realm->save();
        $realm->refresh();

        $this->assertEquals($realm->id, $this->realmFinderService->findRandomRealm($this->round, $this->goodRace)->id);
    }
}
{'fraction_non_alphanumeric': 0.17057813394390384, 'fraction_numerical': 0.005914901736309865, 'mean_word_length': 2.753311851056212, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 295, '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': '2394063', 'n_tokens_mistral': 3377, 'n_tokens_neox': 3248, 'n_words': 527}