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.

16.9 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Actions\Post\Queue\ReflowChannelQueue;
use App\Enums\Post\QueuePosition;
use App\Enums\Post\ScheduleMode;
use App\Enums\Post\Status as PostStatus;
use App\Enums\User\TimeFormat;
use App\Enums\User\WeekStart;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Support\PostingSchedule;

function channelSettingsPageSetup(?PostingSchedule $schedule = null): array
{
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create(['account_id' => $user->account_id, 'user_id' => $user->id]);
    $workspace->members()->attach($user->id, membershipPivot('admin'));
    $user->update(['current_workspace_id' => $workspace->id]);
    $channel = SocialAccount::factory()->linkedin()->create([
        'workspace_id' => $workspace->id,
        'posting_goal' => 3,
        'posting_schedule' => $schedule ?? PostingSchedule::empty()->withTime(1, '09:42'),
    ]);

    return [$user->fresh(), $channel];
}

function pickChannelSettingsOption(mixed $page, string $select, string $value): void
{
    $page->click("@{$select}");
    waitFor($page, "@{$select}-option-{$value}");
    $page->click("@{$select}-option-{$value}");
}

function chooseChannelSettingsTimezone(mixed $page, string $search, string $optionKey): void
{
    waitFor($page, '@channel-timezone-trigger');
    $page->click('@channel-timezone-trigger');
    waitFor($page, '@channel-timezone-search');
    $page->type('@channel-timezone-search', $search);
    waitFor($page, "@channel-timezone-option-{$optionKey}");
    $page->click("@channel-timezone-option-{$optionKey}");
}

function waitForChannelSaved(mixed $page, SocialAccount $channel, callable $predicate): void
{
    waitForDatabase($page, fn (): bool => $predicate($channel->fresh()));
}

test('adding a weekday time, removing one and toggling a day persist', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-day-1-time-0942');

    pickChannelSettingsOption($page, 'schedule-add-target', 'weekdays');
    pickChannelSettingsOption($page, 'schedule-add-hour', '18');
    pickChannelSettingsOption($page, 'schedule-add-minute', '30');
    $page->click('@schedule-add-submit');
    waitFor($page, '@schedule-day-5-time-1830');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 6);

    $page->click('@schedule-day-1-time-0942-remove');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 5);

    $page->click('@schedule-day-3-toggle');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->days()[3]['enabled'] === false);

    $fresh = $channel->fresh()->posting_schedule;
    expect($fresh->days()[1]['times'])->toBe(['18:30'])
        ->and($fresh->days()[3]['enabled'])->toBeFalse()
        ->and($fresh->days()[3]['times'])->toBe(['18:30'])
        ->and($fresh->days()[0]['times'])->toBe([]);
    $page->assertNoJavaScriptErrors();
});

test('the time zone and goal save from the page', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    chooseChannelSettingsTimezone($page, 'Warsaw', 'Europe-Warsaw');
    waitFor($page, '@channel-timezone-confirm-submit');
    $page->click('@channel-timezone-confirm-submit');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->timezone === 'Europe/Warsaw');

    $page->click('@channel-goal-increase');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_goal === 4);

    expect($channel->fresh()->timezone)->toBe('Europe/Warsaw')
        ->and($channel->fresh()->posting_goal)->toBe(4)
        ->and($channel->fresh()->posting_schedule->slotCount())->toBe(1);
    $page->assertNoJavaScriptErrors();
});

