> */ private array $calls = []; protected function setUp(): void { parent::setUp(); $this->travelTo(CarbonImmutable::parse('2026-10-22 10:00')); $this->seed([PropertySeeder::class, SiteSeeder::class]); $this->property = Property::sole(); Mail::fake(); Storage::fake('local'); Content::save(['contract.terms' => '
Echte Bedingungen
', 'stay.cancellation' => 'Echtes Storno
']); Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051']); TenantService::query()->create(['tenant_id' => Settings::tenant()->id, 'service' => TenantServiceKey::AiGuestMail->value, 'is_enabled' => true, 'api_key' => 'test-key', 'settings' => ['provider' => 'anthropic', 'small_model' => 'claude-haiku-5-5', 'review_model' => 'claude-opus-5-5']]); app(TenantFeatures::class)->forget(); } public function test_without_the_service_nothing_is_sent_to_a_provider(): void { TenantService::query()->update(['is_enabled' => false]); app(TenantFeatures::class)->forget(); Http::fake(); $this->assertSame('disabled', app(ExtractMessageFacts::class)->handle($this->reply())); Http::assertNothingSent(); } public function test_confident_checked_details_are_taken_over_through_the_fact_intake_without_review(): void { Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'data_request_mode' => 'automatic']]); $this->fakeModels(small: $this->extraction([ $this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12'), $this->fact('guest.postal_code', '10115', '10115 Berlin'), $this->fact('guest.city', 'Berlin', '10115 Berlin'), $this->fact('guest.country', 'DE', 'Lindenallee 12, 10115 Berlin', 0.9), $this->fact('guest.phone', '030 1234567', 'Telefon 030 1234567'), ])); $message = $this->reply(); $this->assertSame('done', app(ExtractMessageFacts::class)->handle($message)); $booking = $message->booking->fresh(); $this->assertSame('Lindenallee 12', $booking->guest->street); $this->assertSame(FactSource::Extraction, CaseFact::query()->where('field', 'guest.street')->where('outcome', CaseFact::APPLIED)->sole()->source); $this->assertSame(BookingStatus::OfferSent, $booking->status, 'complete now: the existing process sent the offer'); $this->assertCount(1, $this->calls, 'no review needed'); $this->assertSame(InboundMessage::PROCESSED, $message->fresh()->status); $this->assertSame(1, ServiceUsage::query()->count()); } /** Precision 2: a contradiction is reviewed even at high confidence and is never taken over automatically. */ public function test_a_contradiction_is_reviewed_regardless_of_the_confidence_and_only_proposed(): void { $this->fakeModels( small: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12', 0.99)]), review: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12', 0.99)]), ); $message = $this->reply(['street' => 'Alter Weg 2']); app(ExtractMessageFacts::class)->handle($message); $this->assertSame('Alter Weg 2', $message->booking->guest->fresh()->street); $this->assertCount(2, $this->calls, 'the review model was asked'); $this->assertSame(['weicht vom gespeicherten Wert ab'], $message->fresh()->extraction['proposed'][0]['problems']); } public function test_a_value_without_evidence_in_the_message_is_not_taken_over(): void { $this->fakeModels( small: $this->extraction([$this->fact('guest.street', 'Musterstraße 1', 'Musterstraße 1', 0.99)]), review: $this->extraction([$this->fact('guest.street', 'Musterstraße 1', 'Musterstraße 1', 0.99)]), ); $message = $this->reply(); app(ExtractMessageFacts::class)->handle($message); $this->assertNull($message->booking->guest->fresh()->street); $this->assertContains('Belegstelle nicht in der Nachricht', $message->fresh()->extraction['proposed'][0]['problems']); } public function test_two_models_reading_a_value_differently_leave_it_to_the_landlord(): void { $this->fakeModels( small: $this->extraction([$this->fact('guest.phone', '030 1234567', 'Telefon 030 1234567', 0.3)]), review: $this->extraction([$this->fact('guest.phone', '030 123456', 'Telefon 030 1234567', 0.95)]), ); $message = $this->reply(); app(ExtractMessageFacts::class)->handle($message); $this->assertNull($message->booking->guest->fresh()->phone); $this->assertContains('die beiden Modelle lesen den Wert unterschiedlich', $message->fresh()->extraction['proposed'][0]['problems']); } /** M06: a phone number is completed, a wish to stay longer only becomes a request. */ public function test_a_wish_to_change_the_stay_is_only_a_request(): void { $text = 'Unsere Nummer: 030 1234567. Können wir bis zum 11.11.2026 bleiben?'; $this->fakeModels( small: $this->extraction([$this->fact('guest.phone', '030 1234567', 'Unsere Nummer: 030 1234567')], [['field' => 'booking.departure', 'value' => '2026-11-11', 'evidence' => 'Können wir bis zum 11.11.2026 bleiben?']], 'change'), review: $this->extraction([$this->fact('guest.phone', '030 1234567', 'Unsere Nummer: 030 1234567')], [['field' => 'booking.departure', 'value' => '2026-11-11', 'evidence' => 'Können wir bis zum 11.11.2026 bleiben?']], 'change'), ); $message = $this->reply(text: $text); app(ExtractMessageFacts::class)->handle($message); $booking = $message->booking->fresh(); $this->assertSame('030 1234567', $booking->guest->phone); $this->assertSame('2026-11-10', $booking->departure->toDateString(), 'the stay is unchanged'); $this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ChangeRequest)->count()); } public function test_an_answer_that_does_not_fit_the_schema_or_a_refusal_waits_for_the_landlord(): void { $this->fakeModels(small: ['facts' => 'kaputt']); $broken = $this->reply(); $this->assertSame('failed', app(ExtractMessageFacts::class)->handle($broken)); $this->assertCount(2, $this->calls, 'one repetition'); $this->calls = []; Http::fake(['*' => Http::response(['content' => [], 'stop_reason' => 'refusal', 'model' => 'claude-haiku-5-5'])]); $refused = $this->reply(id: '<2@x>', email: 'zwei@example.org', arrival: '2026-11-20', departure: '2026-11-23'); $this->assertSame('failed', app(ExtractMessageFacts::class)->handle($refused)); $this->assertSame(1, BookingTask::query()->where('booking_id', $refused->booking_id)->where('kind', BookingTaskKind::InboundReview)->count(), 'the landlord still has the message to look at'); } public function test_the_request_uses_structured_outputs_and_the_review_model_has_the_refusal_fallback(): void { $this->fakeModels( small: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12', 0.3)]), review: $this->extraction([]), ); app(ExtractMessageFacts::class)->handle($this->reply()); [$small, $review] = $this->calls; $this->assertSame(['claude-haiku-5-5', 'json_schema'], [$small['body']['model'], $small['body']['output_config']['format']['type']]); $this->assertArrayNotHasKey('fallbacks', $small['body']); $this->assertSame(['claude-opus-5-5', 'default', 'server-side-fallback-2026-07-01'], [$review['body']['model'], $review['body']['fallbacks'], $review['beta']]); $this->assertSame('2026-10-21', $small['version']); $this->assertStringNotContainsString('{{', $small['body']['system']); $this->assertStringContainsString('Lindenallee 12', $review['body']['system'], 'the review sees the first extraction'); } public function test_the_landlord_takes_over_a_proposal_in_the_inbox(): void { $this->actingAs(User::factory()->create()); $this->fakeModels( small: $this->extraction([$this->fact('guest.city', 'Berlin', '10115 Berlin', 0.4)]), review: $this->extraction([$this->fact('guest.city', 'Berlin', '10115 Berlin', 0.6)]), ); $message = $this->reply(); app(ExtractMessageFacts::class)->handle($message); $this->assertNull($message->booking->guest->fresh()->city); Livewire::test(ListInboundMessages::class)->callAction(TestAction::make('applyProposals')->table($message->fresh()), ['fields' => ['guest.city']]); $this->assertSame('Berlin', $message->booking->guest->fresh()->city); $this->assertSame(FactSource::Landlord, CaseFact::query()->where('field', 'guest.city')->latest('id')->first()->source); } /** M05: wrong values and missed details are counted separately. */ public function test_the_evaluation_reports_missed_details(): void { $this->fakeModels(small: $this->extraction([$this->fact('guest.street', 'Lindenallee 12', 'Lindenallee 12')])); $this->artisan('ai:evaluate', ['tenant' => Settings::tenant()->id])->expectsOutputToContain('Übersehene Angaben:')->assertSuccessful(); } public function test_the_prompt_is_versioned_and_every_placeholder_is_filled(): void { $booking = $this->reply()->booking; $prompt = app(MessageExtractor::class)->prompt('', $booking); $this->assertSame('v1', MessageExtractor::promptVersion()); $this->assertStringContainsString('non-binding request', $prompt); $this->assertStringContainsString('guest.street', $prompt); $this->assertStringContainsString('guest.email=g', $prompt, 'known values are masked'); $this->assertStringNotContainsString('gesa@example.org', $prompt); } /** * @param array