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.

7.4 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

/**
 * @return array{0: User, 1: array<string, SocialAccount>}
 */
function composerPreviewWorkspace(): array
{
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'user_id' => $user->id,
        'account_id' => $user->account_id,
    ]);
    $workspace->members()->attach($user->id, membershipPivot('admin'));
    $user->update(['current_workspace_id' => $workspace->id]);
    subscribeAccount($user->account);

    $accounts = collect(['x', 'tiktok', 'instagram', 'youtube'])
        ->mapWithKeys(fn (string $platform): array => [
            $platform => SocialAccount::factory()->{$platform}()->create([
                'workspace_id' => $workspace->id,
                'username' => 'paulocastellano',
            ]),
        ])
        ->all();

    return [$user, $accounts];
}

test('the preview panel lists one labelled card per network and renders each network chrome', function () {
    Http::fake();
    [$user] = composerPreviewWorkspace();
    $this->actingAs($user);

    $page = visit(route('app.posts.create'));
    waitForOpen($page, '@post-composer-dialog');
    waitFor($page, '@composer-add-account');

    $page->click('@composer-add-account')
        ->click('@composer-select-all')
        ->fill('@composer-base-content', 'Testing the preview https://trypost.it #hashtags');
    waitForOpen($page, '@post-composer-dialog');
    waitFor($page, '@youtube-preview-subscribe');

    $page->assertSeeIn('@composer-preview-title', 'Post previews')
        ->assertPresent('@composer-preview-info');

    expect($page->script('[...document.querySelectorAll("[data-testid=composer-preview-label]")].map((label) => label.textContent.trim())'))
        ->toEqualCanonicalizing(['X', 'TikTok', 'Instagram', 'YouTube'])
        ->and($page->script('document.querySelectorAll("[data-testid=composer-preview-frame]").length'))->toBe(4);

    expect($page->script(<<<'JS'
        (() => {
            const frames = [...document.querySelectorAll('[data-testid="composer-preview-frame"]')];
            const column = document.querySelector('[data-testid="composer-previews-scroll"]').getBoundingClientRect();
            const style = getComputedStyle(frames[0]);

            return {
                fillColumn: frames.every((frame) => Math.abs(frame.getBoundingClientRect().width - (column.width - 64)) < 2),
                radius: style.borderTopLeftRadius,
                shadow: style.boxShadow,
            };
        })()
    JS))->toEqual(['fillColumn' => true, 'radius' => '8px', 'shadow' => 'none']);

    expect($page->script('document.querySelectorAll("[data-testid=x-preview] [data-testid=preview-actions] svg").length'))->toBe(6)
        ->and($page->script('[...document.querySelectorAll("[data-testid=x-preview-content] .underline")].map((token) => token.textContent)'))
        ->toBe(['https://trypost.it', '#hashtags']);

    $page->assertSeeIn('@tiktok-preview-sound', 'original sound - paulocastellano')
        ->assertSeeIn('@youtube-preview-subscribe', 'Subscribe')
        ->assertSeeIn('@instagram-preview-username', 'paulocastellano');

    expect($page->script('document.querySelectorAll("[data-testid=youtube-preview] [data-testid=preview-rail-action]").length'))->toBe(4);

    $page->click('@composer-next');
    waitForOpen($page, '@post-composer-dialog');
    waitFor($page, '@composer-customization');

    expect($page->script('document.querySelector("[data-testid=composer-preview-title]").textContent.trim()'))
        ->toMatch('/^(X|TikTok|Instagram|YouTube) preview$/')
        ->and($page->script('document.querySelectorAll("[data-testid=composer-preview-frame]").length'))->toBe(1)
        ->and($page->script('document.querySelectorAll("[data-testid=composer-preview-label]").length'))->toBe(0);

    $page->assertNoJavaScriptErrors();
});

test('a pdf preview crops the browser viewer frame around the page', function () {
    Http::fake();
    [$user] = composerPreviewWorkspace();
    $linkedin = SocialAccount::factory()->linkedin()->create(['workspace_id' => $user->current_workspace_id]);
    $post = Post::factory()->forAccount($linkedin)->create([
        'user_id' => $user->id,
        'content' => 'A deck',
        'media' => [['id' => (string) Str::uuid(), 'type' => 'document', 'path' => 'medias/deck.pdf', 'url' => 'https://example.com/deck.pdf', 'mime_type' => 'application/pdf', 'original_filename' => 'deck.pdf']],
    ]);
    $this->actingAs($user);

    $page = visit(route('app.posts.edit', $post))->resize(1440, 900);
    waitForOpen($page, '@post-composer-dialog');
    waitFor($page, '@linkedin-preview');

    $crop = $page->script(<<<'JS'
        (() => {
            const frame = document.querySelector('[data-testid="linkedin-preview"] [data-testid="preview-media"] > div');
            const viewer = frame.querySelector('iframe');
            const outer = frame.getBoundingClientRect();
            const inner = viewer.getBoundingClientRect();

            return {
                clipped: getComputedStyle(frame).overflow === 'hidden',
                left: Math.round(outer.left - inner.left),
                top: Math.round(outer.top - inner.top),
                right: Math.round(inner.right - outer.right),
                bottom: Math.round(inner.bottom - outer.bottom),
            };
        })()
    JS);

    expect($crop)->toBe(['clipped' => true, 'left' => 8, 'top' => 8, 'right' => 8, 'bottom' => 8]);
    $page->assertNoJavaScriptErrors();
});

test('the x preview lays two images side by side and three as one tall image beside two', function (int $count) {
    Http::fake();
    [$user, $accounts] = composerPreviewWorkspace();
    $media = collect(range(1, $count))->map(fn (int $index): array => [
        'id' => (string) Str::uuid(),
        'type' => 'image',
        'path' => "medias/photo-{$index}.png",
        'url' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
        'mime_type' => 'image/png',
        'original_filename' => "photo-{$index}.png",
        'meta' => ['width' => 1200, 'height' => 800],
    ])->all();
    $post = Post::factory()->forAccount($accounts['x'])->create(['user_id' => $user->id, 'content' => 'Photos', 'media' => $media]);
    $this->actingAs($user);

    $page = visit(route('app.posts.edit', $post))->resize(1440, 900);
    waitForOpen($page, '@post-composer-dialog');
    waitFor($page, '@x-preview');

    $tiles = $page->script(<<<'JS'
        [...document.querySelectorAll('[data-testid="x-preview"] [data-testid="preview-media"] img')].map((image) => {
            const box = image.getBoundingClientRect();
            return [Math.round(box.left), Math.round(box.top), Math.round(box.height)];
        })
    JS);

    expect($page->script('document.querySelector(\'[data-testid="x-preview"] [data-testid="preview-media"]\').dataset.layout'))->toBe('grid')
        ->and($tiles)->toHaveCount($count)
        ->and($tiles[1][0])->toBeGreaterThan($tiles[0][0])
        ->and($tiles[1][1])->toBe($tiles[0][1]);

    if ($count === 3) {
        expect($tiles[2][0])->toBe($tiles[1][0])
            ->and($tiles[2][1])->toBeGreaterThan($tiles[1][1])
            ->and($tiles[0][2])->toBeGreaterThan($tiles[1][2]);
    }

    $page->assertNoJavaScriptErrors();
})->with([2, 3]);