|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Waterline\Tests\Feature; |
| 4 | + |
| 5 | +use Illuminate\Support\Carbon; |
| 6 | +use Waterline\Tests\TestCase; |
| 7 | +use Workflow\V2\Enums\HistoryEventType; |
| 8 | +use Workflow\V2\Enums\ScheduleStatus; |
| 9 | +use Workflow\V2\Models\WorkflowSchedule; |
| 10 | +use Workflow\V2\Models\WorkflowScheduleHistoryEvent; |
| 11 | + |
| 12 | +class V2SchedulesHistoryControllerTest extends TestCase |
| 13 | +{ |
| 14 | + public function testHistoryReturns404WhenScheduleMissing(): void |
| 15 | + { |
| 16 | + $this->getJson('/waterline/api/v2/schedules/missing-id/history') |
| 17 | + ->assertStatus(404) |
| 18 | + ->assertJsonPath('error', 'Schedule not found.'); |
| 19 | + } |
| 20 | + |
| 21 | + public function testHistoryReturnsEventsOrderedBySequence(): void |
| 22 | + { |
| 23 | + $schedule = $this->createSchedule('daily-invoice-sync'); |
| 24 | + |
| 25 | + Carbon::setTestNow('2026-04-24T10:00:00+00:00'); |
| 26 | + WorkflowScheduleHistoryEvent::record( |
| 27 | + $schedule, |
| 28 | + HistoryEventType::ScheduleCreated, |
| 29 | + [ |
| 30 | + 'spec' => ['cron_expressions' => ['0 2 * * *'], 'timezone' => 'UTC'], |
| 31 | + 'action' => ['workflow_type' => 'workflow.test'], |
| 32 | + 'overlap_policy' => 'skip', |
| 33 | + 'next_fire_at' => '2026-04-25T02:00:00+00:00', |
| 34 | + 'command_context' => ['source' => 'api'], |
| 35 | + ], |
| 36 | + ); |
| 37 | + |
| 38 | + Carbon::setTestNow('2026-04-24T10:05:00+00:00'); |
| 39 | + WorkflowScheduleHistoryEvent::record( |
| 40 | + $schedule, |
| 41 | + HistoryEventType::SchedulePaused, |
| 42 | + [ |
| 43 | + 'reason' => 'maintenance', |
| 44 | + 'paused_at' => '2026-04-24T10:05:00+00:00', |
| 45 | + 'command_context' => ['source' => 'waterline'], |
| 46 | + ], |
| 47 | + ); |
| 48 | + |
| 49 | + Carbon::setTestNow('2026-04-24T10:10:00+00:00'); |
| 50 | + WorkflowScheduleHistoryEvent::record( |
| 51 | + $schedule, |
| 52 | + HistoryEventType::ScheduleResumed, |
| 53 | + [ |
| 54 | + 'next_fire_at' => '2026-04-25T02:00:00+00:00', |
| 55 | + 'command_context' => ['source' => 'waterline'], |
| 56 | + ], |
| 57 | + ); |
| 58 | + |
| 59 | + $response = $this->getJson('/waterline/api/v2/schedules/daily-invoice-sync/history') |
| 60 | + ->assertStatus(200) |
| 61 | + ->assertJsonPath('schedule_id', 'daily-invoice-sync') |
| 62 | + ->assertJsonPath('namespace', 'default') |
| 63 | + ->assertJsonPath('has_more', false) |
| 64 | + ->assertJsonPath('next_cursor', null) |
| 65 | + ->assertJsonCount(3, 'events'); |
| 66 | + |
| 67 | + $events = $response->json('events'); |
| 68 | + |
| 69 | + $this->assertSame(1, $events[0]['sequence']); |
| 70 | + $this->assertSame('ScheduleCreated', $events[0]['event_type']); |
| 71 | + $this->assertSame('skip', $events[0]['payload']['overlap_policy']); |
| 72 | + |
| 73 | + $this->assertSame(2, $events[1]['sequence']); |
| 74 | + $this->assertSame('SchedulePaused', $events[1]['event_type']); |
| 75 | + $this->assertSame('maintenance', $events[1]['payload']['reason']); |
| 76 | + |
| 77 | + $this->assertSame(3, $events[2]['sequence']); |
| 78 | + $this->assertSame('ScheduleResumed', $events[2]['event_type']); |
| 79 | + $this->assertArrayHasKey('recorded_at', $events[2]); |
| 80 | + $this->assertArrayHasKey('id', $events[2]); |
| 81 | + } |
| 82 | + |
| 83 | + public function testHistorySupportsCursorPagination(): void |
| 84 | + { |
| 85 | + $schedule = $this->createSchedule('paginated-schedule'); |
| 86 | + |
| 87 | + for ($i = 0; $i < 5; $i++) { |
| 88 | + WorkflowScheduleHistoryEvent::record( |
| 89 | + $schedule, |
| 90 | + HistoryEventType::ScheduleTriggered, |
| 91 | + [ |
| 92 | + 'workflow_instance_id' => 'instance-'.$i, |
| 93 | + 'workflow_run_id' => 'run-'.$i, |
| 94 | + 'schedule_id' => 'paginated-schedule', |
| 95 | + 'schedule_ulid' => $schedule->id, |
| 96 | + 'cron_expression' => '*/5 * * * *', |
| 97 | + 'timezone' => 'UTC', |
| 98 | + 'overlap_policy' => 'skip', |
| 99 | + 'outcome' => 'started', |
| 100 | + 'effective_overlap_policy' => 'skip', |
| 101 | + 'trigger_number' => $i + 1, |
| 102 | + 'occurrence_time' => '2026-04-24T10:0'.$i.':00+00:00', |
| 103 | + 'command_context' => ['source' => 'tick'], |
| 104 | + ], |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + $firstPage = $this->getJson('/waterline/api/v2/schedules/paginated-schedule/history?limit=2') |
| 109 | + ->assertStatus(200) |
| 110 | + ->assertJsonCount(2, 'events') |
| 111 | + ->assertJsonPath('has_more', true) |
| 112 | + ->assertJsonPath('next_cursor', 2); |
| 113 | + |
| 114 | + $this->assertSame(1, $firstPage->json('events.0.sequence')); |
| 115 | + $this->assertSame(2, $firstPage->json('events.1.sequence')); |
| 116 | + |
| 117 | + $secondPage = $this->getJson('/waterline/api/v2/schedules/paginated-schedule/history?limit=2&after_sequence=2') |
| 118 | + ->assertStatus(200) |
| 119 | + ->assertJsonCount(2, 'events') |
| 120 | + ->assertJsonPath('has_more', true) |
| 121 | + ->assertJsonPath('next_cursor', 4); |
| 122 | + |
| 123 | + $this->assertSame(3, $secondPage->json('events.0.sequence')); |
| 124 | + $this->assertSame(4, $secondPage->json('events.1.sequence')); |
| 125 | + |
| 126 | + $thirdPage = $this->getJson('/waterline/api/v2/schedules/paginated-schedule/history?limit=2&after_sequence=4') |
| 127 | + ->assertStatus(200) |
| 128 | + ->assertJsonCount(1, 'events') |
| 129 | + ->assertJsonPath('has_more', false) |
| 130 | + ->assertJsonPath('next_cursor', null); |
| 131 | + |
| 132 | + $this->assertSame(5, $thirdPage->json('events.0.sequence')); |
| 133 | + } |
| 134 | + |
| 135 | + public function testHistoryRespectsConfiguredNamespace(): void |
| 136 | + { |
| 137 | + config()->set('waterline.namespace', 'tenant-a'); |
| 138 | + |
| 139 | + $scheduleA = $this->createSchedule('shared-id', namespace: 'tenant-a'); |
| 140 | + $scheduleB = $this->createSchedule('shared-id', namespace: 'tenant-b'); |
| 141 | + |
| 142 | + WorkflowScheduleHistoryEvent::record( |
| 143 | + $scheduleA, |
| 144 | + HistoryEventType::SchedulePaused, |
| 145 | + [ |
| 146 | + 'reason' => 'tenant-a-pause', |
| 147 | + 'paused_at' => '2026-04-24T10:00:00+00:00', |
| 148 | + 'command_context' => ['source' => 'api'], |
| 149 | + ], |
| 150 | + ); |
| 151 | + |
| 152 | + WorkflowScheduleHistoryEvent::record( |
| 153 | + $scheduleB, |
| 154 | + HistoryEventType::SchedulePaused, |
| 155 | + [ |
| 156 | + 'reason' => 'tenant-b-pause', |
| 157 | + 'paused_at' => '2026-04-24T10:00:00+00:00', |
| 158 | + 'command_context' => ['source' => 'api'], |
| 159 | + ], |
| 160 | + ); |
| 161 | + |
| 162 | + $response = $this->getJson('/waterline/api/v2/schedules/shared-id/history') |
| 163 | + ->assertStatus(200) |
| 164 | + ->assertJsonPath('namespace', 'tenant-a') |
| 165 | + ->assertJsonCount(1, 'events'); |
| 166 | + |
| 167 | + $this->assertSame('tenant-a-pause', $response->json('events.0.payload.reason')); |
| 168 | + } |
| 169 | + |
| 170 | + public function testHistoryClampsLimitAtUpperBound(): void |
| 171 | + { |
| 172 | + $schedule = $this->createSchedule('big-limit'); |
| 173 | + |
| 174 | + WorkflowScheduleHistoryEvent::record( |
| 175 | + $schedule, |
| 176 | + HistoryEventType::ScheduleCreated, |
| 177 | + [ |
| 178 | + 'spec' => ['cron_expressions' => ['0 * * * *'], 'timezone' => 'UTC'], |
| 179 | + 'action' => ['workflow_type' => 'workflow.test'], |
| 180 | + 'overlap_policy' => 'skip', |
| 181 | + 'next_fire_at' => '2026-04-25T00:00:00+00:00', |
| 182 | + 'command_context' => ['source' => 'api'], |
| 183 | + ], |
| 184 | + ); |
| 185 | + |
| 186 | + $this->getJson('/waterline/api/v2/schedules/big-limit/history?limit=99999') |
| 187 | + ->assertStatus(200) |
| 188 | + ->assertJsonCount(1, 'events'); |
| 189 | + |
| 190 | + $this->getJson('/waterline/api/v2/schedules/big-limit/history?limit=0') |
| 191 | + ->assertStatus(200) |
| 192 | + ->assertJsonCount(1, 'events'); |
| 193 | + |
| 194 | + $this->getJson('/waterline/api/v2/schedules/big-limit/history?limit=-5') |
| 195 | + ->assertStatus(200) |
| 196 | + ->assertJsonCount(1, 'events'); |
| 197 | + } |
| 198 | + |
| 199 | + private function createSchedule(string $scheduleId, string $namespace = 'default'): WorkflowSchedule |
| 200 | + { |
| 201 | + return WorkflowSchedule::create([ |
| 202 | + 'schedule_id' => $scheduleId, |
| 203 | + 'namespace' => $namespace, |
| 204 | + 'spec' => ['cron_expressions' => ['0 * * * *'], 'timezone' => 'UTC'], |
| 205 | + 'action' => ['workflow_type' => 'workflow.test', 'workflow_class' => 'Test\\ScheduledWorkflow'], |
| 206 | + 'status' => ScheduleStatus::Active, |
| 207 | + 'overlap_policy' => 'skip', |
| 208 | + 'fires_count' => 0, |
| 209 | + 'failures_count' => 0, |
| 210 | + 'skipped_trigger_count' => 0, |
| 211 | + 'jitter_seconds' => 0, |
| 212 | + 'next_fire_at' => Carbon::now()->addHour(), |
| 213 | + ]); |
| 214 | + } |
| 215 | +} |
0 commit comments