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.

41.7 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Models\Idea;
use App\Models\IdeaStage;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceLabel;
use Pest\Browser\Playwright\Client;

/**
 * @return array{0: User, 1: Workspace, 2: array<string, IdeaStage>}
 */
function createIdeasBoardSetup(): 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]);
    subscribeAccount($user->account);

    $stages = $workspace->ideaStages()->get();

    return [$user->fresh(), $workspace, [
        'todo' => $stages[0],
        'in_progress' => $stages[1],
        'done' => $stages[2],
    ]];
}

function createIdeasBoardIdea(Workspace $workspace, User $user, ?IdeaStage $stage, int $position, string $title): Idea
{
    return Idea::factory()->create([
        'workspace_id' => $workspace->id,
        'user_id' => $user->id,
        'idea_stage_id' => $stage?->id,
        'position' => $position,
        'title' => $title,
    ]);
}

function createIdeasBoardMouse(mixed $page, string $method, array $params = []): void
{
    $page->script('true');
    $awaitable = property_exists($page, 'waitablePage') ? (fn () => $this->waitablePage)->call($page) : $page;
    $playwrightPage = (fn () => $this->page)->call($awaitable);
    $guid = (fn () => $this->guid)->call($playwrightPage);

    iterator_to_array(Client::instance()->execute($guid, $method, $params));
}

/**
 * @return array{x: float, y: float}
 */
function createIdeasBoardPoint(mixed $page, string $testId, float $xRatio = 0.5, float $yRatio = 0.5): array
{
    return $page->script(<<<JS
        (() => {
            const rect = document.querySelector('[data-testid="{$testId}"]').getBoundingClientRect();
            return { x: rect.left + rect.width * {$xRatio}, y: rect.top + rect.height * {$yRatio} };
        })()
    JS);
}

function createIdeasBoardPickUp(mixed $page, string $testId): void
{
    $point = createIdeasBoardPoint($page, $testId);
    createIdeasBoardMouse($page, 'mouseMove', ['x' => $point['x'], 'y' => $point['y']]);
    createIdeasBoardMouse($page, 'mouseDown', ['button' => 'left', 'clickCount' => 1]);
    createIdeasBoardMouse($page, 'mouseMove', ['x' => $point['x'] + 8, 'y' => $point['y'] + 2, 'steps' => 4]);
}

function createIdeasBoardMoveTo(mixed $page, string $testId, float $xRatio, float $yRatio): void
{
    $point = createIdeasBoardPoint($page, $testId, $xRatio, $yRatio);
    createIdeasBoardMouse($page, 'mouseMove', ['x' => $point['x'], 'y' => $point['y'], 'steps' => 30]);
    $page->script('new Promise((resolve) => setTimeout(resolve, 400))');
}

function createIdeasBoardRecordDragImages(mixed $page): void
{
    $page->script(<<<'JS'
        (() => {
            window.__dragImages = [];
            const original = DataTransfer.prototype.setDragImage;
            DataTransfer.prototype.setDragImage = function (image, x, y) {
                window.__dragImages.push(image instanceof HTMLImageElement ? image.src.slice(0, 10) : image.tagName);
                return original.call(this, image, x, y);
            };
        })()
    JS);
}

/**
 * @return list<string>
 */
function createIdeasBoardCardSlots(mixed $page, string $columnKey): array
{
    return $page->script(<<<JS
        [...document.querySelector('[data-testid="idea-column-list-{$columnKey}"]').children]
            .filter((child) => child.offsetHeight > 0 && (child.dataset.sortableIdea || child.dataset.testid === 'idea-card-placeholder'))
            .map((child) => child.dataset.testid === 'idea-card-placeholder' ? 'placeholder' : child.dataset.sortableIdea)
    JS);
}

/**
 * @return list<string>
 */
function createIdeasBoardStageSlots(mixed $page): array
{
    return $page->script(<<<'JS'
        [...document.querySelector('[data-testid="ideas-board"]').children]
            .filter((child) => child.offsetWidth > 0 && (child.dataset.sortableStage || child.dataset.testid === 'idea-stage-placeholder'))
            .map((child) => child.dataset.testid === 'idea-stage-placeholder' ? 'placeholder' : child.dataset.sortableStage)
    JS);
}

function createIdeasBoardIsDropTarget(mixed $page, string $columnKey): bool
{
    return $page->script("document.querySelector('[data-testid=\"idea-column-{$columnKey}\"]').hasAttribute('data-drop-target')");
}

test('dragging a card onto another in the same column reorders it', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$b->id}");
    $page->drag("@idea-card-{$b->id}", "@idea-card-{$a->id}");
    waitForDatabase($page, fn (): bool => $b->refresh()->position < $a->refresh()->position);

    expect($b->refresh()->position)->toBeLessThan($a->refresh()->position);
    $page->assertNoJavaScriptErrors();
});

test('dragging a card onto an empty column moves it there', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$a->id}");
    $page->drag("@idea-card-{$a->id}", "@idea-column-{$stages['done']->id}");
    waitForDatabase($page, fn (): bool => $a->refresh()->idea_stage_id === $stages['done']->id);

    expect($a->refresh()->idea_stage_id)->toBe($stages['done']->id);
    $page->assertNoJavaScriptErrors();
});