test('clear all, generate from goal and copy from another channel', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $other = SocialAccount::factory()->x()->create([
        'workspace_id' => $channel->workspace_id,
        'posting_schedule' => PostingSchedule::empty()->withTime(6, '17:08'),
    ]);
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-clear');
    $page->click('@schedule-clear');
    waitFor($page, '@schedule-clear-confirm');
    $page->click('@schedule-clear-confirm');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 0);

    $page->click('@schedule-generate');
    waitFor($page, '@schedule-generate-goal');
    $page->click('@schedule-generate-goal');
    waitFor($page, '@schedule-generate-confirm');
    $page->click('@schedule-generate-confirm');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 3);

    $page->click('@schedule-generate');
    waitFor($page, '@schedule-generate-copy');
    $page->click('@schedule-generate-copy');
    waitFor($page, "@schedule-generate-copy-{$other->id}");
    $page->click("@schedule-generate-copy-{$other->id}");
    waitFor($page, '@schedule-generate-confirm');
    $page->click('@schedule-generate-confirm');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->days()[6]['times'] === ['17:08']);

    expect($channel->fresh()->posting_schedule->toArray())->toEqual($other->posting_schedule->toArray());
    $page->assertNoJavaScriptErrors();
});

test('the copy from channel search filters the list and a pick asks to confirm', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $x = SocialAccount::factory()->x()->create([
        'workspace_id' => $channel->workspace_id,
        'posting_schedule' => PostingSchedule::empty()->withTime(6, '17:08'),
    ]);
    $bluesky = SocialAccount::factory()->bluesky()->create([
        'workspace_id' => $channel->workspace_id,
        'posting_schedule' => PostingSchedule::empty()->withTime(2, '10:00'),
    ]);
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-generate');
    $page->click('@schedule-generate');
    waitFor($page, '@schedule-generate-copy');
    $page->click('@schedule-generate-copy');
    waitFor($page, '@schedule-generate-copy-search');
    $page->assertVisible("@schedule-generate-copy-{$x->id}")
        ->assertVisible("@schedule-generate-copy-{$bluesky->id}");

    $page->type('@schedule-generate-copy-search', 'Bluesky');
    waitForGone($page, "@schedule-generate-copy-{$x->id}");

    $page->assertMissing("@schedule-generate-copy-{$x->id}")
        ->assertVisible("@schedule-generate-copy-{$bluesky->id}");

    $page->click("@schedule-generate-copy-{$bluesky->id}");
    waitFor($page, '@schedule-generate-confirm');
    $page->assertVisible('@schedule-generate-confirm')
        ->assertNoJavaScriptErrors();
});

test('a fifth time on a day is blocked', function () {
    [$user, $channel] = channelSettingsPageSetup(
        PostingSchedule::empty()->withTime(2, '08:00')->withTime(2, '09:00')->withTime(2, '10:00')->withTime(2, '11:00'),
    );
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-add-target');
    pickChannelSettingsOption($page, 'schedule-add-target', '2');
    waitFor($page, '@schedule-add-limit');

    $page->assertVisible('@schedule-add-limit')->assertMissing('@schedule-add-submit');
    expect($channel->fresh()->posting_schedule->days()[2]['times'])->toHaveCount(4);
    $page->assertNoJavaScriptErrors();
});

test('a channel without a schedule shows the empty state', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $channel->update(['posting_schedule' => null]);
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-empty');

    $page->assertVisible('@schedule-empty')->assertVisible('@schedule-generate')->assertNoJavaScriptErrors();
});

test('two rapid removes on different days both persist', function () {
    [$user, $channel] = channelSettingsPageSetup(
        PostingSchedule::empty()->withTime(1, '09:00')->withTime(2, '10:00')->withTime(3, '11:00'),
    );
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-day-1-time-0900-remove');
    $page->click('@schedule-day-1-time-0900-remove')->click('@schedule-day-2-time-1000-remove');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 1);
    $page->script('new Promise((r) => setTimeout(r, 500))');

    expect($channel->fresh()->posting_schedule->slotCount())->toBe(1)
        ->and($channel->fresh()->posting_schedule->days()[3]['times'])->toBe(['11:00']);
    $page->assertMissing('@schedule-day-1-time-0900')->assertMissing('@schedule-day-2-time-1000')->assertVisible('@schedule-day-3-time-1100');
    $page->assertNoJavaScriptErrors();
});

