Skip to content

Commit a96c7f4

Browse files
author
ityaozm@gmail.com
committed
test(Unit): Add MoonshotGeneratorTest.php and OpenAIGeneratorTest.php
- Added MoonshotGeneratorTest.php and OpenAIGeneratorTest.php files. - Included test cases for generating commit messages and handling exceptions. - Skipped some test cases for further development.
1 parent e61cd71 commit a96c7f4

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/ai-commit.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
use App\GeneratorManager;
14+
use Illuminate\Http\Client\RequestException;
15+
use Illuminate\Support\Facades\Http;
16+
17+
beforeEach(function (): void {
18+
setup_http_fake();
19+
});
20+
21+
it('can generate commit messages', function (): void {
22+
expect(app(GeneratorManager::class)->driver('moonshot'))
23+
->generate('OK')->toBeString()->not->toBeEmpty();
24+
Http::assertSentCount(1);
25+
})->group(__DIR__, __FILE__)->skip();
26+
27+
it('will throw forbidden RequestException', function (): void {
28+
app(GeneratorManager::class)->driver('moonshot')->generate('Forbidden');
29+
})->group(__DIR__, __FILE__)->throws(RequestException::class, 'HTTP request returned status code 403');
30+
31+
it('will throw unauthorized RequestException', function (): void {
32+
app(GeneratorManager::class)
33+
->tap(function (): void {
34+
// reset_http_fake();
35+
})
36+
->driver('moonshot')
37+
->generate('Unauthorized');
38+
})->group(__DIR__, __FILE__)->throws(RequestException::class, 'HTTP request returned status code 401');
39+
40+
/**
41+
* @psalm-suppress UnusedVariable
42+
*/
43+
it('can call writer', function (): void {
44+
foreach ([
45+
'{"id": "cmpl-6or3mHmSgvCePOlM34DK90rm6J0ec", "object": "text_completion", "created": 1677578382, "choices": [{"text": " ", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
46+
47+
',
48+
'{"id": "cmpl-6or3mHmSgvCePOlM34DK90rm6J0ec", "object": "text_completion", "created": 1677578382, "choices": [{"text": "use", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
49+
50+
',
51+
'{"id": "cmpl-6or3mHmSgvCePOlM34DK90rm6J0ec", "object": "text_completion", "created": 1677578382, "choices": [{"text": " App", "index": 0, "logprobs": null, "finish_reason": "length"}], "model": "text-davinci-003"}
52+
53+
',
54+
'[DONE]
55+
56+
',
57+
] as $rowResponse) {
58+
(function (string $rowResponse) use (&$messages): void {
59+
$this->buildWriter($messages)($rowResponse);
60+
})->call(app(GeneratorManager::class)->driver('moonshot'), $rowResponse);
61+
}
62+
63+
expect($messages)->toBe(' use App');
64+
})->group(__DIR__, __FILE__)->skip();

tests/Unit/Generators/OpenAIGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
expect(app(GeneratorManager::class)->driver('openai'))
2323
->generate('OK')->toBeString()->not->toBeEmpty();
2424
Http::assertSentCount(1);
25-
});
25+
})->group(__DIR__, __FILE__);
2626

2727
it('will throw forbidden RequestException', function (): void {
2828
app(GeneratorManager::class)->driver('openai')->generate('Forbidden');

tests/Unit/Support/MoonshotTest.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212

1313
use App\Support\Moonshot;
14-
use Illuminate\Http\Client\RequestException;
1514
use Illuminate\Http\Client\Response;
1615
use Illuminate\Support\Arr;
1716
use Illuminate\Support\Facades\Http;
@@ -37,31 +36,16 @@
3736
->and(Moonshot::sanitizeData($data))->toBeJson();
3837
})->group(__DIR__, __FILE__);
3938

40-
it('can completions', function (): void {
41-
$parameters = config('ai-commit.generators.moonshot.parameters');
42-
$parameters['prompt'] = 'OK';
43-
$response = $this->moonshot->completions($parameters, function (): void {});
44-
45-
expect($response->json('choices.0.text'))->toBeString()->not->toBeEmpty();
46-
Http::assertSentCount(1);
47-
})->group(__DIR__, __FILE__)->skip();
48-
49-
it('will throw RequestException when completions', function (): void {
50-
$parameters = config('ai-commit.generators.moonshot.parameters');
51-
$parameters['prompt'] = 'Too Many Requests';
52-
$this->moonshot->completions($parameters, function (): void {});
53-
})->group(__DIR__, __FILE__)->throws(RequestException::class, 'HTTP request returned status code 429')->skip();
54-
5539
it('can chat completions', function (): void {
56-
$parameters = config('ai-commit.generators.moonshot_chat.parameters');
40+
$parameters = config('ai-commit.generators.moonshot.parameters');
5741
$parameters['messages'] = [
5842
['role' => 'user', 'content' => 'OK'],
5943
];
6044

6145
expect($this->moonshot->chatCompletions($parameters, function (): void {}))->toBeInstanceOf(Response::class)
6246
->body()->toBeJson();
6347
Http::assertSentCount(1);
64-
})->group(__DIR__, __FILE__)->skip();
48+
})->group(__DIR__, __FILE__);
6549

6650
it('can models', function (): void {
6751
expect($this->moonshot->models())->toBeInstanceOf(Response::class)

0 commit comments

Comments
 (0)