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:
@@ -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');
|
||||
});
|
||||
Reference in New Issue
Block a user