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.

17.4 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Enums\Post\PublishStatus;
use App\Enums\PostPlatform\ContentType;
use App\Enums\Repurpose\ItemStatus;
use App\Enums\Repurpose\PauseReason;
use App\Enums\Repurpose\PublishMode;
use App\Enums\Repurpose\SourceFormat;
use App\Enums\Repurpose\Status;
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status as AccountStatus;
use App\Enums\TikTok\PrivacyLevel;
use App\Jobs\Analytics\BootstrapAccountAnalytics;
use App\Jobs\Analytics\CollectAccountDailySnapshot;
use App\Models\Post;
use App\Models\Repurpose;
use App\Models\RepurposeItem;
use App\Models\SocialAccount;
use Illuminate\Support\Facades\Queue;
use Symfony\Component\HttpFoundation\Response;

beforeEach(function () {
    Queue::fake([BootstrapAccountAnalytics::class, CollectAccountDailySnapshot::class]);

    ['plain_token' => $this->token, 'workspace' => $this->workspace] = createApiTestToken();

    $this->source = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::Instagram]);
    $this->tiktok = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::TikTok]);
});

function apiHeaders(string $token): array
{
    return ['Authorization' => "Bearer {$token}", 'Accept' => 'application/json'];
}

function tiktokDestinationPayload(SocialAccount $account): array
{
    return [
        'social_account_id' => $account->id,
        'content_type' => ContentType::TikTokVideo->value,
        'meta' => ['privacy_level' => PrivacyLevel::PublicToEveryone->value],
    ];
}

test('a repurpose can be created, read, updated and deleted', function () {
    $created = $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'source_format' => SourceFormat::Story->value,
            'destinations' => [tiktokDestinationPayload($this->tiktok)],
        ])
        ->assertCreated()
        ->json();

    expect($created['source_format'])->toBe('story')
        ->and($created['status'])->toBe('draft');

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.show', $created['id']))
        ->assertOk()
        ->assertJsonPath('source_social_account_id', $this->source->id);

    $this->withHeaders(apiHeaders($this->token))
        ->putJson(route('api.repurposes.update', $created['id']), [
            'source_social_account_id' => $this->source->id,
            'source_format' => SourceFormat::Reel->value,
            'destinations' => [tiktokDestinationPayload($this->tiktok)],
        ])
        ->assertOk()
        ->assertJsonPath('source_format', 'reel');

    $this->withHeaders(apiHeaders($this->token))
        ->deleteJson(route('api.repurposes.destroy', $created['id']))
        ->assertNoContent();

    expect(Repurpose::count())->toBe(0);
});

test('destination meta survives a round trip through the api', function () {
    $destination = tiktokDestinationPayload($this->tiktok);

    $id = $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [$destination],
        ])
        ->assertCreated()
        ->json('id');

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.show', $id))
        ->assertOk()
        ->assertJsonPath('destinations.0.meta.privacy_level', PrivacyLevel::PublicToEveryone->value);
});

test('a draft accepts a destination that is still missing its required meta', function () {
    $id = $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [[
                'social_account_id' => $this->tiktok->id,
                'content_type' => ContentType::TikTokVideo->value,
                'meta' => [],
            ]],
        ])
        ->assertCreated()
        ->json('id');

    $this->withHeaders(apiHeaders($this->token))
        ->putJson(route('api.repurposes.update', $id), [
            'destinations' => [[
                'social_account_id' => $this->tiktok->id,
                'content_type' => ContentType::TikTokVideo->value,
                'meta' => [],
            ]],
        ])
        ->assertOk();

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.activate', $id))
        ->assertUnprocessable()
        ->assertJsonValidationErrors('destinations');
});