test('a move that cannot reach the server says so and puts the card back', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$a->id}");
    $page->script(<<<'JS'
        (() => {
            const send = XMLHttpRequest.prototype.send;
            XMLHttpRequest.prototype.send = function () {
                XMLHttpRequest.prototype.send = send;
                this.dispatchEvent(new ProgressEvent('error'));
                this.dispatchEvent(new ProgressEvent('loadend'));
            };
        })()
    JS);
    $page->drag("@idea-card-{$a->id}", "@idea-column-{$stages['done']->id}");
    waitFor($page, '@ideas-move-error-toast');
    waitFor($page, "@idea-column-{$stages['todo']->id} @idea-card-{$a->id}");

    $page->assertNoJavaScriptErrors();
    expect($a->refresh()->idea_stage_id)->toBe($stages['todo']->id);
});

test('a new stage is created on enter and nothing is created on escape', function () {
    [$user, $workspace] = createIdeasBoardSetup();
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, '@idea-stage-new');
    $page->click('@idea-stage-new');
    waitFor($page, '@idea-stage-new-input');
    $page->type('@idea-stage-new-input', 'Reviewed')->keys('@idea-stage-new-input', 'Enter');
    waitForDatabase($page, fn (): bool => $workspace->ideaStages()->where('name', 'Reviewed')->exists());

    $stage = $workspace->ideaStages()->where('name', 'Reviewed')->firstOrFail();
    waitFor($page, "@idea-column-{$stage->id}");
    $page->assertVisible("@idea-column-{$stage->id}");

    waitFor($page, '@idea-stage-new');
    $page->click('@idea-stage-new');
    waitFor($page, '@idea-stage-new-input');
    $page->type('@idea-stage-new-input', 'Discarded')->keys('@idea-stage-new-input', 'Escape');
    waitFor($page, '@idea-stage-new');

    expect($workspace->ideaStages()->count())->toBe(4);
    $page->assertMissing('@idea-stage-new-input')
        ->assertNoJavaScriptErrors();
});

test('deleting a stage keeps its ideas under unassigned', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-column-menu-{$stages['todo']->id}");
    $page->click("@idea-column-menu-{$stages['todo']->id}");
    waitFor($page, "@idea-stage-delete-{$stages['todo']->id}");
    $page->click("@idea-stage-delete-{$stages['todo']->id}");
    waitFor($page, '@confirm-delete-action');
    $page->assertMissing('@confirm-delete-input')
        ->click('@confirm-delete-action');
    waitForGone($page, "@idea-column-{$stages['todo']->id}");

    $page->assertMissing("@idea-column-{$stages['todo']->id}");
    expect($page->script("!!document.querySelector('[data-testid=\"idea-column-unassigned\"] [data-testid=\"idea-card-{$a->id}\"]')"))->toBeTrue()
        ->and($a->refresh()->idea_stage_id)->toBeNull();
    $page->assertNoJavaScriptErrors();
});

test('dragging a stage handle onto another column reorders the stages', function () {
    [$user, , $stages] = createIdeasBoardSetup();
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-stage-handle-{$stages['done']->id}");
    $page->hover("@idea-column-{$stages['done']->id}");
    $page->drag("@idea-stage-handle-{$stages['done']->id}", "@idea-column-{$stages['todo']->id}");
    waitForDatabase($page, fn (): bool => $stages['done']->refresh()->position < $stages['in_progress']->refresh()->position);

    expect($stages['done']->refresh()->position)->toBeLessThan($stages['in_progress']->refresh()->position);
    $page->assertNoJavaScriptErrors();
});

test('selected ideas are deleted in bulk and the selection can be cleared', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $c = createIdeasBoardIdea($workspace, $user, $stages['todo'], 2, 'Idea C');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-menu-{$a->id}");
    $page->click("@idea-card-menu-{$a->id}");
    waitFor($page, "@idea-select-{$a->id}");
    $page->click("@idea-select-{$a->id}");
    waitFor($page, "@idea-card-checkbox-{$b->id}");
    $page->click("@idea-card-checkbox-{$b->id}");
    waitFor($page, '@ideas-bulk-delete');
    $page->assertSeeIn('@ideas-bulk-delete', '2')
        ->click('@ideas-bulk-delete');
    waitFor($page, '@ideas-bulk-delete-confirm');
    $page->assertMissing('@confirm-delete-input')
        ->click('@ideas-bulk-delete-confirm');
    waitForDatabase($page, fn (): bool => Idea::whereKey([$a->id, $b->id])->doesntExist());

    expect(Idea::whereKey([$a->id, $b->id])->exists())->toBeFalse()
        ->and(Idea::whereKey($c->id)->exists())->toBeTrue();
    waitForGone($page, '@ideas-bulk-bar');
    $page->assertMissing('@ideas-bulk-bar');

    $page->click("@idea-card-menu-{$c->id}");
    waitFor($page, "@idea-select-{$c->id}");
    $page->click("@idea-select-{$c->id}");
    waitFor($page, '@ideas-clear-selection');
    $page->click('@ideas-clear-selection');
    waitForGone($page, '@ideas-bulk-bar');

    $page->assertMissing('@ideas-bulk-bar')
        ->assertMissing("@idea-card-checkbox-{$c->id}")
        ->assertNoJavaScriptErrors();
});

