Add unit and feature tests for ticket processing and article management

- Implement Fake repositories and services for testing purposes.
- Create tests for Article API including creation, validation, and listing.
- Develop ProcessTicketJobFlowTest to validate ticket processing logic.
- Add QuickReplyAdminTest for creating and updating quick replies.
- Implement TicketAndArticleModelTest to ensure proper cascading deletes and credential encryption.
- Create TicketIngestionTest for ticket creation and job dispatching.
- Add TicketShowPageTest to verify rendering of quick replies and tool calls.
- Implement unit tests for ClassifierPromptBuilder, EmbeddingService, LlmJsonDecoder, QuickReplyResolver, SupportReplyService, TicketResultPayloadBuilder, TicketToolCallService, and ToolCallRequestValidator.
This commit is contained in:
SitiWeb
2026-04-30 02:10:15 +02:00
parent 39bdba2dfb
commit c94d3f85e8
36 changed files with 7445 additions and 467 deletions

View File

@@ -0,0 +1,47 @@
<?php
namespace Tests\Feature;
use App\Models\Article;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ArticleApiTest extends TestCase
{
use RefreshDatabase;
public function test_it_creates_article_with_note_and_allowed_actions(): void
{
$response = $this->postJson('/api/articles', [
'title' => 'DNS instellen',
'content' => 'Stap 1...',
'note' => 'Alleen voor DNS vragen',
'allowed_actions' => ['domain_inf'],
]);
$response->assertStatus(201);
$article = Article::query()->first();
$this->assertNotNull($article);
$this->assertSame('Alleen voor DNS vragen', $article->note);
$this->assertSame(['domain_inf'], $article->allowed_actions);
}
public function test_it_rejects_invalid_allowed_action(): void
{
$response = $this->postJson('/api/articles', [
'title' => 'DNS instellen',
'content' => 'Stap 1...',
'allowed_actions' => ['invalid_action'],
]);
$response->assertStatus(422);
}
public function test_it_lists_articles(): void
{
Article::query()->create(['title' => 'A', 'content' => 'B']);
$response = $this->getJson('/api/articles');
$response->assertOk();
}
}

View File

