feat: MCP tools for events (list/get/create/update/delete/done/ack); EventRules shared by /api and manual

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-05 06:10:41 -03:00
parent 450c165fce
commit ed45a1d516
18 changed files with 711 additions and 56 deletions

View File

@@ -126,12 +126,13 @@ class EventUpserterTest extends TestCase
$this->assertNull($event->due_date);
}
public function test_withdraw_marks_withdrawn_and_reports_missing(): void
public function test_withdraw_marks_withdrawn(): void
{
$this->upserter->upsert($this->source, [$this->item()]);
$this->assertTrue($this->upserter->withdraw($this->source, 'document:918', 'expiry'));
$this->upserter->withdraw($this->upserter->find($this->source, 'document:918', 'expiry'));
$this->assertSame(EventState::Withdrawn, Event::sole()->state);
$this->assertFalse($this->upserter->withdraw($this->source, 'document:918', 'other'));
$this->assertNull($this->upserter->find($this->source, 'document:918', 'other'));
}
}

View File

@@ -0,0 +1,179 @@
<?php
namespace Tests\Feature;
use App\Enums\AfterDue;
use App\Enums\DueMode;
use App\Enums\EventState;
use App\Mcp\HadoServer;
use App\Mcp\Tools\CreateEvent;
use App\Mcp\Tools\DeleteEvent;
use App\Mcp\Tools\GetEvent;
use App\Mcp\Tools\GetProfile;
use App\Mcp\Tools\ListEvents;
use App\Mcp\Tools\MarkAck;
use App\Mcp\Tools\MarkDone;
use App\Mcp\Tools\UpdateEvent;
use App\Mcp\Tools\UpdateProfile;
use App\Models\Event;
use App\Models\Source;
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class McpToolsTest extends TestCase
{
use RefreshDatabase;
private User $user;
private Source $docs;
protected function setUp(): void
{
parent::setUp();
config(['hado.default_tz' => 'UTC']);
CarbonImmutable::setTestNow('2026-09-05T12:00:00Z');
$this->user = User::create(['login' => 'nikita', 'tz' => 'UTC']);
$this->docs = Source::create(['name' => 'docs', 'token_hash' => hash('sha256', 'x')]);
// Как AuthenticateAgent: пользователь токена лежит в атрибутах текущего запроса
$this->app['request']->attributes->set('user', $this->user);
}
protected function tearDown(): void
{
CarbonImmutable::setTestNow();
parent::tearDown();
}
private function docsEvent(User $user, EventState $state = EventState::Today, string $topic = 't', string $due = '2026-09-05'): Event
{
return Event::create([
'source_id' => $this->docs->id, 'user_id' => $user->id, 'source_ref' => 'r', 'topic' => $topic,
'due_mode' => DueMode::Local, 'due_date' => $due, 'due_time' => '24:00:00', 'fire_on' => $due,
'after_due' => AfterDue::Keep, 'payload' => ['title' => "Docs $topic", 'done_label' => 'Поздравил'], 'state' => $state,
'quiet_until' => CarbonImmutable::now()->subDay(),
]);
}
/** Своё событие через тул; id — из базы, TestResponse его наружу не отдаёт. */
private function createOwn(string $title = 'Виза', string $due = '2026-10-05'): int
{
HadoServer::tool(CreateEvent::class, ['title' => $title, 'due_at' => $due])->assertOk();
return Event::orderByDesc('id')->firstOrFail()->id;
}
// --- чтение ---
public function test_list_events_shows_active_events_of_all_sources_with_editable_flag(): void
{
$this->docsEvent($this->user, EventState::Today, 'a');
$this->docsEvent($this->user, EventState::Done, 'closed');
$other = User::create(['login' => 'other', 'tz' => 'UTC']);
$this->docsEvent($other, EventState::Today, 'foreign');
HadoServer::tool(CreateEvent::class, ['title' => 'Своё', 'due_at' => '2026-10-01'])->assertOk();
HadoServer::tool(ListEvents::class)->assertOk()->assertStructuredContent(fn (AssertableJson $j) => $j
->count('events', 2)
->where('events.0.source', 'docs')->where('events.0.editable', false)
->where('events.1.source', 'manual')->where('events.1.editable', true)
->etc());
}
public function test_list_events_filters_by_state_and_archive(): void
{
$this->docsEvent($this->user, EventState::Today, 'a');
$this->docsEvent($this->user, EventState::Scheduled, 'b', '2026-12-01');
$this->docsEvent($this->user, EventState::Expired, 'c', '2026-08-01');
HadoServer::tool(ListEvents::class, ['state' => 'scheduled'])->assertOk()
->assertStructuredContent(fn (AssertableJson $j) => $j->count('events', 1)->where('events.0.topic', 'b')->etc());
HadoServer::tool(ListEvents::class, ['archive' => true])->assertOk()
->assertStructuredContent(fn (AssertableJson $j) => $j->count('events', 1)->where('events.0.topic', 'c')->where('events.0.closed_as', 'Истекло')->etc());
HadoServer::tool(ListEvents::class, ['state' => 'garbage'])->assertHasErrors();
}
public function test_get_event_returns_own_and_hides_foreign(): void
{
$mine = $this->docsEvent($this->user);
$other = User::create(['login' => 'other', 'tz' => 'UTC']);
$foreign = $this->docsEvent($other, EventState::Today, 'f');
HadoServer::tool(GetEvent::class, ['id' => $mine->id])->assertOk()
->assertStructuredContent(fn (AssertableJson $j) => $j->where('id', $mine->id)->where('state', 'today')->where('editable', false)->etc());
HadoServer::tool(GetEvent::class, ['id' => $foreign->id])->assertHasErrors(['не найдено']);
}
// --- свои события (manual) ---
public function test_create_event_uses_inbox_defaults(): void
{
HadoServer::tool(CreateEvent::class, ['title' => 'Виза', 'due_at' => '2026-10-05'])->assertOk()
->assertStructuredContent(fn (AssertableJson $j) => $j
->where('source', 'manual')->where('editable', true)->where('state', 'preparing')
->where('fire_at', '2026-09-05')->where('after_due', 'keep')->where('due_at', '2026-10-05')
->where('payload.title', 'Виза')->where('payload.done_label', 'Сделано')
->etc());
$this->assertSame($this->user->id, Event::sole()->user_id);
}
public function test_create_event_honours_explicit_fields(): void
{
HadoServer::tool(CreateEvent::class, [
'title' => 'Вылет', 'subtitle' => 'SU 123', 'due_at' => '2026-09-10T08:15+02:00', 'fire_at' => '2026-09-10',
'after_due' => 'expire', 'done_label' => 'Улетел', 'deep_link' => '/trips/1',
])->assertOk()->assertStructuredContent(fn (AssertableJson $j) => $j
->where('due_at', '2026-09-10T06:15:00+00:00')->where('fire_at', '2026-09-10')->where('after_due', 'expire')
->where('payload.subtitle', 'SU 123')->where('payload.done_label', 'Улетел')->where('payload.deep_link', '/trips/1')
->etc());
}
public function test_create_event_rejects_fire_after_due(): void
{
HadoServer::tool(CreateEvent::class, ['title' => 'x', 'due_at' => '2026-10-05', 'fire_at' => '2026-10-06'])->assertHasErrors();
HadoServer::tool(CreateEvent::class, ['due_at' => '2026-10-05'])->assertHasErrors(['title']);
$this->assertSame(0, Event::count());
}
public function test_update_event_edits_own_event_in_place(): void
{
$id = $this->createOwn();
HadoServer::tool(UpdateEvent::class, ['id' => $id, 'title' => 'Виза в Японию', 'subtitle' => 'посольство'])->assertOk()
->assertStructuredContent(fn (AssertableJson $j) => $j->where('id', $id)->where('payload.title', 'Виза в Японию')
->where('payload.subtitle', 'посольство')->where('due_at', '2026-10-05')->where('state', 'preparing')->etc());
$this->assertSame(1, Event::count());
}
public function test_update_event_with_new_due_is_a_reschedule(): void
{
$id = $this->createOwn(due: '2026-09-01');
HadoServer::tool(MarkDone::class, ['id' => $id])->assertOk();
// Перенос: состояние пересчитывается даже из терминального, fire_at — заново от нового срока
HadoServer::tool(UpdateEvent::class, ['id' => $id, 'due_at' => '2026-12-01'])->assertOk()
->assertStructuredContent(fn (AssertableJson $j) => $j->where('state', 'scheduled')->where('fire_at', '2026-11-01')->etc());
}
public function test_update_and_delete_refuse_foreign_source_events(): void
{
$docs = $this->docsEvent($this->user);
HadoServer::tool(UpdateEvent::class, ['id' => $docs->id, 'title' => 'Хак'])->assertHasErrors(['источника docs']);
HadoServer::tool(DeleteEvent::class, ['id' => $docs->id])->assertHasErrors(['источника docs']);
$this->assertSame('Docs t', $docs->fresh()->payload['title']);
$this->assertSame(EventState::Today, $docs->fresh()->state);
}
public function test_delete_event_withdraws_own_event(): void
{
$id = $this->createOwn();
HadoServer::tool(DeleteEvent::class, ['id' => $id])->assertOk()
->assertStructuredContent(['id' => $id, 'state' => 'withdrawn']);
$this->assertSame(EventState::Withdrawn, Event::findOrFail($id)->state);
}
}