package com.frameworkium.integration.ai.capture.api.tests;

import com.frameworkium.core.api.tests.BaseAPITest;
import com.frameworkium.integration.ai.capture.api.dto.executions.*;
import com.frameworkium.integration.ai.capture.api.dto.screenshots.CreateScreenshot;
import com.frameworkium.integration.ai.capture.api.dto.screenshots.Screenshot;
import com.frameworkium.integration.ai.capture.api.service.executions.ExecutionService;
import com.frameworkium.integration.ai.capture.api.service.screenshots.ScreenshotService;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.List;
import java.util.stream.Collectors;

import static com.google.common.truth.Truth.assertThat;

/** Tests for the Capture execution API. */
public class CaptureExecutionAPITest extends BaseAPITest {

    private CreateExecution createExMessage;
    private String executionID;

    /**
     * Using {@link BeforeClass} to ensure anything like:
     * https://github.com/cbeust/testng/issues/1660
     * gets caught before we release.
     * This, with threads, is a common pattern.
     */
    @BeforeClass
    public void create_execution() {
        createExMessage = CreateExecution.newCreateInstance();
        executionID = new ExecutionService()
                .createExecution(createExMessage)
                .executionID;
    }

    @Test
    public void execution_appears_in_results() {

        ExecutionResults latestExecutions = new ExecutionService().getExecutions(1, 10);

        List<ExecutionResponse> filteredExecutions = latestExecutions.results.stream()
                .filter(ex -> executionID.equals(ex.executionID))
                .collect(Collectors.toList());

        // ensure only one with our expected ID
        assertThat(filteredExecutions).hasSize(1);

        ExecutionResponse response = filteredExecutions.get(0);
        assertThat(response.createdFrom(createExMessage)).isTrue();
        assertThat(response.currentStatus).isEqualTo("new");

        // check total is at least the number of results returns
        assertThat(latestExecutions.total)
                .isAtLeast(latestExecutions.results.size());
    }

    @Test
    public void can_add_then_view_screenshot() {
        CreateScreenshot createScreenshot = CreateScreenshot.newInstance(executionID);
        new ScreenshotService().createScreenshot(createScreenshot);

        Screenshot returnedScreenshot =
                new ExecutionService().getExecution(executionID).screenshots.get(0);
        assertThat(returnedScreenshot.command).isEqualTo(createScreenshot.command);
        assertThat(returnedScreenshot.imageURL).endsWith(".png");
    }
}

@@ -512,24 +512,70 @@
 hotService;%0A
+import org.openqa.selenium.NotFoundException;%0A
 import org.t
@@ -1505,16 +1505,32 @@
 utions =
+%0A               
  new Exe
@@ -1566,16 +1566,185 @@
 (1, 10);
+%0A        // check total is at least the number of results returns%0A        assertThat(latestExecutions.total)%0A                .isAtLeast(latestExecutions.results.size());
 %0A%0A      
@@ -2178,165 +2178,410 @@
     
+%7D%0A%0A
     
-assertThat(response.currentStatus).isEqualTo(%22new%22);%0A%0A        // check total is at least the number of results returns%0A        assertThat(latestE
+@Test%0A    public void new_execution_has_status_new_and_last_updated_equals_created() %7B%0A        String id = new ExecutionService()%0A                .createExecution(createExMessage)%0A                .executionID;%0A        ExecutionResponse execution = new ExecutionService()%0A                .getExecutions(1, 20)%0A                .results.stream()%0A                .filter(ex -%3E id.equals(ex.e
 xecution
 s.to
@@ -2568,39 +2568,35 @@
 als(ex.execution
-s.total
+ID)
 )%0A              
@@ -2602,49 +2602,188 @@
    .
-isAtLeast(latestExecutions.results.size()
+findFirst().orElseThrow(NotFoundException::new);%0A        assertThat(execution.currentStatus).isEqualTo(%22new%22);%0A        assertThat(execution.lastUpdated).isEqualTo(execution.created
 );%0A 

Fix CaptureExecutionAPITest