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.

27.8 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Actions\Repurpose\ActivateRepurpose;
use App\Actions\Repurpose\DisableRepurpose;
use App\Actions\Repurpose\ResumeRepurpose;
use App\Actions\Repurpose\UpdateRepurpose;
use App\Enums\PostPlatform\ContentType;
use App\Enums\Repurpose\ItemReason;
use App\Enums\Repurpose\PauseReason;
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\Jobs\Repurpose\ProcessRepurposeItem;
use App\Models\Repurpose;
use App\Models\RepurposeItem;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Post\MediaAttacher;
use App\Services\Repurpose\CaptionAdapter;
use App\Support\Repurpose\RepurposeTransition;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Illuminate\Validation\ValidationException;

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

/**
 * @return array{0: Workspace, 1: User, 2: SocialAccount}
 */
function healthWorkspace(): array
{
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create(['user_id' => $user->id]);
    $account = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);

    return [$workspace, $user, $account];
}

/**
 * @return array<string, mixed>
 */
function healthDestination(Workspace $workspace): array
{
    $account = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::TikTok]);

    return [
        'social_account_id' => $account->id,
        'content_type' => ContentType::TikTokVideo->value,
        'meta' => ['privacy_level' => PrivacyLevel::PublicToEveryone->value],
    ];
}

test('deleting the source account leaves the repurpose and its history intact', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create(['user_id' => $user->id]);
    $account = SocialAccount::factory()->for($workspace)->create();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $account->id,
    ]);

    $account->delete();

    expect(Repurpose::query()->whereKey($repurpose->id)->exists())->toBeTrue()
        ->and($repurpose->fresh()->source_social_account_id)->toBeNull();
});

test('applyIfPossible returns null instead of throwing when the status moved on', function () {
    $repurpose = Repurpose::factory()->create(['status' => Status::Paused]);

    $result = RepurposeTransition::applyIfPossible(
        $repurpose,
        [Status::Active],
        fn (Repurpose $locked) => $locked->update(['status' => Status::Paused]),
    );

    expect($result)->toBeNull();
});

test('applyIfPossible applies the change and returns the fresh model', function () {
    $repurpose = Repurpose::factory()->create(['status' => Status::Active]);

    $result = RepurposeTransition::applyIfPossible(
        $repurpose,
        [Status::Active],
        fn (Repurpose $locked) => $locked->update(['status' => Status::Paused]),
    );

    expect($result?->status)->toBe(Status::Paused);
});

test('activation is refused when no destination is usable', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $off = SocialAccount::factory()->for(Workspace::factory()->create())->create([
        'platform' => Platform::Threads,
    ]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [
            ['social_account_id' => $off->id, 'content_type' => ContentType::ThreadsPost->value, 'meta' => []],
        ],
    ]);

    expect(fn () => ActivateRepurpose::execute($repurpose))->toThrow(ValidationException::class);
});

test('activation is refused when the source account is disconnected', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $source->update(['status' => AccountStatus::Disconnected]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [healthDestination($workspace)],
    ]);

    expect(fn () => ActivateRepurpose::execute($repurpose))->toThrow(ValidationException::class);
});

test('activation is refused when the source account was removed', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [healthDestination($workspace)],
    ]);

    $source->delete();

    expect(fn () => ActivateRepurpose::execute($repurpose->fresh()))->toThrow(ValidationException::class);
});

test('resuming a user pause keeps the watermark', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $watermark = now()->subDays(3);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => null,
        'activated_at' => $watermark,
        'destinations' => [healthDestination($workspace)],
    ]);

    expect(ResumeRepurpose::execute($repurpose)->activated_at->timestamp)
        ->toBe($watermark->timestamp);
});

test('resuming a system pause starts from now and clears the reason', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::SourceUnavailable,
        'activated_at' => now()->subDays(3),
        'destinations' => [healthDestination($workspace)],
    ]);

    $resumed = ResumeRepurpose::execute($repurpose);

    expect($resumed->activated_at->isToday())->toBeTrue()
        ->and($resumed->paused_reason)->toBeNull()
        ->and($resumed->next_poll_at)->toBeNull();
});

