<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AuthTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function halaman_login_tampil()
    {
        $this->get(route('login'))->assertOk();
    }

    /** @test */
    public function kasir_dapat_login_dengan_kredensial_benar()
    {
        $user = User::create([
            'name' => 'Kasir', 'username' => 'kasir1',
            'password' => bcrypt('password'),
            'role' => 'kasir', 'is_active' => true,
        ]);

        $this->post(route('login.post'), [
            'username' => 'kasir1',
            'password' => 'password',
        ])->assertRedirect();

        $this->assertAuthenticatedAs($user);
    }

    /** @test */
    public function login_gagal_dengan_password_salah()
    {
        User::create([
            'name' => 'Kasir', 'username' => 'kasir1',
            'password' => bcrypt('password'),
            'role' => 'kasir', 'is_active' => true,
        ]);

        $this->post(route('login.post'), [
            'username' => 'kasir1',
            'password' => 'salah',
        ])->assertSessionHasErrors('username');
    }

    /** @test */
    public function guest_diredirect_ke_login()
    {
        $this->get(route('pos.index'))->assertRedirect(route('login'));
    }
}
