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.

15.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\Mcp\Servers\TryPostServer;
use App\Mcp\Tools\Repurpose\ActivateRepurposeTool;
use App\Mcp\Tools\Repurpose\CreateRepurposeTool;
use App\Mcp\Tools\Repurpose\DeleteRepurposeTool;
use App\Mcp\Tools\Repurpose\GetRepurposeTool;
use App\Mcp\Tools\Repurpose\ListRepurposeItemsTool;
use App\Mcp\Tools\Repurpose\ListRepurposeSourceFormatsTool;
use App\Mcp\Tools\Repurpose\ListRepurposesTool;
use App\Mcp\Tools\Repurpose\PauseRepurposeTool;
use App\Mcp\Tools\Repurpose\UpdateRepurposeTool;
use App\Models\Post;
use App\Models\Repurpose;
use App\Models\RepurposeItem;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Queue;
use Illuminate\Testing\Fluent\AssertableJson;

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

    $this->user = User::factory()->create();
    $this->workspace = Workspace::factory()->create([
        'account_id' => $this->user->account_id,
        'user_id' => $this->user->id,
    ]);
    $this->workspace->members()->attach($this->user->id, membershipPivot('admin'));
    $this->user->update(['current_workspace_id' => $this->workspace->id]);

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

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

test('a repurpose is created with its watched format and destination meta', function () {
    $response = TryPostServer::actingAs($this->user)
        ->tool(CreateRepurposeTool::class, [
            'source_social_account_id' => $this->source->id,
            'source_format' => SourceFormat::Story->value,
            'destinations' => [tiktokDestinationForMcp($this->tiktok)],
        ]);

    $response->assertOk();

    $repurpose = Repurpose::sole();

    expect($repurpose->source_format)->toBe(SourceFormat::Story)
        ->and($repurpose->status)->toBe(Status::Draft)
        ->and($repurpose->destinations)->toEqual([tiktokDestinationForMcp($this->tiktok)]);
});

test('destination meta survives a read back through the get tool', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [tiktokDestinationForMcp($this->tiktok)],
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(GetRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk()
        ->assertSee(PrivacyLevel::PublicToEveryone->value);
});

test('the list tool returns the workspace repurposes and nobody else\'s', function () {
    $reel = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'source_format' => SourceFormat::Reel,
        'created_at' => now()->subHour(),
    ]);

    $story = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'source_format' => SourceFormat::Story,
        'created_at' => now(),
    ]);

    $otherWorkspace = Workspace::factory()->create();
    Repurpose::factory()->create([
        'workspace_id' => $otherWorkspace->id,
        'source_social_account_id' => SocialAccount::factory()->create([
            'workspace_id' => $otherWorkspace->id,
            'platform' => Platform::Instagram,
        ])->id,
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(ListRepurposesTool::class, [])
        ->assertOk()
        ->assertStructuredContent(fn (AssertableJson $json) => $json
            ->has('repurposes', 2)
            ->where('total', 2)
            ->where('repurposes.0.id', $story->id)
            ->where('repurposes.0.source_format', SourceFormat::Story->value)
            ->where('repurposes.1.id', $reel->id)
            ->where('repurposes.1.source_format', SourceFormat::Reel->value)
            ->etc());
});

test('updating replaces the destinations and the watched format', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'source_format' => SourceFormat::Reel,
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(UpdateRepurposeTool::class, [
            'repurpose_id' => $repurpose->id,
            'source_social_account_id' => $this->source->id,
            'source_format' => SourceFormat::Video->value,
            'destinations' => [tiktokDestinationForMcp($this->tiktok)],
        ])
        ->assertOk();

    expect($repurpose->fresh()->source_format)->toBe(SourceFormat::Video)
        ->and($repurpose->fresh()->destinations)->toEqual([tiktokDestinationForMcp($this->tiktok)]);
});

test('activate and pause move the status', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [tiktokDestinationForMcp($this->tiktok)],
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(ActivateRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk();

    expect($repurpose->fresh()->status)->toBe(Status::Active);

    TryPostServer::actingAs($this->user)
        ->tool(PauseRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk();

    expect($repurpose->fresh()->status)->toBe(Status::Paused);
});

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

    TryPostServer::actingAs($this->user)
        ->tool(ActivateRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertHasErrors();

    expect($repurpose->fresh()->status)->toBe(Status::Draft);
});

test('the items tool exposes why a video was skipped', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
    ]);

    RepurposeItem::factory()->for($repurpose)->create([
        'status' => ItemStatus::Skipped,
        'reason' => 'published_via_trypost',
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(ListRepurposeItemsTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk()
        ->assertSee('published_via_trypost');
});

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

    TryPostServer::actingAs($this->user)
        ->tool(GetRepurposeTool::class, ['repurpose_id' => $stranger->id])
        ->assertHasErrors();
});

