feat: enums, schema and models for hado core

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
nikita.hohlov
2026-09-04 06:05:34 -03:00
parent 8d01b7050d
commit 774e0bce1a
18 changed files with 447 additions and 123 deletions

View File

@@ -0,0 +1,91 @@
<?php
namespace Tests\Feature;
use App\Enums\AfterDue;
use App\Enums\ChannelType;
use App\Enums\DueMode;
use App\Enums\EventState;
use App\Models\Channel;
use App\Models\Event;
use App\Models\Source;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SchemaTest extends TestCase
{
use RefreshDatabase;
private function makeEvent(Source $source, User $user, string $topic = 'birthday:2026'): Event
{
return Event::create([
'source_id' => $source->id,
'user_id' => $user->id,
'source_ref' => 'person:42',
'topic' => $topic,
'due_mode' => DueMode::Local,
'due_date' => '2026-08-14',
'due_time' => '24:00:00',
'fire_on' => '2026-08-14',
'after_due' => AfterDue::Expire,
'payload' => ['title' => 'ДР'],
'state' => EventState::Scheduled,
'quiet_until' => CarbonImmutable::parse('2000-01-01T00:00:00Z'),
]);
}
public function test_event_identity_is_unique_per_source_ref_topic(): void
{
$source = Source::create(['name' => 'docs', 'token_hash' => hash('sha256', 'x')]);
$user = User::create(['login' => 'nikita', 'tz' => 'UTC', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
$this->makeEvent($source, $user);
$this->expectException(QueryException::class);
$this->makeEvent($source, $user);
}
public function test_same_source_ref_with_different_topics_coexist(): void
{
$source = Source::create(['name' => 'docs', 'token_hash' => hash('sha256', 'x')]);
$user = User::create(['login' => 'nikita', 'tz' => 'UTC', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
$this->makeEvent($source, $user, 'birthday:2026');
$this->makeEvent($source, $user, 'birthday:2027');
$this->assertSame(2, Event::count());
}
public function test_casts_round_trip(): void
{
$source = Source::create(['name' => 'docs', 'token_hash' => hash('sha256', 'x')]);
$user = User::create(['login' => 'nikita', 'tz' => 'Asia/Shanghai', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
$event = $this->makeEvent($source, $user)->fresh();
$this->assertSame(DueMode::Local, $event->due_mode);
$this->assertSame(EventState::Scheduled, $event->state);
$this->assertSame('2026-08-14', $event->due_date->format('Y-m-d'));
$this->assertSame('24:00:00', $event->due_time);
$this->assertSame(['title' => 'ДР'], $event->payload);
$this->assertFalse($event->isTerminal());
$this->assertSame('Asia/Shanghai', $user->now()->timezoneName);
Channel::create(['user_id' => $user->id, 'type' => ChannelType::Web, 'config' => [], 'enabled' => true]);
$this->assertSame(ChannelType::Web, $user->channels()->first()->type);
}
public function test_state_helpers(): void
{
$this->assertTrue(EventState::Done->isTerminal());
$this->assertTrue(EventState::Expired->isTerminal());
$this->assertTrue(EventState::Withdrawn->isTerminal());
$this->assertFalse(EventState::Today->isTerminal());
$this->assertTrue(EventState::Preparing->isActive());
$this->assertFalse(EventState::Scheduled->isActive());
$this->assertTrue(EventState::Today->countsInBadge());
$this->assertTrue(EventState::Overdue->countsInBadge());
$this->assertFalse(EventState::Preparing->countsInBadge());
$this->assertSame(['scheduled', 'preparing', 'today', 'overdue'], EventState::nonTerminalValues());
}
}