trypost/tests/Feature/Automation/Node/PublishNodeTest.php
Paulo Castellano 31b6544c57 Cover node-run error paths and align webhook failure handling
Wrap the webhook HTTP send in a try/catch so a connection error returns
a clean failed result (reason: request_failed) instead of bubbling up as
a job failure — matching the HTTP request node.

Add tests for the gaps in node-run coverage:
- HTTP request: basic auth, PUT/PATCH/DELETE, non-2xx responses,
  connection exceptions, and an items_path that doesn't resolve to a list.
- Webhook: every HTTP method, header expression resolution, and the new
  connection-failure path.
- Fetch RSS: non-2xx feed responses, malformed XML, items without a
  publish date (skipped), and the link fallback when an item has no guid.
- Delay: unknown unit throws.
- Publish: dry runs don't publish or queue.
2026-06-13 09:56:18 -03:00

65 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Actions\Automation\Node\RunPublishNode;
use App\Enums\Post\Status as PostStatus;
use App\Jobs\PublishPost;
use App\Models\AutomationRun;
use App\Models\Post;
use Illuminate\Support\Facades\Queue;
it('leaves post in draft when mode is draft', function () {
$post = Post::factory()->create(['status' => PostStatus::Draft]);
$run = AutomationRun::factory()->create(['generated_post_id' => $post->id]);
$result = app(RunPublishNode::class)($run, ['mode' => 'draft']);
expect($result->status->value)->toBe('completed');
expect($post->fresh()->status)->toBe(PostStatus::Draft);
});
it('schedules the post when mode is scheduled', function () {
$post = Post::factory()->create(['status' => PostStatus::Draft]);
$run = AutomationRun::factory()->create(['generated_post_id' => $post->id]);
app(RunPublishNode::class)($run, ['mode' => 'scheduled', 'scheduled_offset' => 60]);
$fresh = $post->fresh();
expect($fresh->status)->toBe(PostStatus::Scheduled);
expect($fresh->scheduled_at)->not->toBeNull();
});
it('fails when no generated post exists', function () {
$run = AutomationRun::factory()->create(['generated_post_id' => null]);
$result = app(RunPublishNode::class)($run, ['mode' => 'now']);
expect($result->status->value)->toBe('failed');
});
it('dispatches PublishPost when mode is now', function () {
Queue::fake();
$post = Post::factory()->create(['status' => PostStatus::Draft]);
$run = AutomationRun::factory()->create(['generated_post_id' => $post->id]);
app(RunPublishNode::class)($run, ['mode' => 'now']);
expect($post->fresh()->status)->toBe(PostStatus::Publishing);
Queue::assertPushed(PublishPost::class);
});
it('does not publish or change the post on a dry run', function () {
Queue::fake();
$post = Post::factory()->create(['status' => PostStatus::Draft]);
$run = AutomationRun::factory()->create(['generated_post_id' => $post->id, 'is_dry_run' => true]);
$result = app(RunPublishNode::class)($run, ['mode' => 'now']);
expect($result->status->value)->toBe('completed');
expect($result->output['publish']['dry_run'])->toBeTrue();
expect($post->fresh()->status)->toBe(PostStatus::Draft);
Queue::assertNotPushed(PublishPost::class);
});