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.

2.4 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Actions\Workspace\PurgeWorkspace;
use App\Jobs\PostHog\CaptureAccountEvent;
use App\Models\Media;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Social\GoogleBusinessDerivativeCleaner;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

test('purge workspace deletes the workspace, its media rows and, after commit, their files', function () {
    Storage::fake();

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

    $media = $workspace->addMedia(
        UploadedFile::fake()->image('logo.jpg'),
        'logo',
    );
    $path = $media->path;

    DB::transaction(function () use ($workspace, $path): void {
        PurgeWorkspace::execute($workspace);

        Storage::assertExists($path);
    });

    expect(Workspace::find($workspace->id))->toBeNull();
    expect(Media::find($media->id))->toBeNull();
    Storage::assertMissing($path);
});

test('purge workspace prunes google business jpegs before the cascade', function () {
    Storage::fake();

    $owner = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $owner->account_id,
        'user_id' => $owner->id,
    ]);
    $post = Post::factory()->forAccount(SocialAccount::factory()->googleBusiness()->create([
        'workspace_id' => $workspace->id,
    ]))->create([
        'user_id' => $owner->id,
    ]);
    $path = GoogleBusinessDerivativeCleaner::pathFor($post);
    Storage::put($path, 'image');

    PurgeWorkspace::execute($workspace);

    expect(Workspace::find($workspace->id))->toBeNull();
    Storage::assertMissing($path);
});

test('purge workspace does not track its channels as disconnected', function () {
    enablePostHog();
    $owner = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'account_id' => $owner->account_id,
        'user_id' => $owner->id,
    ]);
    SocialAccount::factory()->count(2)->create(['workspace_id' => $workspace->id]);

    Bus::fake([CaptureAccountEvent::class]);

    PurgeWorkspace::execute($workspace);

    expect(SocialAccount::query()->where('workspace_id', $workspace->id)->exists())->toBeFalse();
    Bus::assertNotDispatched(CaptureAccountEvent::class);
});