trypost/app/Actions/Automation/Run/TestAutomation.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

122 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Automation\Run;
use App\Enums\Automation\Run\Status;
use App\Enums\Automation\Trigger\Type as TriggerType;
use App\Jobs\Automation\ProcessAutomationNode;
use App\Models\Automation;
use App\Models\AutomationRun;
use App\Models\Post;
/**
* Kicks off a manual run from the editor without waiting for the real trigger
* to fire. Mirrors n8n's "Execute workflow" — the same job pipeline runs, but
* the trigger payload is synthesized so the user can see exactly what each
* node does end-to-end. Runs are flagged `is_manual=true` so they're filtered
* from production audit views.
*
* When `$withRealData` is false (the default), the run is marked `is_dry_run`
* so side-effectful nodes (publish, generate, watermark advancement, sibling
* spawning) short-circuit. Dry-run rows are kept briefly so the editor test
* panel can show the completed result, then reaped by the scheduled
* `automation:prune-dry-runs` command once past its grace window.
*/
class TestAutomation
{
public function __invoke(Automation $automation, bool $withRealData = false): AutomationRun
{
$triggerNode = collect($automation->nodes ?? [])->firstWhere('type', 'trigger');
$context = ['trigger' => $this->synthesizePayload($automation, $triggerNode ?? [])];
$firstNodeId = $this->findFirstRealNodeId($automation, $triggerNode);
$run = AutomationRun::create([
'automation_id' => $automation->id,
'status' => Status::Pending,
'is_manual' => true,
'is_dry_run' => ! $withRealData,
'context' => $context,
]);
if ($firstNodeId === null) {
$run->update([
'status' => Status::Failed,
'error' => ['message' => __('automations.errors.no_trigger_connection')],
'finished_at' => now(),
]);
return $run;
}
ProcessAutomationNode::dispatch($run, $firstNodeId);
return $run;
}
/**
* @param array<string, mixed> $triggerNode
* @return array<string, mixed>
*/
private function synthesizePayload(Automation $automation, array $triggerNode): array
{
$type = data_get($triggerNode, 'data.trigger_type');
return match ($type) {
TriggerType::PostPublished->value, TriggerType::PostScheduled->value => $this->synthesizePostPayload($automation, (string) $type),
default => ['event' => $type ?? TriggerType::Schedule->value, 'fired_at' => now()->toIso8601String(), 'manual' => true],
};
}
/**
* Picks the most recent post in the automation's workspace so the test run
* reflects something the user actually sees. Falls back to a placeholder
* payload when the workspace has no posts yet.
*
* @return array<string, mixed>
*/
private function synthesizePostPayload(Automation $automation, string $event): array
{
$post = Post::query()
->where('workspace_id', $automation->workspace_id)
->latest()
->first();
$base = [
'event' => $event,
'fired_at' => now()->toIso8601String(),
'manual' => true,
];
if ($post === null) {
return array_merge($base, ['post' => null, 'fetch_error' => 'no posts in workspace']);
}
return array_merge($base, [
'post' => [
'id' => $post->id,
'content' => $post->content,
'status' => $post->status->value,
'scheduled_at' => $post->scheduled_at?->toIso8601String(),
'published_at' => $post->published_at?->toIso8601String(),
],
]);
}
/**
* @param array<string, mixed>|null $triggerNode
*/
private function findFirstRealNodeId(Automation $automation, ?array $triggerNode): ?string
{
if ($triggerNode === null) {
return null;
}
$connection = collect($automation->connections ?? [])
->firstWhere('source', $triggerNode['id']);
return $connection['target'] ?? null;
}
}