Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

54.6 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Enums\PostPlatform\ContentType;
use App\Enums\SocialAccount\Platform;
use App\Exceptions\PlatformUnavailableException;
use App\Exceptions\Social\ErrorCategory;
use App\Exceptions\Social\FacebookPublishException;
use App\Exceptions\TokenExpiredException;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Media\MediaOptimizer;
use App\Services\Social\FacebookPublisher;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Sleep;

/**
 * @return array<int, array<string, string>>
 */
function facebookVideoMedia(): array
{
    return [
        [
            'id' => 'test-media-video',
            'path' => 'media/2026-01/video.mp4',
            'url' => 'https://example.com/media/2026-01/video.mp4',
            'mime_type' => 'video/mp4',
            'original_filename' => 'video.mp4',
        ],
    ];
}

/**
 * Happy-path fakes for Meta's resumable video flow on a Page edge: start hands
 * back the rupload URL, rupload accepts the hosted file, the status poll
 * reports the fetch complete, finish publishes.
 *
 * @return array<string, mixed>
 */
function facebookVideoUploadFakes(string $edge): array
{
    $graph = config('trypost.platforms.facebook.graph_api');
    $rupload = 'https://'.config('trypost.platforms.facebook.rupload_host');

    return [
        "*/page_123/{$edge}" => Http::sequence()
            ->push([
                'video_id' => 'video_123',
                'upload_url' => "{$rupload}/video-upload/v25.0/video_123",
            ], 200)
            ->push(['success' => true, 'id' => 'reel_456', 'post_id' => 'story_456'], 200),
        "{$rupload}/*" => Http::response(['success' => true], 200),
        "{$graph}/video_123?fields=status*" => Http::response([
            'status' => ['video_status' => 'processing', 'uploading_phase' => ['status' => 'complete']],
        ], 200),
    ];
}

dataset('facebook resumable video formats', [
    'reel' => [ContentType::FacebookReel, 'video_reels'],
    'story' => [ContentType::FacebookStory, 'video_stories'],
]);

beforeEach(function () {
    Sleep::fake();

    $this->user = User::factory()->create();
    $this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);

    $this->socialAccount = SocialAccount::factory()->facebook()->create([
        'workspace_id' => $this->workspace->id,
        'platform_user_id' => 'page_123',
        'username' => 'myfbpage',
        'token_expires_at' => null, // Facebook page tokens don't expire
        'meta' => [
            'page_id' => 'page_123',
            'user_id' => 'fb_user_123',
        ],
    ]);

    $this->post = Post::factory()->forAccount($this->socialAccount)->create([
        'user_id' => $this->user->id,
        'content' => 'Check out this Facebook post!',
        'content_type' => ContentType::FacebookPost,
    ]);
    $this->post->setRelation('socialAccount', $this->socialAccount);

    $this->publisher = new FacebookPublisher;
});

test('facebook publisher can publish text only post', function () {
    Http::fake([
        '*/page_123/feed' => Http::response([
            'id' => 'page_123_post_456',
        ], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result)->toHaveKey('id');
    expect($result)->toHaveKey('url');
    expect($result['id'])->toBe('page_123_post_456');
    expect($result['url'])->toBe('https://www.facebook.com/page_123_post_456');

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/feed')
            && $request['message'] === 'Check out this Facebook post!'
            && ! array_key_exists('link', $request->data());
    });
});