test('resuming is refused while the source is still unusable', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $source->update(['status' => AccountStatus::TokenExpired]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::SourceUnavailable,
        'destinations' => [healthDestination($workspace)],
    ]);

    expect(fn () => ResumeRepurpose::execute($repurpose))->toThrow(ValidationException::class);
});

test('deleting the source account pauses the repurpose', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [healthDestination($workspace)],
    ]);

    $source->delete();

    expect($repurpose->fresh()->status)->toBe(Status::Paused)
        ->and($repurpose->fresh()->paused_reason)->toBe(PauseReason::SourceRemoved);
});

test('a source going token expired pauses the repurpose', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [healthDestination($workspace)],
    ]);

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

    expect($repurpose->fresh()->paused_reason)->toBe(PauseReason::SourceUnavailable);
});

test('a draft repurpose is left alone when its source dies', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [healthDestination($workspace)],
    ]);

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

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

test('a repurpose the user paused does not acquire a system reason', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => null,
        'destinations' => [healthDestination($workspace)],
    ]);

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

    expect($repurpose->fresh()->paused_reason)->toBeNull();
});

test('an unrelated account update does not touch the repurpose', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [healthDestination($workspace)],
    ]);

    $source->update(['last_used_at' => now()]);

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

test('deleting a destination account prunes it from the repurpose', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $keep = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Mastodon]);
    $drop = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Threads]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [
            ['social_account_id' => $keep->id, 'content_type' => ContentType::MastodonPost->value, 'meta' => []],
            ['social_account_id' => $drop->id, 'content_type' => ContentType::ThreadsPost->value, 'meta' => []],
        ],
    ]);

    $drop->delete();

    $destinations = $repurpose->fresh()->destinations;

    expect($destinations)->toHaveCount(1)
        ->and(data_get($destinations, '0.social_account_id'))->toBe($keep->id)
        ->and($repurpose->fresh()->status)->toBe(Status::Active);
});

test('deleting the last destination pauses the repurpose', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $only = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Threads]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [
            ['social_account_id' => $only->id, 'content_type' => ContentType::ThreadsPost->value, 'meta' => []],
        ],
    ]);

    $only->delete();

    expect($repurpose->fresh()->destinations)->toBe([])
        ->and($repurpose->fresh()->paused_reason)->toBe(PauseReason::NoDestinations);
});

test('reconnecting a LinkedIn destination as a page realigns its content type', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $linkedin = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::LinkedIn]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [
            ['social_account_id' => $linkedin->id, 'content_type' => ContentType::LinkedInPost->value, 'meta' => []],
        ],
    ]);

    $linkedin->update(['platform' => Platform::LinkedInPage]);

    expect(data_get($repurpose->fresh()->destinations, '0.content_type'))
        ->toBe(ContentType::LinkedInPagePost->value);
});

test('reconnecting the source resumes the repurpose from now', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $source->update(['status' => AccountStatus::TokenExpired]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::SourceUnavailable,
        'activated_at' => now()->subDays(2),
        'destinations' => [healthDestination($workspace)],
    ]);

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

    $fresh = $repurpose->fresh();

    expect($fresh->status)->toBe(Status::Active)
        ->and($fresh->paused_reason)->toBeNull()
        ->and($fresh->activated_at->isToday())->toBeTrue();
});

test('a user pause is never auto-resumed', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $source->update(['status' => AccountStatus::TokenExpired]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => null,
        'destinations' => [healthDestination($workspace)],
    ]);

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

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

test('a repurpose with no destinations left is not auto-resumed', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $source->update(['status' => AccountStatus::TokenExpired]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::NoDestinations,
        'destinations' => [],
    ]);

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

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

test('disconnecting an account says how many automations it paused', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

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

    Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [healthDestination($workspace)],
    ]);

    $this->actingAs($user)
        ->delete(route('app.channels.disconnect', $source))
        ->assertSessionHas('flash.banner', trans_choice('accounts.flash.disconnected_paused_repurposes', 1, ['count' => 1]));
});