test('the gallery view lists ideas and filters them by label and stage', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $label = WorkspaceLabel::factory()->create(['workspace_id' => $workspace->id]);
    $tagged = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Tagged');
    $tagged->labels()->attach($label->id);
    $inDone = createIdeasBoardIdea($workspace, $user, $stages['done'], 0, 'In done');
    $loose = createIdeasBoardIdea($workspace, $user, null, 0, 'Loose');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, '@ideas-view-gallery');
    $page->click('@ideas-view-gallery');
    waitFor($page, '@ideas-gallery');

    $page->assertVisible("@idea-card-{$tagged->id}")
        ->assertVisible("@idea-card-{$inDone->id}")
        ->assertVisible("@idea-card-{$loose->id}");
    expect($page->script('location.search'))->toContain('view=gallery');

    $page->click('@ideas-filter-labels-filter');
    waitFor($page, "@ideas-filter-labels-option-{$label->id}");
    $page->click("@ideas-filter-labels-option-{$label->id}");
    waitForGone($page, "@idea-card-{$loose->id}");

    $page->assertVisible("@idea-card-{$tagged->id}")
        ->assertMissing("@idea-card-{$loose->id}")
        ->assertMissing("@idea-card-{$inDone->id}");

    $page->click("@ideas-filter-labels-option-{$label->id}");
    waitFor($page, "@idea-card-{$loose->id}");

    $page->click('@ideas-filter-labels-untagged-checkbox');
    waitForGone($page, "@idea-card-{$tagged->id}");
    $page->assertVisible("@idea-card-{$loose->id}")
        ->assertMissing("@idea-card-{$tagged->id}");
    expect($page->script('location.search'))->toContain('untagged=1');

    $page->click('@ideas-filter-labels-clear');
    waitFor($page, "@idea-card-{$tagged->id}");
    $page->assertVisible("@idea-card-{$loose->id}")
        ->assertPresent('@ideas-filter-labels-settings');
    $page->keys('@ideas-filter-labels-search', 'Escape');
    waitForGone($page, '@ideas-filter-labels-search');
    $page->click('@ideas-filter-stages-filter');
    waitFor($page, "@ideas-filter-stages-option-{$stages['done']->id}");
    $page->assertScript("document.querySelector('[data-testid=\"ideas-filter-stages-option-{$stages['done']->id}\"]').getBoundingClientRect().height <= 32", true);
    $page->click("@ideas-filter-stages-option-{$stages['done']->id}");
    waitForGone($page, "@idea-card-{$loose->id}");

    $page->assertVisible("@idea-card-{$inDone->id}")
        ->assertMissing("@idea-card-{$tagged->id}")
        ->assertMissing("@idea-card-{$loose->id}")
        ->assertNoJavaScriptErrors();
});

test('a drop made while a move is in flight is applied after the first move completes', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$b->id}");
    $page->script(<<<'JS'
        (() => {
            const open = XMLHttpRequest.prototype.open;
            const send = XMLHttpRequest.prototype.send;
            window.__ideaMoves = [];
            XMLHttpRequest.prototype.open = function (method, url, ...rest) {
                this.__ideaMove = method.toUpperCase() === 'PUT' && String(url).endsWith('/move');
                return open.call(this, method, url, ...rest);
            };
            XMLHttpRequest.prototype.send = function (body) {
                if (!this.__ideaMove) return send.call(this, body);
                window.__ideaMoves.push(body);
                if (window.__ideaMoves.length === 1) {
                    window.__releaseIdeaMove = () => send.call(this, body);
                    return;
                }
                return send.call(this, body);
            };
        })();
    JS);

    $page->drag("@idea-card-{$a->id}", "@idea-column-{$stages['done']->id}");
    waitForCondition($page, 'window.__ideaMoves.length === 1');
    $page->drag("@idea-card-{$b->id}", "@idea-column-{$stages['done']->id}");
    waitForState($page, "@idea-column-{$stages['done']->id} [data-idea-id=\"{$b->id}\"]", 'true');

    expect($page->script('window.__ideaMoves.length'))->toBe(1)
        ->and($a->refresh()->idea_stage_id)->toBe($stages['todo']->id)
        ->and($b->refresh()->idea_stage_id)->toBe($stages['todo']->id);

    $page->script('window.__releaseIdeaMove()');
    waitForDatabase($page, fn (): bool => $b->refresh()->idea_stage_id === $stages['done']->id);

    $order = $page->script("[...document.querySelectorAll('[data-testid=\"idea-column-{$stages['done']->id}\"] [data-idea-id]')].map((el) => el.dataset.ideaId)");
    expect($page->script('window.__ideaMoves.length'))->toBe(2)
        ->and($a->refresh()->idea_stage_id)->toBe($stages['done']->id)
        ->and($b->refresh()->idea_stage_id)->toBe($stages['done']->id)
        ->and(Idea::where('idea_stage_id', $stages['done']->id)->orderBy('position')->pluck('id')->all())->toBe($order);
    $page->assertNoJavaScriptErrors();
});

