feat: state, window and quiet rules
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
83
core/tests/Unit/Scheduling/QuietRuleTest.php
Normal file
83
core/tests/Unit/Scheduling/QuietRuleTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Scheduling;
|
||||
|
||||
use App\Enums\AfterDue;
|
||||
use App\Enums\DueMode;
|
||||
use App\Enums\EventState;
|
||||
use App\Models\Event;
|
||||
use App\Models\User;
|
||||
use App\Scheduling\DueResolver;
|
||||
use App\Scheduling\QuietRule;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Tests\TestCase; // Laravel TestCase: Eloquent date-casts need the connection grammar even in memory
|
||||
|
||||
class QuietRuleTest extends TestCase
|
||||
{
|
||||
private User $user;
|
||||
private DueResolver $due;
|
||||
private QuietRule $rule;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = new User(['login' => 'n', 'tz' => 'UTC', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
|
||||
$this->due = new DueResolver;
|
||||
$this->rule = new QuietRule($this->due);
|
||||
}
|
||||
|
||||
private function event(string $due, string $fireOn, string $time = '24:00:00'): Event
|
||||
{
|
||||
return new Event([
|
||||
'due_mode' => DueMode::Local, 'due_date' => $due, 'due_time' => $time, 'fire_on' => $fireOn,
|
||||
'after_due' => AfterDue::Keep, 'state' => EventState::Preparing, 'payload' => ['title' => 't'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function at(string $s): CarbonImmutable
|
||||
{
|
||||
return CarbonImmutable::parse($s, 'UTC');
|
||||
}
|
||||
|
||||
public function test_preparing_ack_is_quiet_until_next_cascade_point(): void
|
||||
{
|
||||
$e = $this->event('2026-09-30', '2026-08-31'); // точки: 30,15,7,3,1 → 31.08, 15.09, 23.09, 27.09, 29.09
|
||||
$due = $this->due->resolve($e, $this->user);
|
||||
|
||||
$q = $this->rule->afterAck($e, $this->user, EventState::Preparing, $due, $this->at('2026-09-02 10:00'));
|
||||
$this->assertSame('2026-09-15 00:00', $q->format('Y-m-d H:i'));
|
||||
|
||||
$q = $this->rule->afterAck($e, $this->user, EventState::Preparing, $due, $this->at('2026-09-15 10:00'));
|
||||
$this->assertSame('2026-09-23 00:00', $q->format('Y-m-d H:i'), 'на самой точке — до следующей');
|
||||
|
||||
$q = $this->rule->afterAck($e, $this->user, EventState::Preparing, $due, $this->at('2026-09-29 10:00'));
|
||||
$this->assertSame('2026-09-30 00:00', $q->format('Y-m-d H:i'), 'точек больше нет — до старта дня срока');
|
||||
}
|
||||
|
||||
public function test_today_ack_skips_the_next_hour(): void
|
||||
{
|
||||
$e = $this->event('2026-09-30', '2026-09-30');
|
||||
$due = $this->due->resolve($e, $this->user);
|
||||
|
||||
$q = $this->rule->afterAck($e, $this->user, EventState::Today, $due, $this->at('2026-09-30 11:17'));
|
||||
$this->assertSame('2026-09-30 13:00', $q->format('Y-m-d H:i'));
|
||||
}
|
||||
|
||||
public function test_overdue_ack_skips_tomorrow(): void
|
||||
{
|
||||
$e = $this->event('2026-09-30', '2026-09-30');
|
||||
$due = $this->due->resolve($e, $this->user);
|
||||
|
||||
$q = $this->rule->afterAck($e, $this->user, EventState::Overdue, $due, $this->at('2026-10-05 15:00'));
|
||||
$this->assertSame('2026-10-07 00:00', $q->format('Y-m-d H:i'));
|
||||
}
|
||||
|
||||
public function test_scheduled_ack_changes_nothing(): void
|
||||
{
|
||||
$e = $this->event('2026-09-30', '2026-09-30');
|
||||
$due = $this->due->resolve($e, $this->user);
|
||||
$now = $this->at('2026-09-01 15:00');
|
||||
|
||||
$this->assertTrue($this->rule->afterAck($e, $this->user, EventState::Scheduled, $due, $now)->eq($now));
|
||||
}
|
||||
}
|
||||
89
core/tests/Unit/Scheduling/StateResolverTest.php
Normal file
89
core/tests/Unit/Scheduling/StateResolverTest.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Scheduling;
|
||||
|
||||
use App\Enums\AfterDue;
|
||||
use App\Enums\DueMode;
|
||||
use App\Enums\EventState;
|
||||
use App\Models\Event;
|
||||
use App\Models\User;
|
||||
use App\Scheduling\DueResolver;
|
||||
use App\Scheduling\StateResolver;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Tests\TestCase; // Laravel TestCase: Eloquent date-casts need the connection grammar even in memory
|
||||
|
||||
class StateResolverTest extends TestCase
|
||||
{
|
||||
private User $user;
|
||||
private StateResolver $resolver;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = new User(['login' => 'n', 'tz' => 'America/Montevideo', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
|
||||
$this->resolver = new StateResolver(new DueResolver);
|
||||
}
|
||||
|
||||
private function event(string $due, string $fireOn, AfterDue $afterDue = AfterDue::Keep, string $time = '24:00:00'): Event
|
||||
{
|
||||
return new Event([
|
||||
'due_mode' => DueMode::Local, 'due_date' => $due, 'due_time' => $time, 'fire_on' => $fireOn,
|
||||
'after_due' => $afterDue, 'state' => EventState::Scheduled, 'payload' => ['title' => 't'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function at(string $local): CarbonImmutable
|
||||
{
|
||||
return CarbonImmutable::parse($local, 'America/Montevideo');
|
||||
}
|
||||
|
||||
public function test_deadline_walks_through_states(): void
|
||||
{
|
||||
$e = $this->event('2026-09-30', '2026-08-31', AfterDue::Keep);
|
||||
|
||||
$this->assertSame(EventState::Scheduled, $this->resolver->resolve($e, $this->user, $this->at('2026-08-30 23:59')));
|
||||
$this->assertSame(EventState::Preparing, $this->resolver->resolve($e, $this->user, $this->at('2026-08-31 00:00')));
|
||||
$this->assertSame(EventState::Preparing, $this->resolver->resolve($e, $this->user, $this->at('2026-09-29 23:59')));
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-09-30 00:00')));
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-09-30 23:59')));
|
||||
$this->assertSame(EventState::Overdue, $this->resolver->resolve($e, $this->user, $this->at('2026-10-01 00:00')));
|
||||
}
|
||||
|
||||
public function test_day_of_never_prepares_and_expires(): void
|
||||
{
|
||||
$e = $this->event('2026-08-14', '2026-08-14', AfterDue::Expire);
|
||||
|
||||
$this->assertSame(EventState::Scheduled, $this->resolver->resolve($e, $this->user, $this->at('2026-08-13 23:59')));
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-08-14 00:00')));
|
||||
$this->assertSame(EventState::Expired, $this->resolver->resolve($e, $this->user, $this->at('2026-08-15 00:00')));
|
||||
}
|
||||
|
||||
public function test_timed_moment_ends_today_mid_day(): void
|
||||
{
|
||||
$e = $this->event('2026-09-10', '2026-09-03', AfterDue::Expire, '14:00:00');
|
||||
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-09-10 13:59')));
|
||||
$this->assertSame(EventState::Expired, $this->resolver->resolve($e, $this->user, $this->at('2026-09-10 14:00')));
|
||||
}
|
||||
|
||||
public function test_early_moment_starts_today_the_evening_before(): void
|
||||
{
|
||||
$e = $this->event('2026-09-10', '2026-09-03', AfterDue::Expire, '07:00:00');
|
||||
|
||||
$this->assertSame(EventState::Preparing, $this->resolver->resolve($e, $this->user, $this->at('2026-09-09 19:59')));
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-09-09 20:00')));
|
||||
$this->assertSame(EventState::Expired, $this->resolver->resolve($e, $this->user, $this->at('2026-09-10 07:00')));
|
||||
}
|
||||
|
||||
public function test_fixed_flight_uses_user_timezone(): void
|
||||
{
|
||||
$e = new Event([
|
||||
'due_mode' => DueMode::Fixed, 'due_instant' => CarbonImmutable::parse('2026-09-10T08:15:00+02:00'),
|
||||
'fire_on' => '2026-09-03', 'after_due' => AfterDue::Expire, 'state' => EventState::Scheduled, 'payload' => ['title' => 't'],
|
||||
]);
|
||||
// 03:15 по Монтевидео → раньше активных часов → today с 20:00 накануне
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-09-09 20:00')));
|
||||
$this->assertSame(EventState::Today, $this->resolver->resolve($e, $this->user, $this->at('2026-09-10 03:14')));
|
||||
$this->assertSame(EventState::Expired, $this->resolver->resolve($e, $this->user, $this->at('2026-09-10 03:15')));
|
||||
}
|
||||
}
|
||||
95
core/tests/Unit/Scheduling/WindowResolverTest.php
Normal file
95
core/tests/Unit/Scheduling/WindowResolverTest.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Scheduling;
|
||||
|
||||
use App\Enums\EventState;
|
||||
use App\Models\User;
|
||||
use App\Scheduling\Due;
|
||||
use App\Scheduling\WindowResolver;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Tests\TestCase; // Laravel TestCase: Eloquent date-casts need the connection grammar even in memory
|
||||
|
||||
class WindowResolverTest extends TestCase
|
||||
{
|
||||
private User $user;
|
||||
private WindowResolver $windows;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = new User(['login' => 'n', 'tz' => 'UTC', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
|
||||
$this->windows = new WindowResolver(30, 10, [9, 14, 20]);
|
||||
}
|
||||
|
||||
private function at(string $s): CarbonImmutable
|
||||
{
|
||||
return CarbonImmutable::parse($s, 'UTC');
|
||||
}
|
||||
|
||||
private function wholeDay(string $ymd): Due
|
||||
{
|
||||
$day = $this->at($ymd);
|
||||
return new Due($day, $day->addDay());
|
||||
}
|
||||
|
||||
public function test_outside_active_hours_there_is_no_window(): void
|
||||
{
|
||||
$due = $this->wholeDay('2026-09-30');
|
||||
$this->assertNull($this->windows->current(EventState::Preparing, $this->user, $due, $this->at('2026-09-20 08:59')));
|
||||
$this->assertNull($this->windows->current(EventState::Preparing, $this->user, $due, $this->at('2026-09-20 22:00')));
|
||||
$this->assertNull($this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 23:00')));
|
||||
}
|
||||
|
||||
public function test_preparing_and_overdue_have_one_daily_window(): void
|
||||
{
|
||||
$due = $this->wholeDay('2026-09-30');
|
||||
foreach ([EventState::Preparing, EventState::Overdue] as $state) {
|
||||
$w = $this->windows->current($state, $this->user, $due, $this->at('2026-09-20 12:34'));
|
||||
$this->assertSame('2026-09-20 09:00', $w->start->format('Y-m-d H:i'));
|
||||
$this->assertSame('2026-09-20 22:00', $w->end->format('Y-m-d H:i'));
|
||||
$this->assertTrue($w->fallbackAllowed);
|
||||
$this->assertSame('2026-09-20 21:30', $w->fallbackAt->format('Y-m-d H:i'));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_today_has_hourly_windows_with_fallback_only_in_listed_hours(): void
|
||||
{
|
||||
$due = $this->wholeDay('2026-09-30');
|
||||
|
||||
$w = $this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 09:20'));
|
||||
$this->assertSame('2026-09-30 09:00', $w->start->format('Y-m-d H:i'));
|
||||
$this->assertSame('2026-09-30 10:00', $w->end->format('Y-m-d H:i'));
|
||||
$this->assertTrue($w->fallbackAllowed);
|
||||
$this->assertSame('2026-09-30 09:50', $w->fallbackAt->format('Y-m-d H:i'));
|
||||
|
||||
$w = $this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 11:05'));
|
||||
$this->assertSame('2026-09-30 11:00', $w->start->format('Y-m-d H:i'));
|
||||
$this->assertFalse($w->fallbackAllowed);
|
||||
|
||||
$w = $this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 21:59'));
|
||||
$this->assertSame('2026-09-30 22:00', $w->end->format('Y-m-d H:i'));
|
||||
}
|
||||
|
||||
public function test_today_window_is_truncated_by_due_moment_and_becomes_fallback(): void
|
||||
{
|
||||
$day = $this->at('2026-09-30');
|
||||
|
||||
$due = new Due($day, $day->setTime(9, 30));
|
||||
$w = $this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 09:05'));
|
||||
$this->assertSame('2026-09-30 09:30', $w->end->format('Y-m-d H:i'));
|
||||
$this->assertSame('2026-09-30 09:20', $w->fallbackAt->format('Y-m-d H:i'));
|
||||
|
||||
$due = new Due($day, $day->setTime(11, 15));
|
||||
$w = $this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 11:02'));
|
||||
$this->assertSame('2026-09-30 11:15', $w->end->format('Y-m-d H:i'));
|
||||
$this->assertTrue($w->fallbackAllowed, 'обрезанное окно всегда fallback-окно');
|
||||
$this->assertSame('2026-09-30 11:05', $w->fallbackAt->format('Y-m-d H:i'));
|
||||
}
|
||||
|
||||
public function test_no_window_once_due_moment_passed(): void
|
||||
{
|
||||
$day = $this->at('2026-09-30');
|
||||
$due = new Due($day, $day->setTime(11, 15));
|
||||
$this->assertNull($this->windows->current(EventState::Today, $this->user, $due, $this->at('2026-09-30 11:20')));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user