<?php

declare(strict_types=1);

use App\Services\Supplier\PlaywrightBridge;
use Illuminate\Support\Facades\Cache;

beforeEach(function () {
    config([
        'services.supplier.login' => 'test_login',
        'services.supplier.password' => 'test_password',
        'services.supplier.portal_url' => 'https://crm.bp-gr.ru',
    ]);
    Cache::store('redis')->forget('supplier:session');
});

afterEach(function () {
    Cache::store('redis')->forget('supplier:session');
});

test('command supplier:session:refresh dispatches job synchronously and writes cache', function () {
    $bridge = Mockery::mock(PlaywrightBridge::class);
    /** @var PlaywrightBridge $bridge */
    $bridge->shouldReceive('refreshSession')->andReturn([
        'phpsessid' => 'cmd_sess',
        'csrf' => 'cmd_csrf',
        'refreshed_at' => '2026-05-11T10:00:00Z',
    ]);
    app()->instance(PlaywrightBridge::class, $bridge);

    // @phpstan-ignore-next-line method.notFound (Pest TestCall->artisan() mixin)
    $this->artisan('supplier:session:refresh')->assertExitCode(0);

    expect(Cache::store('redis')->get('supplier:session'))->toBeArray()
        ->and(Cache::store('redis')->get('supplier:session')['phpsessid'])->toBe('cmd_sess');
});
