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.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
<?php
declare(strict_types=1);
use App\Actions\Analytics\ListChannelPublicationPerformance;
use App\Actions\Analytics\ResolveAnalyticsAccountKey;
use App\Dto\Analytics\DateRange;
use App\Enums\Analytics\PublicationAvailability;
use App\Enums\Analytics\PublicationContentType;
use App\Enums\SocialAccount\Platform;
use App\Jobs\Analytics\BootstrapAccountAnalytics;
use App\Jobs\Analytics\CollectAccountDailySnapshot;
use App\Models\AnalyticsPublication;
use App\Models\AnalyticsPublicationDailySnapshot;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Analytics\ChannelMetrics;
use App\Support\Analytics\SyncCadence;
use Carbon\CarbonImmutable;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Inertia\Testing\AssertableInertia as Assert;
use Mockery\MockInterface;
beforeEach(function () {
Queue::fake([BootstrapAccountAnalytics::class, CollectAccountDailySnapshot::class]);
$this->travelTo('2026-09-30 12:00 UTC');
$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->instagram = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
]);
});
/**
* @param array<string, mixed> $metrics
* @param array<string, mixed> $attributes
*/
function channelInsightsPublication(SocialAccount $account, string $publishedAt, array $metrics = [], array $attributes = []): AnalyticsPublication
{
$publication = AnalyticsPublication::factory()->create([
'workspace_id' => $account->workspace_id,
'social_account_id' => $account->id,
'social_account_key' => $account->id,
'network' => $account->platform->network(),
'platform_user_id' => $account->platform_user_id,
'platform' => $account->platform,
'provider_published_at' => CarbonImmutable::parse($publishedAt, 'UTC'),
...$attributes,
]);
AnalyticsPublicationDailySnapshot::factory()->create([
'publication_id' => $publication->id,
'date' => CarbonImmutable::parse($publishedAt, 'UTC')->toDateString(),
...$metrics,
]);
return $publication;
}
test('the insights page renders the channel report and its publications', function () {
$publication = channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 8, 'comments_count' => 2], [
'preview_metadata' => ['thumbnail_url' => 'https://cdn.example.com/a.jpg'],
]);
$post = Post::factory()->forAccount($this->instagram)->published()->create(['user_id' => $this->user->id]);
$insecure = channelInsightsPublication($this->instagram, '2026-09-19 10:00:00', ['reactions_count' => 3], [
'preview_metadata' => ['thumbnail_url' => 'http://cdn.example.com/b.jpg'],
'post_id' => $post->id,
]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('channels/Insights')
->where('supported', true)
->where('channel.id', $this->instagram->id)
->has('channel.posting_goal')
->has('channel.sent_this_week')
->where('report.summary.posts.value', 2)
->where('report.summary.reactions.value', 11)
->missing('report.available_metrics')
->where('availableMetrics', fn ($metrics): bool => collect($metrics)->contains('reactions'))
->missing('report.filters')
->where('filters.range', '30d')
->where('filters.start', '2026-09-01')
->where('filters.end', '2026-09-30')
->where('filters.period', 'current')
->where('filters.sort', 'reactions')
->where('sortableMetrics', ChannelMetrics::sortable())
->has('publications.data', 2)
->where('publications.data.0.id', $publication->id)
->where('publications.data.0.rank', 1)
->where('publications.data.0.thumbnail_url', 'https://cdn.example.com/a.jpg')
->where('publications.data.0.metrics.reactions', 8)
->where('publications.data.0.post_id', null)
->where('publications.data.0.permalink', $publication->permalink)
->where('publications.data.1.id', $insecure->id)
->where('publications.data.1.post_id', $post->id)
->where('sync', SyncCadence::toArray())
->where('publications.data.1.rank', 2)
->where('publications.data.1.thumbnail_url', null)
->etc());
});
test('publications show the post media from our bucket instead of the network preview', function () {
$image = ['id' => 'image', 'path' => 'medias/a.jpg', 'url' => 'https://cdn.trypost.test/medias/a.jpg', 'type' => 'image', 'mime_type' => 'image/jpeg'];
$video = ['id' => 'video', 'path' => 'medias/reel.mp4', 'url' => 'https://cdn.trypost.test/medias/reel.mp4', 'type' => 'video', 'mime_type' => 'video/mp4', 'meta' => ['cover_offset_ms' => 1500]];
$imagePost = Post::factory()->forAccount($this->instagram)->published()->create(['media' => [$image]]);
$videoPost = Post::factory()->forAccount($this->instagram)->published()->create(['media' => [$video]]);
$expired = ['preview_metadata' => ['thumbnail_url' => 'https://scontent.cdninstagram.com/expired.jpg?oe=6ACC450F']];
channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 8], [...$expired, 'post_id' => $imagePost->id]);
channelInsightsPublication($this->instagram, '2026-09-19 10:00:00', ['reactions_count' => 3], [...$expired, 'post_id' => $videoPost->id]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('publications.data.0.thumbnail_url', $image['url'])
->where('publications.data.0.media.url', $image['url'])
->where('publications.data.1.media.url', $video['url'])
->where('publications.data.1.media.type', 'video')
->where('publications.data.1.media.meta.cover_offset_ms', 1500)
->etc());
});
test('publications sort by the chosen metric with nulls last and ties by published date', function () {
$older = channelInsightsPublication($this->instagram, '2026-09-10 10:00:00', ['reactions_count' => 5, 'views_count' => 100]);
$top = channelInsightsPublication($this->instagram, '2026-09-11 10:00:00', ['reactions_count' => 10, 'views_count' => null]);
$middle = channelInsightsPublication($this->instagram, '2026-09-12 10:00:00', ['reactions_count' => null, 'views_count' => 50]);
$newer = channelInsightsPublication($this->instagram, '2026-09-13 10:00:00', ['reactions_count' => 5, 'views_count' => 100]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('publications.data', fn ($rows) => collect($rows)->pluck('id')->all() === [$top->id, $newer->id, $older->id, $middle->id])
->etc());
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'sort' => 'views']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('filters.sort', 'views')
->where('publications.data', fn ($rows) => collect($rows)->pluck('id')->all() === [$newer->id, $older->id, $middle->id, $top->id])
->etc());
});
test('engagement rate sorts by engagement over exposure', function () {
$low = channelInsightsPublication($this->instagram, '2026-09-10 10:00:00', ['engagement_count' => 1, 'exposure_count' => 3]);
$high = channelInsightsPublication($this->instagram, '2026-09-11 10:00:00', ['engagement_count' => 2, 'exposure_count' => 3]);
$unexposed = channelInsightsPublication($this->instagram, '2026-09-12 10:00:00', ['engagement_count' => 5, 'exposure_count' => 0]);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'sort' => 'engagement_rate']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('publications.data', fn ($rows) => collect($rows)->pluck('id')->all() === [$high->id, $low->id, $unexposed->id])
->where('publications.data.0.metrics.engagement_rate', 66.67)
->where('publications.data.2.metrics.engagement_rate', null)
->etc());
});
test('the previous period lists only publications in the previous range', function () {
$previous = channelInsightsPublication($this->instagram, '2026-09-05 10:00:00', ['reactions_count' => 1]);
$current = channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 1]);
$query = ['account' => $this->instagram, 'range' => 'custom', 'start' => '2026-09-11', 'end' => '2026-09-20'];
$this->actingAs($this->user)
->get(route('app.channels.insights', [...$query, 'period' => 'previous']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('report.previous_range.start', '2026-09-01')
->where('report.previous_range.end', '2026-09-10')
->where('filters.period', 'previous')
->has('publications.data', 1)
->where('publications.data.0.id', $previous->id)
->etc());
$this->actingAs($this->user)
->get(route('app.channels.insights', $query))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->has('publications.data', 1)
->where('publications.data.0.id', $current->id)
->etc());
});
test('the publication table shows ten posts per numbered page whatever the default page size', function () {
config(['app.pagination.default' => 25]);
$size = ListChannelPublicationPerformance::INSIGHTS_PAGE_SIZE;
foreach (range(1, $size + 3) as $day) {
channelInsightsPublication($this->instagram, CarbonImmutable::parse('2026-08-01 10:00:00', 'UTC')->addDays($day % 28)->toDateTimeString(), ['reactions_count' => $day]);
}
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'range' => 'custom', 'start' => '2026-08-01', 'end' => '2026-08-31']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->has('publications.data', $size)
->where('publications.current_page', 1)
->where('publications.last_page', 2)
->where('publications.per_page', $size)
->where('publications.total', $size + 3)
->where('publications.data.0.metrics.reactions', $size + 3)
->etc());
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'range' => 'custom', 'start' => '2026-08-01', 'end' => '2026-08-31', 'page' => 2]))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->has('publications.data', 3)
->where('publications.current_page', 2)
->where('publications.data.0.rank', $size + 1)
->where('publications.data.2.metrics.reactions', 1)
->etc());
});
test('the publication page must be a positive number', function () {
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'page' => 0]))
->assertSessionHasErrors('page');
});
test('the publication table lists only the top posts of the period', function () {
$top = ListChannelPublicationPerformance::TOP_POSTS;
foreach (range(1, $top + 5) as $index) {
channelInsightsPublication($this->instagram, CarbonImmutable::parse('2026-08-01 10:00:00', 'UTC')->addMinutes($index)->toDateTimeString(), ['reactions_count' => $index]);
}
$page = fn (int $number) => $this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'range' => 'custom', 'start' => '2026-08-01', 'end' => '2026-08-31', 'page' => $number]))
->assertOk();
$page(5)->assertInertia(fn (Assert $inertia) => $inertia
->has('publications.data', 10)
->where('publications.data.0.rank', 41)
->where('publications.last_page', 5)
->where('publications.total', $top)
->etc());
$page(6)->assertInertia(fn (Assert $inertia) => $inertia
->has('publications.data', 0)
->etc());
});
test('a sort metric no publication reports falls back to published date order', function () {
$older = channelInsightsPublication($this->instagram, '2026-09-10 10:00:00', ['comments_count' => 3]);
$newer = channelInsightsPublication($this->instagram, '2026-09-12 10:00:00', ['comments_count' => 1]);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'sort' => 'reactions']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('filters.sort', 'comments')
->where('publications.data', fn ($rows) => collect($rows)->pluck('id')->all() === [$older->id, $newer->id])
->etc());
expect(app(ListChannelPublicationPerformance::class)->handle($this->instagram, new DateRange(CarbonImmutable::parse('2026-09-01'), CarbonImmutable::parse('2026-09-30')), 'saves')->pluck('id')->all())
->toBe([$newer->id, $older->id]);
});
test('a valid sort the channel does not report falls back to the first available sortable metric', function () {
$older = channelInsightsPublication($this->instagram, '2026-09-10 10:00:00', ['reactions_count' => 3]);
$newer = channelInsightsPublication($this->instagram, '2026-09-12 10:00:00', ['reactions_count' => 1]);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'sort' => 'saves']))
->assertOk()
->assertSessionHasNoErrors()
->assertInertia(fn (Assert $page) => $page
->where('filters.sort', 'reactions')
->where('publications.data', fn ($rows) => collect($rows)->pluck('id')->all() === [$older->id, $newer->id])
->etc());
});
test('the default sort is the first sortable metric the channel reports', function () {
$low = channelInsightsPublication($this->instagram, '2026-09-10 10:00:00', ['views_count' => 5]);
$high = channelInsightsPublication($this->instagram, '2026-09-12 10:00:00', ['views_count' => 50]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('availableMetrics', ['followers', 'posts', 'views'])
->where('filters.sort', 'views')
->where('publications.data', fn ($rows) => collect($rows)->pluck('id')->all() === [$high->id, $low->id])
->etc());
});
test('an unknown or injected sort is a validation error', function (string $sort) {
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'sort' => $sort]))
->assertRedirect()
->assertSessionHasErrors('sort');
})->with(['bogus', 'views;drop']);
test('an unsupported channel renders without a report or publications', function () {
$linkedin = SocialAccount::factory()->linkedin()->create(['workspace_id' => $this->workspace->id]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $linkedin))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('channels/Insights')
->where('supported', false)
->where('channel.id', $linkedin->id)
->missing('report')
->missing('publications'));
});
test('a channel of another workspace is not found, even with an invalid sort', function () {
$foreign = SocialAccount::factory()->create(['platform' => Platform::Instagram]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $foreign))
->assertNotFound();
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $foreign, 'sort' => 'bogus']))
->assertNotFound();
});
test('a member who needs approval can open the insights page', function () {
$requester = User::factory()->create(['account_id' => $this->user->account_id]);
$this->workspace->members()->attach($requester->id, membershipPivot('approval'));
$requester->update(['current_workspace_id' => $this->workspace->id]);
$this->actingAs($requester)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('channels/Insights')
->where('supported', true)
->etc());
});
test('a range preset resolves through the preset action without clamping to the channel data', function () {
channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 1]);
$recent = channelInsightsPublication($this->instagram, '2026-09-28 10:00:00', ['reactions_count' => 1]);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'range' => '7d']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->where('filters.range', '7d')
->where('filters.start', '2026-09-24')
->where('filters.end', '2026-09-30')
->where('report.range.start', '2026-09-24')
->where('report.range.end', '2026-09-30')
->has('publications.data', 1)
->where('publications.data.0.id', $recent->id)
->etc());
});
test('empty dates with a preset range are ignored instead of rejected', function () {
channelInsightsPublication($this->instagram, '2026-09-28 10:00:00', ['reactions_count' => 1]);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'range' => '7d', 'start' => '', 'end' => '']))
->assertOk()
->assertSessionHasNoErrors()
->assertInertia(fn (Assert $page) => $page
->where('filters.range', '7d')
->where('filters.start', '2026-09-24')
->where('filters.end', '2026-09-30')
->etc());
});
test('a full load resolves the channel analytics key once', function () {
channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 1]);
$this->partialMock(ResolveAnalyticsAccountKey::class, fn (MockInterface $mock) => $mock
->shouldReceive('for')
->once()
->passthru());
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->has('report.summary')
->has('availableMetrics')
->has('publications.data', 1)
->etc());
});
test('a sort or period change reloads the table and filters without the report', function () {
channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 8, 'views_count' => 3]);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'sort' => 'views']))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->reloadOnly(['publications', 'filters'], fn (Assert $reload) => $reload
->where('filters.sort', 'views')
->has('publications.data', 1)
->missing('report')
->missing('availableMetrics')));
});
test('a full load resolves the analytics bounds once', function () {
channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 1]);
$boundsQueries = 0;
DB::listen(function (QueryExecuted $query) use (&$boundsQueries): void {
if (str_contains($query->sql, 'MIN(date) as earliest')) {
$boundsQueries++;
}
});
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk();
expect($boundsQueries)->toBe(1);
});
test('the coverage poll reloads the report without recomputing the available metrics', function () {
channelInsightsPublication($this->instagram, '2026-09-20 10:00:00', ['reactions_count' => 8]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->reloadOnly('report', fn (Assert $reload) => $reload
->has('report.summary')
->missing('availableMetrics')
->missing('publications')));
});
test('a publication the network deleted stays in the table without its link', function () {
$live = channelInsightsPublication($this->instagram, now('UTC')->subDays(4)->toDateTimeString(), ['reactions_count' => 9]);
$deleted = channelInsightsPublication($this->instagram, now('UTC')->subDays(5)->toDateTimeString(), ['reactions_count' => 3], [
'availability' => PublicationAvailability::Deleted,
]);
$this->actingAs($this->user)
->get(route('app.channels.insights', $this->instagram))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->has('publications.data', 2)
->where('publications.data.0.id', $live->id)
->where('publications.data.0.permalink', $live->permalink)
->where('publications.data.1.id', $deleted->id)
->where('publications.data.1.permalink', null)
->etc());
});
test('the channel page defers its own posting activity and applies its post type filter', function () {
$other = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id, 'platform' => Platform::Instagram]);
$yesterday = now('UTC')->subDay()->setTime(12, 0)->toDateTimeString();
channelInsightsPublication($this->instagram, $yesterday, attributes: ['content_type' => PublicationContentType::Story]);
channelInsightsPublication($this->instagram, $yesterday, attributes: ['content_type' => PublicationContentType::Image]);
channelInsightsPublication($other, $yesterday);
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram]))
->assertInertia(fn (Assert $page) => $page
->missing('postingActivity')
->loadDeferredProps(fn (Assert $reload) => $reload
->where('postingActivity.total', 2)
->where('postingActivity.current_streak', 1)
->has('postingActivity.days', 365)));
$this->actingAs($this->user)
->get(route('app.channels.insights', ['account' => $this->instagram, 'types' => ['instagram_story']]))
->assertInertia(fn (Assert $page) => $page
->loadDeferredProps(fn (Assert $reload) => $reload
->where('postingActivity.total', 1)));
});