@@ -0,0 +1,216 @@
<?php
namespace Tests\Feature;
use App\Jobs\ProcessTicketJob;
use App\Models\Article;
use App\Models\QuickReply;
use App\Models\Ticket;
use App\Services\EmbeddingService;
use App\Services\KnowledgeGapService;
use App\Services\QuickReplyResolver;
use App\Services\SemanticSearchService;
use App\Services\SupportReplyService;
use App\Services\TicketNormalizationService;
use App\Services\TicketProcessingLoggerService;
use App\Services\TicketResultPayloadBuilder;
use App\Services\Tools\TicketToolCallService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProcessTicketJobFlowTest extends TestCase
{
use RefreshDatabase;
public function test_it_uses_quick_reply_and_skips_tool_call(): void
{
$article = Article::query()->create(['title' => 'DNS', 'content' => 'stappen']);
$quickReply = QuickReply::query()->create(['title' => 'DNS Quick', 'content' => 'Gebruik DNS quick antwoord', 'is_active' => true]);
$article->quickReplies()->sync([$quickReply->id]);
$ticket = Ticket::query()->create(['message' => 'DNS vraag', 'status' => 'queued']);
$embedding = $this->createMock(EmbeddingService::class);
$embedding->method('embed')->willReturn(array_fill(0, 768, 0.1));
$embedding->method('context')->willReturn(['provider_instance_id' => 'p1', 'embedding_model' => 'm1']);
$semantic = $this->createMock(SemanticSearchService::class);
$semantic->method('findBestArticle')->willReturn([
'best_article' => $article,
'confidence' => 0.9,
'explanation' => 'match',
'top_3_candidates' => [],
'top_5_candidates' => [],
'retrieval_meta' => [],
'requested_tool_call' => ['action' => 'domain_inf', 'parameters' => ['sld' => 'example', 'tld' => 'nl']],
'classifier_raw_response' => ['mode' => 'llm'],
]);
$normalizer = $this->createMock(TicketNormalizationService::class);
$normalizer->method('normalize')->willReturn([
'normalized_message' => 'dns vraag',
'redaction_report' => ['language' => 'nl'],
]);
$logger = app(TicketProcessingLoggerService::class);
$knowledgeGap = $this->createMock(KnowledgeGapService::class);
$knowledgeGap->method('shouldCreateDraft')->willReturn(false);
$toolCallService = $this->createMock(TicketToolCallService::class);
$toolCallService->expects($this->never())->method('executeRequestedTool');
$quickReplyResolver = $this->createMock(QuickReplyResolver::class);
$quickReplyResolver->method('resolveForArticle')->willReturn($quickReply);
$supportReply = $this->createMock(SupportReplyService::class);
$supportReply->expects($this->never())->method('build');
$job = new ProcessTicketJob($ticket->id);
$job->handle(
$embedding,
$semantic,
$normalizer,
$logger,
$knowledgeGap,
$toolCallService,
$quickReplyResolver,
$supportReply,
new TicketResultPayloadBuilder
);
$ticket->refresh();
$this->assertSame('completed', $ticket->status);
$this->assertSame('Gebruik DNS quick antwoord', $ticket->support_reply);
$this->assertSame($quickReply->id, $ticket->result_payload['quick_reply']['id']);
}
public function test_it_executes_tool_call_when_no_quick_reply_exists(): void
{
$article = Article::query()->create(['title' => 'DNS', 'content' => 'stappen', 'allowed_actions' => ['domain_inf']]);
$ticket = Ticket::query()->create([
'message' => 'DNS vraag',
'status' => 'queued',
'api_credentials' => ['apiuser' => 'u', 'apipassword' => 'p'],
]);
$embedding = $this->createMock(EmbeddingService::class);
$embedding->method('embed')->willReturn(array_fill(0, 768, 0.1));
$embedding->method('context')->willReturn(['provider_instance_id' => 'p1', 'embedding_model' => 'm1']);
$semantic = $this->createMock(SemanticSearchService::class);
$semantic->method('findBestArticle')->willReturn([
'best_article' => $article,
'confidence' => 0.9,
'explanation' => 'match',
'top_3_candidates' => [],
'top_5_candidates' => [],
'retrieval_meta' => [],
'requested_tool_call' => ['action' => 'domain_inf', 'parameters' => ['sld' => 'example', 'tld' => 'nl']],
'classifier_raw_response' => ['mode' => 'llm'],
]);
$normalizer = $this->createMock(TicketNormalizationService::class);
$normalizer->method('normalize')->willReturn([
'normalized_message' => 'dns vraag',
'redaction_report' => ['language' => 'nl'],
]);
$knowledgeGap = $this->createMock(KnowledgeGapService::class);
$knowledgeGap->method('shouldCreateDraft')->willReturn(false);
$quickReplyResolver = $this->createMock(QuickReplyResolver::class);
$quickReplyResolver->method('resolveForArticle')->willReturn(null);
$toolRecord = new \App\Models\TicketToolCall([
'action' => 'domain_inf',
'status' => 'success',
'parameters' => ['sld' => 'example', 'tld' => 'nl'],
]);
$toolCallService = $this->createMock(TicketToolCallService::class);
$toolCallService->method('executeRequestedTool')->willReturn($toolRecord);
$supportReply = $this->createMock(SupportReplyService::class);
$supportReply->method('build')->willReturn('1. Doe stap 1');
$job = new ProcessTicketJob($ticket->id);
$job->handle(
$embedding,
$semantic,
$normalizer,
app(TicketProcessingLoggerService::class),
$knowledgeGap,
$toolCallService,
$quickReplyResolver,
$supportReply,
new TicketResultPayloadBuilder
);
$ticket->refresh();
$this->assertSame('completed', $ticket->status);
$this->assertSame('1. Doe stap 1', $ticket->support_reply);
$this->assertSame('domain_inf', $ticket->result_payload['requested_tool_call']['action']);
}
public function test_it_marks_knowledge_gap_and_skips_customer_reply(): void
{
$article = Article::query()->create(['title' => 'DNS', 'content' => 'stappen']);
$ticket = Ticket::query()->create(['message' => 'DNS vraag', 'status' => 'queued']);
$embedding = $this->createMock(EmbeddingService::class);
$embedding->method('embed')->willReturn(array_fill(0, 768, 0.1));
$embedding->method('context')->willReturn(['provider_instance_id' => 'p1', 'embedding_model' => 'm1']);
$semantic = $this->createMock(SemanticSearchService::class);
$semantic->method('findBestArticle')->willReturn([
'best_article' => $article,
'confidence' => 0.2,
'explanation' => 'low confidence',
'top_3_candidates' => [],
'top_5_candidates' => [],
'retrieval_meta' => [],
'requested_tool_call' => null,
'classifier_raw_response' => ['mode' => 'llm'],
]);
$normalizer = $this->createMock(TicketNormalizationService::class);
$normalizer->method('normalize')->willReturn([
'normalized_message' => 'dns vraag',
'redaction_report' => ['language' => 'nl'],
]);
$knowledgeGap = $this->createMock(KnowledgeGapService::class);
$knowledgeGap->method('shouldCreateDraft')->willReturn(true);
$knowledgeGap->method('suggestArticleDraft')->willReturn([
'title' => 'Nieuwe DNS handleiding',
'content' => 'Nog aan te vullen',
]);
$toolCallService = $this->createMock(TicketToolCallService::class);
$toolCallService->expects($this->never())->method('executeRequestedTool');
$quickReplyResolver = $this->createMock(QuickReplyResolver::class);
$quickReplyResolver->expects($this->never())->method('resolveForArticle');
$supportReply = $this->createMock(SupportReplyService::class);
$supportReply->expects($this->never())->method('build');
$job = new ProcessTicketJob($ticket->id);
$job->handle(
$embedding,
$semantic,
$normalizer,
app(TicketProcessingLoggerService::class),
$knowledgeGap,
$toolCallService,
$quickReplyResolver,
$supportReply,
new TicketResultPayloadBuilder
);
$ticket->refresh();
$this->assertTrue($ticket->needs_article_draft);
$this->assertNull($ticket->support_reply);
$this->assertSame('Nieuwe DNS handleiding', $ticket->result_payload['draft_article_suggestion']['title']);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Tests\Feature;
use App\Livewire\Admin\QuickReplyManager;
use App\Models\QuickReply;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class QuickReplyAdminTest extends TestCase
{
use RefreshDatabase;
public function test_it_can_create_and_update_quick_reply(): void
{
Livewire::test(QuickReplyManager::class)
->set('title', 'DNS Basis')
->set('content', 'Gebruik deze stappen')
->set('isActive', true)
->call('save');
$reply = QuickReply::query()->first();
$this->assertNotNull($reply);
Livewire::test(QuickReplyManager::class)
->set("editRows.{$reply->id}.title", 'DNS Geupdated')
->set("editRows.{$reply->id}.content", 'Nieuwe content')
->set("editRows.{$reply->id}.is_active", false)
->call('updateQuickReply', $reply->id);
$reply->refresh();
$this->assertSame('DNS Geupdated', $reply->title);
$this->assertFalse($reply->is_active);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Tests\Feature;
use App\Models\Article;
use App\Models\QuickReply;
use App\Models\Ticket;
use App\Models\TicketToolCall;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class TicketAndArticleModelTest extends TestCase
{
use RefreshDatabase;
public function test_article_quick_reply_pivot_and_cascade_delete(): void
{
$article = Article::query()->create(['title' => 'DNS', 'content' => 'x']);
$reply = QuickReply::query()->create(['title' => 'Quick', 'content' => 'y', 'is_active' => true]);
$article->quickReplies()->sync([$reply->id]);
$this->assertCount(1, $article->quickReplies);
$article->delete();
$pivotCount = DB::table('article_quick_reply')->count();
$this->assertSame(0, $pivotCount);
}
public function test_ticket_credentials_are_stored_encrypted_and_decrypted_via_cast(): void
{
$ticket = Ticket::query()->create([
'message' => 'vraag',
'api_credentials' => ['apiuser' => 'demo', 'apipassword' => 'secret'],
]);
$this->assertSame('demo', $ticket->api_credentials['apiuser']);
$raw = DB::table('tickets')->where('id', $ticket->id)->value('api_credentials');
$this->assertIsString($raw);
$this->assertStringNotContainsString('secret', $raw);
}
public function test_ticket_tool_call_casts_arrays(): void
{
$ticket = Ticket::query()->create(['message' => 'vraag']);
$article = Article::query()->create(['title' => 'DNS', 'content' => 'x']);
$toolCall = TicketToolCall::query()->create([
'ticket_id' => $ticket->id,
'article_id' => $article->id,
'action' => 'domain_inf',
'status' => 'success',
'parameters' => ['sld' => 'example', 'tld' => 'nl'],
'response' => ['ok' => true],
]);
$toolCall->refresh();
$this->assertSame('example', $toolCall->parameters['sld']);
$this->assertTrue($toolCall->response['ok']);
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Tests\Feature;
use App\Jobs\ProcessTicketJob;
use App\Models\Ticket;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class TicketIngestionTest extends TestCase
{
use RefreshDatabase;
public function test_it_ingests_ticket_and_dispatches_processing_job(): void
{
Queue::fake();
$response = $this->postJson('/api/tickets', [
'message' => 'Mijn mail werkt niet',
'api_credentials' => [
'apiuser' => 'demo',
'apipassword' => 'secret',
],
]);
$response->assertStatus(202)
->assertJsonPath('status', 'queued');
$ticket = Ticket::query()->latest('id')->first();
$this->assertNotNull($ticket);
$this->assertSame('queued', $ticket->status);
$this->assertSame('demo', $ticket->api_credentials['apiuser']);
$raw = DB::table('tickets')->where('id', $ticket->id)->value('api_credentials');
$this->assertIsString($raw);
$this->assertStringNotContainsString('secret', $raw);
Queue::assertPushed(ProcessTicketJob::class);
}
public function test_it_validates_required_message(): void
{
$response = $this->postJson('/api/tickets', []);
$response->assertStatus(422);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Tests\Feature;
use App\Models\Article;
use App\Models\Ticket;
use App\Models\TicketToolCall;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TicketShowPageTest extends TestCase
{
use RefreshDatabase;
public function test_ticket_show_renders_quick_reply_and_tool_call_sections(): void
{
$article = Article::query()->create(['title' => 'DNS', 'content' => 'x']);
$ticket = Ticket::query()->create([
'message' => 'vraag',
'status' => 'completed',
'best_article_id' => $article->id,
'support_reply' => 'Gebruik deze stappen',
'result_payload' => [
'quick_reply' => ['id' => 1, 'title' => 'DNS Quick'],
'draft_article_suggestion' => null,
],
]);
TicketToolCall::query()->create([
'ticket_id' => $ticket->id,
'article_id' => $article->id,
'action' => 'domain_inf',
'status' => 'success',
'parameters' => ['sld' => 'example', 'tld' => 'nl'],
'response' => ['ok' => true],
]);
$response = $this->get("/admin/tickets/{$ticket->id}");
$response->assertOk();
$response->assertSee('Snelantwoord gebruikt', false);
$response->assertSee('Toolcalls', false);
}
}