test('several drops made while a move is in flight are all saved, in order, after it completes', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $c = createIdeasBoardIdea($workspace, $user, $stages['todo'], 2, 'Idea C');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$c->id}");
    $page->script(<<<'JS'
        (() => {
            const open = XMLHttpRequest.prototype.open;
            const send = XMLHttpRequest.prototype.send;
            window.__ideaMoves = [];
            XMLHttpRequest.prototype.open = function (method, url, ...rest) {
                this.__ideaMove = method.toUpperCase() === 'PUT' && String(url).endsWith('/move');
                return open.call(this, method, url, ...rest);
            };
            XMLHttpRequest.prototype.send = function (body) {
                if (!this.__ideaMove) return send.call(this, body);
                window.__ideaMoves.push(body);
                if (window.__ideaMoves.length === 1) {
                    window.__releaseIdeaMove = () => send.call(this, body);
                    return;
                }
                return send.call(this, body);
            };
        })();
    JS);

    $page->drag("@idea-card-{$a->id}", "@idea-column-{$stages['done']->id}");
    waitForCondition($page, 'window.__ideaMoves.length === 1');
    $page->drag("@idea-card-{$b->id}", "@idea-column-{$stages['done']->id}");
    waitForState($page, "@idea-column-{$stages['done']->id} [data-idea-id=\"{$b->id}\"]", 'true');
    $page->drag("@idea-card-{$c->id}", "@idea-column-{$stages['done']->id}");
    waitForState($page, "@idea-column-{$stages['done']->id} [data-idea-id=\"{$c->id}\"]", 'true');

    $page->script('window.__releaseIdeaMove()');
    waitForDatabase($page, fn (): bool => $c->refresh()->idea_stage_id === $stages['done']->id);

    $order = $page->script("[...document.querySelectorAll('[data-testid=\"idea-column-{$stages['done']->id}\"] [data-idea-id]')].map((el) => el.dataset.ideaId)");
    expect($page->script('window.__ideaMoves.length'))->toBe(3)
        ->and(Idea::whereKey([$a->id, $b->id, $c->id])->pluck('idea_stage_id')->unique()->all())->toBe([$stages['done']->id])
        ->and(Idea::where('idea_stage_id', $stages['done']->id)->orderBy('position')->pluck('id')->all())->toBe($order);
    $page->assertNoJavaScriptErrors();
});

test('a card dropped twice while a move is in flight is saved where it was dropped last', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$b->id}");
    $page->script(<<<'JS'
        (() => {
            const open = XMLHttpRequest.prototype.open;
            const send = XMLHttpRequest.prototype.send;
            window.__ideaMoves = [];
            XMLHttpRequest.prototype.open = function (method, url, ...rest) {
                this.__ideaMove = method.toUpperCase() === 'PUT' && String(url).endsWith('/move');
                return open.call(this, method, url, ...rest);
            };
            XMLHttpRequest.prototype.send = function (body) {
                if (!this.__ideaMove) return send.call(this, body);
                window.__ideaMoves.push(body);
                if (window.__ideaMoves.length === 1) {
                    window.__releaseIdeaMove = () => send.call(this, body);
                    return;
                }
                return send.call(this, body);
            };
        })();
    JS);

    $page->drag("@idea-card-{$b->id}", "@idea-column-{$stages['done']->id}");
    waitForCondition($page, 'window.__ideaMoves.length === 1');
    $page->drag("@idea-card-{$a->id}", "@idea-column-{$stages['in_progress']->id}");
    waitForState($page, "@idea-column-{$stages['in_progress']->id} [data-idea-id=\"{$a->id}\"]", 'true');
    $page->drag("@idea-card-{$a->id}", "@idea-column-{$stages['done']->id}");
    waitForState($page, "@idea-column-{$stages['done']->id} [data-idea-id=\"{$a->id}\"]", 'true');

    $page->script('window.__releaseIdeaMove()');
    waitForDatabase($page, fn (): bool => $a->refresh()->idea_stage_id === $stages['done']->id);

    waitForState($page, "@idea-column-{$stages['done']->id} [data-idea-id=\"{$a->id}\"]", 'true');

    expect($page->script('window.__ideaMoves.length'))->toBe(2)
        ->and($b->refresh()->idea_stage_id)->toBe($stages['done']->id);
    $page->assertNoJavaScriptErrors();
});

