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.

1.9 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Actions\Workspace\DeleteWorkspace;
use App\Jobs\PostHog\SyncAccountUsage;
use App\Models\Account;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Bus;

test('delete workspace dispatches SyncAccountUsage when PostHog is enabled', function () {
    config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);

    $account = Account::factory()->create();
    $user = User::factory()->create(['account_id' => $account->id]);
    $account->update(['owner_id' => $user->id]);

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

    Bus::fake();

    DeleteWorkspace::execute($workspace);

    expect(Workspace::find($workspace->id))->toBeNull();

    Bus::assertDispatched(SyncAccountUsage::class, function ($job) use ($account) {
        return $job->accountId === (string) $account->id
            && $job->workspaceId === null;
    });
});

test('delete workspace does not dispatch SyncAccountUsage when PostHog is disabled', function () {
    config(['services.posthog.enabled' => false, 'services.posthog.api_key' => null]);

    $account = Account::factory()->create();
    $user = User::factory()->create(['account_id' => $account->id]);
    $account->update(['owner_id' => $user->id]);

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

    Bus::fake();

    DeleteWorkspace::execute($workspace);

    expect(Workspace::find($workspace->id))->toBeNull();

    Bus::assertNotDispatched(SyncAccountUsage::class);
});