fix(automations): preview shows the newest feed item, not the oldest

A manual/dry "test with real data" run surfaces a single item and never fans
out, but it was taking the oldest new item (items are sorted oldest-first for
production fan-out). Surface the newest item in preview instead — what the user
expects to test against. Real runs are unchanged. Works for every feed format.
This commit is contained in:
Paulo Castellano 2026-06-16 11:26:05 -03:00
parent 4bb7d450a3
commit 109ad7dfc0
2 changed files with 19 additions and 4 deletions

View file

@ -96,14 +96,24 @@ public function __invoke(AutomationRun $run, array $config): NodeRunResult
return NodeRunResult::completed(['fetch' => ['count' => 0]], nextHandle: self::NO_ITEMS_HANDLE);
}
$first = array_shift($newItems);
$total = count($newItems);
if (! $isPreview) {
$this->spawnSiblings($run, $nodeId, $newItems);
// A preview (manual/dry test) surfaces ONE item and never fans out, so it
// shows the newest item — what the user expects to test against. A real run
// takes the oldest new item and fans the rest out as siblings, preserving
// feed chronology across branches (items are sorted oldest-first).
if ($isPreview) {
return NodeRunResult::completed([
'fetch' => ['count' => $total, 'spawned' => 0],
'fetched' => end($newItems),
]);
}
$first = array_shift($newItems);
$this->spawnSiblings($run, $nodeId, $newItems);
return NodeRunResult::completed([
'fetch' => ['count' => count($newItems) + 1, 'spawned' => $isPreview ? 0 : count($newItems)],
'fetch' => ['count' => $total, 'spawned' => count($newItems)],
'fetched' => $first,
]);
}

View file

@ -193,6 +193,11 @@
expect($result->status)->toBe(NodeRunStatus::Completed);
expect($result->output['fetch']['count'])->toBeGreaterThan(0);
// ...surfaces the NEWEST item (key 'c', 15 Jun) — not the oldest — so the
// test reflects what the user just published, and spawns no siblings.
expect($result->output['fetched']['key'])->toBe('c');
expect($result->output['fetch']['spawned'])->toBe(0);
// ...and never advances the persisted watermark.
$state = AutomationNodeState::for($automation->id, 'fetch_1');
expect($state->data['last_item_date'])->toBe('2030-01-01T00:00:00+00:00');