['provider' => 'fake', 'model' => 'model-a', 'max_tokens' => 1000], 'ai.tasks.edit' => ['provider' => 'fake', 'model' => 'model-b', 'max_tokens' => 1000], ]); $manager = app(AiManager::class); $this->assertSame('model-a', $manager->ask('generate', 'system', 'Store name: Kedai Ali')->model); $this->assertSame('model-b', $manager->ask('edit', 'system', 'prompt')->model); $this->assertInstanceOf(FakeDriver::class, $manager->driver('fake')); $this->assertInstanceOf(ClaudeDriver::class, $manager->driver('claude')); $this->assertInstanceOf(OpenAiCompatibleDriver::class, $manager->driver('deepseek')); } public function test_switching_the_generation_model_is_only_a_settings_change() { Http::fake([ 'api.deepseek.com/*' => Http::response([ 'choices' => [['message' => ['content' => '{"ok":true}'], 'finish_reason' => 'stop']], 'usage' => ['prompt_tokens' => 11, 'completion_tokens' => 22], ]), ]); // The same two values that AI_GENERATE_PROVIDER and AI_GENERATE_MODEL set in .env. config([ 'ai.tasks.generate.provider' => 'deepseek', 'ai.tasks.generate.model' => 'deepseek-reasoner', 'ai.providers.deepseek.api_key' => 'test-key', ]); $response = app(AiManager::class)->ask('generate', 'the system text', 'the prompt'); $this->assertSame('{"ok":true}', $response->text); $this->assertSame('deepseek', $response->provider); $this->assertSame('deepseek-reasoner', $response->model); $this->assertSame(11, $response->inputTokens); $this->assertSame(22, $response->outputTokens); Http::assertSent(fn (Request $request) => $request->url() === 'https://api.deepseek.com/chat/completions' && $request->hasHeader('Authorization', 'Bearer test-key') && $request['model'] === 'deepseek-reasoner' // The provider's own limit caps the task's larger request. && $request['max_tokens'] === 8000 && $request['messages'][0] === ['role' => 'system', 'content' => 'the system text'] && $request['messages'][1] === ['role' => 'user', 'content' => 'the prompt']); } public function test_the_claude_driver_sends_the_request_and_reads_only_text_blocks() { $sent = []; $stack = HandlerStack::create(new MockHandler([ new Response(200, ['Content-Type' => 'application/json'], (string) json_encode([ 'id' => 'msg_test', 'type' => 'message', 'role' => 'assistant', 'model' => 'claude-sonnet-5-5', 'content' => [ ['type' => 'thinking', 'thinking' => '', 'signature' => 'sig'], ['type' => 'text', 'text' => '{"hello":'], ['type' => 'text', 'text' => '"world"}'], ], 'stop_reason' => 'end_turn', 'stop_sequence' => null, 'usage' => ['input_tokens' => 30, 'output_tokens' => 40], ])), ])); $stack->push(Middleware::history($sent)); $driver = new ClaudeDriver('claude', ['api_key' => 'test-key', 'thinking' => true], new GuzzleClient(['handler' => $stack])); $response = $driver->complete(new AiRequest('generate', 'claude-sonnet-5-5', 'the system text', 'the prompt', 16000)); $this->assertSame('{"hello":"world"}', $response->text); $this->assertSame(30, $response->inputTokens); $this->assertSame(40, $response->outputTokens); $body = json_decode((string) $sent[0]['request']->getBody(), true); $this->assertSame('claude-sonnet-5-5', $body['model']); $this->assertSame(16000, $body['max_tokens']); $this->assertSame('the system text', $body['system']); $this->assertSame(['type' => 'adaptive'], $body['thinking']); $this->assertStringContainsString('test-key', $sent[0]['request']->getHeaderLine('Authorization')); } public function test_a_missing_api_key_is_reported_as_a_setup_problem() { config(['ai.providers.claude.api_key' => null, 'ai.providers.deepseek.api_key' => null]); foreach (['claude', 'deepseek'] as $provider) { try { app(AiManager::class)->driver($provider)->complete(new AiRequest('generate', 'm', 's', 'p', 100)); $this->fail("Expected [$provider] to refuse without an API key."); } catch (AiException $e) { $this->assertSame(AiException::SETUP, $e->reason); } } } public function test_an_unknown_provider_or_task_is_reported_as_a_setup_problem() { config(['ai.tasks.generate.provider' => 'nobody']); foreach (['generate', 'no-such-task'] as $task) { try { app(AiManager::class)->ask($task, 's', 'p'); $this->fail('Expected a setup problem.'); } catch (AiException $e) { $this->assertSame(AiException::SETUP, $e->reason); } } } public function test_a_provider_error_is_reported_as_a_provider_problem() { Http::fake(['api.deepseek.com/*' => Http::response(['error' => 'busy'], 503)]); config(['ai.providers.deepseek.api_key' => 'test-key']); $this->expectException(AiException::class); $this->expectExceptionMessage('HTTP 503'); app(AiManager::class)->driver('deepseek')->complete(new AiRequest('edit', 'm', 's', 'p', 100)); } }