test('meet my posting goal is disabled with a hint once the goal is met', function () {
    [$user, $channel] = channelSettingsPageSetup(
        PostingSchedule::empty()->withTime(1, '09:00')->withTime(2, '10:00')->withTime(3, '11:00'),
    );
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-generate');
    $page->click('@schedule-generate');
    waitFor($page, '@schedule-generate-goal');

    $page->assertAttribute('@schedule-generate-goal', 'data-disabled', '')
        ->assertAttribute('@schedule-generate-copy', 'data-disabled', '')
        ->assertScript('getComputedStyle(document.querySelector("[data-testid=schedule-generate-goal]")).opacity', '1')
        ->assertScript('(() => { const toggle = document.querySelector("[data-testid=schedule-day-1-toggle]").getBoundingClientRect(); return `${toggle.width}x${toggle.height}`; })()', '30x16')
        ->assertScript('document.querySelector("[data-testid=schedule-day-1-toggle] svg") === null', true);
    $page->assertScript('(() => { const toggle = document.querySelector("[data-testid=schedule-day-1-toggle]"); const label = toggle.parentElement.querySelector("span").getBoundingClientRect(); const box = toggle.getBoundingClientRect(); return Math.abs((label.top + label.height / 2) - (box.top + box.height / 2)) <= 1 && label.height < 20; })()', true);

    $page->hover('@schedule-generate-goal-hint');
    waitFor($page, '@schedule-generate-goal-tooltip');

    $page->assertVisible('@schedule-generate-goal-tooltip')
        ->assertMissing('@schedule-generate-confirm');
    expect($channel->fresh()->posting_schedule->slotCount())->toBe(3);
    $page->assertNoJavaScriptErrors();
});

test('the schedule grid follows the week start and the 12-hour clock', function () {
    [$user, $channel] = channelSettingsPageSetup(PostingSchedule::empty()->withTime(0, '14:30'));
    $user->update(['week_starts_on' => WeekStart::Sunday, 'time_format' => TimeFormat::TwelveHour]);
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@schedule-day-0-time-1430-meridiem');

    $page->assertScript("document.querySelector('[data-testid^=\"schedule-day-\"]').dataset.testid", 'schedule-day-0')
        ->assertSeeIn('@schedule-day-0-time-1430-hour', '2')
        ->assertSeeIn('@schedule-day-0-time-1430-meridiem', 'PM')
        ->assertNoJavaScriptErrors();
});

test('the channel time zone picker suggests the browser time zone first', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    waitFor($page, '@channel-timezone-trigger');
    $page->click('@channel-timezone-trigger');
    waitFor($page, '@channel-timezone-detected');

    $page->assertVisible('@channel-timezone-detected')->assertNoJavaScriptErrors();
});

test('changing the time zone asks first and cancel and escape keep the old zone', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    chooseChannelSettingsTimezone($page, 'Warsaw', 'Europe-Warsaw');
    waitFor($page, '@channel-timezone-confirm-dialog');

    $order = $page->script('[...document.querySelectorAll(\'[data-testid="channel-timezone-confirm-dialog"] button[data-testid]\')].map((button) => button.dataset.testid)');
    expect($order)->toBe(['channel-timezone-confirm-cancel', 'channel-timezone-confirm-submit']);
    $page->assertSeeIn('@channel-timezone-confirm-dialog', 'Europe/Warsaw')
        ->click('@channel-timezone-confirm-cancel');
    waitForGone($page, '@channel-timezone-confirm-dialog');

    $page->assertSeeIn('@channel-timezone-trigger', 'UTC');
    expect($channel->fresh()->timezone)->toBe('UTC');

    chooseChannelSettingsTimezone($page, 'Warsaw', 'Europe-Warsaw');
    waitFor($page, '@channel-timezone-confirm-cancel');
    $page->keys('@channel-timezone-confirm-cancel', 'Escape');
    waitForGone($page, '@channel-timezone-confirm-dialog');
    $page->assertMissing('@channel-timezone-confirm-dialog');
    expect($channel->fresh()->timezone)->toBe('UTC');

    $page->assertNoJavaScriptErrors();
});

