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.

5.4 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Dto\MediaItem;
use App\Enums\Post\Status;
use App\Enums\PostPlatform\ContentType;
use App\Models\Media;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Storage;

afterEach(function () {
    Storage::delete(Media::query()->pluck('path')->all());
});

function seedYouTubeDescriptionEditor(): array
{
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create(['user_id' => $user->id]);
    $workspace->members()->attach($user->id, membershipPivot('member'));
    $user->update(['current_workspace_id' => $workspace->id]);
    $asset = Media::factory()->video()->temporaryUpload($workspace)->create([
        'size' => filesize(base_path('tests/fixtures/sample.mp4')),
    ]);
    Storage::put($asset->path, (string) file_get_contents(base_path('tests/fixtures/sample.mp4')));

    $posts = collect(range(1, 2))->map(function (int $number) use ($workspace, $user, $asset) {
        $account = SocialAccount::factory()->youtube()->create([
            'workspace_id' => $workspace->id,
            'username' => "ytchannel{$number}",
        ]);

        return Post::factory()->forAccount($account, ContentType::YouTubeShort)->create([
            'user_id' => $user->id,
            'content' => 'Short title',
            'status' => Status::Draft,
            'media' => [MediaItem::fromMedia($asset)->toArray()],
            'meta' => ['description' => "Channel {$number}"],
        ]);
    });
    test()->actingAs($user);

    return [$posts[0], $posts];
}

test('youtube description editor counts UTF-8 bytes and saves only its independent post', function (int $width, int $height) {
    [$post, $posts] = seedYouTubeDescriptionEditor();
    $page = visit(route('app.posts.edit', $post))->resize($width, $height);
    waitFor($page, '@youtube-description-0');
    $page->assertValue('@youtube-description-0', 'Channel 1');

    $page->fill('@youtube-description-0', str_repeat('é', 2500))
        ->assertAttributeMissing('@youtube-description-0', 'aria-invalid');
    $page->fill('@youtube-description-0', str_repeat('é', 2501))
        ->assertAttribute('@youtube-description-0', 'aria-invalid', 'true');

    $description = "First channel description 😀 ação\nhttps://example.com\nif (a < b && c > d) {}";
    $page->fill('@youtube-description-0', $description)
        ->assertAttributeMissing('@youtube-description-0', 'aria-invalid');

    if ($width < 1024) {
        $page->click('@composer-view-preview');
    }
    $page->click('details > summary')
        ->assertSeeIn('@youtube-preview-description', 'if (a < b && c > d) {}')
        ->assertNoJavaScriptErrors();
    if ($width < 1024) {
        $page->click('@composer-view-edit');
    }

    $page->click('@composer-save-draft')->assertMissing('@post-composer-dialog');
    expect(data_get($posts[0]->fresh()->meta, 'description'))->toBe($description)
        ->and(data_get($posts[1]->fresh()->meta, 'description'))->toBe('Channel 2')
        ->and($post->fresh()->content)->toBe('Short title');
})->with([[375, 812]]);

test('youtube description clearing restores content fallback without changing another post', function (string $description) {
    [$post, $posts] = seedYouTubeDescriptionEditor();
    $page = visit(route('app.posts.edit', $post))->resize(375, 812);
    waitFor($page, '@youtube-description-0');
    $page->fill('@youtube-description-0', $description)
        ->click('@composer-save-draft')
        ->assertMissing('@post-composer-dialog');

    expect(data_get($posts[0]->fresh()->meta, 'description'))->toBeNull()
        ->and(data_get($posts[1]->fresh()->meta, 'description'))->toBe('Channel 2');

    $page = visit(route('app.posts.edit', $post))->resize(375, 812);
    $page->click('@composer-view-preview')
        ->click('details > summary')
        ->assertSeeIn('@youtube-preview-description', 'Short title')
        ->assertNoJavaScriptErrors();
})->with([
    'empty description' => [''],
]);

test('youtube description long preview stays inside the preview card', function (int $width, int $height) {
    [$post, $posts] = seedYouTubeDescriptionEditor();
    $description = "Full description\n".str_repeat('https://example.com/'.str_repeat('a', 100)."\n", 35);
    $posts[0]->update(['meta' => ['description' => $description]]);
    $page = visit(route('app.posts.edit', $post))->resize($width, $height);
    if ($width < 1024) {
        $page->click('@composer-view-preview');
    }
    $page->click('details > summary')->assertSeeIn('@youtube-preview-description', 'Full description');

    $layout = $page->script(<<<'JS'
        (() => {
            const description = document.querySelector('[data-testid=youtube-preview-description]');
            const card = description.closest('[data-testid=youtube-preview]').getBoundingClientRect();
            const rect = description.getBoundingClientRect();
            return {
                scrolls: description.scrollHeight > description.clientHeight,
                bounded: description.clientHeight <= 128,
                insideCard: rect.top >= card.top && rect.bottom <= card.bottom,
                noOverflow: document.documentElement.scrollWidth <= window.innerWidth,
            };
        })();
    JS);
    expect($layout)->toEqual(['scrolls' => true, 'bounded' => true, 'insideCard' => true, 'noOverflow' => true]);
    $page->assertNoJavaScriptErrors();
})->with([[375, 812]]);