test('disconnecting an account with no automations keeps the plain message', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

    $account = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);

    $this->actingAs($user)
        ->delete(route('app.channels.disconnect', $account))
        ->assertSessionHas('flash.banner', __('accounts.flash.disconnected'));
});

test('deleting the last destination account also reports the automation it paused', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

    $source = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);
    $only = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Threads]);

    Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [
            ['social_account_id' => $only->id, 'content_type' => ContentType::ThreadsPost->value, 'meta' => []],
        ],
    ]);

    $this->actingAs($user)
        ->delete(route('app.channels.disconnect', $only))
        ->assertSessionHas('flash.banner', trans_choice('accounts.flash.disconnected_paused_repurposes', 1, ['count' => 1]));
});

test('pruning a destination from a draft repurpose does not pause it', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $only = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Threads]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [
            ['social_account_id' => $only->id, 'content_type' => ContentType::ThreadsPost->value, 'meta' => []],
        ],
    ]);

    $only->delete();

    expect($repurpose->fresh()->destinations)->toBe([])
        ->and($repurpose->fresh()->status)->toBe(Status::Draft)
        ->and($repurpose->fresh()->paused_reason)->toBeNull();
});

test('a supported content type survives a platform change untouched', function () {

    [$workspace, $user, $source] = healthWorkspace();

    $instagram = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [
            ['social_account_id' => $instagram->id, 'content_type' => ContentType::InstagramReel->value, 'meta' => []],
        ],
    ]);

    $instagram->update(['platform' => Platform::InstagramFacebook]);

    expect(data_get($repurpose->fresh()->destinations, '0.content_type'))
        ->toBe(ContentType::InstagramReel->value);
});

test('an item whose destination account was deleted records no usable destination', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $gone = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Threads]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [
            ['social_account_id' => $gone->id, 'content_type' => ContentType::ThreadsPost->value, 'meta' => []],
        ],
    ]);

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

    $repurpose->update(['destinations' => $repurpose->destinations]);
    $gone->forceDelete();

    (new ProcessRepurposeItem($item, 'https://example.com/v.mp4', 'caption'))
        ->handle(app(MediaAttacher::class), app(CaptionAdapter::class));

    expect($item->fresh()->reason)->toBe(ItemReason::NoUsableDestinations);
});

test('picking a new source for an orphan and resuming starts from now', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $destination = healthDestination($workspace);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'activated_at' => now()->subDays(30),
        'destinations' => [$destination],
    ]);

    $source->delete();

    expect($repurpose->fresh()->paused_reason)->toBe(PauseReason::SourceRemoved)
        ->and($repurpose->fresh()->source_social_account_id)->toBeNull();

    $replacement = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);

    UpdateRepurpose::execute($repurpose, ['source_social_account_id' => $replacement->id]);

    $resumed = ResumeRepurpose::execute($repurpose->fresh());

    expect($resumed->status)->toBe(Status::Active)
        ->and($resumed->paused_reason)->toBeNull()
        ->and($resumed->activated_at->isToday())->toBeTrue();
});

test('an orphan cannot be resumed before a new source is picked', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [healthDestination($workspace)],
    ]);

    $source->delete();

    expect(fn () => ResumeRepurpose::execute($repurpose->fresh()))
        ->toThrow(ValidationException::class);
});

test('a failure inside the sync never breaks the account operation', function () {
    Log::spy();

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

    $account = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);

    Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $account->id,
        'status' => Status::Active,
        'destinations' => ['not-an-object'],
    ]);

    $this->actingAs($user)
        ->delete(route('app.channels.disconnect', $account))
        ->assertRedirect();

    expect(SocialAccount::query()->whereKey($account->id)->exists())->toBeFalse();

    Log::shouldHaveReceived('error')->withArgs(fn (string $message): bool => $message === 'Repurpose account sync failed');
});

test('turning a system-paused repurpose off clears the reason with it', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Paused,
        'paused_reason' => PauseReason::SourceUnavailable,
        'destinations' => [healthDestination($workspace)],
    ]);

    $disabled = DisableRepurpose::execute($repurpose);

    expect($disabled->status)->toBe(Status::Disabled)
        ->and($disabled->paused_reason)->toBeNull();
});