test('facebook publisher can publish single image post', function () {
    $this->post->update([
        'media' => [
            [
                'id' => 'test-media-id',
                'path' => 'media/2026-01/image.jpg',
                'url' => 'https://example.com/media/2026-01/image.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image.jpg',
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response([
            'id' => 'photo_123',
            'post_id' => 'page_123_photo_post_456',
        ], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result)->toHaveKey('id');
    expect($result['id'])->toBe('page_123_photo_post_456');

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/photos')
            && $request['message'] === 'Check out this Facebook post!';
    });
});

test('facebook publisher can publish multi image post', function () {
    $mediaItems = [];
    for ($i = 1; $i <= 3; $i++) {
        $mediaItems[] = [
            'id' => "test-media-{$i}",
            'path' => "media/2026-01/image{$i}.jpg",
            'url' => "https://example.com/media/2026-01/image{$i}.jpg",
            'mime_type' => 'image/jpeg',
            'original_filename' => "image{$i}.jpg",
        ];
    }
    $this->post->update([
        'media' => $mediaItems]);

    Http::fake([
        '*/page_123/photos' => Http::sequence()
            ->push(['id' => 'photo_1'], 200)
            ->push(['id' => 'photo_2'], 200)
            ->push(['id' => 'photo_3'], 200),
        '*/page_123/feed' => Http::response([
            'id' => 'page_123_multi_post_789',
        ], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result)->toHaveKey('id');
    expect($result['id'])->toBe('page_123_multi_post_789');

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/feed')
            && str_contains($request->header('Content-Type')[0] ?? '', 'application/x-www-form-urlencoded')
            && ($request->data()['attached_media[0]'] ?? null) === json_encode(['media_fbid' => 'photo_1'])
            && ($request->data()['attached_media[1]'] ?? null) === json_encode(['media_fbid' => 'photo_2']);
    });
});

test('facebook publisher sends graph api requests as form-urlencoded not json', function () {
    $this->post->update([
        'media' => [
            [
                'id' => 'test-media-id',
                'path' => 'media/2026-01/image.jpg',
                'url' => 'https://example.com/media/2026-01/image.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image.jpg',
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response(['id' => 'photo_123', 'post_id' => 'post_123'], 200),
    ]);

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/photos')
            && str_contains($request->header('Content-Type')[0] ?? '', 'application/x-www-form-urlencoded')
            && ! str_contains($request->header('Content-Type')[0] ?? '', 'application/json');
    });
});

test('facebook publisher can publish video post', function () {
    $this->post->update([
        'media' => [
            [
                'id' => 'test-media-video',
                'path' => 'media/2026-01/video.mp4',
                'url' => 'https://example.com/media/2026-01/video.mp4',
                'mime_type' => 'video/mp4',
                'original_filename' => 'video.mp4',
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/videos' => Http::response([
            'id' => 'video_123',
        ], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result)->toHaveKey('id');
    expect($result['id'])->toBe('video_123');
    expect($result['url'])->toBe('https://www.facebook.com/page_123/videos/video_123');

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/videos')
            && $request['description'] === 'Check out this Facebook post!';
    });
});

test('facebook publisher fits an image story to 9:16 and publishes it through photo_stories', function () {
    Storage::fake();
    $this->post->update(['content_type' => ContentType::FacebookStory]);
    $this->post->update(['media' => [[
        'id' => 'test-media-story',
        'path' => 'media/2026-01/story.jpg',
        'url' => 'https://example.com/media/2026-01/story.jpg',
        'mime_type' => 'image/jpeg',
        'original_filename' => 'story.jpg',
    ]]]);

    $optimizer = Mockery::mock(MediaOptimizer::class);
    $optimizer->shouldReceive('fitToCanvas')->once()->with(Mockery::type('string'), 1080, 1920)->andReturnUsing(function (string $tempFile): string {
        $fitted = tempnam(sys_get_temp_dir(), 'fb_fit_');
        copy($tempFile, $fitted);

        return $fitted;
    });
    app()->instance(MediaOptimizer::class, $optimizer);

    Http::fake([
        '*/page_123/photos' => Http::response(['id' => 'photo_story_1'], 200),
        '*/page_123/photo_stories' => Http::response(['success' => true, 'post_id' => 'story_789'], 200),
        '*' => Http::response(file_get_contents(__DIR__.'/../../../fixtures/1x1.png'), 200, ['Content-Type' => 'image/png']),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result)->toBe([
        'id' => 'story_789',
        'url' => 'https://www.facebook.com/stories/page_123/story_789',
    ]);

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/photos')
        && $request['published'] === 'false'
        && str_contains((string) $request['url'], 'social-crops/')
        && ! str_contains((string) $request['url'], 'example.com'));
    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/photo_stories')
        && $request['photo_id'] === 'photo_story_1');
    Http::assertNotSent(fn ($request) => str_contains($request->url(), 'video_stories'));
});

test('facebook publisher fails an image story when the photo upload is refused', function () {
    Storage::fake();
    $this->post->update(['content_type' => ContentType::FacebookStory]);
    $this->post->update(['media' => [[
        'id' => 'test-media-story',
        'path' => 'media/2026-01/story.jpg',
        'url' => 'https://example.com/media/2026-01/story.jpg',
        'mime_type' => 'image/jpeg',
        'original_filename' => 'story.jpg',
    ]]]);

    $optimizer = Mockery::mock(MediaOptimizer::class);
    $optimizer->shouldReceive('fitToCanvas')->andReturnUsing(function (string $tempFile): string {
        $fitted = tempnam(sys_get_temp_dir(), 'fb_fit_');
        copy($tempFile, $fitted);

        return $fitted;
    });
    app()->instance(MediaOptimizer::class, $optimizer);

    Http::fake([
        '*/page_123/photos' => Http::response(['error' => ['message' => 'Invalid image', 'code' => 100]], 400),
        '*' => Http::response(file_get_contents(__DIR__.'/../../../fixtures/1x1.png'), 200, ['Content-Type' => 'image/png']),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook could not upload the story photo.');

    Http::assertNotSent(fn ($request) => str_contains($request->url(), 'photo_stories'));
});

test('facebook publisher rejects a reel without a video', function (array $media) {
    $this->post->update(['content_type' => ContentType::FacebookReel]);
    $this->post->update(['media' => $media]);

    Http::fake();

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook Reels require a video file.');

    Http::assertNothingSent();
})->with([
    'no media' => [[]],
    'image' => [[[
        'id' => 'test-media-image',
        'path' => 'media/2026-01/image.jpg',
        'url' => 'https://example.com/media/2026-01/image.jpg',
        'mime_type' => 'image/jpeg',
        'original_filename' => 'image.jpg',
    ]]],
]);

test('facebook publisher rejects a story without media', function () {
    $this->post->update(['content_type' => ContentType::FacebookStory]);
    $this->post->update(['media' => []]);

    Http::fake();

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook Stories require an image or a video file.');

    Http::assertNothingSent();
});

test('facebook publisher can publish reel', function () {
    $this->post->update(['content_type' => ContentType::FacebookReel]);
    $this->post->update(['media' => facebookVideoMedia()]);

    Http::fake(facebookVideoUploadFakes('video_reels'));

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('reel_456');
    expect($result['url'])->toBe('https://www.facebook.com/reel/reel_456');

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/video_reels')
        && $request['upload_phase'] === 'finish'
        && $request['video_id'] === 'video_123'
        && $request['video_state'] === 'PUBLISHED'
        && $request['description'] === 'Check out this Facebook post!');
});

test('facebook publisher sends no alt text with a video or a reel, which take none', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => [[...facebookVideoMedia()[0], 'meta' => ['alt_text' => 'a cyclist crossing a bridge']]]]);

    Http::fake(facebookVideoUploadFakes($edge));

    $this->publisher->publish($this->post);

    Http::assertSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}"));
    Http::assertNotSent(fn ($request) => array_key_exists('alt_text_custom', $request->data())
        || array_key_exists('alt_text', $request->data()));
})->with([
    'video' => [ContentType::FacebookPost, 'videos'],
    'reel' => [ContentType::FacebookReel, 'video_reels'],
]);

