<?php

it('locks and marks a self-serve start occupied before opening the entrance relay', function (): void {
    $commandTrait = file_get_contents(app_path('modules/selfserve/traits/selfserve_lane_command_t.php'));

    expect($commandTrait)->not->toBeFalse();
    expect($commandTrait)->toContain('set_if_absent_with_expiration');

    $startCaseOffset = strpos($commandTrait, 'case selfserve_lane_command::START:');
    expect($startCaseOffset)->not->toBeFalse();

    $startCase = substr($commandTrait, (int)$startCaseOffset, 5000);
    $lockOffset = strpos($startCase, '$start_lock_token = $this->acquireLaneStartCommandLock();');
    $occupiedOffset = strpos($startCase, '$this->setLaneStatus(selfserve_lane_status::OCCUPIED);');
    $openOffset = strpos($startCase, '$this->openEntrancePortForWashStart();');
    $finallyOffset = strpos($startCase, '} finally {');
    $releaseOffset = strpos($startCase, '$this->releaseLaneStartCommandLock($start_lock_token);');

    expect($lockOffset)->not->toBeFalse()
        ->and($occupiedOffset)->not->toBeFalse()
        ->and($openOffset)->not->toBeFalse()
        ->and($finallyOffset)->not->toBeFalse()
        ->and($releaseOffset)->not->toBeFalse()
        ->and($lockOffset)->toBeLessThan($occupiedOffset)
        ->and($occupiedOffset)->toBeLessThan($openOffset)
        ->and($openOffset)->toBeLessThan($finallyOffset)
        ->and($finallyOffset)->toBeLessThan($releaseOffset);
});
