fedi-feed-router/tests/Feature/Livewire/ChannelsTest.php

200 lines
6.6 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Livewire;
use App\Livewire\Channels;
use App\Models\Language;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\PlatformInstance;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class ChannelsTest extends TestCase
{
use RefreshDatabase;
private function instanceWithActiveAccount(): PlatformInstance
{
$instance = PlatformInstance::factory()->create(['url' => 'https://lemmy.world']);
PlatformAccount::factory()->create([
'instance_url' => 'https://lemmy.world',
'is_active' => true,
]);
return $instance;
}
public function test_add_button_renders_when_channels_exist(): void
{
PlatformChannel::factory()->create(['name' => 'existing_channel']);
Livewire::test(Channels::class)
->assertSee('Add Channel')
->assertSee('existing_channel');
}
public function test_add_button_renders_in_empty_state(): void
{
Livewire::test(Channels::class)
->assertSee('No channels')
->assertSee('Add Channel');
}
public function test_open_create_modal_shows_modal(): void
{
Livewire::test(Channels::class)
->assertSet('showCreateModal', false)
->call('openCreateModal')
->assertSet('showCreateModal', true);
}
public function test_create_channel_requires_name(): void
{
$instance = $this->instanceWithActiveAccount();
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newPlatformInstanceId', $instance->id)
->call('createChannel')
->assertHasErrors(['newName' => 'required']);
}
public function test_create_channel_requires_platform_instance(): void
{
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'tech_community')
->call('createChannel')
->assertHasErrors(['newPlatformInstanceId' => 'required']);
}
public function test_create_channel_succeeds_and_attaches_account(): void
{
$instance = $this->instanceWithActiveAccount();
$language = Language::factory()->create();
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'tech_community')
->set('newPlatformInstanceId', $instance->id)
->set('newLanguageId', $language->id)
->set('newDescription', 'A tech community')
->call('createChannel')
->assertHasNoErrors()
->assertSet('showCreateModal', false);
$this->assertDatabaseHas('platform_channels', [
'name' => 'tech_community',
'display_name' => 'Tech_community',
'platform_instance_id' => $instance->id,
'language_id' => $language->id,
'description' => 'A tech community',
'is_active' => true,
]);
$channel = PlatformChannel::where('name', 'tech_community')->firstOrFail();
$this->assertCount(1, $channel->platformAccounts);
}
public function test_create_channel_leaves_description_null_when_blank(): void
{
$instance = $this->instanceWithActiveAccount();
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'tech_community')
->set('newPlatformInstanceId', $instance->id)
->call('createChannel')
->assertHasNoErrors();
$this->assertDatabaseHas('platform_channels', [
'name' => 'tech_community',
'description' => null,
]);
}
public function test_create_channel_rejects_non_slug_name(): void
{
$instance = $this->instanceWithActiveAccount();
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'Tech News')
->set('newPlatformInstanceId', $instance->id)
->call('createChannel')
->assertHasErrors(['newName' => 'regex']);
$this->assertDatabaseCount('platform_channels', 0);
}
public function test_create_channel_rejects_duplicate_name_on_same_instance(): void
{
$instance = $this->instanceWithActiveAccount();
PlatformChannel::factory()->create([
'platform_instance_id' => $instance->id,
'name' => 'tech_community',
]);
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'tech_community')
->set('newPlatformInstanceId', $instance->id)
->call('createChannel')
->assertHasErrors(['newName' => 'unique'])
->assertSet('showCreateModal', true);
$this->assertDatabaseCount('platform_channels', 1);
}
public function test_create_channel_allows_same_name_on_different_instance(): void
{
$instanceA = $this->instanceWithActiveAccount();
$instanceB = PlatformInstance::factory()->create(['url' => 'https://lemmy.other']);
PlatformAccount::factory()->create([
'instance_url' => 'https://lemmy.other',
'is_active' => true,
]);
PlatformChannel::factory()->create([
'platform_instance_id' => $instanceB->id,
'name' => 'tech_community',
]);
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'tech_community')
->set('newPlatformInstanceId', $instanceA->id)
->call('createChannel')
->assertHasNoErrors();
$this->assertDatabaseCount('platform_channels', 2);
}
public function test_create_channel_surfaces_no_active_accounts_error(): void
{
// Instance exists but has no active account for its url.
$instance = PlatformInstance::factory()->create(['url' => 'https://lemmy.world']);
Livewire::test(Channels::class)
->call('openCreateModal')
->set('newName', 'tech_community')
->set('newPlatformInstanceId', $instance->id)
->call('createChannel')
->assertHasErrors('newPlatformInstanceId')
->assertSet('showCreateModal', true);
$this->assertDatabaseCount('platform_channels', 0);
}
public function test_toggle_flips_active_state(): void
{
$channel = PlatformChannel::factory()->create(['is_active' => true]);
Livewire::test(Channels::class)
->call('toggle', $channel->id);
$this->assertFalse($channel->fresh()->is_active);
}
}