From 73c0a716b9e5ff11b26d9f42cb5b9863737fc9ed Mon Sep 17 00:00:00 2001 From: Lars Date: Tue, 3 Nov 2026 13:55:35 +0100 Subject: [PATCH] v0.11: durchgehender Integrationstest des MVP (Anfrage bis Buchung, Portalbuchung) Co-Authored-By: Claude Opus 5.5 --- tests/Feature/V011EndToEndTest.php | 147 +++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tests/Feature/V011EndToEndTest.php diff --git a/tests/Feature/V011EndToEndTest.php b/tests/Feature/V011EndToEndTest.php new file mode 100644 index 0000000..a078a84 --- /dev/null +++ b/tests/Feature/V011EndToEndTest.php @@ -0,0 +1,147 @@ +travelTo(CarbonImmutable::parse('2026-10-22 10:00')); + $this->seed([PropertySeeder::class, SiteSeeder::class]); + $this->property = Property::sole(); + Storage::fake('local'); + Content::save(['contract.terms' => '

Echte Bedingungen

', 'stay.cancellation' => '

Echtes Storno

']); + config(['mail.default' => 'array', 'fewo.spam.min_seconds' => 0, 'fewo.ai.api_key' => 'test-key']); + Mail::swap(new MailManager($this->app)); + Settings::tenant()->update([ + 'bank_iban' => 'DE02120300000000202051', + 'test_recipient' => 'test@example.org', + 'process_defaults' => ['processing' => 'automatic', 'data_request_mode' => 'automatic'], + ]); + Mailbox::query()->create(['tenant_id' => Settings::tenant()->id, 'host' => 'imap.example.org', 'username' => 'anfragen@example.org', 'password' => 'x', 'mode' => Mailbox::FULL, 'address' => 'anfragen@example.org', 'is_active' => true]); + TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true]); + app(TenantFeatures::class)->forget(); + } + + public function test_from_an_incomplete_website_request_to_the_binding_booking(): void + { + // 1. The guest asks on the website without an address. + Livewire::test(BookingRequest::class) + ->call('selectDate', '2026-11-07')->call('selectDate', '2026-11-10') + ->set('firstName', 'Gesa')->set('lastName', 'Gast')->set('email', 'gesa@example.org')->set('privacyAccepted', true) + ->call('submit')->assertHasNoErrors(); + $booking = Booking::query()->sole(); + + // 2. Clarification hold of the automatic mode, a data request went out (test operation: only to the test address). + $this->assertSame([BookingStatus::Reserved, Booking::HOLD_CLARIFICATION], [$booking->status, $booking->hold_purpose]); + $this->assertTrue($booking->hold_expires_at->equalTo(now()->addMinutes(15))); + $request = $booking->openDataRequest(); + $this->assertSame(DataRequest::OPEN, $request->status); + $question = BookingMessage::query()->where('booking_id', $booking->id)->where('subject', 'like', '%Bitte ergänzen%')->sole(); + $this->assertSame('test', $question->audience, 'test operation: not delivered to the guest'); + + // 3. The guest answers by e-mail; the inbox reads it, assigns it by the reply reference and stores it once. + $reply = new PortalMail('', 'test@example.org', 'Re: '.$question->subject, "Hallo,\nunsere Anschrift: Lindenallee 12, 10115 Berlin, Deutschland.\nGesa", CarbonImmutable::now(), inReplyTo: $question->message_id); + $reader = Mockery::mock(MailboxReader::class); + $reader->shouldReceive('fetchAll')->andReturn([$reply]); + $this->app->instance(MailboxReader::class, $reader); + $this->artisan('channels:fetch-mail')->assertSuccessful(); + $this->artisan('channels:fetch-mail')->assertSuccessful(); + + $message = InboundMessage::query()->sole(); + $this->assertSame([InboundMessage::ASSIGNED, $booking->id, 'reply', true], [$message->status, $message->booking_id, $message->match_reason, $message->test_mode]); + $this->assertNull($request->fresh()->next_reminder_at, 'the answer stops the reminder'); + + // 4. Extraction (faked provider) → rules → fact intake → the existing process sends the offer. + $this->fakeExtraction([ + ['field' => 'guest.street', 'value' => 'Lindenallee 12', 'evidence' => 'Lindenallee 12', 'confidence' => 0.97], + ['field' => 'guest.postal_code', 'value' => '10115', 'evidence' => '10115 Berlin', 'confidence' => 0.97], + ['field' => 'guest.city', 'value' => 'Berlin', 'evidence' => '10115 Berlin', 'confidence' => 0.97], + ]); + $this->artisan('messages:extract')->assertSuccessful(); + + $booking->refresh(); + $this->assertSame('Lindenallee 12', $booking->guest->street); + $this->assertSame('DE', $booking->guest->country, 'confirmed with the website form'); + $this->assertSame(3, CaseFact::query()->where('booking_id', $booking->id)->where('outcome', CaseFact::APPLIED)->where('source', 'llm')->count()); + $this->assertSame([BookingStatus::OfferSent, Booking::HOLD_OFFER], [$booking->status, $booking->hold_purpose]); + $this->assertTrue($booking->hold_expires_at->equalTo(now()->addHours(48)), 'the offer hold is the unchanged one'); + $this->assertSame(DataRequest::COMPLETED, $request->fresh()->status); + + // 5. The guest accepts the offer → binding booking. + $this->post(URL::temporarySignedRoute('offers.accept', $booking->hold_expires_at, ['booking' => $booking, 'v' => $booking->offer_version]), ['terms' => '1'])->assertRedirect(); + $this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status); + $this->assertSame(0, BookingMessage::query()->where('audience', 'guest')->count(), 'nothing reached a guest in test operation'); + } + + /** E-036 / M12: a booking binding on the portal stays binding without guest data and gets no clarification. */ + public function test_a_binding_portal_booking_stays_binding_and_is_only_completed(): void + { + $portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'portal_booking', 'settings' => ['email_senders' => 'fewo-direkt.de']]); + + $booking = app(TakeOverExternalBooking::class)->handle($this->property, $portal, CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-08'), ['portal_reference' => 'HA-7']); + + $this->assertSame(BookingStatus::Confirmed, $booking->status); + $this->assertNull($booking->guest_id); + $this->assertNull($booking->openDataRequest(), 'no way to the guest yet: nothing is sent'); + $this->assertSame('none', $booking->clarificationState()['state']); + $this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status); + } + + /** + * @param list> $facts + */ + private function fakeExtraction(array $facts): void + { + Http::fake(fn (Request $request) => Http::response([ + 'id' => 'msg', 'type' => 'message', 'role' => 'assistant', 'model' => $request->data()['model'], + 'content' => [['type' => 'text', 'text' => json_encode(['facts' => $facts, 'change_requests' => [], 'intent' => 'answer', 'uncertainties' => []])]], + 'stop_reason' => 'end_turn', 'usage' => ['input_tokens' => 10, 'output_tokens' => 10], + ])); + } +}