test('deleting removes the repurpose', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(DeleteRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk();

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

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

    TryPostServer::actingAs($this->user)
        ->tool(CreateRepurposeTool::class, [
            'source_social_account_id' => $this->source->id,
            'destinations' => [tiktokDestinationForMcp($stranger)],
        ])
        ->assertHasErrors();

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

test('the list tool pages instead of returning everything at once', function () {
    config()->set('app.pagination.default', 1);

    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,
        ]);
    }

    TryPostServer::actingAs($this->user)
        ->tool(ListRepurposesTool::class, [])
        ->assertOk()
        ->assertStructuredContent(fn (AssertableJson $json) => $json
            ->has('repurposes', 1)
            ->where('total', 2)
            ->where('per_page', 1)
            ->where('current_page', 1)
            ->where('last_page', 2)
            ->etc());

    TryPostServer::actingAs($this->user)
        ->tool(ListRepurposesTool::class, ['page' => 2])
        ->assertOk()
        ->assertStructuredContent(fn (AssertableJson $json) => $json
            ->has('repurposes', 1)
            ->where('current_page', 2)
            ->etc());
});

test('the publishing mode is settable through mcp', function () {
    TryPostServer::actingAs($this->user)
        ->tool(CreateRepurposeTool::class, [
            'source_social_account_id' => $this->source->id,
            'publish_mode' => PublishMode::Draft->value,
        ])
        ->assertOk()
        ->assertStructuredContent(fn (AssertableJson $json) => $json
            ->where('publish_mode', PublishMode::Draft->value)
            ->etc());

    expect(Repurpose::where('workspace_id', $this->workspace->id)->sole()->publish_mode)
        ->toBe(PublishMode::Draft);
});

test('the get tool reports why a repurpose stopped', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::SourceRemoved,
        'destinations' => [tiktokDestinationForMcp($this->tiktok)],
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(GetRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk()
        ->assertSee(PauseReason::SourceRemoved->value);
});

test('activating through the tool is refused while the source is unusable', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [tiktokDestinationForMcp($this->tiktok)],
    ]);

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

    TryPostServer::actingAs($this->user)
        ->tool(ActivateRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertHasErrors();

    expect($repurpose->fresh()->status)->toBe(Status::Draft);
});

test('the items tool carries each replicated post status', function () {
    $repurpose = Repurpose::factory()->create([
        'workspace_id' => $this->workspace->id,
        '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,
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(ListRepurposeItemsTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk()
        ->assertSee(PublishStatus::Published->value);
});

test('the update tool keeps a draft destination that is still missing its board', function () {
    $pinterest = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::Pinterest]);

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

    TryPostServer::actingAs($this->user)
        ->tool(UpdateRepurposeTool::class, [
            'repurpose_id' => $repurpose->id,
            'destinations' => [[
                'social_account_id' => $pinterest->id,
                'content_type' => ContentType::PinterestVideoPin->value,
                'meta' => [],
            ]],
        ])
        ->assertOk();

    expect($repurpose->fresh()->destinations)->toHaveCount(1);

    TryPostServer::actingAs($this->user)
        ->tool(ActivateRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertHasErrors();

    expect($repurpose->fresh()->status)->toBe(Status::Draft);
});

test('the update tool refuses to drop the board an active repurpose publishes with', function () {
    $pinterest = SocialAccount::factory()->for($this->workspace)->create(['platform' => Platform::Pinterest]);

    $repurpose = Repurpose::factory()->active()->create([
        'workspace_id' => $this->workspace->id,
        'source_social_account_id' => $this->source->id,
        'destinations' => [[
            'social_account_id' => $pinterest->id,
            'content_type' => ContentType::PinterestVideoPin->value,
            'meta' => ['board_id' => 'board-1'],
        ]],
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(UpdateRepurposeTool::class, [
            'repurpose_id' => $repurpose->id,
            'destinations' => [[
                'social_account_id' => $pinterest->id,
                'content_type' => ContentType::PinterestVideoPin->value,
                'meta' => [],
            ]],
        ])
        ->assertHasErrors();

    expect(data_get($repurpose->fresh()->destinations, '0.meta.board_id'))->toBe('board-1');
});

test('the source formats tool lists what a repurpose can watch', function () {
    TryPostServer::actingAs($this->user)
        ->tool(ListRepurposeSourceFormatsTool::class)
        ->assertOk()
        ->assertSee(SourceFormat::Reel->value);
});

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

    TryPostServer::actingAs($this->user)
        ->tool(CreateRepurposeTool::class, [
            'source_social_account_id' => $this->source->id,
            'destinations' => [[
                'social_account_id' => $googleBusiness->id,
                'content_type' => ContentType::GoogleBusinessPost->value,
            ]],
        ])
        ->assertHasErrors([__('repurposes.errors.destination_not_supported')]);

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

test('activating through the tool 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' => [tiktokDestinationForMcp($this->tiktok)],
    ]);

    TryPostServer::actingAs($this->user)
        ->tool(ActivateRepurposeTool::class, ['repurpose_id' => $repurpose->id])
        ->assertOk()
        ->assertStructuredContent(fn (AssertableJson $json) => $json
            ->where('status', Status::Active->value)
            ->missing('source_account')
            ->etc());
});