test('move to stage works on a filtered board and appends to the end of the stage', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $label = WorkspaceLabel::factory()->create(['workspace_id' => $workspace->id]);
    $existing = createIdeasBoardIdea($workspace, $user, $stages['done'], 0, 'Already done');
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Tagged A');
    $a->labels()->attach($label->id);
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index', ['labels' => [$label->id]]));
    waitFor($page, '@ideas-drag-disabled-hint');
    waitFor($page, "@idea-card-menu-{$a->id}");
    $page->click("@idea-card-menu-{$a->id}");
    waitFor($page, "@idea-move-{$a->id}");
    $page->click("@idea-move-{$a->id}");
    waitFor($page, "@idea-move-to-{$a->id}-{$stages['done']->id}");
    $page->click("@idea-move-to-{$a->id}-{$stages['done']->id}");
    waitForDatabase($page, fn (): bool => $a->refresh()->idea_stage_id === $stages['done']->id);

    expect($a->refresh()->idea_stage_id)->toBe($stages['done']->id)
        ->and($a->position)->toBeGreaterThan($existing->refresh()->position);
    waitForState($page, "@idea-column-{$stages['done']->id} @idea-card-{$a->id}", 'true');
    expect($page->script("!!document.querySelector('[data-testid=\"idea-column-{$stages['done']->id}\"] [data-testid=\"idea-card-{$a->id}\"]')"))->toBeTrue();
    $page->assertNoJavaScriptErrors();
});

test('changing a filter clears the selection', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $label = WorkspaceLabel::factory()->create(['workspace_id' => $workspace->id]);
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-menu-{$a->id}");
    $page->click("@idea-card-menu-{$a->id}");
    waitFor($page, "@idea-select-{$a->id}");
    $page->click("@idea-select-{$a->id}");
    waitFor($page, '@ideas-bulk-bar');
    waitForCondition($page, "document.body.style.pointerEvents !== 'none' && !document.querySelector('[role=\"menu\"]')");

    $page->click('@ideas-filter-labels-filter');
    waitFor($page, '@ideas-filter-labels-untagged-checkbox');
    $page->click('@ideas-filter-labels-untagged-checkbox');
    waitForGone($page, '@ideas-bulk-bar');

    $page->assertMissing('@ideas-bulk-bar')
        ->assertNoJavaScriptErrors();
});

test('deleting a single idea asks for confirmation without typing a keyword', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-menu-{$a->id}");
    $page->click("@idea-card-menu-{$a->id}");
    waitFor($page, "@idea-delete-{$a->id}");
    $page->click("@idea-delete-{$a->id}");
    waitFor($page, '@confirm-delete-action');

    expect(trim($page->script("document.querySelector('[data-testid=\"confirm-delete-modal\"] [data-slot=\"dialog-description\"]')?.textContent ?? ''")))->toBe(__('create.ideas.delete_confirm_body'));

    expect($page->script("[...document.querySelectorAll('[data-testid=\"confirm-delete-cancel\"], [data-testid=\"confirm-delete-action\"]')].map((button) => button.offsetHeight)"))->toBe([32, 32]);

    $page->assertMissing('@confirm-delete-input')
        ->click('@confirm-delete-action');
    waitForDatabase($page, fn (): bool => Idea::whereKey($a->id)->doesntExist());

    expect(Idea::whereKey($a->id)->exists())->toBeFalse();
    $page->assertNoJavaScriptErrors();
});

test('a lifted placeholder shows where a dragged card lands, in its own column and in another one', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $c = createIdeasBoardIdea($workspace, $user, $stages['todo'], 2, 'Idea C');
    $d = createIdeasBoardIdea($workspace, $user, $stages['done'], 0, 'Idea D');
    $this->actingAs($user);
    $todo = $stages['todo']->id;
    $done = $stages['done']->id;

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$d->id}");
    $height = $page->script("document.querySelector('[data-testid=\"idea-card-{$c->id}\"]').parentElement.offsetHeight");
    createIdeasBoardRecordDragImages($page);

    createIdeasBoardPickUp($page, "idea-card-{$c->id}");
    waitFor($page, '@idea-card-placeholder');

    expect($page->script('window.__dragImages'))->toBe(['data:image'])
        ->and($page->script("document.querySelector('[data-testid=\"idea-card-placeholder-preview\"]')?.textContent"))->toContain('Idea C')
        ->and($page->script("document.querySelector('[data-testid=\"idea-card-placeholder\"]').offsetHeight"))->toBe($height)
        ->and(createIdeasBoardCardSlots($page, $todo))->toBe([$a->id, $b->id, 'placeholder']);

    createIdeasBoardMoveTo($page, "idea-card-{$a->id}", 0.5, 0.25);

    expect(createIdeasBoardCardSlots($page, $todo))->toBe(['placeholder', $a->id, $b->id])
        ->and(createIdeasBoardIsDropTarget($page, $todo))->toBeTrue();

    createIdeasBoardMoveTo($page, "idea-card-{$d->id}", 0.5, 0.75);

    expect(createIdeasBoardCardSlots($page, $done))->toBe([$d->id, 'placeholder'])
        ->and(createIdeasBoardCardSlots($page, $todo))->toBe([$a->id, $b->id])
        ->and(createIdeasBoardIsDropTarget($page, $done))->toBeTrue()
        ->and(createIdeasBoardIsDropTarget($page, $todo))->toBeFalse();

    createIdeasBoardMouse($page, 'mouseUp', ['button' => 'left', 'clickCount' => 1]);
    waitForDatabase($page, fn (): bool => $c->refresh()->idea_stage_id === $done);

    expect($c->refresh()->idea_stage_id)->toBe($done)
        ->and(Idea::where('idea_stage_id', $done)->orderBy('position')->pluck('id')->all())->toBe([$d->id, $c->id]);
    waitForGone($page, '@idea-card-placeholder');
    expect(createIdeasBoardCardSlots($page, $done))->toBe([$d->id, $c->id]);
    $page->assertNoJavaScriptErrors();
});

