trove/packages/Lvl0/FediDiscover/tests/Feature/InstanceModelTest.php

113 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Tests\TestCase;
class InstanceModelTest extends TestCase
{
use RefreshDatabase;
public function test_it_persists_and_retrieves_an_instance(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
]);
$instance = Instance::first();
$this->assertNotNull($instance);
$this->assertSame(InstanceType::Mastodon, $instance->type);
$this->assertSame('https://mastodon.social', $instance->url);
$this->assertTrue($instance->enabled);
$this->assertSame(600, $instance->interval_seconds);
$this->assertSame(['token' => 'abc123'], $instance->extras);
}
public function test_enabled_is_fillable_and_cast_to_boolean(): void
{
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => false,
'interval_seconds' => 600,
]);
$this->assertFalse($instance->fresh()->enabled);
}
public function test_last_polled_at_is_fillable_and_cast_to_datetime(): void
{
$polledAt = Carbon::parse('2026-04-23 12:00:00');
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'last_polled_at' => $polledAt,
]);
$fresh = $instance->fresh();
$this->assertInstanceOf(Carbon::class, $fresh->last_polled_at);
$this->assertTrue($fresh->last_polled_at->equalTo($polledAt));
}
public function test_last_seen_id_defaults_to_null(): void
{
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
]);
$this->assertNull($instance->fresh()->last_seen_id);
}
public function test_last_seen_id_is_fillable_and_persists_as_string(): void
{
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'last_seen_id' => '109876543210',
]);
$this->assertSame('109876543210', $instance->fresh()->last_seen_id);
}
public function test_enabled_scope_returns_only_enabled_instances(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://enabled.example',
'enabled' => true,
'interval_seconds' => 600,
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://disabled.example',
'enabled' => false,
'interval_seconds' => 600,
]);
$enabled = Instance::enabled()->get();
$this->assertCount(1, $enabled);
$this->assertSame('https://enabled.example', $enabled->first()->url);
}
}