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\Enums\SocialAccount\Platform;
use App\Jobs\Analytics\BootstrapAccountAnalytics;
use App\Jobs\Analytics\CollectAccountDailySnapshot;
use App\Models\AnalyticsAccountDailySnapshot;
use App\Models\AnalyticsPublication;
use App\Models\AnalyticsPublicationDailySnapshot;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Queue;
/**
* @param array<string, mixed> $metrics
*/
function metricsCardPublication(SocialAccount $account, CarbonImmutable $publishedAt, array $metrics): AnalyticsPublication
{
$publication = AnalyticsPublication::factory()->create([
'workspace_id' => $account->workspace_id,
'social_account_id' => $account->id,
'social_account_key' => $account->id,
'platform' => $account->platform,
'network' => $account->platform->network(),
'platform_user_id' => $account->platform_user_id,
'provider_published_at' => $publishedAt,
]);
AnalyticsPublicationDailySnapshot::factory()->create([
'publication_id' => $publication->id,
'date' => $publishedAt->toDateString(),
...$metrics,
]);
return $publication;
}
function metricsCardFollowers(SocialAccount $account, CarbonImmutable $date, int $followers): void
{
AnalyticsAccountDailySnapshot::factory()->create([
'workspace_id' => $account->workspace_id,
'social_account_id' => $account->id,
'social_account_key' => $account->id,
'platform' => $account->platform,
'network' => $account->platform->network(),
'platform_user_id' => $account->platform_user_id,
'date' => $date->toDateString(),
'followers_count' => $followers,
]);
}
beforeEach(function () {
Queue::fake([BootstrapAccountAnalytics::class, CollectAccountDailySnapshot::class]);
$this->user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $this->user->id, 'account_id' => $this->user->account_id]);
$workspace->members()->attach($this->user->id, membershipPivot('admin'));
$this->user->update(['current_workspace_id' => $workspace->id]);
subscribeAccount($this->user->account);
$this->instagram = SocialAccount::factory()->create([
'workspace_id' => $workspace->id,
'platform' => Platform::Instagram,
]);
$today = CarbonImmutable::today('UTC');
foreach (range(0, 94) as $daysAgo) {
metricsCardFollowers($this->instagram, $today->subDays($daysAgo), 3000 + (60 - $daysAgo) * 5);
}
foreach ([2, 5, 9, 14, 20, 26] as $index => $daysAgo) {
metricsCardPublication($this->instagram, $today->subDays($daysAgo)->setTime(10, 0), [
'reactions_count' => 10 + $index,
'views_count' => 300 + $index * 20,
'reach_count' => 200 + $index * 10,
'saves_count' => 2,
'watch_time_milliseconds' => 120000,
'metrics' => ['profile_visits' => ['value' => 3, 'unit' => 'count', 'availability' => 'available']],
]);
}
metricsCardPublication($this->instagram, $today->subDays(40)->setTime(10, 0), [
'reactions_count' => 4,
'views_count' => 90,
'reach_count' => 80,
'metrics' => ['profile_visits' => ['value' => 1, 'unit' => 'count', 'availability' => 'available']],
]);
$this->actingAs($this->user);
});
test('the metrics card loads after the page and opens on content impact', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metrics-chart');
$page->assertMissing('@insights-metrics-skeleton')
->assertAttribute('@insights-preset-content_impact', 'aria-pressed', 'true')
->assertVisible('@insights-preset-audience_growth')
->assertVisible('@insights-preset-visibility')
->assertVisible('@insights-preset-follower_growth_rate')
->assertSeeIn('@insights-metrics-legend-posts-value', '6')
->assertSeeIn('@insights-metrics-legend-posts-change', '+500%')
->assertSeeIn('@insights-metrics-legend-followers-change', '+150')
->assertSeeIn('@insights-card-net_followers-value', '150')
->assertScript('/Posts|Followers/.test(document.querySelector("[data-testid=insights-metrics-chart]").textContent)', false)
->assertVisible('@insights-metrics-legend-followers')
->assertPresent('@insights-metrics-table')
->assertScript('document.querySelectorAll("[data-testid=insights-metrics-bar]").length > 0', true)
->assertScript('document.querySelectorAll("[data-testid=insights-metrics-chart] svg").length > 0', true)
->assertMissing('@followers-bar')
->assertMissing('@posts-chart-bar');
waitFor($page, '@insights-posts');
$page->assertScript('Boolean(document.querySelector("[data-testid=insights-posts]").compareDocumentPosition(document.querySelector("[data-testid=insights-metrics]")) & Node.DOCUMENT_POSITION_FOLLOWING)', true)
->assertScript(<<<'JS'
[...document.querySelectorAll('[data-testid="insights-metrics-controls"] button')]
.map((button) => [button.classList.contains('h-7'), Math.round(button.getBoundingClientRect().height)])
.every(([small, height]) => small && height === 28)
JS, true)
->assertScript(<<<'JS'
(() => {
const preset = Math.round(document.querySelector('[data-testid="insights-range-30d"]').getBoundingClientRect().height);
return [...document.querySelectorAll('[data-testid="insights-metrics-modes"] button')]
.every((button) => Math.round(button.getBoundingClientRect().height) === preset);
})()
JS, true)
->assertScript('document.querySelectorAll("[data-testid=insights-metrics-controls] button, [data-testid=insights-metrics-modes] button").length', 9)
->assertNoJavaScriptErrors();
});
test('the card shows a skeleton while the series reloads for a new range', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metrics-chart');
$page->script(<<<'JS'
window.__sawMetricsSkeleton = false;
new MutationObserver(() => {
if (document.querySelector('[data-testid="insights-metrics-skeleton"]')) {
window.__sawMetricsSkeleton = true;
}
}).observe(document.body, { childList: true, subtree: true });
JS);
$page->click('@insights-range-7d');
waitForQueryParameter($page, 'range', '7d');
waitForCondition($page, 'window.__sawMetricsSkeleton');
waitForState($page, '@insights-metrics-chart', 'true');
$page->assertScript('window.__sawMetricsSkeleton', true)
->assertVisible('@insights-metrics-chart')
->assertNoJavaScriptErrors();
});
test('the metric menu lists the channel metrics and switches the chart to one metric', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metric-menu');
$page->click('@insights-metric-menu');
waitFor($page, '@insights-metric-option-reach');
$page->assertScript('[...document.querySelectorAll("[data-testid^=insights-metric-option-]:not([data-testid$=-check])")].map((item) => item.dataset.testid.replace("insights-metric-option-", "")).join(",")', 'followers,reach,profile_visits,posts,net_followers')
->assertSeeIn('@insights-metric-option-profile_visits', 'Profile Views')
->assertSeeIn('@insights-metric-option-net_followers', 'Net New Followers')
->assertPresent('@insights-metric-option-posts-check')
->assertMissing('@insights-metric-option-views')
->assertMissing('@insights-metric-option-impressions')
->click('@insights-metric-option-reach');
waitFor($page, '@insights-metrics-legend-reach');
$page->assertAttribute('@insights-metric-primary', 'aria-pressed', 'true')
->assertAttribute('@insights-preset-content_impact', 'aria-pressed', 'false')
->assertSeeIn('@insights-metric-primary', 'Reach')
->assertMissing('@insights-metrics-legend-posts')
->assertSeeIn('@insights-metrics-legend-reach', 'Reach')
->assertSeeIn('@insights-metrics-legend-reach-value', '1.4K')
->assertVisible('@insights-metrics-post-totals-note')
->click('@insights-metric-menu');
waitFor($page, '@insights-metric-option-reach-check');
$page->assertMissing('@insights-metric-option-posts-check')
->assertNoJavaScriptErrors();
});
test('the follower growth rate preset draws the last twelve months and ignores the filters', function () {
$page = visit(route('app.channels.insights', ['account' => $this->instagram, 'range' => '7d']));
waitFor($page, '@insights-preset-follower_growth_rate');
$page->click('@insights-preset-follower_growth_rate');
waitFor($page, '@insights-metrics-legend-growth_rate');
$page->assertAttribute('@insights-preset-follower_growth_rate', 'aria-pressed', 'true')
->assertSeeIn('@insights-metrics-legend-growth_rate', 'Growth Rate')
->assertScript('document.querySelector("[data-testid=insights-metrics-legend-growth_rate-value]").textContent.trim().endsWith("%")', true)
->assertSeeIn('@insights-metrics-filters-note', "Date and label filters don't apply to this chart")
->assertMissing('@insights-metrics-modes')
->assertVisible('@insights-metrics-chart')
->assertScript('document.querySelectorAll("[data-testid=insights-metrics-table] tbody tr").length', 12)
->assertScript('document.querySelector("[data-testid=insights-metrics-insight]").dataset.insight.startsWith("growth_rate.")', true)
->click('@insights-preset-audience_growth');
waitFor($page, '@insights-metrics-modes');
$page->assertMissing('@insights-metrics-filters-note')
->assertNoJavaScriptErrors();
});
test('presets swap the chart series and the selected preset and period survive a filter change', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-preset-audience_growth');
$page->click('@insights-preset-audience_growth')
->click('@insights-metrics-mode-both');
waitFor($page, '@insights-metrics-legend-net_followers-previous');
$page->click('@insights-post-type-filter');
waitFor($page, '@insights-post-type-option-instagram_reel');
$page->click('@insights-post-type-option-instagram_reel');
waitForCondition($page, '[...new URLSearchParams(location.search)].some(([key, value]) => key.startsWith("types[") && value === "instagram_reel")');
waitForState($page, '@insights-metrics-chart', 'true');
waitForState($page, '@insights-preset-audience_growth', "element.getAttribute('aria-pressed') === 'true'");
waitForState($page, '@insights-metrics-mode-both', "element.getAttribute('aria-pressed') === 'true'");
waitForState($page, '@insights-metrics-legend-net_followers-previous', 'true');
$page->assertScript('decodeURIComponent(location.search).includes("instagram_reel")', true)
->assertAttribute('@insights-preset-audience_growth', 'aria-pressed', 'true')
->assertAttribute('@insights-metrics-mode-both', 'aria-pressed', 'true')
->assertVisible('@insights-metrics-legend-net_followers-previous')
->assertVisible('@insights-metrics-legend-followers')
->assertMissing('@insights-metrics-legend-posts');
$page->click('@insights-preset-visibility');
waitFor($page, '@insights-metrics-legend-profile_visits');
$page->assertVisible('@insights-metrics-legend-followers')
->assertNoJavaScriptErrors();
});
test('a one-day range still draws the posts of that day', function () {
$today = CarbonImmutable::today('UTC');
$page = visit(route('app.channels.insights', ['account' => $this->instagram, 'range' => 'custom', 'start' => $today->subDays(2)->toDateString(), 'end' => $today->subDays(2)->toDateString()]));
waitFor($page, '@insights-metrics-legend-posts-value');
$page->assertSeeIn('@insights-metrics-legend-posts-value', '1')
->assertVisible('@insights-metrics-chart')
->assertMissing('@insights-metrics-no-history')
->assertNoJavaScriptErrors();
});
test('the mode toggle shows the comparison period and both periods', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metrics-mode-both');
$page->assertMissing('@insights-metrics-legend-posts-previous')
->assertSeeIn('@insights-metrics-mode-previous', 'Comparison')
->click('@insights-metrics-mode-both');
waitFor($page, '@insights-metrics-legend-posts-previous');
$page->assertSeeIn('@insights-metrics-legend-posts-previous', '1')
->assertSeeIn('@insights-metrics-legend-posts-value', '6')
->assertAttribute('@insights-metrics-mode-both', 'aria-pressed', 'true')
->assertScript('document.querySelectorAll("[data-testid=insights-metrics-bar]").length > 0', true)
->click('@insights-metrics-mode-previous');
waitForState($page, '@insights-metrics-legend-posts-value', "element.textContent.trim() === '1'");
$page->assertSeeIn('@insights-metrics-legend-posts-value', '1')
->assertMissing('@insights-metrics-legend-posts-previous')
->assertMissing('@insights-metrics-legend-posts-change')
->assertNoJavaScriptErrors();
});
test('the insight banner reads the selected chart against the previous period', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metrics-insight');
$page->assertAttribute('@insights-metrics-insight', 'data-insight', 'content_impact.down')
->assertSeeIn('@insights-metrics-insight', 'Your content impact dropped this period.')
->click('@insights-metric-primary');
waitForState($page, '@insights-metrics-insight', "element.dataset.insight === 'posts.up'");
$page->assertSeeIn('@insights-metrics-insight', 'You posted more than last period.')
->click('@insights-preset-audience_growth');
waitForState($page, '@insights-metrics-insight', "element.dataset.insight === 'audience.up'");
$page->assertSeeIn('@insights-metrics-insight', 'Your audience grew this period.')
->assertMissing('@insights-metrics-post-totals-note')
->click('@insights-metrics-mode-previous');
waitForState($page, '@insights-metrics-mode-previous', "element.getAttribute('aria-pressed') === 'true'");
$page->assertAttribute('@insights-metrics-insight', 'data-insight', 'audience.up')
->click('@insights-metrics-mode-both');
waitForState($page, '@insights-metrics-insight', "element.dataset.insight.startsWith('audience_compared')");
$page->assertSeeIn('@insights-metrics-insight', 'Your audience grew')
->click('@insights-preset-visibility');
waitForState($page, '@insights-metrics-insight', "element.dataset.insight === 'visibility.up'");
$page->assertSeeIn('@insights-metrics-insight', 'More people discovered you than last period.')
->assertNoJavaScriptErrors();
});
test('on a phone the presets wrap and the mode toggle sits under the legend', function () {
$page = visit(route('app.channels.insights', $this->instagram))->resize(390, 844);
waitFor($page, '@insights-metrics-chart');
$page->assertScript('document.documentElement.scrollWidth <= window.innerWidth', true)
->assertScript(<<<'JS'
(() => {
const card = document.querySelector('[data-testid="insights-metrics"]').getBoundingClientRect();
return [...document.querySelectorAll('[data-testid="insights-metrics-controls"] button')]
.every((button) => button.getBoundingClientRect().right <= card.right + 1);
})()
JS, true)
->assertScript(<<<'JS'
(() => {
const legend = document.querySelector('[data-testid="insights-metrics-legend"]').getBoundingClientRect();
const toggle = document.querySelector('[data-testid="insights-metrics-mode-both"]').getBoundingClientRect();
return toggle.top >= legend.bottom;
})()
JS, true)
->assertNoJavaScriptErrors();
});
test('the column chooser hides and adds columns and remembers the choice', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-columns-trigger');
$page->assertVisible('@insights-sort-views')
->assertMissing('@insights-column-head-watch_time_minutes')
->click('@insights-columns-trigger');
waitFor($page, '@insights-column-views');
$page->assertMissing('@insights-column-impressions')
->click('@insights-column-views');
waitForGone($page, '@insights-sort-views');
$page->click('@insights-column-watch_time_minutes');
waitFor($page, '@insights-column-head-watch_time_minutes');
$page->assertMissing('@insights-sort-views')
->assertVisible('@insights-column-head-watch_time_minutes')
->assertVisible('@insights-column-views');
$page->refresh();
waitFor($page, '@insights-column-head-watch_time_minutes');
$page->assertMissing('@insights-sort-views')
->assertVisible('@insights-sort-reactions')
->assertNoJavaScriptErrors();
});
test('columns kept in a shape the table cannot read fall back to the default ones', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-columns-trigger');
setBrowserStorageItem($page, $this->user, 'insights.columns.instagram', 5);
$page->refresh();
waitFor($page, '@insights-columns-trigger');
$page->assertVisible('@insights-sort-views')
->assertMissing('@insights-column-head-watch_time_minutes')
->assertNoJavaScriptErrors();
});
test('a chart whose metrics have nothing in the previous period offers no comparison', function () {
$fresh = SocialAccount::factory()->create([
'workspace_id' => $this->instagram->workspace_id,
'platform' => Platform::Instagram,
]);
$today = CarbonImmutable::today('UTC');
foreach (range(0, 9) as $daysAgo) {
metricsCardFollowers($fresh, $today->subDays($daysAgo), 500 - $daysAgo);
}
$page = visit(route('app.channels.insights', $fresh));
waitFor($page, '@insights-metric-menu');
$page->click('@insights-metric-menu');
waitFor($page, '@insights-metric-option-followers');
$page->click('@insights-metric-option-followers');
waitFor($page, '@insights-metrics-legend-followers');
$page->assertMissing('@insights-metrics-mode-current')
->assertMissing('@insights-metrics-mode-both')
->assertSeeIn('@insights-metrics-legend-followers-change', '+9')
->assertNoJavaScriptErrors();
});
test('the axis prints each tick exactly, with the decimals a half step needs', function () {
$busy = SocialAccount::factory()->create([
'workspace_id' => $this->instagram->workspace_id,
'platform' => Platform::Instagram,
]);
$day = CarbonImmutable::today('UTC')->subDays(3)->setTime(10, 0);
foreach (range(1, 9) as $minutes) {
metricsCardPublication($busy, $day->addMinutes($minutes), ['reactions_count' => 1]);
}
$page = visit(route('app.channels.insights', $busy));
waitFor($page, '@insights-metrics-left-tick');
$page->assertScript('Array.from(document.querySelectorAll("[data-testid=insights-metrics-left-tick]"), (tick) => tick.textContent.trim()).join(",")', '0,2.5,5,7.5,10')
->assertNoJavaScriptErrors();
});
test('net new followers change by a count, never a percentage', function () {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metric-menu');
$page->click('@insights-metric-menu');
waitFor($page, '@insights-metric-option-net_followers');
$page->click('@insights-metric-option-net_followers');
waitFor($page, '@insights-metrics-legend-net_followers-change');
expect($page->script('document.querySelector(\'[data-testid="insights-metrics-legend-net_followers-change"]\').innerText.trim()'))->toMatch('/^[+-]?\d+$/');
$page->assertNoJavaScriptErrors();
});
test('a metric shows no change when the previous period had none of it', function () {
$fresh = SocialAccount::factory()->create([
'workspace_id' => $this->instagram->workspace_id,
'platform' => Platform::Instagram,
]);
$today = CarbonImmutable::today('UTC');
metricsCardFollowers($fresh, $today->subDays(1), 400);
foreach ([3, 1] as $daysAgo) {
metricsCardPublication($fresh, $today->subDays($daysAgo)->setTime(10, 0), ['reactions_count' => 1]);
}
$page = visit(route('app.channels.insights', $fresh));
waitFor($page, '@insights-metrics-legend-posts-value');
$page->assertSeeIn('@insights-metrics-legend-posts-value', '2')
->assertMissing('@insights-metrics-legend-posts-change')
->assertNoJavaScriptErrors();
});
test('the channel chart shares the period the view shows: the previous one in Comparison, both in Both', function (string $mode) {
$page = visit(route('app.channels.insights', $this->instagram));
waitFor($page, '@insights-metrics-legend-followers-value');
$current = trim((string) $page->script('document.querySelector(\'[data-testid="insights-metrics-legend-followers-value"]\').innerText'));
$page->click("@insights-metrics-mode-{$mode}");
waitForState($page, "@insights-metrics-mode-{$mode}", "element.getAttribute('aria-pressed') === 'true'");
recordCanvasDrawing($page);
$page->click('@share-channel-metrics');
waitForState($page, '@share-chart-preview', "element.dataset.state === 'ready'");
$drawn = $page->script('window.__drawn.map((entry) => entry.text)');
expect($page->script('window.__patterns > 0'))->toBeTrue()
->and(in_array($current, $drawn, true))->toBe($mode === 'both')
->and(in_array('+500%', $drawn, true))->toBe($mode === 'both');
$page->assertAttribute('@share-chart-preview', 'data-shape', 'combo')
->assertNoJavaScriptErrors();
})->with(['previous', 'both']);
test('a wide legend in the Both view shrinks on the share card to leave the channel’s name its room', function () {
$wide = SocialAccount::factory()->create([
'workspace_id' => $this->instagram->workspace_id,
'platform' => Platform::Instagram,
'display_name' => 'Castellano Creative Studio',
]);
$today = CarbonImmutable::today('UTC');
foreach (range(0, 94) as $daysAgo) {
metricsCardFollowers($wide, $today->subDays($daysAgo), 987654 - $daysAgo * 1111);
}
foreach (range(1, 60, 3) as $daysAgo) {
metricsCardPublication($wide, $today->subDays($daysAgo)->setTime(10, 0), ['reactions_count' => 1]);
}
$page = visit(route('app.channels.insights', $wide));
waitFor($page, '@insights-metrics-mode-both');
$page->click('@insights-metrics-mode-both');
waitForState($page, '@insights-metrics-mode-both', "element.getAttribute('aria-pressed') === 'true'");
recordCanvasDrawing($page);
$page->click('@share-channel-metrics');
waitForState($page, '@share-chart-preview', "element.dataset.state === 'ready'");
$figureSize = $page->script('Number(window.__drawn.findLast((entry) => entry.text === "987.7K").font.match(/(\\d+(?:\\.\\d+)?)px/)[1])');
expect($figureSize)->toBeLessThan(52);
$page->assertNoJavaScriptErrors();
});