Skip to content

Commit

Permalink
refactor(app/Commands): update CommitCommand.php
Browse files Browse the repository at this point in the history
- Add comments with links to related resources.
- Modify tryFixMessage() method to fix JSON messages.
- Add missing import statement.
- Update PHPDoc annotations.
-  Remove unused code.
- Update test dataset.
  • Loading branch information
[email protected] committed Oct 16, 2024
1 parent ca2c1f7 commit 1c412e0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 38 deletions.
7 changes: 6 additions & 1 deletion app/Commands/CommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,18 @@ private function getPrompt(string $cachedDiff, string $type): string
});
}

/**
* @see https://github.com/josdejong/jsonrepair
* @see https://github.com/adhocore/php-json-fixer
*/
private function tryFixMessage(string $message): string
{
return (new JsonFixer())
// ->missingValue('')
->silent()
->fix(
str(substr($message, (int) strpos($message, '{')))
str($message)
->substr((int) strpos($message, '{'))
// ->match('/\{.*\}/s')
->replaceMatches('/[[:cntrl:]]/mu', '')
->replace(
Expand Down
38 changes: 38 additions & 0 deletions tests/Datasets/CommitCommandParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
* This file is part of the guanguans/ai-commit.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

dataset('commit command parameters', [
[
/*'parameters' =>*/ [],
],
[
/*'parameters' =>*/ [
'--dry-run' => true,
],
],
[
/*'parameters' =>*/ [
'--diff' => <<<'DIFF'
tests/Pest.php | 1 +
tests/Unit/ConfigManagerTest.php | 2 +-
DIFF,
],
],
]);
19 changes: 0 additions & 19 deletions tests/Datasets/InvalidJsons.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@
* This source file is subject to the MIT license that is bundled.
*/

dataset('commit command parameters', [
[
/*'parameters' =>*/ [],
],
[
/*'parameters' =>*/ [
'--dry-run' => true,
],
],
[
/*'parameters' =>*/ [
'--diff' => <<<'DIFF'
tests/Pest.php | 1 +
tests/Unit/ConfigManagerTest.php | 2 +-
DIFF,
],
],
]);

dataset('invalid jsons', [
[
'json' => '',
Expand Down
31 changes: 31 additions & 0 deletions tests/Datasets/InvalidMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
* This file is part of the guanguans/ai-commit.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

dataset('invalid messages', [
[
'message' => <<<'MESSAGE'
{
"subject": "fix(app/Generators): update GithubCopilotCliGenerator to include binary command",
"body": "- Change the command array in the `resolve` function call to include `[\'binary\', \'copilot\', \'explain\', $prompt]` as the command\\n- Update the `mustRun` function
callback to handle output formatting\\n- Add debug statements to output the generated `$output` variable and perform a `dd()` call\\n- Return the generated `$output` variable"
}
MESSAGE,
],
]);
30 changes: 30 additions & 0 deletions tests/Unit/Commands/CommitCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
* This file is part of the guanguans/ai-commit.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

use App\Commands\CommitCommand;

it('can try fix message', function (string $message): void {
expect($message)->not->toBeJson()
->and(
(function (string $message): string {
return $this->tryFixMessage($message);
})->call(app(CommitCommand::class), $message)
)->toBeJson();
})->group(__DIR__, __FILE__)->with('invalid messages');
18 changes: 0 additions & 18 deletions tests/Unit/Generators/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* This source file is subject to the MIT license that is bundled.
*/

use App\Commands\CommitCommand;
use App\GeneratorManager;

beforeEach(function (): void {
Expand All @@ -30,20 +29,3 @@
it('can run string cmd', function (): void {
expect($this->generator->runProcess('echo foo'))->isSuccessful()->toBeTrue();
})->group(__DIR__, __FILE__);

it('can sanitize output to JSON', function (): void {
$output = <<<'OUTPUT'
{
"subject": "fix(app/Generators): update GithubCopilotCliGenerator to include binary command",
"body": "- Change the command array in the `resolve` function call to include `[\'binary\', \'copilot\', \'explain\', $prompt]` as the command\\n- Update the `mustRun` function
callback to handle output formatting\\n- Add debug statements to output the generated `$output` variable and perform a `dd()` call\\n- Return the generated `$output` variable"
}
OUTPUT;

expect($output)->not->toBeJson()
->and(
(function (string $message): string {
return $this->tryFixMessage($message);
})->call(app(CommitCommand::class), $output)
)->toBeJson();
})->group(__DIR__, __FILE__);

0 comments on commit 1c412e0

Please sign in to comment.