221 lines
6.5 KiB
PHP
221 lines
6.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Lvl0\FediDiscover\Tests\Feature;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Lvl0\FediDiscover\Config\InstanceType;
|
|
use Lvl0\FediDiscover\Models\Instance;
|
|
use Tests\TestCase;
|
|
|
|
class ValidateInstancesCommandTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_exits_zero_when_the_database_is_empty(): void
|
|
{
|
|
$this->artisan('fedi-discover:validate')
|
|
->assertExitCode(0);
|
|
}
|
|
|
|
public function test_it_exits_zero_when_all_instances_are_valid(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->assertExitCode(0);
|
|
}
|
|
|
|
public function test_it_exits_nonzero_when_a_row_has_an_invalid_url(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'not-a-url',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->assertExitCode(1);
|
|
}
|
|
|
|
public function test_it_exits_nonzero_when_a_row_has_a_zero_interval(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => true,
|
|
'interval_seconds' => 0,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->assertExitCode(1);
|
|
}
|
|
|
|
public function test_it_reports_summary_of_valid_and_invalid_counts(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://hachyderm.io',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'bogus',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->expectsOutputToContain('3')
|
|
->expectsOutputToContain('2 valid')
|
|
->expectsOutputToContain('1 invalid')
|
|
->assertExitCode(1);
|
|
}
|
|
|
|
public function test_it_does_not_fail_fast_and_reports_every_invalid_row(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'bogus-one',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$second = Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => true,
|
|
'interval_seconds' => 0,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->expectsOutputToContain('bogus-one')
|
|
->expectsOutputToContain((string) $second->id)
|
|
->assertExitCode(1);
|
|
}
|
|
|
|
public function test_it_includes_the_validation_error_message_for_each_invalid_row(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'not-a-url',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->expectsOutputToContain('Invalid URL: not-a-url')
|
|
->assertExitCode(1);
|
|
}
|
|
|
|
public function test_summary_counts_are_accurate_when_mixed(): void
|
|
{
|
|
// 2 valid
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://hachyderm.io',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
// 3 invalid (different defects)
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'bogus-one',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://fosstodon.org',
|
|
'enabled' => true,
|
|
'interval_seconds' => 0,
|
|
'extras' => [],
|
|
]);
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'also-bad',
|
|
'enabled' => true,
|
|
'interval_seconds' => -5,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate')
|
|
->expectsOutputToContain('5')
|
|
->expectsOutputToContain('2 valid')
|
|
->expectsOutputToContain('3 invalid')
|
|
->assertExitCode(1);
|
|
}
|
|
|
|
public function test_it_exits_zero_with_enabled_only_when_no_enabled_instances_exist(): void
|
|
{
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => false,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate', ['--enabled-only' => true])
|
|
->assertExitCode(0);
|
|
}
|
|
|
|
public function test_it_exits_zero_with_an_enabled_only_flag_when_disabled_rows_are_invalid(): void
|
|
{
|
|
// A disabled row that would fail InstanceConfig validation
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'broken-and-disabled',
|
|
'enabled' => false,
|
|
'interval_seconds' => 0,
|
|
'extras' => [],
|
|
]);
|
|
|
|
// A valid enabled row
|
|
Instance::create([
|
|
'type' => InstanceType::Mastodon,
|
|
'url' => 'https://mastodon.social',
|
|
'enabled' => true,
|
|
'interval_seconds' => 600,
|
|
'extras' => [],
|
|
]);
|
|
|
|
$this->artisan('fedi-discover:validate', ['--enabled-only' => true])
|
|
->assertExitCode(0);
|
|
}
|
|
}
|