test('an active repurpose cannot drop the meta its destination needs to publish', function () {
    $repurpose = Repurpose::factory()->for($this->workspace)->create([
        'source_social_account_id' => $this->source->id,
        'status' => Status::Active,
        'destinations' => [tiktokDestinationPayload($this->tiktok)],
    ]);

    $this->withHeaders(apiHeaders($this->token))
        ->putJson(route('api.repurposes.update', $repurpose), [
            'destinations' => [[
                'social_account_id' => $this->tiktok->id,
                'content_type' => ContentType::TikTokVideo->value,
                'meta' => [],
            ]],
        ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('destinations.0.meta.privacy_level');
});

test('a destination format that cannot carry a video is rejected', function () {
    $pinterest = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::Pinterest]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [[
                'social_account_id' => $pinterest->id,
                'content_type' => ContentType::PinterestPin->value,
            ]],
        ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('destinations.0.content_type');
});

test('the index lists the workspace repurposes', function () {
    foreach ([SourceFormat::Reel, SourceFormat::Story] as $format) {
        Repurpose::factory()->create([
            'workspace_id' => $this->workspace->id,
            'source_social_account_id' => $this->source->id,
            'source_format' => $format,
        ]);
    }

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.index'))
        ->assertOk()
        ->assertJsonCount(2, 'data')
        ->assertJsonPath('meta.per_page', (int) config('app.pagination.default'))
        ->assertJsonPath('data.0.source_account.id', $this->source->id);
});

test('the status transitions are exposed', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [tiktokDestinationPayload($this->tiktok)],
    ]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.activate', $repurpose))
        ->assertOk()
        ->assertJsonPath('status', Status::Active->value);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.pause', $repurpose))
        ->assertOk()
        ->assertJsonPath('status', Status::Paused->value);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.resume', $repurpose))
        ->assertOk()
        ->assertJsonPath('status', Status::Active->value);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.disable', $repurpose))
        ->assertOk()
        ->assertJsonPath('status', Status::Disabled->value);
});

test('activating without a destination fails validation', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
    ]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.activate', $repurpose))
        ->assertUnprocessable();
});

test('items are paginated at the documented page size', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
    ]);

    RepurposeItem::factory()->count(30)->for($repurpose)->create(['status' => ItemStatus::Published]);

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.items', $repurpose))
        ->assertOk()
        ->assertJsonCount((int) config('app.pagination.default'), 'data')
        ->assertJsonPath('meta.total', 30);
});

test('a repurpose from another workspace is not reachable', function () {
    $stranger = Repurpose::factory()->create();

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.show', $stranger))
        ->assertNotFound();
});

test('updating a repurpose from another workspace is not found before any validation', function () {
    $stranger = Repurpose::factory()->create();

    $this->withHeaders(apiHeaders($this->token))
        ->putJson(route('api.repurposes.update', $stranger), [
            'source_social_account_id' => 'not-an-account',
            'destinations' => 'nope',
        ])
        ->assertNotFound();
});

test('an account from another workspace is rejected as a source', function () {
    $stranger = SocialAccount::factory()->create(['platform' => Platform::Instagram]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), ['source_social_account_id' => $stranger->id])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('source_social_account_id');
});

test('an account from another workspace is rejected as a destination', function () {
    $stranger = SocialAccount::factory()->create(['platform' => Platform::TikTok]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [tiktokDestinationPayload($stranger)],
        ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('destinations.0.social_account_id');
});

test('a network we cannot download from is rejected as a source', function () {
    $tiktokSource = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::YouTube]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), ['source_social_account_id' => $tiktokSource->id])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('source_social_account_id');
});

test('a content type from another network is rejected for a destination', function () {
    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [[
                'social_account_id' => $this->tiktok->id,
                'content_type' => ContentType::YouTubeShort->value,
            ]],
        ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors('destinations.0.content_type');
});

test('a poll is refused as the format a repurposed video publishes as', function (string $platform, ContentType $type) {
    $account = SocialAccount::factory()->for($this->workspace)->{$platform}()->create();

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [[
                'social_account_id' => $account->id,
                'content_type' => $type->value,
            ]],
        ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['destinations.0.content_type' => __('repurposes.errors.destination_needs_video')]);
})->with([
    'x poll' => ['x', ContentType::XPoll],
    'mastodon poll' => ['mastodon', ContentType::MastodonPoll],
    'threads poll' => ['threads', ContentType::ThreadsPoll],
]);