test('confirming the time zone saves it and reflows the queue into the new zone slots', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $post = Post::factory()->forAccount($channel)->create([
        'user_id' => $user->id,
        'status' => PostStatus::Scheduled,
        'schedule_mode' => ScheduleMode::Queue,
        'scheduled_at' => null,
    ]);
    ReflowChannelQueue::handle($channel, $post, QueuePosition::Next);
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel));
    chooseChannelSettingsTimezone($page, 'Warsaw', 'Europe-Warsaw');
    waitFor($page, '@channel-timezone-confirm-submit');
    $page->click('@channel-timezone-confirm-submit');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->timezone === 'Europe/Warsaw');

    waitForDatabase($page, fn (): bool => $post->fresh()->scheduled_at->setTimezone('Europe/Warsaw')->format('H:i') === '09:42');

    $local = $post->fresh()->scheduled_at->setTimezone('Europe/Warsaw');
    expect($local->format('H:i'))->toBe('09:42')
        ->and($local->dayOfWeek)->toBe(1);
    $page->assertNoJavaScriptErrors();
});

test('on a phone the schedule stacks one day per row in week-start order without horizontal scroll', function () {
    [$user, $channel] = channelSettingsPageSetup(PostingSchedule::empty()->withTime(1, '09:42')->withTime(0, '14:30'));
    $user->update(['week_starts_on' => WeekStart::Sunday, 'time_format' => TimeFormat::TwelveHour]);
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel))->resize(390, 844);
    waitFor($page, '@schedule-day-1-time-0942');

    $page->assertScript(<<<'JS'
        (() => {
            const days = [...document.querySelectorAll('[data-testid^="schedule-day-"]')]
                .filter((el) => /^schedule-day-\d$/.test(el.dataset.testid));
            const rects = days.map((el) => el.getBoundingClientRect());
            const order = days.map((el) => el.dataset.testid.slice(-1)).join('');
            const stacked = rects.every((rect, i) => i === 0 || (rect.top >= rects[i - 1].bottom - 1 && Math.abs(rect.left - rects[0].left) < 1));

            return order === '0123456' && stacked && document.documentElement.scrollWidth <= window.innerWidth;
        })()
    JS, true)
        ->assertVisible('@schedule-day-2-empty')
        ->assertSeeIn('@schedule-day-2-empty', 'No posting times')
        ->assertNoJavaScriptErrors();
});

test('on a phone toggling a day, adding and removing a time persist', function () {
    [$user, $channel] = channelSettingsPageSetup();
    $this->actingAs($user);

    $page = visit(route('app.channels.settings', $channel))->resize(390, 844);
    waitFor($page, '@schedule-day-1-time-0942');

    pickChannelSettingsOption($page, 'schedule-add-target', 'weekdays');
    pickChannelSettingsOption($page, 'schedule-add-hour', '18');
    pickChannelSettingsOption($page, 'schedule-add-minute', '30');
    $page->click('@schedule-add-submit');
    waitFor($page, '@schedule-day-5-time-1830');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 6);

    $page->click('@schedule-day-1-time-0942-remove');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->slotCount() === 5);

    $page->click('@schedule-day-3-toggle');
    waitForChannelSaved($page, $channel, fn (SocialAccount $c): bool => $c->posting_schedule->days()[3]['enabled'] === false);

    $fresh = $channel->fresh()->posting_schedule;
    expect($fresh->days()[1]['times'])->toBe(['18:30'])
        ->and($fresh->days()[3]['enabled'])->toBeFalse();
    $page->assertScript('document.documentElement.scrollWidth <= window.innerWidth', true)
        ->assertNoJavaScriptErrors();
});