174 lines
7.9 KiB
PHP
174 lines
7.9 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Process;
|
||
|
||
use App\Actions\CreateReservation;
|
||
use App\Communication\CommunicationRules;
|
||
use App\Communication\IncomingMessage;
|
||
use App\Communication\MessageDecision;
|
||
use App\Communication\MessageIntake;
|
||
use App\Enums\BookingTaskKind;
|
||
use App\Enums\Medium;
|
||
use App\Enums\MessageEffect;
|
||
use App\Enums\MessageIntent;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Models\InboundMessage;
|
||
use App\Models\Property;
|
||
use App\Pricing\StayRequest;
|
||
use App\Support\Settings;
|
||
use Carbon\CarbonImmutable;
|
||
use Database\Seeders\PropertySeeder;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use PHPUnit\Framework\Attributes\Group;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* V2.1 cases E and F (docs/Prozessarchitektur/V2.1/02 §6): medium, intent and effect are separate. WhatsApp and chat are
|
||
* test cases of the architecture (no provider): an assigned reply keeps its origin, a question without a case causes no
|
||
* case, everything unclear or contradictory goes to review.
|
||
*/
|
||
#[Group('module:communication.medium')]
|
||
class MessageMediumIntentTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private Property $property;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
|
||
$this->seed(PropertySeeder::class);
|
||
$this->property = Property::sole();
|
||
Mail::fake();
|
||
config(['communication.media.whatsapp.active' => true, 'communication.media.chat.active' => true]);
|
||
}
|
||
|
||
/** Case E: a reply by WhatsApp with a verified reference reaches the case, origin preserved, nothing taken over by itself. */
|
||
public function test_case_e_a_whatsapp_reply_with_verified_reference_reports_to_the_case_and_keeps_its_origin(): void
|
||
{
|
||
$booking = $this->request();
|
||
|
||
$decision = $this->receive(Medium::WhatsApp, 'Wir kommen gegen 16 Uhr an, Kennzeichen HRO-AB 123', verified: $booking->id, consent: true);
|
||
|
||
$this->assertSame(MessageIntent::ProvideData, $decision->intent);
|
||
$this->assertTrue($decision->booking->is($booking));
|
||
$this->assertTrue($decision->has(MessageEffect::ContributeFacts));
|
||
$this->assertTrue($decision->has(MessageEffect::ReportToCase));
|
||
$message = InboundMessage::query()->sole();
|
||
$this->assertSame('whatsapp', $message->medium);
|
||
$this->assertSame($booking->id, $message->booking_id);
|
||
$this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::InboundReview)->count());
|
||
$this->assertSame($booking->status, $booking->fresh()->status, 'the message changes no booking status');
|
||
$this->assertSame(1, Booking::query()->count());
|
||
}
|
||
|
||
public function test_the_same_message_twice_is_stored_and_decided_once(): void
|
||
{
|
||
$booking = $this->request();
|
||
|
||
$this->receive(Medium::Chat, 'Ankunft 15 Uhr', verified: $booking->id, id: 'c-1');
|
||
$again = $this->receive(Medium::Chat, 'Ankunft 15 Uhr', verified: $booking->id, id: 'c-1');
|
||
|
||
$this->assertSame(1, InboundMessage::query()->count());
|
||
$this->assertSame(1, BookingTask::query()->where('kind', BookingTaskKind::InboundReview)->count());
|
||
$this->assertTrue($again->booking->is($booking));
|
||
}
|
||
|
||
/** Case F: an inquiry without a case via chat answers or hands off – and never creates a case. */
|
||
public function test_case_f_a_general_question_creates_no_case_and_only_allows_information(): void
|
||
{
|
||
$decision = $this->receive(Medium::Chat, 'Sind Hunde erlaubt und gibt es einen Parkplatz?');
|
||
|
||
$this->assertSame(MessageIntent::GeneralQuestion, $decision->intent);
|
||
$this->assertSame([MessageEffect::AnswerInformation], $decision->effects);
|
||
$this->assertNull($decision->booking);
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$this->assertSame(0, BookingTask::query()->count());
|
||
}
|
||
|
||
public function test_a_stay_inquiry_via_whatsapp_refers_to_the_inquiry_path_and_books_nothing(): void
|
||
{
|
||
$decision = $this->receive(Medium::WhatsApp, 'Ist die Wohnung vom 12.12. bis 19.12. frei? Wir würden gern buchen.', consent: true);
|
||
|
||
$this->assertSame(MessageIntent::StayInquiry, $decision->intent);
|
||
$this->assertSame([MessageEffect::HandoffInquiry], $decision->effects);
|
||
$this->assertSame(0, Booking::query()->count());
|
||
}
|
||
|
||
public function test_same_sender_or_name_alone_never_assigns_a_case(): void
|
||
{
|
||
$booking = $this->request();
|
||
|
||
$decision = $this->receive(Medium::WhatsApp, 'Hier Gesa Gast, wir kommen um 16 Uhr an.', sender: 'gesa@example.org', consent: true);
|
||
|
||
$this->assertNull($decision->booking);
|
||
$this->assertSame([MessageEffect::Review], $decision->effects);
|
||
$this->assertSame(InboundMessage::NEEDS_REVIEW, InboundMessage::query()->sole()->status);
|
||
$this->assertSame(0, BookingTask::query()->where('booking_id', $booking->id)->count());
|
||
}
|
||
|
||
public function test_a_contradicting_case_number_in_the_text_sends_the_message_to_review(): void
|
||
{
|
||
$booking = $this->request();
|
||
|
||
$decision = $this->receive(Medium::Chat, 'Zu Vorgang 2099-0001: Ankunft 17 Uhr', verified: $booking->id);
|
||
|
||
$this->assertNull($decision->booking);
|
||
$this->assertSame([MessageEffect::Review], $decision->effects);
|
||
$this->assertStringContainsString('widersprüchlich', implode(' ', $decision->reasons));
|
||
}
|
||
|
||
public function test_an_unclear_message_goes_to_review(): void
|
||
{
|
||
$decision = $this->receive(Medium::Chat, 'ok danke');
|
||
|
||
$this->assertSame(MessageIntent::Unclear, $decision->intent);
|
||
$this->assertSame([MessageEffect::Review], $decision->effects);
|
||
}
|
||
|
||
public function test_a_medium_without_consent_or_switched_off_only_reaches_review(): void
|
||
{
|
||
$booking = $this->request();
|
||
|
||
$noConsent = $this->receive(Medium::WhatsApp, 'Ankunft 16 Uhr', verified: $booking->id, consent: false, id: 'w-1');
|
||
config(['communication.media.chat.active' => false]);
|
||
$off = $this->receive(Medium::Chat, 'Ankunft 16 Uhr', verified: $booking->id, id: 'c-9');
|
||
|
||
$this->assertSame([MessageEffect::Review], $noConsent->effects);
|
||
$this->assertSame([MessageEffect::Review], $off->effects);
|
||
$this->assertSame(0, BookingTask::query()->where('booking_id', $booking->id)->count());
|
||
}
|
||
|
||
public function test_the_landlord_may_only_narrow_never_widen_the_rules(): void
|
||
{
|
||
$rules = app(CommunicationRules::class);
|
||
Settings::tenant()->update(['communication_rules' => [
|
||
'media' => ['chat' => ['active' => false], 'email' => ['active' => true]],
|
||
'effects' => ['whatsapp' => ['provide_data' => ['contribute_facts']]],
|
||
]]);
|
||
|
||
$this->assertFalse($rules->mediumActive(Medium::Chat));
|
||
$this->assertTrue($rules->mediumActive(Medium::WhatsApp));
|
||
$this->assertSame([MessageEffect::ReportToCase, MessageEffect::Review], $rules->effects(Medium::WhatsApp, MessageIntent::ProvideData));
|
||
|
||
// Widening is ignored: the ceiling keeps WhatsApp off even when the tenant says "active".
|
||
config(['communication.media.whatsapp.active' => false]);
|
||
Settings::tenant()->update(['communication_rules' => ['media' => ['whatsapp' => ['active' => true]]]]);
|
||
$this->assertFalse($rules->mediumActive(Medium::WhatsApp));
|
||
}
|
||
|
||
private function request(): Booking
|
||
{
|
||
return app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org'])->fresh();
|
||
}
|
||
|
||
private function receive(Medium $medium, string $text, ?int $verified = null, bool $consent = false, string $sender = '+491701234567', string $id = 'm-1'): MessageDecision
|
||
{
|
||
return app(MessageIntake::class)->handle(new IncomingMessage($medium, $sender, $text, CarbonImmutable::now(), $id, $verified, $consent));
|
||
}
|
||
}
|