test('a card dropped outside every column goes back where it was', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $a = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Idea A');
    $b = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Idea B');
    $this->actingAs($user);
    $todo = $stages['todo']->id;

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$b->id}");

    createIdeasBoardPickUp($page, "idea-card-{$b->id}");
    waitFor($page, '@idea-card-placeholder');
    createIdeasBoardMoveTo($page, "idea-card-{$a->id}", 0.5, 0.25);

    expect(createIdeasBoardCardSlots($page, $todo))->toBe(['placeholder', $a->id]);

    createIdeasBoardMoveTo($page, 'idea-stage-new', 0.5, 0.5);

    expect(createIdeasBoardCardSlots($page, $todo))->toBe([$a->id, 'placeholder'])
        ->and(createIdeasBoardIsDropTarget($page, $todo))->toBeFalse();

    createIdeasBoardMouse($page, 'mouseUp', ['button' => 'left', 'clickCount' => 1]);
    $page->script('new Promise((resolve) => setTimeout(resolve, 800))');

    expect(Idea::where('idea_stage_id', $todo)->orderBy('position')->pluck('id')->all())->toBe([$a->id, $b->id])
        ->and(createIdeasBoardCardSlots($page, $todo))->toBe([$a->id, $b->id]);
    $page->assertNoJavaScriptErrors();
});

test('a dragged stage leaves a column-sized placeholder where it will land', function () {
    [$user, , $stages] = createIdeasBoardSetup();
    $this->actingAs($user);
    $todo = $stages['todo']->id;
    $progress = $stages['in_progress']->id;
    $done = $stages['done']->id;

    $page = visit(route('app.create.ideas.index'))->resize(1440, 900);
    waitFor($page, "@idea-column-{$done}");
    $width = $page->script("document.querySelector('[data-testid=\"idea-column-{$done}\"]').offsetWidth");
    createIdeasBoardRecordDragImages($page);

    $page->hover("@idea-column-{$done}");
    createIdeasBoardPickUp($page, "idea-stage-handle-{$done}");
    waitFor($page, '@idea-stage-placeholder');

    expect($page->script('window.__dragImages'))->toBe(['data:image'])
        ->and($page->script("document.querySelector('[data-testid=\"idea-stage-placeholder\"]').offsetWidth"))->toBe($width)
        ->and($page->script("document.querySelector('[data-testid=\"idea-stage-placeholder-preview\"]')?.textContent"))->toContain($stages['done']->name)
        ->and(createIdeasBoardStageSlots($page))->toBe([$todo, $progress, 'placeholder']);

    createIdeasBoardMoveTo($page, "idea-column-{$todo}", 0.25, 0.5);

    expect(createIdeasBoardStageSlots($page))->toBe(['placeholder', $todo, $progress]);

    createIdeasBoardMouse($page, 'mouseUp', ['button' => 'left', 'clickCount' => 1]);
    waitForDatabase($page, fn (): bool => $stages['done']->refresh()->position < $stages['todo']->refresh()->position);

    expect($stages['done']->refresh()->position)->toBeLessThan($stages['todo']->refresh()->position);
    waitForGone($page, '@idea-stage-placeholder');
    expect(createIdeasBoardStageSlots($page))->toBe([$done, $todo, $progress]);
    $page->assertNoJavaScriptErrors();
});

test('the gallery stage filter has an unassigned option that combines with stages', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $loose = createIdeasBoardIdea($workspace, $user, null, 0, 'Loose');
    $inTodo = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'In todo');
    $inDone = createIdeasBoardIdea($workspace, $user, $stages['done'], 0, 'In done');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index', ['view' => 'gallery']));
    waitFor($page, '@ideas-filter-stages-filter');
    $page->click('@ideas-filter-stages-filter');
    waitFor($page, '@ideas-filter-stages-unassigned');

    expect($page->script("(() => { const rows = [...document.querySelector('[data-testid=\"ideas-filter-stages-unassigned\"]').parentElement.parentElement.querySelectorAll('[data-testid=\"ideas-filter-stages-unassigned\"], [data-testid^=\"ideas-filter-stages-option-\"]')]; return rows[0].dataset.testid; })()"))->toBe('ideas-filter-stages-unassigned')
        ->and($page->script("document.querySelector('[data-testid=\"ideas-filter-stages-unassigned\"]').getBoundingClientRect().height"))->toBeLessThanOrEqual(32);

    $page->click('@ideas-filter-stages-unassigned-checkbox');
    waitForGone($page, "@idea-card-{$inTodo->id}");

    $page->assertVisible("@idea-card-{$loose->id}")
        ->assertMissing("@idea-card-{$inDone->id}")
        ->assertSeeIn('@ideas-filter-stages-count', '1');
    expect($page->script('location.search'))->toContain('unassigned=1');

    $page->click("@ideas-filter-stages-option-{$stages['todo']->id}");
    waitFor($page, "@idea-card-{$inTodo->id}");

    $page->assertVisible("@idea-card-{$loose->id}")
        ->assertMissing("@idea-card-{$inDone->id}")
        ->assertSeeIn('@ideas-filter-stages-count', '2');

    $page->click('@ideas-filter-stages-toggle-all');
    waitFor($page, "@idea-card-{$inDone->id}");

    $page->assertVisible("@idea-card-{$loose->id}")
        ->assertVisible("@idea-card-{$inTodo->id}")
        ->assertMissing('@ideas-filter-stages-count');
    expect($page->script('location.search'))->not->toContain('unassigned');
    $page->assertNoJavaScriptErrors();
});

