AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Tests\Feature\Ai;
  3. use App\Ai\AiException;
  4. use App\Ai\AiManager;
  5. use App\Ai\AiRequest;
  6. use App\Ai\Drivers\ClaudeDriver;
  7. use App\Ai\Drivers\FakeDriver;
  8. use App\Ai\Drivers\OpenAiCompatibleDriver;
  9. use GuzzleHttp\Client as GuzzleClient;
  10. use GuzzleHttp\Handler\MockHandler;
  11. use GuzzleHttp\HandlerStack;
  12. use GuzzleHttp\Middleware;
  13. use GuzzleHttp\Psr7\Response;
  14. use Illuminate\Http\Client\Request;
  15. use Illuminate\Support\Facades\Http;
  16. use Tests\TestCase;
  17. class AiManagerTest extends TestCase
  18. {
  19. public function test_each_task_uses_the_provider_and_model_from_settings()
  20. {
  21. config([
  22. 'ai.tasks.generate' => ['provider' => 'fake', 'model' => 'model-a', 'max_tokens' => 1000],
  23. 'ai.tasks.edit' => ['provider' => 'fake', 'model' => 'model-b', 'max_tokens' => 1000],
  24. ]);
  25. $manager = app(AiManager::class);
  26. $this->assertSame('model-a', $manager->ask('generate', 'system', 'Store name: Kedai Ali')->model);
  27. $this->assertSame('model-b', $manager->ask('edit', 'system', 'prompt')->model);
  28. $this->assertInstanceOf(FakeDriver::class, $manager->driver('fake'));
  29. $this->assertInstanceOf(ClaudeDriver::class, $manager->driver('claude'));
  30. $this->assertInstanceOf(OpenAiCompatibleDriver::class, $manager->driver('deepseek'));
  31. }
  32. public function test_switching_the_generation_model_is_only_a_settings_change()
  33. {
  34. Http::fake([
  35. 'api.deepseek.com/*' => Http::response([
  36. 'choices' => [['message' => ['content' => '{"ok":true}'], 'finish_reason' => 'stop']],
  37. 'usage' => ['prompt_tokens' => 11, 'completion_tokens' => 22],
  38. ]),
  39. ]);
  40. // The same two values that AI_GENERATE_PROVIDER and AI_GENERATE_MODEL set in .env.
  41. config([
  42. 'ai.tasks.generate.provider' => 'deepseek',
  43. 'ai.tasks.generate.model' => 'deepseek-reasoner',
  44. 'ai.providers.deepseek.api_key' => 'test-key',
  45. ]);
  46. $response = app(AiManager::class)->ask('generate', 'the system text', 'the prompt');
  47. $this->assertSame('{"ok":true}', $response->text);
  48. $this->assertSame('deepseek', $response->provider);
  49. $this->assertSame('deepseek-reasoner', $response->model);
  50. $this->assertSame(11, $response->inputTokens);
  51. $this->assertSame(22, $response->outputTokens);
  52. Http::assertSent(fn (Request $request) => $request->url() === 'https://api.deepseek.com/chat/completions'
  53. && $request->hasHeader('Authorization', 'Bearer test-key')
  54. && $request['model'] === 'deepseek-reasoner'
  55. // The provider's own limit caps the task's larger request.
  56. && $request['max_tokens'] === 8000
  57. && $request['messages'][0] === ['role' => 'system', 'content' => 'the system text']
  58. && $request['messages'][1] === ['role' => 'user', 'content' => 'the prompt']);
  59. }
  60. public function test_the_claude_driver_sends_the_request_and_reads_only_text_blocks()
  61. {
  62. $sent = [];
  63. $stack = HandlerStack::create(new MockHandler([
  64. new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([
  65. 'id' => 'msg_test',
  66. 'type' => 'message',
  67. 'role' => 'assistant',
  68. 'model' => 'claude-sonnet-5-5',
  69. 'content' => [
  70. ['type' => 'thinking', 'thinking' => '', 'signature' => 'sig'],
  71. ['type' => 'text', 'text' => '{"hello":'],
  72. ['type' => 'text', 'text' => '"world"}'],
  73. ],
  74. 'stop_reason' => 'end_turn',
  75. 'stop_sequence' => null,
  76. 'usage' => ['input_tokens' => 30, 'output_tokens' => 40],
  77. ])),
  78. ]));
  79. $stack->push(Middleware::history($sent));
  80. $driver = new ClaudeDriver('claude', ['api_key' => 'test-key', 'thinking' => true], new GuzzleClient(['handler' => $stack]));
  81. $response = $driver->complete(new AiRequest('generate', 'claude-sonnet-5-5', 'the system text', 'the prompt', 16000));
  82. $this->assertSame('{"hello":"world"}', $response->text);
  83. $this->assertSame(30, $response->inputTokens);
  84. $this->assertSame(40, $response->outputTokens);
  85. $body = json_decode((string) $sent[0]['request']->getBody(), true);
  86. $this->assertSame('claude-sonnet-5-5', $body['model']);
  87. $this->assertSame(16000, $body['max_tokens']);
  88. $this->assertSame('the system text', $body['system']);
  89. $this->assertSame(['type' => 'adaptive'], $body['thinking']);
  90. $this->assertStringContainsString('test-key', $sent[0]['request']->getHeaderLine('Authorization'));
  91. }
  92. public function test_a_missing_api_key_is_reported_as_a_setup_problem()
  93. {
  94. config(['ai.providers.claude.api_key' => null, 'ai.providers.deepseek.api_key' => null]);
  95. foreach (['claude', 'deepseek'] as $provider) {
  96. try {
  97. app(AiManager::class)->driver($provider)->complete(new AiRequest('generate', 'm', 's', 'p', 100));
  98. $this->fail("Expected [$provider] to refuse without an API key.");
  99. } catch (AiException $e) {
  100. $this->assertSame(AiException::SETUP, $e->reason);
  101. }
  102. }
  103. }
  104. public function test_an_unknown_provider_or_task_is_reported_as_a_setup_problem()
  105. {
  106. config(['ai.tasks.generate.provider' => 'nobody']);
  107. foreach (['generate', 'no-such-task'] as $task) {
  108. try {
  109. app(AiManager::class)->ask($task, 's', 'p');
  110. $this->fail('Expected a setup problem.');
  111. } catch (AiException $e) {
  112. $this->assertSame(AiException::SETUP, $e->reason);
  113. }
  114. }
  115. }
  116. public function test_a_provider_error_is_reported_as_a_provider_problem()
  117. {
  118. Http::fake(['api.deepseek.com/*' => Http::response(['error' => 'busy'], 503)]);
  119. config(['ai.providers.deepseek.api_key' => 'test-key']);
  120. $this->expectException(AiException::class);
  121. $this->expectExceptionMessage('HTTP 503');
  122. app(AiManager::class)->driver('deepseek')->complete(new AiRequest('edit', 'm', 's', 'p', 100));
  123. }
  124. }