-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update error handling and test cases in GenerateCalendarJob
Enhanced error handling in GenerateCalendarJob and updated related test cases. During calendar generation, errors are now logged with more details including the error code and message. Improved execution flow by using fail() instead of release().
- Loading branch information
Showing
4 changed files
with
198 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
use App\Events\IcsEventProcessed; | ||
use App\Jobs\GenerateCalendarJob; | ||
use App\Models\IcsEvent; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Http; | ||
use OpenAI\Laravel\Facades\OpenAI; | ||
use OpenAI\Responses\Chat\CreateResponse; | ||
|
||
test('ICS event is updated with successful OpenAI response', function () { | ||
$ics = IcsEvent::factory()->icsProcessed()->create(); | ||
$validIcsData = str_replace("\n", '\\n', $ics->ics); | ||
$ics->update(['ics' => null]); | ||
$ics->refresh(); | ||
|
||
expect($ics->ics)->toBeNull() | ||
->and($validIcsData)->toBeString(); | ||
|
||
Event::fake(); | ||
OpenAI::fake([ | ||
CreateResponse::fake([ | ||
'choices' => [ | ||
[ | ||
'message' => [ | ||
'role' => 'assistant', | ||
'content' => '{"ics": "' . $validIcsData . '"}', | ||
], | ||
], | ||
], | ||
]), | ||
]); | ||
|
||
GenerateCalendarJob::dispatch($ics); | ||
Event::assertDispatched(IcsEventProcessed::class); | ||
|
||
$ics->refresh(); | ||
expect($ics->ics)->not()->toBeNull() | ||
->and($ics->error)->toBeNull() | ||
->and($ics->getSummary())->toBeString(); | ||
}); | ||
|
||
test('ICS event is updated with error from OpenAI response', function () { | ||
$ics = IcsEvent::factory()->create(); | ||
|
||
expect($ics->ics)->toBeNull() | ||
->and($ics->error)->toBeNull(); | ||
|
||
Event::fake(); | ||
OpenAI::fake([ | ||
CreateResponse::fake([ | ||
'choices' => [ | ||
[ | ||
'message' => [ | ||
'role' => 'assistant', | ||
'content' => '{"error": "Sorry an error is faked here!"}', | ||
], | ||
], | ||
], | ||
]), | ||
]); | ||
|
||
GenerateCalendarJob::dispatch($ics); | ||
Event::assertDispatched(IcsEventProcessed::class); | ||
|
||
$ics->refresh(); | ||
expect($ics->ics)->toBeNull() | ||
->and($ics->error)->toEqual('Sorry an error is faked here!'); | ||
}); | ||
|
||
test('ICS event is updated with data from Mistral in case OpenAI fails', function () { | ||
$ics = IcsEvent::factory()->icsProcessed()->create(); | ||
$validIcsData = str_replace("\n", '\\n', $ics->ics); | ||
$ics->update(['ics' => null]); | ||
$ics->refresh(); | ||
|
||
expect($ics->ics)->toBeNull() | ||
->and($validIcsData)->toBeString(); | ||
|
||
Event::fake(); | ||
OpenAI::fake([ | ||
]); | ||
|
||
Http::fake([ | ||
'api.mistral.ai/*' => Http::response([ | ||
'choices' => [0 => [ | ||
'index' => 0, | ||
'message' => [ | ||
'role' => 'assistant', | ||
'content' => '{"ics": "' . $validIcsData . '"}', | ||
], | ||
'finish_reason' => 'stop', | ||
], ], | ||
'usage' => [ | ||
'prompt_tokens' => 14, | ||
'total_tokens' => 29, | ||
'completion_tokens' => 15, | ||
], | ||
]), | ||
]); | ||
|
||
GenerateCalendarJob::dispatch($ics); | ||
Event::assertDispatched(IcsEventProcessed::class); | ||
|
||
$ics->refresh(); | ||
expect($ics->ics)->not()->toBeNull() | ||
->and($ics->error)->toBeNull() | ||
->and($ics->getSummary())->toBeString(); | ||
}); | ||
|
||
test('ICS event is updated with error from Mistral response after OpenAI fails', function () { | ||
$ics = IcsEvent::factory()->create(); | ||
|
||
expect($ics->ics)->toBeNull() | ||
->and($ics->error)->toBeNull(); | ||
|
||
Event::fake(); | ||
OpenAI::fake([]); | ||
Http::fake([ | ||
'api.mistral.ai/*' => Http::response([ | ||
'choices' => [0 => [ | ||
'index' => 0, | ||
'message' => [ | ||
'role' => 'assistant', | ||
'content' => '{"error": "Sorry an error is faked here!"}', | ||
], | ||
'finish_reason' => 'stop', | ||
], ], | ||
'usage' => [ | ||
'prompt_tokens' => 14, | ||
'total_tokens' => 29, | ||
'completion_tokens' => 15, | ||
], | ||
]), | ||
]); | ||
|
||
GenerateCalendarJob::dispatch($ics); | ||
Event::assertDispatched(IcsEventProcessed::class); | ||
|
||
$ics->refresh(); | ||
expect($ics->ics)->toBeNull() | ||
->and($ics->error)->toEqual('Sorry an error is faked here!'); | ||
}); | ||
|
||
test('ICS event is updated with error if both Mistral and OpenAI fail', function () { | ||
$ics = IcsEvent::factory()->create(); | ||
|
||
expect($ics->ics)->toBeNull() | ||
->and($ics->error)->toBeNull(); | ||
|
||
Event::fake(); | ||
OpenAI::fake([]); | ||
Http::fake([ | ||
'api.mistral.ai/*' => Http::response("We're down, don't bother!", Symfony\Component\HttpFoundation\Response::HTTP_SERVICE_UNAVAILABLE), | ||
]); | ||
|
||
GenerateCalendarJob::dispatch($ics); | ||
|
||
Event::assertDispatched(IcsEventProcessed::class); | ||
|
||
$ics->refresh(); | ||
|
||
expect($ics->ics)->toBeNull() | ||
->and($ics->error)->toEqual("I'm sorry, my servers are having hiccups. Please try again in 30-60 minutes!"); | ||
}); |