<?php

use App\Services\ProjectCreator;
use App\Services\ProjectDoctor;

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

    $creator = new ProjectCreator(new ProjectDoctor());

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

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

it('builds non-personalized env values for a new project', function () {
    $creator = new ProjectCreator(new ProjectDoctor());

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

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