trypost/tests/Feature/Automation/Node/HttpRequestNodeTest.php
Paulo Castellano 4efaa0bf99 Harden automations module: full-post generation, reliable runs, editor UX
Generation
- Generate node now produces the full post (text + AI image + carousel)
  via a shared PostImagePipeline extracted from StreamPostCreation
- Generate config UI mirrors the /posts/create wizard (carousel slide
  count, include-image toggle); drop the decorative format/unsplash keys

Flow correctness
- RSS/HTTP nodes expose named has-items (default) and no-items output
  handles, labeled and colored like the Condition node
- AdvanceAutomationRun records a no_matching_edge terminal instead of
  completing silently; "0 new items" feedback in the test panel
- Manual/test runs no longer persist the production dedup watermark

Run reliability
- Pause truly halts in-flight runs (production only; manual test runs
  always run regardless of automation status)
- ProcessAutomationNode::failed() marks the run failed
- automation:recover-stuck-runs and automation:prune-dry-runs commands

Webhook / HTTP
- Branded User-Agent (config-driven) on outbound webhook + http_request
- Webhook fails on invalid JSON instead of silently sending {}
- HTTP custom headers editor; CodeMirror-based CodeEditor for JSON

Editor UX
- Header Test button only opens the panel; the panel has a Run button
  (saves first) and owns the with-real-data toggle
- Clicking a node closes the test panel and opens its config
- Node cards: max-width + truncate so long URLs don't grow the node
2026-06-10 20:45:01 -03:00

176 lines
6.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Actions\Automation\Node\RunHttpRequestNode;
use App\Enums\Automation\NodeRun\Status as NodeRunStatus;
use App\Jobs\Automation\ProcessAutomationNode;
use App\Models\Automation;
use App\Models\AutomationNodeState;
use App\Models\AutomationRun;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
afterEach(fn () => Carbon::setTestNow());
beforeEach(fn () => Bus::fake());
it('runs in single-response mode when items_path is empty', function () {
Http::fake([
'api.example.com/*' => Http::response(['title' => 'Hello', 'id' => 42], 200),
]);
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
$result = app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/lookup',
'method' => 'GET',
'auth_type' => 'none',
]);
expect($result->status)->toBe(NodeRunStatus::Completed);
expect($result->output['fetched'])->toBe(['title' => 'Hello', 'id' => 42]);
expect($result->output['fetch']['spawned'])->toBe(0);
});
it('processes first new item and spawns siblings when items_path is set', function () {
Carbon::setTestNow('2026-01-15 10:00:00');
Http::fake([
'api.example.com/*' => Http::response([
'items' => [
['id' => 'a1', 'published_at' => '2025-12-31T00:00:00Z'],
['id' => 'a2', 'published_at' => '2026-01-05T00:00:00Z'],
['id' => 'a3', 'published_at' => '2026-01-10T00:00:00Z'],
],
], 200),
]);
$automation = Automation::factory()->active()->create([
'nodes' => [
['id' => 'trigger_1', 'type' => 'trigger', 'position' => ['x' => 0, 'y' => 0], 'data' => ['trigger_type' => 'schedule']],
['id' => 'http_1', 'type' => 'http_request', 'position' => ['x' => 200, 'y' => 0], 'data' => []],
['id' => 'end_1', 'type' => 'end', 'position' => ['x' => 400, 'y' => 0], 'data' => []],
],
'connections' => [
['id' => 'e1', 'source' => 'trigger_1', 'target' => 'http_1'],
['id' => 'e2', 'source' => 'http_1', 'target' => 'end_1'],
],
]);
AutomationNodeState::create([
'automation_id' => $automation->id,
'node_id' => 'http_1',
'data' => ['last_item_date' => '2025-12-30T00:00:00+00:00'],
]);
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
$result = app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/items',
'method' => 'GET',
'auth_type' => 'none',
'items_path' => 'items',
'item_key_path' => 'id',
'item_date_path' => 'published_at',
]);
expect($result->output['fetched']['id'])->toBe('a1');
expect($result->output['fetch']['count'])->toBe(3);
expect($result->output['fetch']['spawned'])->toBe(2);
Bus::assertDispatchedTimes(ProcessAutomationNode::class, 2);
});
it('sends bearer token header decrypting it on the fly', function () {
Http::fake(['api.example.com/*' => Http::response(['ok' => true], 200)]);
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/me',
'method' => 'GET',
'auth_type' => 'bearer',
'auth_token' => Crypt::encryptString('secret-token-123'),
]);
Http::assertSent(fn ($request) => $request->hasHeader('Authorization', 'Bearer secret-token-123'));
});
it('sends api_key header with custom header name', function () {
Http::fake(['api.example.com/*' => Http::response(['ok' => true], 200)]);
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/me',
'method' => 'GET',
'auth_type' => 'api_key',
'auth_header_name' => 'X-Custom-Key',
'auth_token' => 'plain-text-key',
]);
Http::assertSent(fn ($request) => $request->hasHeader('X-Custom-Key', 'plain-text-key'));
});
it('posts a body rendered from the template with run context', function () {
Http::fake(['api.example.com/*' => Http::response(['ok' => true], 200)]);
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create([
'current_node_id' => 'http_1',
'context' => ['trigger' => ['post' => ['id' => 'post-42']]],
]);
app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/notify',
'method' => 'POST',
'auth_type' => 'none',
'body_template' => '{"post_id":"{{ trigger.post.id }}"}',
]);
Http::assertSent(fn ($request) => $request->method() === 'POST' && $request['post_id'] === 'post-42');
});
it('sends the branded user-agent header', function () {
Http::fake(['api.example.com/*' => Http::response(['ok' => true], 200)]);
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/me',
'method' => 'GET',
'auth_type' => 'none',
'headers' => ['User-Agent' => 'user-supplied-agent'],
]);
Http::assertSent(fn ($request) => $request->hasHeader('User-Agent', config('trypost.user_agent')));
});
it('sends custom headers configured in the editor', function () {
Http::fake(['api.example.com/*' => Http::response(['ok' => true], 200)]);
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
app(RunHttpRequestNode::class)($run, [
'url' => 'https://api.example.com/me',
'method' => 'GET',
'auth_type' => 'none',
'headers' => ['X-Custom' => 'v'],
]);
Http::assertSent(fn ($request) => $request->hasHeader('X-Custom', 'v'));
});
it('fails when url is missing', function () {
$automation = Automation::factory()->active()->create();
$run = AutomationRun::factory()->for($automation)->create(['current_node_id' => 'http_1']);
$result = app(RunHttpRequestNode::class)($run, []);
expect($result->status)->toBe(NodeRunStatus::Failed);
});