test('activating clears any reason left from an earlier stop', function () {
    [$workspace, $user, $source] = healthWorkspace();

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Disabled,
        'paused_reason' => PauseReason::NoDestinations,
        'destinations' => [healthDestination($workspace)],
    ]);

    $activated = ActivateRepurpose::execute($repurpose);

    expect($activated->status)->toBe(Status::Active)
        ->and($activated->paused_reason)->toBeNull();
});

test('changing the source of a never-activated repurpose invents no watermark', function () {
    [$workspace, $user, $source] = healthWorkspace();
    $other = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Facebook]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'activated_at' => null,
        'destinations' => [healthDestination($workspace)],
    ]);

    UpdateRepurpose::execute($repurpose, ['source_social_account_id' => $other->id]);

    expect($repurpose->fresh()->activated_at)->toBeNull();
});

test('a draft saves a destination that is still missing its required meta', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

    $source = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);
    $pinterest = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Pinterest]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
    ]);

    $this->actingAs($user)
        ->put(route('app.repurposes.update', $repurpose), [
            'destinations' => [[
                'social_account_id' => $pinterest->id,
                'content_type' => ContentType::PinterestVideoPin->value,
                'meta' => [],
            ]],
        ])
        ->assertSessionHasNoErrors();

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

test('a draft missing its required meta cannot be activated', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

    $source = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);
    $pinterest = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Pinterest]);

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Draft,
        'destinations' => [[
            'social_account_id' => $pinterest->id,
            'content_type' => ContentType::PinterestVideoPin->value,
            'meta' => [],
        ]],
    ]);

    $this->actingAs($user)
        ->post(route('app.repurposes.activate', $repurpose))
        ->assertSessionHasErrors('destinations');

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

test('an active repurpose cannot drop the meta its destination needs to publish', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

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

    $repurpose = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [[
            'social_account_id' => $tiktok->id,
            'content_type' => ContentType::TikTokVideo->value,
            'meta' => ['privacy_level' => PrivacyLevel::PublicToEveryone->value],
        ]],
    ]);

    $this->actingAs($user)
        ->put(route('app.repurposes.update', $repurpose), [
            'destinations' => [[
                'social_account_id' => $tiktok->id,
                'content_type' => ContentType::TikTokVideo->value,
                'meta' => [],
            ]],
        ])
        ->assertSessionHasErrors('destinations.0.meta.privacy_level');

    expect(data_get($repurpose->fresh()->destinations, '0.meta.privacy_level'))->toBe(PrivacyLevel::PublicToEveryone->value);
});

test('a repurpose refuses music on an instagram login reel destination when it is updated or activated', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $user->account_id,
        'user_id' => $user->id,
    ]);
    $user->update(['current_workspace_id' => $workspace->id]);

    $source = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::TikTok]);
    $instagram = SocialAccount::factory()->for($workspace)->create(['platform' => Platform::Instagram]);
    $destination = [
        'social_account_id' => $instagram->id,
        'content_type' => ContentType::InstagramReel->value,
        'meta' => ['audio' => ['id' => '1234567890']],
    ];

    $active = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => $source->id,
        'status' => Status::Active,
        'destinations' => [[...$destination, 'meta' => []]],
    ]);

    $this->actingAs($user)
        ->put(route('app.repurposes.update', $active), ['destinations' => [$destination]])
        ->assertSessionHasErrors(['destinations.0.meta.audio' => __('posts.form.instagram.music.requires_facebook')]);

    $draft = Repurpose::factory()->for($workspace)->create([
        'source_social_account_id' => SocialAccount::factory()->for($workspace)->create(['platform' => Platform::TikTok])->id,
        'status' => Status::Draft,
        'destinations' => [$destination],
    ]);

    $this->actingAs($user)
        ->post(route('app.repurposes.activate', $draft))
        ->assertSessionHasErrors(['destinations' => __('posts.form.instagram.music.requires_facebook')]);

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