test('facebook publisher can publish video story', function () {
    $this->post->update(['content_type' => ContentType::FacebookStory]);
    $this->post->update(['media' => facebookVideoMedia()]);

    Http::fake(facebookVideoUploadFakes('video_stories'));

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('story_456');
    expect($result['url'])->toBe('https://www.facebook.com/stories/page_123/story_456');

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/video_stories')
        && $request['upload_phase'] === 'finish'
        && $request['video_id'] === 'video_123'
        && ! array_key_exists('video_state', $request->data()));
});

test('facebook publisher hands meta the hosted url and never downloads the video', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    Http::fake(facebookVideoUploadFakes($edge));

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        if (! str_contains($request->url(), config('trypost.platforms.facebook.rupload_host'))) {
            return false;
        }

        return $request->method() === 'POST'
            && ($request->header('file_url')[0] ?? null) === 'https://example.com/media/2026-01/video.mp4'
            && str_starts_with($request->header('Authorization')[0] ?? '', 'OAuth ')
            && $request->body() === '';
    });

    Http::assertNotSent(fn ($request) => str_contains($request->url(), 'example.com/media'));

    Http::assertNotSent(fn ($request) => $request->method() === 'POST'
        && str_contains($request->url(), config('trypost.platforms.facebook.graph_api').'/video_123'));

    Sleep::assertNeverSlept();

    expect(glob(sys_get_temp_dir().'/fb_reel_*') ?: [])->toBeEmpty();
})->with('facebook resumable video formats');

test('facebook publisher waits for meta to fetch the video before finishing', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => Http::sequence()
            ->push(['status' => ['video_status' => 'processing', 'uploading_phase' => ['status' => 'not_started']]], 200)
            ->push(['status' => ['video_status' => 'processing', 'uploading_phase' => ['status' => 'in_progress', 'bytes_transfered' => 1024]]], 200)
            ->push(['status' => ['video_status' => 'processing', 'uploading_phase' => ['status' => 'complete']]], 200),
    ]);

    $this->publisher->publish($this->post);

    Sleep::assertSleptTimes(2);
    Sleep::assertSequence([
        Sleep::for(5)->seconds(),
        Sleep::for(5)->seconds(),
    ]);
})->with('facebook resumable video formats');