test('the api refuses a transition the interface would never offer', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [tiktokDestinationPayload($this->tiktok)],
    ]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.pause', $repurpose))
        ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.disable', $repurpose))
        ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.activate', $repurpose))
        ->assertOk();

    $watermark = $repurpose->fresh()->activated_at;

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.activate', $repurpose))
        ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);

    expect($repurpose->fresh()->activated_at->equalTo($watermark))->toBeTrue();

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.resume', $repurpose))
        ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
});

test('the publishing mode round-trips through the api', function () {
    $created = $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'publish_mode' => PublishMode::Draft->value,
        ])
        ->assertStatus(Response::HTTP_CREATED)
        ->assertJsonPath('publish_mode', PublishMode::Draft->value);

    $this->withHeaders(apiHeaders($this->token))
        ->putJson(route('api.repurposes.update', $created->json('id')), [
            'publish_mode' => PublishMode::Publish->value,
        ])
        ->assertOk()
        ->assertJsonPath('publish_mode', PublishMode::Publish->value);

    $this->withHeaders(apiHeaders($this->token))
        ->putJson(route('api.repurposes.update', $created->json('id')), ['publish_mode' => 'whenever'])
        ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
});

test('the api exposes why a repurpose stopped and refuses to resume it while broken', function () {
    $repurpose = Repurpose::factory()->for($this->workspace)->create([
        'source_social_account_id' => $this->source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::SourceUnavailable,
        'destinations' => [tiktokDestinationPayload($this->tiktok)],
    ]);

    $this->source->update(['status' => AccountStatus::Disconnected]);

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.show', $repurpose))
        ->assertOk()
        ->assertJsonPath('paused_reason', PauseReason::SourceUnavailable->value);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.resume', $repurpose))
        ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
        ->assertJsonValidationErrors('source_social_account_id');
});

test('the api activity list carries each replicated post status', function () {
    $repurpose = Repurpose::factory()->for($this->workspace)->create([
        'source_social_account_id' => $this->source->id,
    ]);

    $item = RepurposeItem::factory()->for($repurpose)->create();

    $post = Post::factory()->tiktok()->published()->create([
        'workspace_id' => $this->workspace->id,
        'repurpose_item_id' => $item->id,
    ]);

    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurposes.items', $repurpose))
        ->assertOk()
        ->assertJsonPath('data.0.posts.0.id', $post->id)
        ->assertJsonPath('data.0.posts.0.platform', Platform::TikTok->value)
        ->assertJsonPath('data.0.posts.0.publish_status', PublishStatus::Published->value);
});

test('the source formats a repurpose can watch are listed', function () {
    $this->withHeaders(apiHeaders($this->token))
        ->getJson(route('api.repurpose-source-formats.index'))
        ->assertOk()
        ->assertJsonCount(count(SourceFormat::cases()), 'data')
        ->assertJsonPath('data.0.value', SourceFormat::Reel->value);
});

test('google business is rejected as a destination', function () {
    $googleBusiness = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::GoogleBusiness]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.store'), [
            'source_social_account_id' => $this->source->id,
            'destinations' => [[
                'social_account_id' => $googleBusiness->id,
                'content_type' => ContentType::GoogleBusinessPost->value,
            ]],
        ])
        ->assertUnprocessable()
        ->assertJsonValidationErrors(['destinations.0.social_account_id' => __('repurposes.errors.destination_not_supported')]);
});

test('activating returns the same body when PostHog is enabled', function () {
    enablePostHog();
    Queue::fake();

    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [tiktokDestinationPayload($this->tiktok)],
    ]);

    $this->withHeaders(apiHeaders($this->token))
        ->postJson(route('api.repurposes.activate', $repurpose))
        ->assertOk()
        ->assertJsonPath('status', Status::Active->value)
        ->assertJsonMissingPath('source_account');
});