<?php

use App\Actions\Init\BuildEnvValues;
use App\Actions\Init\EnsureTargetDirectory;

it('rejects non-empty target directories', function () {
    $directory = sys_get_temp_dir() . '/probots-init-' . bin2hex(random_bytes(5));
    mkdir($directory, 0777, true);
    file_put_contents($directory . '/keep.txt', 'keep');

    $action = new EnsureTargetDirectory();

    expect(fn() => $action->handle($directory))
        ->toThrow(RuntimeException::class, sprintf('Target directory `%s` is not empty.', $directory));

    unlink($directory . '/keep.txt');
    rmdir($directory);
});

it('builds env values for a new project', function () {
    $action = new BuildEnvValues();

    $values = $action->handle('client-project', 'client-project.prbts', 'client_project', 'host.db.docker.internal:7689');

    expect($values['VIRTUAL_HOST'])->toBe('client-project.prbts');
    expect($values['DB_NAME'])->toBe('client_project');
    expect($values['THEME_NAME'])->toBe('theme');
    expect($values['PROJECT_SLUG'])->toBe('client-project');
    expect($values['PORT_DEV'])->toBe($values['PORT_WP'] + 1);
});