test('facebook publisher retries a rupload failure meta documents as retryable', function (int $status, array $body) {
    $this->post->update(['content_type' => ContentType::FacebookReel]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $rupload = 'https://'.config('trypost.platforms.facebook.rupload_host');

    Http::fake([
        ...facebookVideoUploadFakes('video_reels'),
        "{$rupload}/*" => Http::response($body, $status),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(PlatformUnavailableException::class);

    Http::assertNotSent(fn ($request) => ($request['upload_phase'] ?? null) === 'finish');
})->with([
    'retriable flag' => [400, ['debug_info' => ['retriable' => true, 'type' => 'OtherError', 'message' => 'Temporary failure']]],
    'server error' => [503, ['debug_info' => ['retriable' => false, 'type' => 'ServiceUnavailable', 'message' => 'Service unavailable']]],
]);

test('facebook publisher fails a rupload error meta flags not retriable by its documented type', function (string $type, string $message, ErrorCategory $category) {
    $this->post->update(['content_type' => ContentType::FacebookReel]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $rupload = 'https://'.config('trypost.platforms.facebook.rupload_host');

    Http::fake([
        ...facebookVideoUploadFakes('video_reels'),
        "{$rupload}/*" => Http::response(['debug_info' => [
            'retriable' => false,
            'type' => $type,
            'message' => 'The video file could not be read from file_url.',
        ]], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(fn (FacebookPublishException $exception) => expect($exception->userMessage)
            ->toBe(__($message, ['platform' => Platform::Facebook->label()]))
            ->and($exception->category)->toBe($category)
            ->and($exception->platformErrorCode)->toBe($type));

    Http::assertNotSent(fn ($request) => ($request['upload_phase'] ?? null) === 'finish');
})->with([
    'processing failed' => ['ProcessingFailedError', 'posts.errors.facebook.processing_failed', ErrorCategory::MediaFormat],
    'partial request' => ['PartialRequestError', 'posts.errors.facebook.upload_incomplete', ErrorCategory::ServerError],
    'invalid upload offset' => ['OffsetInvalidError', 'posts.errors.facebook.upload_incomplete', ErrorCategory::ServerError],
    'undocumented type' => ['InvalidFileError', 'posts.errors.unrecognized_error', ErrorCategory::Unknown],
]);

test('facebook publisher fails when start does not return upload_url', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    Http::fake([
        "*/page_123/{$edge}" => Http::response(['video_id' => 'video_123'], 200),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook did not start the video upload. Please try again.');

    Http::assertSentCount(1);
})->with('facebook resumable video formats');

test('facebook publisher rejects an upload_url outside the rupload host', function (ContentType $contentType, string $edge, string $uploadUrl) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    Http::fake([
        "*/page_123/{$edge}" => Http::response([
            'video_id' => 'video_123',
            'upload_url' => $uploadUrl,
        ], 200),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook returned an invalid upload URL.');

    Http::assertSentCount(1);
})->with('facebook resumable video formats')->with([
    'another host' => 'https://evil.example/steal-token',
    'plain http' => 'http://rupload.facebook.com/video-upload/v25.0/video_123',
    'userinfo trick' => 'https://[email protected]/video-upload/v25.0/video_123',
    'not a url' => 'video_123',
]);

test('facebook publisher trusts the rupload host from config', function () {
    config()->set('trypost.platforms.facebook.rupload_host', 'rupload.example.test');

    $this->post->update(['content_type' => ContentType::FacebookStory]);
    $this->post->update(['media' => facebookVideoMedia()]);

    Http::fake([
        ...facebookVideoUploadFakes('video_stories'),
        'https://rupload.example.test/*' => Http::response(['success' => true], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('story_456');

    Http::assertSent(fn ($request) => str_starts_with($request->url(), 'https://rupload.example.test/'));
});

test('facebook publisher maps a rupload rejection and does not finish', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $rupload = 'https://'.config('trypost.platforms.facebook.rupload_host');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$rupload}/*" => Http::response(['error' => ['message' => 'Problem with file', 'code' => 6000]], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Problem with file. Try with another file.');

    Http::assertNotSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}")
        && $request['upload_phase'] === 'finish');
})->with('facebook resumable video formats');

test('facebook publisher does not finish when rupload does not confirm success', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $rupload = 'https://'.config('trypost.platforms.facebook.rupload_host');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$rupload}/*" => Http::response(['success' => false], 200),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook did not accept the video. Please try again.');

    Http::assertNotSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}")
        && $request['upload_phase'] === 'finish');
})->with('facebook resumable video formats');

test('facebook publisher reschedules when rupload cannot be reached', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $rupload = 'https://'.config('trypost.platforms.facebook.rupload_host');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$rupload}/*" => fn () => throw new ConnectionException('cURL error 28: Connection timed out after 10003 milliseconds'),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(function (PlatformUnavailableException $exception): void {
            expect($exception->retryDelaySeconds)->toBe(60)
                ->and($exception->getMessage())->toContain('video upload unreachable');
        });

    Http::assertNotSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}")
        && $request['upload_phase'] === 'finish');
})->with('facebook resumable video formats');

test('facebook publisher reschedules when the status check cannot be reached and keeps the token out of the message', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => fn ($request) => throw new ConnectionException("cURL error 28: Operation timed out for {$request->url()}"),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(function (PlatformUnavailableException $exception): void {
            expect($exception->getMessage())
                ->toContain('video status unreachable')
                ->toContain('access_token=[REDACTED]')
                ->not->toContain($this->socialAccount->access_token);
        });
})->with('facebook resumable video formats');

test('facebook publisher keeps polling the video status through a transient graph error', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => Http::sequence()
            ->push(['error' => ['message' => 'Service temporarily unavailable', 'code' => 2]], 500)
            ->push(['status' => ['video_status' => 'processing', 'uploading_phase' => ['status' => 'complete']]], 200),
    ]);

    $this->publisher->publish($this->post);

    Sleep::assertSleptTimes(1);
})->with('facebook resumable video formats');

test('facebook publisher stops polling the video status on a confirmed graph rejection', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => Http::response([
            'error' => ['message' => 'Unsupported get request.', 'type' => 'GraphMethodException', 'code' => 100],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Unsupported get request.');

    Sleep::assertNeverSlept();

    Http::assertNotSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}")
        && $request['upload_phase'] === 'finish');
})->with('facebook resumable video formats');

test('facebook publisher surfaces the video processing error instead of finishing', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => Http::response([
            'status' => [
                'video_status' => 'processing',
                'uploading_phase' => ['status' => 'complete'],
                'processing_phase' => [
                    'status' => 'not_started',
                    'error' => ['message' => 'Resolution too low. Video must have a minimum resolution of 540p.'],
                ],
            ],
        ], 200),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Resolution too low. Video must have a minimum resolution of 540p.');

    Http::assertNotSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}")
        && $request['upload_phase'] === 'finish');
})->with('facebook resumable video formats');

test('facebook publisher fails when the upload session expires', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => Http::response(['status' => ['video_status' => 'expired']], 200),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook could not process the video. Please try another file.');
})->with('facebook resumable video formats');

test('facebook publisher gives up on a video fetch that never completes', function (ContentType $contentType, string $edge) {
    $this->post->update(['content_type' => $contentType]);
    $this->post->update(['media' => facebookVideoMedia()]);

    $graph = config('trypost.platforms.facebook.graph_api');

    Http::fake([
        ...facebookVideoUploadFakes($edge),
        "{$graph}/video_123?fields=status*" => Http::response([
            'status' => ['video_status' => 'processing', 'uploading_phase' => ['status' => 'in_progress']],
        ], 200),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook took too long to fetch the video. Please try again.');

    Sleep::assertSleptTimes(59);

    Http::assertNotSent(fn ($request) => str_contains($request->url(), "/page_123/{$edge}")
        && $request['upload_phase'] === 'finish');
})->with('facebook resumable video formats');

test('facebook publisher reschedules a graph post when facebook cannot be reached', function () {
    Http::fake([
        '*/page_123/feed' => fn () => throw new ConnectionException('cURL error 28: Connection timed out'),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(PlatformUnavailableException::class);
});

test('facebook publisher throws exception on api error', function () {
    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => [
                'message' => 'Invalid request',
                'type' => 'GraphMethodException',
                'code' => 100,
            ],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(Exception::class);
});

test('facebook publisher throws token expired exception on oauth error', function () {
    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => [
                'message' => 'Error validating access token',
                'type' => 'OAuthException',
                'code' => 190,
            ],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(TokenExpiredException::class);
});

test('facebook publisher throws token expired exception on session expired subcode', function () {
    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => [
                'message' => 'Session has expired',
                'type' => 'OAuthException',
                'code' => 190,
                'error_subcode' => 463,
            ],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(TokenExpiredException::class);
});

test('facebook publisher throws exception for unsupported content type', function () {
    $this->post->update(['content_type' => ContentType::InstagramFeed]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(Exception::class, 'Unsupported Facebook content type');
});

test('facebook publisher throws exception when multi image upload fails', function () {
    $mediaItems = [];
    for ($i = 1; $i <= 3; $i++) {
        $mediaItems[] = [
            'id' => "test-media-{$i}",
            'path' => "media/2026-01/image{$i}.jpg",
            'url' => "https://example.com/media/2026-01/image{$i}.jpg",
            'mime_type' => 'image/jpeg',
            'original_filename' => "image{$i}.jpg",
        ];
    }
    $this->post->update([
        'media' => $mediaItems]);

    Http::fake([
        '*/page_123/photos' => Http::response([
            'error' => ['message' => 'Upload failed'],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(Exception::class, 'Failed to upload any images to Facebook');
});

test('facebook publisher publishes the multi image post with the photos facebook accepted', function () {
    $mediaItems = [];
    for ($i = 1; $i <= 3; $i++) {
        $mediaItems[] = [
            'id' => "test-media-{$i}",
            'path' => "media/2026-01/image{$i}.jpg",
            'url' => "https://example.com/media/2026-01/image{$i}.jpg",
            'mime_type' => 'image/jpeg',
            'original_filename' => "image{$i}.jpg",
        ];
    }
    $this->post->update(['media' => $mediaItems]);

    Http::fake([
        '*/page_123/photos' => Http::sequence()
            ->push(['id' => 'photo_1'], 200)
            ->push(['error' => ['message' => 'Upload failed', 'code' => 100]], 400)
            ->push(['id' => 'photo_3'], 200),
        '*/page_123/feed' => Http::response(['id' => 'page_123_partial_789'], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('page_123_partial_789');

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && ($request->data()['attached_media[0]'] ?? null) === json_encode(['media_fbid' => 'photo_1'])
        && ($request->data()['attached_media[1]'] ?? null) === json_encode(['media_fbid' => 'photo_3'])
        && ! array_key_exists('attached_media[2]', $request->data()));
});

test('facebook publisher rejects a text post that is only whitespace', function () {
    $this->post->update(['content' => "   \n\t "]);

    Http::fake();

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Facebook text posts require content');

    Http::assertNothingSent();
});

test('facebook publisher throws exception for unsupported media type', function () {
    $this->post->update([
        'media' => [
            [
                'id' => 'test-media-doc',
                'path' => 'media/2026-01/doc.pdf',
                'url' => 'https://example.com/media/2026-01/doc.pdf',
                'mime_type' => 'application/pdf',
                'original_filename' => 'doc.pdf',
            ],
        ],
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(Exception::class, 'Unsupported media type for Facebook');
});

test('facebook publisher throws exception for text post with null content', function () {
    $this->post->update(['content' => null]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(Exception::class, 'Facebook text posts require content');
});

test('facebook publisher can publish single image with null content', function () {
    $this->post->update([
        'content' => null,
        'media' => [
            [
                'id' => 'test-media-id',
                'path' => 'media/2026-01/test-image.jpg',
                'url' => 'https://example.com/media/2026-01/test-image.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'test.jpg',
            ],
        ],
    ]);

    Http::fake([
        'https://graph.facebook.com/v25.0/page_123/photos' => Http::response([
            'id' => 'photo-123',
            'post_id' => 'post-123',
        ], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('post-123');
});

test('single image post without caption omits message from payload', function () {
    $this->post->update([
        'content' => null,
        'media' => [
            [
                'id' => 'test-media-id',
                'path' => 'media/2026-01/image.jpg',
                'url' => 'https://example.com/media/2026-01/image.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image.jpg',
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response(['id' => 'photo-123', 'post_id' => 'post-123'], 200),
    ]);

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/photos')
            && ! array_key_exists('message', $request->data());
    });
});

test('multi-image post without caption omits message from payload', function () {
    $mediaItems = [];
    for ($i = 1; $i <= 2; $i++) {
        $mediaItems[] = [
            'id' => "test-media-{$i}",
            'path' => "media/2026-01/image{$i}.jpg",
            'url' => "https://example.com/media/2026-01/image{$i}.jpg",
            'mime_type' => 'image/jpeg',
            'original_filename' => "image{$i}.jpg",
        ];
    }

    $this->post->update(['content' => null, 'media' => $mediaItems]);

    Http::fake([
        '*/page_123/photos' => Http::sequence()
            ->push(['id' => 'photo_1'], 200)
            ->push(['id' => 'photo_2'], 200),
        '*/page_123/feed' => Http::response(['id' => 'multi_post_789'], 200),
    ]);

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/feed')
            && ! array_key_exists('message', $request->data());
    });
});

test('video post without description omits description from payload', function () {
    $this->post->update([
        'content' => null,
        'media' => [
            [
                'id' => 'test-media-video',
                'path' => 'media/2026-01/video.mp4',
                'url' => 'https://example.com/media/2026-01/video.mp4',
                'mime_type' => 'video/mp4',
                'original_filename' => 'video.mp4',
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/videos' => Http::response(['id' => 'video_123'], 200),
    ]);

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/videos')
            && ! array_key_exists('description', $request->data());
    });
});

test('reel post without description omits description from payload (finish phase)', function () {
    $this->post->update(['content_type' => ContentType::FacebookReel]);

    $this->post->update([
        'content' => null,
        'media' => [
            [
                'id' => 'test-media-reel',
                'path' => 'media/2026-01/reel.mp4',
                'url' => 'https://example.com/media/2026-01/reel.mp4',
                'mime_type' => 'video/mp4',
                'original_filename' => 'reel.mp4',
            ],
        ],
    ]);

    Http::fake(facebookVideoUploadFakes('video_reels'));

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        if (! str_contains($request->url(), '/page_123/video_reels')) {
            return false;
        }

        $data = $request->data();

        return data_get($data, 'upload_phase') === 'finish'
            && ! array_key_exists('description', $data);
    });
});

test('facebook single image post publishes the original image and ignores a legacy aspect ratio', function (?string $legacyRatio) {
    Storage::fake();

    $this->post->update(['meta' => $legacyRatio === null ? [] : ['aspect_ratio' => $legacyRatio]]);
    $this->post->update([
        'media' => [
            ['id' => 'm1', 'path' => 'media/a.jpg', 'url' => 'https://example.com/media/a.jpg', 'mime_type' => 'image/jpeg', 'original_filename' => 'a.jpg'],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response(['id' => 'photo_1', 'post_id' => 'post_1'], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('post_1')
        ->and(Storage::allFiles())->toBeEmpty();

    Http::assertSentCount(1);
    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/photos')
        && $request['url'] === 'https://example.com/media/a.jpg');
    Http::assertNotSent(fn ($request) => $request->url() === 'https://example.com/media/a.jpg');
})->with([
    'no ratio' => [null],
    'legacy 1:1' => ['1:1'],
    'legacy 4:5' => ['4:5'],
    'legacy 16:9' => ['16:9'],
    'legacy original' => ['original'],
]);

test('facebook multi image post uploads every original image and ignores a legacy aspect ratio', function () {
    Storage::fake();

    $this->post->update(['meta' => ['aspect_ratio' => '4:5']]);
    $this->post->update([
        'media' => [
            ['id' => 'm1', 'path' => 'media/a.jpg', 'url' => 'https://example.com/media/a.jpg', 'mime_type' => 'image/jpeg', 'original_filename' => 'a.jpg'],
            ['id' => 'm2', 'path' => 'media/b.jpg', 'url' => 'https://example.com/media/b.jpg', 'mime_type' => 'image/jpeg', 'original_filename' => 'b.jpg'],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::sequence()
            ->push(['id' => 'up_1'], 200)
            ->push(['id' => 'up_2'], 200),
        '*/page_123/feed' => Http::response(['id' => 'post_1'], 200),
    ]);

    $this->publisher->publish($this->post);

    expect(Storage::allFiles())->toBeEmpty();

    $photoUrls = collect(Http::recorded())
        ->map(fn (array $pair) => $pair[0])
        ->filter(fn ($request) => str_contains($request->url(), '/page_123/photos'))
        ->map(fn ($request) => [$request['url'], $request['published']])
        ->values()
        ->all();

    expect($photoUrls)->toBe([
        ['https://example.com/media/a.jpg', 'false'],
        ['https://example.com/media/b.jpg', 'false'],
    ]);

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && $request['attached_media[0]'] === json_encode(['media_fbid' => 'up_1'])
        && $request['attached_media[1]'] === json_encode(['media_fbid' => 'up_2']));
    Http::assertNotSent(fn ($request) => str_starts_with($request->url(), 'https://example.com/'));
});

test('facebook publisher sends capped alt text on single image post', function () {
    $longAlt = str_repeat('a', 1500);

    $this->post->update([
        'media' => [
            [
                'id' => 'test-media-id',
                'path' => 'media/2026-01/image.jpg',
                'url' => 'https://example.com/media/2026-01/image.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image.jpg',
                'meta' => ['alt_text' => $longAlt],
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response(['id' => 'photo_123', 'post_id' => 'post_123'], 200),
    ]);

    $this->publisher->publish($this->post);

    $expectedAlt = mb_substr($longAlt, 0, Platform::Facebook->altTextMaxLength());

    Http::assertSent(function ($request) use ($expectedAlt) {
        return str_contains($request->url(), '/page_123/photos')
            && data_get($request->data(), 'alt_text_custom') === $expectedAlt
            && strlen($expectedAlt) === Platform::Facebook->altTextMaxLength();
    });
});

test('facebook publisher sends capped alt text for each image in multi image post', function () {
    $longAlt1 = str_repeat('b', 1200);
    $longAlt2 = str_repeat('c', 1200);

    $this->post->update([
        'media' => [
            [
                'id' => 'm1',
                'path' => 'media/2026-01/image1.jpg',
                'url' => 'https://example.com/media/2026-01/image1.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image1.jpg',
                'meta' => ['alt_text' => $longAlt1],
            ],
            [
                'id' => 'm2',
                'path' => 'media/2026-01/image2.jpg',
                'url' => 'https://example.com/media/2026-01/image2.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image2.jpg',
                'meta' => ['alt_text' => $longAlt2],
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::sequence()
            ->push(['id' => 'photo_1'], 200)
            ->push(['id' => 'photo_2'], 200),
        '*/page_123/feed' => Http::response(['id' => 'multi_post_alt'], 200),
    ]);

    $this->publisher->publish($this->post);

    $expectedAlt1 = mb_substr($longAlt1, 0, Platform::Facebook->altTextMaxLength());
    $expectedAlt2 = mb_substr($longAlt2, 0, Platform::Facebook->altTextMaxLength());

    Http::assertSent(function ($request) use ($expectedAlt1) {
        return str_contains($request->url(), '/page_123/photos')
            && data_get($request->data(), 'alt_text_custom') === $expectedAlt1;
    });

    Http::assertSent(function ($request) use ($expectedAlt2) {
        return str_contains($request->url(), '/page_123/photos')
            && data_get($request->data(), 'alt_text_custom') === $expectedAlt2;
    });
});

test('facebook publisher omits alt_text_custom from photos payload when no alt text is set', function () {
    $this->post->update([
        'media' => [
            [
                'id' => 'test-media-id',
                'path' => 'media/2026-01/image.jpg',
                'url' => 'https://example.com/media/2026-01/image.jpg',
                'mime_type' => 'image/jpeg',
                'original_filename' => 'image.jpg',
            ],
        ],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response(['id' => 'photo_123', 'post_id' => 'post_123'], 200),
    ]);

    $this->publisher->publish($this->post);

    Http::assertSent(function ($request) {
        return str_contains($request->url(), '/page_123/photos')
            && ! array_key_exists('alt_text_custom', $request->data());
    });
});

test('facebook publisher keeps links intact', function () {
    config()->set('trypost.platforms.x.defuse_links', true);

    $this->post->update(['content' => 'New post: https://acme.com/blog']);

    Http::fake(['*/page_123/feed' => Http::response(['id' => 'page_123_post_456'], 200)]);

    $this->publisher->publish($this->post);

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && $request['message'] === 'New post: https://acme.com/blog'
        && $request['link'] === 'https://acme.com/blog');
});

test('facebook publisher sends no link when the user dropped the link preview', function () {
    $this->post->update(['content' => 'New post: https://acme.com/blog']);
    $this->post->update(['meta' => ['link_preview' => false]]);

    Http::fake(['*/page_123/feed' => Http::response(['id' => 'page_123_post_456'], 200)]);

    $this->publisher->publish($this->post);

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && $request['message'] === 'New post: https://acme.com/blog'
        && ! array_key_exists('link', $request->data()));
});

test('facebook publisher retries a text post without the link when the scrape fails', function () {
    $this->post->update(['content' => 'Read https://example.com/post today']);

    Http::fake([
        '*/page_123/feed' => Http::sequence()
            ->push(['error' => ['message' => 'There was a problem scraping the URL.', 'code' => 1609005]], 400)
            ->push(['id' => 'page_123_post_456'], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('page_123_post_456');

    $feed = collect(Http::recorded())
        ->map(fn (array $pair) => $pair[0])
        ->filter(fn ($request) => str_contains($request->url(), '/page_123/feed'))
        ->values();

    expect($feed)->toHaveCount(2)
        ->and($feed[0]['link'])->toBe('https://example.com/post')
        ->and($feed[1]->data())->not->toHaveKey('link')
        ->and($feed[1]['message'])->toBe('Read https://example.com/post today');
});

test('facebook publisher does not log an error when the rejected link is published without it', function () {
    $this->post->update(['content' => 'Read https://example.com/post today']);

    Log::spy();

    Http::fake([
        '*/page_123/feed' => Http::sequence()
            ->push(['error' => ['message' => 'There was a problem scraping the URL.', 'code' => 1609005]], 400)
            ->push(['id' => 'page_123_post_456'], 200),
    ]);

    $this->publisher->publish($this->post);

    Log::shouldHaveReceived('warning')->once();
    Log::shouldNotHaveReceived('error');
});

test('facebook publisher logs an error when a text post fails for a reason other than the link', function () {
    $this->post->update(['content' => 'Read https://example.com/post']);

    Log::spy();

    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => ['message' => 'Duplicate post', 'code' => 506],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class);

    Log::shouldHaveReceived('error')->once();
    Log::shouldNotHaveReceived('warning');
});

test('facebook publisher does not drop the link on an unrelated feed error', function () {
    $this->post->update(['content' => 'Read https://example.com/post']);

    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => ['message' => 'Duplicate post', 'code' => 506],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class);

    Http::assertSentCount(1);
});

test('facebook publisher does not send a facebook-owned url as the link', function (string $url) {
    $this->post->update(['content' => "See {$url}"]);

    Http::fake(['*/page_123/feed' => Http::response(['id' => 'page_123_post_456'], 200)]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('page_123_post_456');

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && $request['message'] === "See {$url}"
        && ! array_key_exists('link', $request->data()));

    Http::assertSentCount(1);
})->with([
    'www' => 'https://www.facebook.com/somepage',
    'mobile' => 'https://m.facebook.com/somepage',
    'apex' => 'https://facebook.com/events/1',
    'fb.com' => 'https://fb.com/somepage',
    'fb.me' => 'https://fb.me/abc',
    'subdomain' => 'https://l.facebook.com/l.php?u=https://example.com',
]);

test('facebook publisher uses the first url that facebook can preview', function () {
    $this->post->update(['content' => 'See https://www.facebook.com/page and https://example.com/post.']);

    Http::fake(['*/page_123/feed' => Http::response(['id' => 'page_123_post_456'], 200)]);

    $this->publisher->publish($this->post);

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && $request['message'] === 'See https://www.facebook.com/page and https://example.com/post.'
        && $request['link'] === 'https://example.com/post');
});

test('facebook publisher still attaches a link on a host that only looks like facebook', function () {
    $this->post->update(['content' => 'See https://notfacebook.com/post']);

    Http::fake(['*/page_123/feed' => Http::response(['id' => 'page_123_post_456'], 200)]);

    $this->publisher->publish($this->post);

    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/feed')
        && $request['link'] === 'https://notfacebook.com/post');
});

test('facebook publisher retries without the link when facebook rejects the url', function (array $error) {
    $this->post->update(['content' => 'Read https://example.com/post today']);

    Http::fake([
        '*/page_123/feed' => Http::sequence()
            ->push(['error' => $error], 400)
            ->push(['id' => 'page_123_post_456'], 200),
    ]);

    $result = $this->publisher->publish($this->post);

    expect($result['id'])->toBe('page_123_post_456');

    $feed = collect(Http::recorded())
        ->map(fn (array $pair) => $pair[0])
        ->filter(fn ($request) => str_contains($request->url(), '/page_123/feed'))
        ->values();

    expect($feed)->toHaveCount(2)
        ->and($feed[0]['link'])->toBe('https://example.com/post')
        ->and($feed[1]->data())->not->toHaveKey('link');
})->with([
    'invalid url' => [['message' => 'The url you supplied is invalid', 'code' => 1500]],
    'facebook.com link' => [['message' => 'Permissions error', 'code' => 200, 'error_subcode' => 1609008]],
]);

test('facebook publisher does not drop the link on a permissions error without the facebook-url subcode', function () {
    $this->post->update(['content' => 'Read https://example.com/post']);

    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => ['message' => 'Permissions error', 'code' => 200],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class);

    Http::assertSentCount(1);
});

test('facebook publisher fails the post when the retry without the link is rejected too', function () {
    $this->post->update(['content' => 'Read https://example.com/post']);

    Log::spy();

    Http::fake([
        '*/page_123/feed' => Http::sequence()
            ->push(['error' => ['message' => 'There was a problem scraping the URL.', 'code' => 1609005]], 400)
            ->push(['error' => ['message' => 'Duplicate post', 'code' => 506]], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(FacebookPublishException::class, 'Duplicate post detected. Please modify content.');

    Http::assertSentCount(2);
    Log::shouldHaveReceived('warning')->once();
    Log::shouldHaveReceived('error')->once();
});

test('facebook publisher does not retry a text post with a link when the token is dead', function () {
    $this->post->update(['content' => 'Read https://example.com/post']);

    Http::fake([
        '*/page_123/feed' => Http::response([
            'error' => ['message' => 'Invalid OAuth access token.', 'code' => 190],
        ], 400),
    ]);

    expect(fn () => $this->publisher->publish($this->post))
        ->toThrow(TokenExpiredException::class);

    Http::assertSentCount(1);
});

test('facebook publisher does not attach a link card when the post has media', function () {
    $this->post->update([
        'content' => 'Read https://example.com/post',
        'media' => [[
            'id' => 'test-media-id',
            'path' => 'media/2026-01/image.jpg',
            'url' => 'https://example.com/media/2026-01/image.jpg',
            'mime_type' => 'image/jpeg',
            'original_filename' => 'image.jpg',
        ]],
    ]);

    Http::fake([
        '*/page_123/photos' => Http::response([
            'id' => 'photo_123',
            'post_id' => 'page_123_photo_post_456',
        ], 200),
    ]);

    $this->publisher->publish($this->post);

    Http::assertNotSent(fn ($request) => str_contains($request->url(), '/page_123/feed'));
    Http::assertSent(fn ($request) => str_contains($request->url(), '/page_123/photos')
        && ! array_key_exists('link', $request->data()));
});