test('the new stage button turns into an empty column in place and escape brings it back', function () {
    [$user, , $stages] = createIdeasBoardSetup();
    $this->actingAs($user);
    $done = $stages['done']->id;

    $page = visit(route('app.create.ideas.index'))->resize(1600, 900);
    waitFor($page, '@idea-stage-new');
    $before = $page->script("(() => { const rect = document.querySelector('[data-testid=\"idea-stage-new\"]').getBoundingClientRect(); return { left: rect.left, top: rect.top }; })()");

    $page->click('@idea-stage-new');
    $after = $page->script(<<<JS
        new Promise((resolve) => requestAnimationFrame(() => {
            const slot = document.querySelector('[data-testid="idea-stage-new-slot"]').getBoundingClientRect();
            const column = document.querySelector('[data-testid="idea-column-{$done}"]');
            const title = column.querySelector('h2').getBoundingClientRect();
            const input = document.querySelector('[data-testid="idea-stage-new-input"]');
            const field = input?.getBoundingClientRect();
            resolve({
                button: !!document.querySelector('[data-testid="idea-stage-new"]'),
                left: slot.left,
                width: slot.width,
                height: slot.height,
                columnWidth: column.getBoundingClientRect().width,
                columnHeight: column.getBoundingClientRect().height,
                titleMiddle: title.top + title.height / 2,
                inputMiddle: field ? field.top + field.height / 2 : null,
                focused: document.activeElement === input,
            });
        }))
    JS);

    expect($after['button'])->toBeFalse()
        ->and(abs($after['left'] - $before['left']))->toBeLessThanOrEqual(1)
        ->and($after['width'])->toBe($after['columnWidth'])
        ->and($after['height'])->toBe($after['columnHeight'])
        ->and(abs($after['inputMiddle'] - $after['titleMiddle']))->toBeLessThanOrEqual(1)
        ->and($after['focused'])->toBeTrue();

    $page->keys('@idea-stage-new-input', 'Escape');
    $restored = $page->script(<<<'JS'
        new Promise((resolve) => requestAnimationFrame(() => {
            const button = document.querySelector('[data-testid="idea-stage-new"]');
            const rect = button?.getBoundingClientRect();
            resolve({
                input: !!document.querySelector('[data-testid="idea-stage-new-input"]'),
                left: rect?.left ?? null,
                top: rect?.top ?? null,
                focused: document.activeElement === button,
            });
        }))
    JS);

    expect($restored['input'])->toBeFalse()
        ->and(abs($restored['left'] - $before['left']))->toBeLessThanOrEqual(1)
        ->and(abs($restored['top'] - $before['top']))->toBeLessThanOrEqual(1)
        ->and($restored['focused'])->toBeTrue();
    $page->assertNoJavaScriptErrors();
});

test('on a phone the label filter shows only its icon and the page does not scroll sideways', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    createIdeasBoardIdea($workspace, $user, null, 0, 'First');
    foreach ($stages as $stage) {
        foreach (range(1, 3) as $position) {
            createIdeasBoardIdea($workspace, $user, $stage, $position, "Idea {$position}");
        }
    }
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'))->resize(390, 844);
    waitFor($page, '@ideas-filter-labels-filter');
    waitFor($page, '@ideas-board');

    expect($page->script(<<<'JS'
        (() => {
            const trigger = document.querySelector('[data-testid="ideas-filter-labels-filter"]');
            const visibleText = [...trigger.querySelectorAll('span')].filter((span) => span.offsetParent !== null && span.textContent.trim() !== '').length;

            return {
                visibleText,
                label: trigger.getAttribute('aria-label') !== null,
                overflow: document.documentElement.scrollWidth - window.innerWidth,
                boardScrolls: document.querySelector('[data-testid="ideas-board"]').scrollWidth > document.querySelector('[data-testid="ideas-board"]').clientWidth,
            };
        })()
    JS))->toBe(['visibleText' => 0, 'label' => true, 'overflow' => 0, 'boardScrolls' => true]);

    $page->resize(1280, 900);
    waitForState($page, '@ideas-filter-labels-filter', "[...element.querySelectorAll('span')].some((span) => span.offsetParent !== null && span.textContent.trim() !== '')");
    expect($page->script("[...document.querySelector('[data-testid=\"ideas-filter-labels-filter\"]').querySelectorAll('span')].some((span) => span.offsetParent !== null && span.textContent.trim() !== '')"))->toBeTrue();
    $page->assertNoJavaScriptErrors();
});

