From fcd0aca5de6abe054805f72d78891a78c95dd955 Mon Sep 17 00:00:00 2001 From: "ityaozm@gmail.com" Date: Mon, 30 Sep 2024 20:56:16 +0800 Subject: [PATCH] test(Ernie, Pest, ConfigManagerTest): Refactor response handling and update test setup - Refactor the response handling in Ernie.php to simplify the assignment of the response variable. - Update the test setup in Pest.php to set the application version dynamically. - Modify the skip method in ConfigManagerTest.php for better clarity in test grouping. --- app/Support/Ernie.php | 4 +- tests/Datasets/InvalidJsons.php | 30 +++++++++++++- tests/Feature/CommitCommandTest.php | 22 ++++++++-- tests/Feature/ConfigCommandTest.php | 10 ++++- tests/Feature/ThanksCommandTest.php | 8 ++++ tests/Pest.php | 17 ++++++-- tests/TestCase.php | 2 + tests/Unit/ConfigManagerTest.php | 10 ++++- tests/Unit/Exceptions/HandlerTest.php | 40 +++++-------------- tests/Unit/GeneratorManagerTest.php | 8 ++++ tests/Unit/Generators/BitoGeneratorTest.php | 8 ++++ .../Unit/Generators/ErnieBotGeneratorTest.php | 8 ++++ .../Generators/ErnieBotTurboGeneratorTest.php | 8 ++++ .../Unit/Generators/MoonshotGeneratorTest.php | 8 ++++ .../Generators/OpenAIChatGeneratorTest.php | 8 ++++ tests/Unit/Generators/OpenAIGeneratorTest.php | 8 ++++ tests/Unit/Macros/StrMacroTest.php | 8 ++++ tests/Unit/Support/ErnieTest.php | 8 ++++ tests/Unit/Support/FoundationSDKTest.php | 8 ++++ tests/Unit/Support/JsonFixerTest.php | 17 +++++--- tests/Unit/Support/MoonshotTest.php | 8 ++++ tests/Unit/Support/OpenAITest.php | 8 ++++ 22 files changed, 208 insertions(+), 48 deletions(-) diff --git a/app/Support/Ernie.php b/app/Support/Ernie.php index e20c6fe..3df9e9d 100644 --- a/app/Support/Ernie.php +++ b/app/Support/Ernie.php @@ -208,9 +208,7 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi )); if ($rowData && empty($response->body())) { - $response = new Response( - $response->toPsrResponse()->withBody(Utils::streamFor(self::sanitizeData($rowData))) - ); + $response = new Response($response->toPsrResponse()->withBody(Utils::streamFor(self::sanitizeData($rowData)))); } return tap($response->throw(), static function (Response $response): void { diff --git a/tests/Datasets/InvalidJsons.php b/tests/Datasets/InvalidJsons.php index ca2c741..af5222d 100644 --- a/tests/Datasets/InvalidJsons.php +++ b/tests/Datasets/InvalidJsons.php @@ -1,5 +1,13 @@ [], + ], + [ + 'parameters' => [ + '--dry-run' => true, + ], + ], + [ + 'parameters' => [ + // '--diff' => <<<'DIFF' + // tests/Pest.php | 1 + + // tests/Unit/ConfigManagerTest.php | 2 +- + // DIFF + // , + ], + ], +]); + +dataset('invalid jsons', [ [ 'json' => '', 'expect' => '', diff --git a/tests/Feature/CommitCommandTest.php b/tests/Feature/CommitCommandTest.php index 7d21d45..dca0846 100755 --- a/tests/Feature/CommitCommandTest.php +++ b/tests/Feature/CommitCommandTest.php @@ -1,5 +1,13 @@ 1677143178, 'model' => 'text-davinci-003', 'choices' => [ - 0 => [ + [ 'text' => 'invalid json', // 无效响应 'index' => 0, 'logprobs' => null, @@ -87,7 +95,12 @@ ->group(__DIR__, __FILE__) ->throws(TaskException::class, 'The generated commit message('); -it('can generate and commit message', function (): void { +it('can generate and commit message', function (array $parameters): void { + // 添加文件到暂存区 + file_put_contents(repository_path('playground.random'), Str::random()); + Process::fromShellCommandline('git rm -rf --cached repository/', fixtures_path())->run(); + Process::fromShellCommandline('git add playground.random', repository_path())->mustRun(); + // 设置 git 信息 Process::fromShellCommandline('git config user.email yaozm', repository_path())->mustRun(); Process::fromShellCommandline('git config user.name ityaozm@gmail.com', repository_path())->mustRun(); @@ -99,7 +112,7 @@ ]); $this - ->artisan(CommitCommand::class, [ + ->artisan(CommitCommand::class, $parameters + [ 'path' => repository_path(), '--generator' => 'openai', '--no-edit' => true, @@ -117,12 +130,13 @@ ->expectsConfirmation('Do you want to commit this message?', 'yes') ->assertSuccessful(); }) + ->with('commit command parameters') ->depends('it will throw TaskException(The generated commit message is an invalid JSON)') ->group(__DIR__, __FILE__); afterAll(static function (): void { // 清理 playground 仓库 - Process::fromShellCommandline('git reset HEAD^', repository_path())->run(); + Process::fromShellCommandline('git reset $(git rev-list --max-parents=0 HEAD)', repository_path())->run(); // Process::fromShellCommandline('git checkout -- .', repository_path())->run(); Process::fromShellCommandline('git checkout HEAD -- .', repository_path())->run(); Process::fromShellCommandline('git add tests/Fixtures/repository/', base_path())->mustRun(); diff --git a/tests/Feature/ConfigCommandTest.php b/tests/Feature/ConfigCommandTest.php index f6d2af7..b4ddf6d 100755 --- a/tests/Feature/ConfigCommandTest.php +++ b/tests/Feature/ConfigCommandTest.php @@ -1,5 +1,13 @@ assertSuccessful(); }) ->group(__DIR__, __FILE__) - ->with(['null', 'true', 'false', '0.0', '0', json_encode(['foo' => 'bar'])]); + ->with(['null', 'true', 'false', '0.0', '0', json_encode(['foo' => 'bar'], JSON_THROW_ON_ERROR)]); it('can get config', function (): void { $this->artisan(ConfigCommand::class, [ diff --git a/tests/Feature/ThanksCommandTest.php b/tests/Feature/ThanksCommandTest.php index 6a63cff..6e2722f 100644 --- a/tests/Feature/ThanksCommandTest.php +++ b/tests/Feature/ThanksCommandTest.php @@ -1,5 +1,13 @@ beforeEach(function (): void { // setup_http_fake(); + config()->set('app.version', 'v'.config('app.version')); }) ->afterEach(function (): void { }) @@ -40,8 +49,8 @@ | */ -expect()->extend('toBeOne', function () { - return $this->toBe(1); +expect()->extend('toBeTwo', function () { + return $this->toBe(2); }); /* @@ -57,6 +66,8 @@ /** * @param object|string $class + * + * @throws \ReflectionException */ function class_namespace($class): string { @@ -165,7 +176,7 @@ function setup_http_fake(): void 'created' => 1677143178, 'model' => 'text-davinci-003', 'choices' => [ - 0 => [ + [ 'text' => $text, 'index' => 0, 'logprobs' => null, diff --git a/tests/TestCase.php b/tests/TestCase.php index c8a0add..45e76b9 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -37,6 +37,8 @@ public static function tearDownAfterClass(): void /** * This method is called before each test. + * + * @throws \JsonException */ protected function setUp(): void { diff --git a/tests/Unit/ConfigManagerTest.php b/tests/Unit/ConfigManagerTest.php index 162e234..face5c2 100644 --- a/tests/Unit/ConfigManagerTest.php +++ b/tests/Unit/ConfigManagerTest.php @@ -1,5 +1,13 @@ expects($this->once()) ->willReturn(false); expect(ConfigManager::localPath())->toBeString(); -})->skip()->group(__DIR__, __FILE__); +})->group(__DIR__, __FILE__)->skip(); it('can put local config file', function (): void { expect(ConfigManager::create())->putLocal()->toBeInt(); diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 3eb6b1a..901a62e 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -1,6 +1,12 @@ app['env'] = 'production'; - $output = Mockery::spy(OutputInterface::class); - /** @noinspection PhpVoidFunctionResultUsedInspection */ - expect($this->app->get(Handler::class)) - ->renderForConsole( - $output, - new ValidationException( - $this->app->get(Factory::class)->make( - ['foo' => 'bar'], - ['foo' => 'int'] - ) - ) - )->toBeNull() - ->renderForConsole( - $output, - Mockery::spy(\Exception::class) +it('can map ValidationException', function (): void { + expect(app(ExceptionHandler::class)) + ->report( + new ValidationException($this->app->get(Factory::class)->make(['foo' => 'bar'], ['foo' => 'int'])) )->toBeNull(); })->group(__DIR__, __FILE__); - -it('can report exception', function (): void { - $this->app['env'] = 'testing'; - $exception = new Exception('foo'); - $handler = $this->app->get(Handler::class); - expect($handler)->shouldReport($exception)->toBeTrue(); - - // $this->app['env'] = 'production'; - // expect($handler)->shouldReport($exception)->toBeFalse(); -})->group(__DIR__, __FILE__)->skip(); diff --git a/tests/Unit/GeneratorManagerTest.php b/tests/Unit/GeneratorManagerTest.php index 9286433..0b84fae 100644 --- a/tests/Unit/GeneratorManagerTest.php +++ b/tests/Unit/GeneratorManagerTest.php @@ -1,5 +1,13 @@ fix($json)->toBe($expect); -})->group(__DIR__, __FILE__)->with('InvalidJsons'); +})->group(__DIR__, __FILE__)->with('invalid jsons'); it('can fix invalid json with missing value', function (): void { expect(new JsonFixer()) ->missingValue('') - ->fix(substr(json_encode([1, 2, 3]), 0, 5))->toBeJson(); - - expect(new JsonFixer()) + ->fix(substr(json_encode([1, 2, 3], JSON_THROW_ON_ERROR), 0, 5))->toBeJson() + ->and(new JsonFixer()) ->silent() - ->fix($json = substr(json_encode([1, 2, 3]), 3))->toBe($json) + ->fix($json = substr(json_encode([1, 2, 3], JSON_THROW_ON_ERROR), 3))->toBe($json) ->not->toBeJson(); })->group(__DIR__, __FILE__); it('will throw RuntimeException', function (): void { (new JsonFixer()) ->silent(false) - ->fix(substr(json_encode([1, 2, 3]), 3)); + ->fix(substr(json_encode([1, 2, 3], JSON_THROW_ON_ERROR), 3)); })->group(__DIR__, __FILE__)->throws(RuntimeException::class, 'Could not fix JSON (tried padding ``)'); diff --git a/tests/Unit/Support/MoonshotTest.php b/tests/Unit/Support/MoonshotTest.php index 895df69..a2c3346 100644 --- a/tests/Unit/Support/MoonshotTest.php +++ b/tests/Unit/Support/MoonshotTest.php @@ -1,5 +1,13 @@