File size: 27,245 Bytes
1501522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74acf19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1501522
74acf19
 
1501522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74acf19
1501522
 
 
 
 
 
 
 
 
 
74acf19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1501522
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<?php

namespace Tests\Feature;

use App\Models\User;
use App\Services\OpenAiChatService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ChatTest extends TestCase
{
    use RefreshDatabase;

    public function test_guest_redirected_from_chat_page(): void
    {
        $response = $this->get('/chat');

        $response->assertRedirect('/login');
    }

    public function test_authenticated_user_can_open_chat_page(): void
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->get('/chat');

        $response->assertRedirect('/tools');
    }

    public function test_authenticated_user_can_open_chat_assistant_page(): void
    {
        $user = User::factory()->create();
        config([
            'services.openai.chat_base_url' => 'https://router.huggingface.co/v1',
            'services.openai.chat_model' => 'CohereLabs/c4ai-command-r-08-2024:cohere',
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $response = $this->actingAs($user)->get('/chat/assistant');

        $response->assertOk();
        $response->assertSee('What do you want to create?');
        $response->assertSee('OpenAI');
        $response->assertSee('Create a mockup');
    }

    public function test_authenticated_user_can_open_chat_assistant_page_with_deepseek_label(): void
    {
        $user = User::factory()->create();
        config([
            'services.openai.assistant_provider' => 'deepseek',
            'services.openai.assistant_model' => 'deepseek-chat',
            'services.deepseek.base_url' => 'https://api.deepseek.com',
        ]);

        $response = $this->actingAs($user)->get('/chat/assistant');

        $response->assertOk();
        $response->assertSee('DeepSeek');
        $response->assertSee('deepseek-chat');
    }

    public function test_user_can_send_message_and_get_ai_reply(): void
    {
        $this->app->instance(OpenAiChatService::class, new class extends OpenAiChatService {
            public function reply(array $messages): string
            {
                return 'Ini balasan dari AI (mock).';
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat', [
            'prompt' => 'Halo AI',
        ]);

        $response->assertRedirect('/tools');

        $conversation = $user->chatConversations()->with('messages')->first();

        $this->assertNotNull($conversation);
        $this->assertCount(2, $conversation->messages);
        $this->assertSame('user', $conversation->messages[0]->role);
        $this->assertSame('Halo AI', $conversation->messages[0]->content);
        $this->assertSame('assistant', $conversation->messages[1]->role);
        $this->assertSame('Ini balasan dari AI (mock).', $conversation->messages[1]->content);
    }

    public function test_user_can_send_message_from_assistant_page_and_get_ai_reply(): void
    {
        config([
            'services.openai.base_url' => 'https://api.openai.com/v1',
            'services.openai.chat_base_url' => 'https://router.huggingface.co/v1',
            'services.openai.chat_model' => 'CohereLabs/c4ai-command-r-08-2024:cohere',
            'services.openai.assistant_provider' => 'openai',
            'services.openai.assistant_id' => 'asst_medilis_123',
            'services.openai.assistant_use_hosted_threads' => true,
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $capture = new \stdClass();
        $capture->threadIds = [];
        $capture->prompts = [];
        $capture->instructions = [];

        $this->app->instance(OpenAiChatService::class, new class($capture) extends OpenAiChatService {
            public function __construct(private \stdClass $capture)
            {
            }

            public function assistantReply(
                string $prompt,
                ?string $threadId = null,
                ?string $additionalInstructions = null,
                ?string $model = null
            ): array {
                \PHPUnit\Framework\Assert::assertSame('https://api.openai.com/v1', rtrim((string) config('services.openai.chat_base_url'), '/'));
                \PHPUnit\Framework\Assert::assertSame('gpt-4o', (string) $model);

                $this->capture->threadIds[] = $threadId;
                $this->capture->prompts[] = $prompt;
                $this->capture->instructions[] = $additionalInstructions;

                return [
                    'thread_id' => $threadId ?: 'thread_medilis_001',
                    'content' => 'Struktur landing page klinik: hero premium, daftar layanan unggulan, social proof, FAQ singkat, lalu CTA booking yang jelas.',
                ];
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'Buatkan konsep landing page klinik.',
        ]);

        $response->assertRedirect('/chat/assistant');

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertCount(2, $conversation->messages);
        $this->assertSame('thread_medilis_001', $conversation->openai_thread_id);
        $this->assertSame('user', $conversation->messages[0]->role);
        $this->assertSame('Buatkan konsep landing page klinik.', $conversation->messages[0]->content);
        $this->assertSame('assistant', $conversation->messages[1]->role);
        $this->assertSame('Struktur landing page klinik: hero premium, daftar layanan unggulan, social proof, FAQ singkat, lalu CTA booking yang jelas.', $conversation->messages[1]->content);
        $this->assertSame([null], $capture->threadIds);
        $this->assertStringContainsString('Buatkan konsep landing page klinik.', $capture->prompts[0]);
        $this->assertStringContainsString('jangan jawab dengan template generik', (string) $capture->instructions[0]);
    }

    public function test_assistant_shows_limit_message_when_daily_quota_is_exhausted(): void
    {
        config([
            'services.openai.daily_message_limit' => 1,
        ]);

        $this->app->instance(OpenAiChatService::class, new class extends OpenAiChatService {
            public function reply(array $messages): string
            {
                throw new \RuntimeException('reply seharusnya tidak dipanggil ketika limit harian habis.');
            }
        });

        $user = User::factory()->create();
        $conversation = $user->chatConversations()->create([
            'title' => 'Assistant',
        ]);

        for ($i = 1; $i <= 5; $i++) {
            $conversation->messages()->create([
                'role' => 'user',
                'content' => "Prompt ke-{$i} hari ini",
            ]);
        }

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'Prompt kedua yang harus kena limit',
        ]);

        $response->assertRedirect('/chat/assistant');
        $response->assertSessionHas('chat_error');

        $latestAssistant = $conversation->fresh('messages')->messages
            ->where('role', 'assistant')
            ->last();

        $this->assertNotNull($latestAssistant);
        $this->assertStringContainsString('Batas harian chat (5) sudah tercapai', (string) $latestAssistant->content);
    }

    public function test_assistant_route_reuses_saved_openai_thread_id_for_follow_up_messages(): void
    {
        config([
            'services.openai.base_url' => 'https://api.openai.com/v1',
            'services.openai.assistant_provider' => 'openai',
            'services.openai.assistant_id' => 'asst_medilis_123',
            'services.openai.assistant_use_hosted_threads' => true,
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $capture = new \stdClass();
        $capture->threadIds = [];

        $this->app->instance(OpenAiChatService::class, new class($capture) extends OpenAiChatService {
            public function __construct(private \stdClass $capture)
            {
            }

            public function assistantReply(
                string $prompt,
                ?string $threadId = null,
                ?string $additionalInstructions = null,
                ?string $model = null
            ): array {
                $this->capture->threadIds[] = $threadId;

                return [
                    'thread_id' => $threadId ?: 'thread_medilis_001',
                    'content' => 'Tiga CTA klinik gigi: booking konsultasi, cek slot dokter minggu ini, dan mulai treatment estetik dari sesi awal.',
                ];
            }
        });

        $user = User::factory()->create();

        $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'Buat 3 CTA untuk klinik gigi.',
        ])->assertRedirect('/chat/assistant');

        $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'jangan template',
        ])->assertRedirect('/chat/assistant');

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertSame('thread_medilis_001', $conversation->openai_thread_id);
        $this->assertSame([null, 'thread_medilis_001'], $capture->threadIds);
    }

    public function test_assistant_defaults_to_direct_openai_chat_even_if_assistant_id_exists(): void
    {
        config([
            'services.openai.base_url' => 'https://api.openai.com/v1',
            'services.openai.chat_base_url' => 'https://router.huggingface.co/v1',
            'services.openai.chat_model' => 'CohereLabs/c4ai-command-r-08-2024:cohere',
            'services.openai.assistant_provider' => 'openai',
            'services.openai.assistant_id' => 'asst_medilis_123',
            'services.openai.assistant_use_hosted_threads' => false,
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $this->app->instance(OpenAiChatService::class, new class extends OpenAiChatService {
            public function assistantReply(
                string $prompt,
                ?string $threadId = null,
                ?string $additionalInstructions = null,
                ?string $model = null
            ): array {
                throw new \RuntimeException('assistantReply seharusnya tidak dipakai saat hosted threads dimatikan.');
            }

            public function reply(array $messages): string
            {
                \PHPUnit\Framework\Assert::assertSame('https://api.openai.com/v1', rtrim((string) config('services.openai.chat_base_url'), '/'));
                \PHPUnit\Framework\Assert::assertSame('gpt-4o', (string) config('services.openai.chat_model'));

                return 'Ini jawaban OpenAI direct chat yang cukup spesifik, terstruktur, dan tidak berupa template bantuan umum.';
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'cek struktur halaman klinik ini',
        ]);

        $response->assertRedirect('/chat/assistant');

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertNull($conversation->openai_thread_id);
        $this->assertSame('Ini jawaban OpenAI direct chat yang cukup spesifik, terstruktur, dan tidak berupa template bantuan umum.', $conversation->messages->last()->content);
    }

    public function test_assistant_can_use_deepseek_direct_chat(): void
    {
        config([
            'services.openai.assistant_provider' => 'deepseek',
            'services.openai.assistant_model' => 'deepseek-chat',
            'services.deepseek.base_url' => 'https://api.deepseek.com',
            'services.deepseek.api_key' => 'deepseek_test_key',
            'services.openai.assistant_use_hosted_threads' => false,
        ]);

        $this->app->instance(OpenAiChatService::class, new class extends OpenAiChatService {
            public function assistantReply(
                string $prompt,
                ?string $threadId = null,
                ?string $additionalInstructions = null,
                ?string $model = null
            ): array {
                throw new \RuntimeException('assistantReply tidak boleh dipakai untuk DeepSeek direct chat.');
            }

            public function reply(array $messages): string
            {
                \PHPUnit\Framework\Assert::assertSame('https://api.deepseek.com', rtrim((string) config('services.openai.chat_base_url'), '/'));
                \PHPUnit\Framework\Assert::assertSame('deepseek-chat', (string) config('services.openai.chat_model'));
                \PHPUnit\Framework\Assert::assertSame('deepseek_test_key', (string) config('services.openai.chat_api_key'));

                return 'Aku belum bisa buka URL itu langsung, tapi aku bisa bantu audit struktur, copy, dan flow fitur dari data yang kamu kirim.';
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'cek halaman klinik ini',
        ]);

        $response->assertRedirect('/chat/assistant');

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertNull($conversation->openai_thread_id);
        $this->assertSame('Aku belum bisa buka URL itu langsung, tapi aku bisa bantu audit struktur, copy, dan flow fitur dari data yang kamu kirim.', $conversation->messages->last()->content);
    }

    public function test_assistant_direct_chat_retries_when_first_reply_is_generic_low_signal(): void
    {
        config([
            'services.openai.assistant_provider' => 'deepseek',
            'services.openai.assistant_model' => 'deepseek-chat',
            'services.deepseek.base_url' => 'https://api.deepseek.com',
            'services.deepseek.api_key' => 'deepseek_test_key',
            'services.openai.assistant_use_hosted_threads' => false,
        ]);

        $counter = new \stdClass();
        $counter->value = 0;

        $this->app->instance(OpenAiChatService::class, new class($counter) extends OpenAiChatService {
            public function __construct(private \stdClass $counter)
            {
            }

            public function reply(array $messages): string
            {
                $this->counter->value++;

                if ($this->counter->value === 1) {
                    return 'Saya siap membantu dengan berbagai pertanyaan atau tugas terkait coding. Apa yang ingin Anda diskusikan?';
                }

                $systemMessages = collect($messages)->where('role', 'system')->pluck('content')->implode("\n");
                \PHPUnit\Framework\Assert::assertStringContainsString('masih terlalu generik atau mengulang respons lama', $systemMessages);

                return 'Bisa. Aku belum bisa akses URL itu langsung dari server ini, tapi aku bisa bantu audit konten, UX, dan copy dari screenshot atau source code yang kamu kirim.';
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'apakah anda bisa cek projek https://medilis.cloud ?',
        ]);

        $response->assertRedirect('/chat/assistant');
        $this->assertSame(2, $counter->value);

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertStringContainsString('Aku belum bisa akses URL itu langsung', $conversation->messages->last()->content);
    }

    public function test_assistant_direct_chat_uses_fallback_when_replies_stay_generic_after_retry(): void
    {
        config([
            'services.openai.assistant_provider' => 'deepseek',
            'services.openai.assistant_model' => 'deepseek-chat',
            'services.deepseek.base_url' => 'https://api.deepseek.com',
            'services.deepseek.api_key' => 'deepseek_test_key',
            'services.openai.assistant_use_hosted_threads' => false,
        ]);

        $counter = new \stdClass();
        $counter->value = 0;

        $this->app->instance(OpenAiChatService::class, new class($counter) extends OpenAiChatService {
            public function __construct(private \stdClass $counter)
            {
            }

            public function reply(array $messages): string
            {
                $this->counter->value++;

                return 'Saya siap membantu dengan berbagai pertanyaan atau tugas terkait coding. Apa yang ingin Anda diskusikan atau butuhkan bantuan saya?';
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'Bikin versi yang lebih singkat dan jelas',
        ]);

        $response->assertRedirect('/chat/assistant');
        $this->assertSame(2, $counter->value);

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $latest = (string) $conversation->messages->last()->content;
        $this->assertStringContainsString('Versi singkat dan jelas', $latest);
        $this->assertStringNotContainsString('Saya siap membantu dengan berbagai pertanyaan', $latest);
    }

    public function test_assistant_direct_chat_returns_fallback_when_provider_throws_exception(): void
    {
        config([
            'services.openai.assistant_provider' => 'deepseek',
            'services.openai.assistant_model' => 'deepseek-chat',
            'services.deepseek.base_url' => 'https://api.deepseek.com',
            'services.deepseek.api_key' => 'deepseek_test_key',
            'services.openai.assistant_use_hosted_threads' => false,
        ]);

        $this->app->instance(OpenAiChatService::class, new class extends OpenAiChatService {
            public function reply(array $messages): string
            {
                throw new \RuntimeException('Connection timed out while contacting provider');
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'apakah anda bisa cek projek https://medilis.cloud ?',
        ]);

        $response->assertRedirect('/chat/assistant');
        $response->assertSessionHas('chat_error');

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertStringContainsString('Aku belum bisa akses URL itu langsung', (string) $conversation->messages->last()->content);
    }

    public function test_hosted_assistant_retries_when_first_reply_is_template_like(): void
    {
        config([
            'services.openai.base_url' => 'https://api.openai.com/v1',
            'services.openai.assistant_provider' => 'openai',
            'services.openai.assistant_id' => 'asst_medilis_123',
            'services.openai.assistant_use_hosted_threads' => true,
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $capture = new \stdClass();
        $capture->prompts = [];
        $capture->instructions = [];
        $capture->calls = 0;

        $this->app->instance(OpenAiChatService::class, new class($capture) extends OpenAiChatService {
            public function __construct(private \stdClass $capture)
            {
            }

            public function assistantReply(
                string $prompt,
                ?string $threadId = null,
                ?string $additionalInstructions = null,
                ?string $model = null
            ): array {
                $this->capture->calls++;
                $this->capture->prompts[] = $prompt;
                $this->capture->instructions[] = $additionalInstructions;

                if ($this->capture->calls === 1) {
                    return [
                        'thread_id' => 'thread_medilis_001',
                        'content' => 'Saya selalu siap membantu Anda dengan berbagai pertanyaan dan tugas terkait coding maupun tanya jawab umum. Silakan ajukan pertanyaan atau tugas Anda, dan saya akan berusaha memberikan jawaban terbaik.',
                    ];
                }

                return [
                    'thread_id' => 'thread_medilis_001',
                    'content' => 'Aku belum bisa cek server atau browse URL itu langsung dari sini. Kalau kamu mau, aku bisa audit struktur halaman, copy, atau flow fitur berdasarkan screenshot, source code, atau error yang kamu kirim.',
                ];
            }
        });

        $user = User::factory()->create();

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'apakah anda bisa cek projek https://medilis.cloud ?',
        ]);

        $response->assertRedirect('/chat/assistant');
        $this->assertSame(2, $capture->calls);
        $this->assertStringContainsString('Jawab pesan user terakhir secara konkret', $capture->prompts[0]);
        $this->assertStringContainsString('KOREKSI WAJIB', $capture->prompts[1]);
        $this->assertStringContainsString('Koreksi keras', (string) $capture->instructions[1]);

        $conversation = $user->chatConversations()
            ->where('title', 'Assistant')
            ->with('messages')
            ->first();

        $this->assertNotNull($conversation);
        $this->assertSame('Aku belum bisa cek server atau browse URL itu langsung dari sini. Kalau kamu mau, aku bisa audit struktur halaman, copy, atau flow fitur berdasarkan screenshot, source code, atau error yang kamu kirim.', $conversation->messages->last()->content);
    }

    public function test_assistant_follow_up_critique_rewrites_previous_answer_instead_of_answering_generically(): void
    {
        config([
            'services.openai.base_url' => 'https://api.openai.com/v1',
            'services.openai.chat_base_url' => 'https://router.huggingface.co/v1',
            'services.openai.chat_model' => 'CohereLabs/c4ai-command-r-08-2024:cohere',
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $user = User::factory()->create();
        $conversation = $user->chatConversations()->create([
            'title' => 'Assistant',
        ]);

        $conversation->messages()->create([
            'role' => 'user',
            'content' => 'Tulis 3 CTA singkat untuk klinik gigi premium',
        ]);
        $conversation->messages()->create([
            'role' => 'assistant',
            'content' => "1. Jadwalkan kunjungan Anda sekarang.\n2. Senyum sempurna dengan perawatan premium kami.\n3. Hubungi kami untuk informasi lebih lanjut.",
        ]);

        $capture = new \stdClass();
        $capture->lastUserMessage = null;

        $this->app->instance(OpenAiChatService::class, new class($capture) extends OpenAiChatService {
            public function __construct(private \stdClass $capture)
            {
            }

            public function reply(array $messages): string
            {
                $this->capture->lastUserMessage = collect($messages)->reverse()->firstWhere('role', 'user');

                return "1. Booking scaling gigi premium hari ini.\n2. Konsultasi cepat dengan dokter gigi estetik kami.\n3. Senyum rapi dimulai dari sesi pertama Anda.";
            }
        });

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'apakah anda sudah cukup pintar ? tidak menjawab dengan template',
        ]);

        $response->assertRedirect('/chat/assistant');

        $latestReply = $conversation->fresh('messages')->messages->last();
        $lastUserMessage = (string) data_get($capture->lastUserMessage, 'content', '');

        $this->assertSame('assistant', $latestReply->role);
        $this->assertStringContainsString('Booking scaling gigi premium hari ini.', $latestReply->content);
        $this->assertStringContainsString('MODE: REWRITE_TOTAL', $lastUserMessage);
        $this->assertStringContainsString('Tulis 3 CTA singkat untuk klinik gigi premium', $lastUserMessage);
        $this->assertStringContainsString('tidak menjawab dengan template', strtolower($lastUserMessage));
        $this->assertStringContainsString('hubungi kami untuk informasi lebih lanjut', strtolower($lastUserMessage));
    }

    public function test_assistant_retries_when_first_critique_reply_is_still_generic(): void
    {
        config([
            'services.openai.base_url' => 'https://api.openai.com/v1',
            'services.openai.assistant_model' => 'gpt-4o',
        ]);

        $user = User::factory()->create();
        $conversation = $user->chatConversations()->create([
            'title' => 'Assistant',
        ]);

        $conversation->messages()->create([
            'role' => 'user',
            'content' => 'Tulis 3 CTA singkat untuk klinik gigi premium',
        ]);
        $conversation->messages()->create([
            'role' => 'assistant',
            'content' => '1. Jadwalkan kunjungan Anda sekarang. 2. Hubungi kami untuk informasi lebih lanjut.',
        ]);

        $counter = new \stdClass();
        $counter->value = 0;

        $this->app->instance(OpenAiChatService::class, new class($counter) extends OpenAiChatService {
            public function __construct(private \stdClass $counter)
            {
            }

            public function reply(array $messages): string
            {
                $this->counter->value++;

                if ($this->counter->value === 1) {
                    return 'Jadwalkan kunjungan Anda sekarang dan hubungi kami untuk informasi lebih lanjut.';
                }

                $systemMessages = collect($messages)->where('role', 'system')->pluck('content')->implode("\n");
                \PHPUnit\Framework\Assert::assertStringContainsString('Jawaban sebelumnya masih terlalu generik', $systemMessages);

                return '1. Reservasi veneer premium minggu ini. 2. Konsultasi estetik gigi dalam 15 menit. 3. Mulai treatment senyum rapi dari sesi awal.';
            }
        });

        $response = $this->actingAs($user)->post('/chat/assistant', [
            'prompt' => 'jangan template',
        ]);

        $response->assertRedirect('/chat/assistant');
        $this->assertSame(2, $counter->value);

        $latestReply = $conversation->fresh('messages')->messages->last();
        $this->assertStringContainsString('Reservasi veneer premium minggu ini.', $latestReply->content);
    }
}