test('scrolling a column loads its next ten ideas and the count comes from the database', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $ideas = collect(range(0, 14))->map(fn (int $position): Idea => createIdeasBoardIdea($workspace, $user, $stages['todo'], $position, "Idea {$position}"));
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    $column = $stages['todo']->id;
    waitFor($page, "@idea-card-{$ideas[9]->id}");

    expect(createIdeasBoardCardSlots($page, $column))->toBe($ideas->take(10)->pluck('id')->all());
    $page->assertSeeIn("@idea-column-count-{$column}", '15');

    $page->script("document.querySelector('[data-testid=\"idea-column-scroll-{$column}\"]').scrollTop = 100000");
    waitForCount($page, "@idea-column-list-{$column} [data-sortable-idea]", 15);

    expect(createIdeasBoardCardSlots($page, $column))->toBe($ideas->pluck('id')->all());
    $page->assertSeeIn("@idea-column-count-{$column}", '15')
        ->assertNoJavaScriptErrors();
});

test('dragging a card into a partly loaded column keeps the unloaded ideas in order', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $done = collect(range(0, 11))->map(fn (int $position): Idea => createIdeasBoardIdea($workspace, $user, $stages['done'], $position, "Done {$position}"));
    $moving = createIdeasBoardIdea($workspace, $user, $stages['todo'], 0, 'Moving');
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-{$done[1]->id}");
    $page->drag("@idea-card-{$moving->id}", "@idea-card-{$done[1]->id}");
    waitForDatabase($page, fn (): bool => $moving->refresh()->idea_stage_id === $stages['done']->id);

    $order = Idea::where('idea_stage_id', $stages['done']->id)->orderBy('position')->get(['id', 'position']);
    $shown = createIdeasBoardCardSlots($page, $stages['done']->id);

    expect($order->pluck('position')->all())->toBe(range(0, 12))
        ->and($order->pluck('id')->reject(fn (string $id): bool => $id === $moving->id)->values()->all())->toBe($done->pluck('id')->all())
        ->and(array_slice($order->pluck('id')->all(), 0, count($shown)))->toBe($shown)
        ->and($shown)->toContain($moving->id);
    waitForState($page, "@idea-column-count-{$stages['done']->id}", "element.textContent.trim() === '13'");
    $page->assertSeeIn("@idea-column-count-{$stages['done']->id}", '13')
        ->assertNoJavaScriptErrors();
});

test('the gallery stage filter search ignores accents and matches each word in any order', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $stages['in_progress']->update(['name' => 'Revisão final']);
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index', ['view' => 'gallery']));
    waitFor($page, '@ideas-filter-stages-filter');
    $page->click('@ideas-filter-stages-filter');
    waitFor($page, '@ideas-filter-stages-search');
    $page->type('@ideas-filter-stages-search', 'FINAL revisao');
    waitForGone($page, "@ideas-filter-stages-option-{$stages['todo']->id}");

    $page->assertPresent("@ideas-filter-stages-option-{$stages['in_progress']->id}")
        ->assertMissing("@ideas-filter-stages-option-{$stages['todo']->id}")
        ->assertMissing("@ideas-filter-stages-option-{$stages['done']->id}")
        ->assertMissing('@ideas-filter-stages-unassigned')
        ->assertNoJavaScriptErrors();
});

test('on a phone the label filter opens as a sheet', function () {
    [$user, $workspace] = createIdeasBoardSetup();
    WorkspaceLabel::factory()->create(['workspace_id' => $workspace->id]);
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'))->resize(390, 844);
    waitFor($page, '@ideas-filter-labels-filter');
    $page->click('@ideas-filter-labels-filter');
    waitFor($page, '@ideas-filter-labels-sheet');

    $page->assertVisible('@ideas-filter-labels-sheet')
        ->assertNoJavaScriptErrors();
});

test('an idea card shows its labels as chips in their own color', function () {
    [$user, $workspace, $stages] = createIdeasBoardSetup();
    $label = WorkspaceLabel::factory()->create(['workspace_id' => $workspace->id, 'name' => 'Launch', 'color' => '#7c3aed']);
    $idea = createIdeasBoardIdea($workspace, $user, $stages['todo'], 1, 'Spring sale');
    $idea->labels()->attach($label);
    $this->actingAs($user);

    $page = visit(route('app.create.ideas.index'));
    waitFor($page, "@idea-card-label-{$idea->id}-{$label->id}");

    $page->assertSeeIn("@idea-card-label-{$idea->id}-{$label->id}", 'Launch')
        ->assertScript("getComputedStyle(document.querySelector('[data-testid=\"idea-card-label-{$idea->id}-{$label->id}\"]')).backgroundColor", 'rgb(124, 58, 237)')
        ->assertNoJavaScriptErrors();
});