Add a Publisher abstraction and queued job to publish from the UI

Introduce Publisher/PublisherManager (WordPress + Reddit implementations)
so publishing logic is shared between the CLI, the queue, and future
network integrations. Add a "Publish" button on the Posts page that
dispatches PublishPostJob instead of blocking the request — Reddit's
publisher shells out to the existing Playwright sidecar and can take
tens of seconds, so it can't run synchronously in a web request.

Adds a `target` field (e.g. subreddit) and `publishing` status to Post,
plus a `publish_error` field so failures surface in the UI. Refactors
posts:publish to delegate to WordPressPublisher without changing its
CLI output contract (existing tests pass unchanged).

Also fixes two bugs found along the way: UpdatePostRequest was silently
dropping `published_url` from PATCH requests (never in its validation
rules, despite AI_API.md documenting it), and three Livewire index-page
tests were broken by the earlier auth change (never re-run after adding
the `auth` middleware) since they hit routes via HTTP without a logged-in
user.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
m105
2026-07-21 14:04:45 +03:00
co-authored by Claude Sonnet 5
parent 0cf72b44b7
commit 9278d2e8a0
26 changed files with 735 additions and 76 deletions
+52
View File
@@ -0,0 +1,52 @@
<?php
use App\Livewire\Auth\Login;
use App\Models\User;
use Livewire\Livewire;
test('guests are redirected to login when visiting a protected page', function () {
$this->get(route('sites.index'))
->assertRedirect(route('login'));
});
test('the api stays open to guests', function () {
$this->getJson('/api/sites')->assertOk();
});
test('users can log in with correct credentials', function () {
$user = User::factory()->create(['password' => bcrypt('correct-password')]);
Livewire::test(Login::class)
->set('email', $user->email)
->set('password', 'correct-password')
->call('login')
->assertRedirect(route('sites.index'));
$this->assertAuthenticatedAs($user);
});
test('users cannot log in with incorrect credentials', function () {
$user = User::factory()->create(['password' => bcrypt('correct-password')]);
Livewire::test(Login::class)
->set('email', $user->email)
->set('password', 'wrong-password')
->call('login')
->assertHasErrors('email');
$this->assertGuest();
});
test('authenticated users are redirected away from the login page', function () {
$this->actingAs(User::factory()->create())
->get(route('login'))
->assertRedirect();
});
test('users can log out', function () {
$this->actingAs(User::factory()->create())
->post(route('logout'))
->assertRedirect('/login');
$this->assertGuest();
});
+82
View File
@@ -0,0 +1,82 @@
<?php
use App\Jobs\PublishPostJob;
use App\Models\Post;
use App\Models\Site;
use App\Models\SocialAccount;
use App\Support\Publishing\PublisherManager;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Process;
test('it publishes and updates the post on success', function () {
Http::fake([
'*/wp-json/wp/v2/posts' => Http::response(['link' => 'https://wp.example.com/post'], 201),
]);
$site = Site::factory()->wordpress()->create();
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'publishing']);
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
expect($post->fresh())
->status->toBe('published')
->published_url->toBe('https://wp.example.com/post')
->publish_error->toBeNull();
});
test('it marks the post failed when the publish attempt fails', function () {
Http::fake([
'*/wp-json/wp/v2/posts' => Http::response(['message' => 'nope'], 401),
]);
$site = Site::factory()->wordpress()->create();
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'publishing']);
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
expect($post->fresh())
->status->toBe('failed')
->publish_error->toBe('nope');
});
test('it reverts a rejected post to draft instead of failed', function () {
$site = Site::factory()->wordpress()->create();
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => null, 'status' => 'publishing']);
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
expect($post->fresh())
->status->toBe('draft')
->publish_error->toContain('no title');
});
test('it reverts to draft when no publisher supports the post type', function () {
$post = Post::factory()->create(['type' => 'twitter', 'status' => 'publishing']);
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
expect($post->fresh())
->status->toBe('draft')
->publish_error->toContain('No publisher available');
});
test('it publishes a reddit post via the sidecar', function () {
Process::fake([
'*' => Process::result(output: json_encode(['success' => true, 'url' => 'https://reddit.com/r/test/comments/abc'])),
]);
$account = SocialAccount::factory()->create(['network' => 'reddit']);
$post = Post::factory()->create([
'type' => 'reddit',
'title' => 'Hi',
'target' => 'test',
'social_account_id' => $account->id,
'status' => 'publishing',
]);
(new PublishPostJob($post->id))->handle(app(PublisherManager::class));
expect($post->fresh())
->status->toBe('published')
->published_url->toBe('https://reddit.com/r/test/comments/abc');
});
+30 -1
View File
@@ -1,16 +1,20 @@
<?php
use App\Jobs\PublishPostJob;
use App\Livewire\Posts\Index;
use App\Models\Post;
use App\Models\Site;
use App\Models\SocialAccount;
use App\Models\User;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
test('posts index page lists existing posts', function () {
$site = Site::factory()->create(['name' => 'My Blog']);
Post::factory()->create(['site_id' => $site->id, 'title' => 'Hello World Post']);
$this->get(route('posts.index'))
$this->actingAs(User::factory()->create())
->get(route('posts.index'))
->assertOk()
->assertSee('Hello World Post')
->assertSee('My Blog');
@@ -79,3 +83,28 @@ test('a post can be deleted', function () {
expect(Post::find($post->id))->toBeNull();
});
test('publishing a post queues the job and marks it publishing', function () {
Queue::fake();
$site = Site::factory()->wordpress()->create();
$post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'draft']);
Livewire::test(Index::class)->call('publish', $post->id);
expect($post->fresh()->status)->toBe('publishing');
Queue::assertPushed(PublishPostJob::class, fn ($job) => $job->postId === $post->id);
});
test('publishing a post with no available publisher shows a notice and does not queue anything', function () {
Queue::fake();
$post = Post::factory()->create(['type' => 'twitter', 'status' => 'draft']);
Livewire::test(Index::class)
->call('publish', $post->id)
->assertSee("No publisher available yet for post type 'twitter'");
expect($post->fresh()->status)->toBe('draft');
Queue::assertNotPushed(PublishPostJob::class);
});
+3 -1
View File
@@ -2,12 +2,14 @@
use App\Livewire\Sites\Index;
use App\Models\Site;
use App\Models\User;
use Livewire\Livewire;
test('sites index page lists existing sites', function () {
$site = Site::factory()->create(['name' => 'Example Site']);
$this->get(route('sites.index'))
$this->actingAs(User::factory()->create())
->get(route('sites.index'))
->assertOk()
->assertSee('Example Site');
});
@@ -2,12 +2,14 @@
use App\Livewire\SocialAccounts\Index;
use App\Models\SocialAccount;
use App\Models\User;
use Livewire\Livewire;
test('social accounts index page lists existing accounts', function () {
SocialAccount::factory()->create(['username' => 'example_user']);
$this->get(route('social-accounts.index'))
$this->actingAs(User::factory()->create())
->get(route('social-accounts.index'))
->assertOk()
->assertSee('example_user');
});
@@ -0,0 +1,25 @@
<?php
use App\Models\Post;
use App\Support\Publishing\PublisherManager;
use App\Support\Publishing\RedditPublisher;
use App\Support\Publishing\WordPressPublisher;
test('it resolves the first publisher that supports the post', function () {
$manager = new PublisherManager([
new WordPressPublisher,
new RedditPublisher,
]);
expect($manager->resolve(Post::factory()->make(['type' => 'blog'])))->toBeInstanceOf(WordPressPublisher::class);
expect($manager->resolve(Post::factory()->make(['type' => 'reddit'])))->toBeInstanceOf(RedditPublisher::class);
});
test('it returns null when no publisher supports the post', function () {
$manager = new PublisherManager([
new WordPressPublisher,
new RedditPublisher,
]);
expect($manager->resolve(Post::factory()->make(['type' => 'twitter'])))->toBeNull();
});
@@ -0,0 +1,110 @@
<?php
use App\Models\Post;
use App\Models\SocialAccount;
use App\Support\Publishing\RedditPublisher;
use Illuminate\Support\Facades\Process;
test('it supports reddit posts only', function () {
$publisher = new RedditPublisher;
expect($publisher->supports(Post::factory()->make(['type' => 'reddit'])))->toBeTrue();
expect($publisher->supports(Post::factory()->make(['type' => 'blog'])))->toBeFalse();
});
test('it rejects a post with no title', function () {
$post = Post::factory()->for(SocialAccount::factory()->state(['network' => 'reddit']), 'socialAccount')
->create(['type' => 'reddit', 'title' => null, 'target' => 'test']);
$result = (new RedditPublisher)->publish($post);
expect($result->success)->toBeFalse();
expect($result->attempted)->toBeFalse();
expect($result->error)->toContain('no title');
});
test('it rejects a post with no target subreddit', function () {
$post = Post::factory()->for(SocialAccount::factory()->state(['network' => 'reddit']), 'socialAccount')
->create(['type' => 'reddit', 'title' => 'Hi', 'target' => null]);
$result = (new RedditPublisher)->publish($post);
expect($result->success)->toBeFalse();
expect($result->attempted)->toBeFalse();
expect($result->error)->toContain('no target subreddit');
});
test('it rejects a post with no linked reddit account', function () {
$post = Post::factory()->create(['type' => 'reddit', 'title' => 'Hi', 'target' => 'test', 'social_account_id' => null]);
$result = (new RedditPublisher)->publish($post);
expect($result->success)->toBeFalse();
expect($result->attempted)->toBeFalse();
expect($result->error)->toContain('no linked Reddit social account');
});
test('it publishes successfully via the sidecar', function () {
Process::fake([
'*' => Process::result(output: json_encode(['success' => true, 'url' => 'https://reddit.com/r/test/comments/abc123'])),
]);
$account = SocialAccount::factory()->create(['network' => 'reddit', 'username' => 'redditor']);
$post = Post::factory()->create([
'type' => 'reddit',
'title' => 'Hi',
'target' => 'test',
'social_account_id' => $account->id,
]);
$result = (new RedditPublisher)->publish($post);
expect($result->success)->toBeTrue();
expect($result->url)->toBe('https://reddit.com/r/test/comments/abc123');
Process::assertRan(function ($process) use ($account) {
return str_contains($process->command[0], 'sidecars/reddit-post/run.sh')
&& in_array("--username={$account->username}", $process->command)
&& in_array('--subreddit=test', $process->command);
});
});
test('it treats a sidecar-reported failure as an attempted, failed publish', function () {
Process::fake([
'*' => Process::result(output: json_encode(['success' => false, 'error' => 'invalid credentials'])),
]);
$account = SocialAccount::factory()->create(['network' => 'reddit']);
$post = Post::factory()->create([
'type' => 'reddit',
'title' => 'Hi',
'target' => 'test',
'social_account_id' => $account->id,
]);
$result = (new RedditPublisher)->publish($post);
expect($result->success)->toBeFalse();
expect($result->attempted)->toBeTrue();
expect($result->error)->toBe('invalid credentials');
});
test('it fails gracefully when the sidecar output is unparseable', function () {
Process::fake([
'*' => Process::result(output: 'not json'),
]);
$account = SocialAccount::factory()->create(['network' => 'reddit']);
$post = Post::factory()->create([
'type' => 'reddit',
'title' => 'Hi',
'target' => 'test',
'social_account_id' => $account->id,
]);
$result = (new RedditPublisher)->publish($post);
expect($result->success)->toBeFalse();
expect($result->attempted)->toBeTrue();
expect($result->error)->toContain('unparseable');
});