Skip to content

Commit caf8b29

Browse files
committed
test: GrokAI facade test
1 parent 4251f6e commit caf8b29

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/Feature/GrokAiTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use GrokPHP\Laravel\Facades\GrokAI;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
use GrokPHP\Client\Exceptions\GrokException;
9+
10+
class GrokAiTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
/**
15+
* Test basic chat request.
16+
*/
17+
public function test_it_can_send_a_chat_request(): void
18+
{
19+
$response = GrokAI::chat([
20+
['role' => 'user', 'content' => 'Tell me a joke!']
21+
])->full();
22+
23+
$this->assertArrayHasKey('choices', $response);
24+
$this->assertNotEmpty($response['choices']);
25+
$this->assertEquals('assistant', $response['choices'][0]['message']['role']);
26+
}
27+
28+
/**
29+
* Test getting only the AI content.
30+
*/
31+
public function test_it_returns_only_ai_response(): void
32+
{
33+
$content = GrokAI::chat([
34+
['role' => 'user', 'content' => 'Tell me a joke!']
35+
])->content();
36+
37+
$this->assertIsString($content);
38+
$this->assertNotEmpty($content);
39+
}
40+
41+
/**
42+
* Test image analysis using Vision.
43+
*/
44+
public function test_it_can_analyze_an_image(): void
45+
{
46+
$response = GrokAI::vision()->analyze(
47+
'https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg',
48+
'Describe this image'
49+
)->full();
50+
51+
$this->assertArrayHasKey('choices', $response);
52+
$this->assertNotEmpty($response['choices']);
53+
$this->assertEquals('assistant', $response['choices'][0]['message']['role']);
54+
}
55+
56+
/**
57+
* Test exception handling when using an empty API key.
58+
*/
59+
public function test_it_throws_exception_for_empty_api_key(): void
60+
{
61+
config(['grok.api_key' => '']);
62+
63+
$this->expectException(GrokException::class);
64+
$this->expectExceptionMessage('No API key provided');
65+
66+
GrokAI::chat([
67+
['role' => 'user', 'content' => 'Tell me a joke!']
68+
]);
69+
}
70+
71+
/**
72+
* Test exception handling when using an invalid API key.
73+
* @return void
74+
*/
75+
public function test_it_throws_exception_for_invalid_api_key(): void
76+
{
77+
config(['grok.api_key' => 'invalid-key']);
78+
79+
$this->expectException(GrokException::class);
80+
$this->expectExceptionMessageMatches('/Incorrect API key/');
81+
82+
GrokAI::chat([
83+
['role' => 'user', 'content' => 'Tell me a joke!']
84+
]);
85+
}
86+
87+
/**
88+
* Test exception when using an unsupported model with image input.
89+
*/
90+
public function test_it_throws_exception_for_invalid_model_with_image(): void
91+
{
92+
$this->expectException(GrokException::class);
93+
$this->expectExceptionMessage('The model does not support image input but some images are present in the request.');
94+
95+
GrokAI::vision()->analyze(
96+
'https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg',
97+
'What is in this image?',
98+
model: \GrokPHP\Client\Enums\Model::GROK_2
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)