141 lines
5.1 KiB
PHP
141 lines
5.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Enums\PageStatusEnum;
|
||
|
|
use App\Listeners\UrlDiscoveredListener;
|
||
|
|
use App\Models\Page;
|
||
|
|
use App\Models\PageLink;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
use Illuminate\Events\CallQueuedListener;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\Queue;
|
||
|
|
use Lvl0\FediDiscover\Config\InstanceType;
|
||
|
|
use Lvl0\FediDiscover\Events\UrlDiscovered;
|
||
|
|
use Lvl0\FediDiscover\Models\Instance;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class UrlDiscoveryTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Helpers
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
private function makeInstance(): Instance
|
||
|
|
{
|
||
|
|
return Instance::factory()
|
||
|
|
->type(InstanceType::Mastodon)
|
||
|
|
->enabled()
|
||
|
|
->create();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function makeEvent(Instance $instance, array $overrides = []): UrlDiscovered
|
||
|
|
{
|
||
|
|
return new UrlDiscovered(
|
||
|
|
url: $overrides['url'] ?? 'https://example-blog.com/article',
|
||
|
|
instanceId: $overrides['instanceId'] ?? $instance->id,
|
||
|
|
discoveredAt: $overrides['discoveredAt'] ?? CarbonImmutable::parse('2026-04-26T12:00:00Z'),
|
||
|
|
postUrl: array_key_exists('postUrl', $overrides) ? $overrides['postUrl'] : 'https://mastodon.social/@alice/109876543210',
|
||
|
|
postBody: array_key_exists('postBody', $overrides) ? $overrides['postBody'] : 'check this out https://example-blog.com/article',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Test 9 — happy path
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
public function test_listener_creates_target_page_and_source_page_with_link(): void
|
||
|
|
{
|
||
|
|
$instance = $this->makeInstance();
|
||
|
|
$discoveredAt = CarbonImmutable::parse('2026-04-26T12:00:00Z');
|
||
|
|
|
||
|
|
$event = new UrlDiscovered(
|
||
|
|
url: 'https://example-blog.com/article',
|
||
|
|
instanceId: $instance->id,
|
||
|
|
discoveredAt: $discoveredAt,
|
||
|
|
postUrl: 'https://mastodon.social/@alice/109876543210',
|
||
|
|
postBody: 'check this out https://example-blog.com/article',
|
||
|
|
);
|
||
|
|
|
||
|
|
event($event);
|
||
|
|
|
||
|
|
// Target page
|
||
|
|
$targetPage = Page::where('url', 'https://example-blog.com/article')->first();
|
||
|
|
$this->assertNotNull($targetPage);
|
||
|
|
$this->assertSame(PageStatusEnum::Discovered, $targetPage->status);
|
||
|
|
$this->assertSame($instance->id, $targetPage->instance_id);
|
||
|
|
|
||
|
|
// Source page
|
||
|
|
$sourcePage = Page::where('url', 'https://mastodon.social/@alice/109876543210')->first();
|
||
|
|
$this->assertNotNull($sourcePage);
|
||
|
|
$this->assertSame(PageStatusEnum::Fetched, $sourcePage->status);
|
||
|
|
$this->assertSame($instance->id, $sourcePage->instance_id);
|
||
|
|
$this->assertNotNull($sourcePage->fetched_at);
|
||
|
|
$this->assertTrue($discoveredAt->equalTo($sourcePage->fetched_at));
|
||
|
|
|
||
|
|
// Edge
|
||
|
|
$link = PageLink::where('source_page_id', $sourcePage->id)
|
||
|
|
->where('target_page_id', $targetPage->id)
|
||
|
|
->first();
|
||
|
|
$this->assertNotNull($link);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Test 10 — idempotency
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
public function test_listener_is_idempotent_on_repeated_event(): void
|
||
|
|
{
|
||
|
|
$instance = $this->makeInstance();
|
||
|
|
$event = $this->makeEvent($instance);
|
||
|
|
|
||
|
|
event($event);
|
||
|
|
event($event);
|
||
|
|
|
||
|
|
$this->assertSame(2, Page::count());
|
||
|
|
$this->assertSame(1, PageLink::count());
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Test 11 — null postUrl: only target page, no edge
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
public function test_listener_with_null_post_url_creates_only_target_page(): void
|
||
|
|
{
|
||
|
|
$instance = $this->makeInstance();
|
||
|
|
$event = $this->makeEvent($instance, ['postUrl' => null, 'postBody' => null]);
|
||
|
|
|
||
|
|
event($event);
|
||
|
|
|
||
|
|
$this->assertSame(1, Page::count());
|
||
|
|
$this->assertSame(0, PageLink::count());
|
||
|
|
|
||
|
|
$targetPage = Page::where('url', 'https://example-blog.com/article')->first();
|
||
|
|
$this->assertNotNull($targetPage);
|
||
|
|
$this->assertSame(PageStatusEnum::Discovered, $targetPage->status);
|
||
|
|
$this->assertSame($instance->id, $targetPage->instance_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Test 12 — listener is queued, not run inline
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
public function test_listener_is_pushed_to_queue_not_run_inline(): void
|
||
|
|
{
|
||
|
|
Queue::fake();
|
||
|
|
|
||
|
|
$instance = $this->makeInstance();
|
||
|
|
$event = $this->makeEvent($instance);
|
||
|
|
|
||
|
|
event($event);
|
||
|
|
|
||
|
|
Queue::assertPushed(CallQueuedListener::class, function (CallQueuedListener $job): bool {
|
||
|
|
return $job->class === UrlDiscoveredListener::class;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|