diff --git a/app/Containers/VikonIntegration/Tasks/PollPartStatusTask.php b/app/Containers/VikonIntegration/Tasks/PollPartStatusTask.php new file mode 100644 index 0000000..9bdc007 --- /dev/null +++ b/app/Containers/VikonIntegration/Tasks/PollPartStatusTask.php @@ -0,0 +1,50 @@ +maxAttempts; $attempt++) { + $response = $this->http->getWithToken( + "pull_updates/getStatusPartGenerationByNewCoreJson?operation_identity={$operationIdentity}", + $accessToken + ); + + $body = $response->json(); + $status = $body['status'] ?? 'unknown'; + + Log::info('Vikon poll part status', [ + 'operation' => $operationIdentity, + 'status' => $status, + 'attempt' => $attempt + 1, + ]); + + if ($status === 'completed') { + return ['status' => 'completed']; + } + + if ($status === 'failed') { + return [ + 'status' => 'failed', + 'error' => $body['message'] ?? 'Unknown error', + ]; + } + + if ($attempt < $this->maxAttempts - 1) { + sleep($this->interval); + } + } + + return ['status' => 'timeout']; + } +} diff --git a/app/Containers/VikonIntegration/Tests/Unit/PollPartStatusTaskTest.php b/app/Containers/VikonIntegration/Tests/Unit/PollPartStatusTaskTest.php new file mode 100644 index 0000000..41f3bbc --- /dev/null +++ b/app/Containers/VikonIntegration/Tests/Unit/PollPartStatusTaskTest.php @@ -0,0 +1,74 @@ +shouldReceive('json')->once()->andReturn(['status' => 'completed']); + + $http->shouldReceive('getWithToken') + ->once() + ->with( + Mockery::on(fn ($endpoint) => str_contains($endpoint, 'getStatusPartGeneration')), + 'test-token' + ) + ->andReturn($response); + + $task = new PollPartStatusTask($http, 3, 50); + $result = $task->run('op-123', 'test-token'); + + $this->assertEquals('completed', $result['status']); + } + + public function test_returns_failed_when_status_is_failed(): void + { + $http = Mockery::mock(HttpTask::class); + $response = Mockery::mock(Response::class); + $response->shouldReceive('json')->once()->andReturn([ + 'status' => 'failed', + 'message' => 'Generation error', + ]); + + $http->shouldReceive('getWithToken') + ->once() + ->andReturn($response); + + $task = new PollPartStatusTask($http, 3, 50); + $result = $task->run('op-123', 'test-token'); + + $this->assertEquals('failed', $result['status']); + $this->assertEquals('Generation error', $result['error'] ?? null); + } + + public function test_returns_timeout_after_max_attempts(): void + { + $http = Mockery::mock(HttpTask::class); + $response = Mockery::mock(Response::class); + $response->shouldReceive('json')->andReturn(['status' => 'pending']); + + $http->shouldReceive('getWithToken') + ->times(3) + ->andReturn($response); + + $task = new PollPartStatusTask($http, 0, 3); // interval=0, max=3 + $result = $task->run('op-123', 'test-token'); + + $this->assertEquals('timeout', $result['status']); + } +}