package cd.casic.server;

import cd.casic.ci.process.constant.TestCaseAIGeneratorConstant;
import cd.casic.ci.process.dto.req.testCase.TestCaseAITaskCreateReq;
import cd.casic.ci.process.process.service.testCase.TestCaseAIGeneratorService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


@SpringBootTest(classes = {OpsServerApplication.class})
@ActiveProfiles("local")
@Slf4j
@WebAppConfiguration
public class TestCaseAIGeneratorTest {
    @Resource
    private TestCaseAIGeneratorService service;

    @Test
    public void targetUpload() throws IOException {
        //{"success":true,"msg":"","id":"8711fd176b686eebcc4e3f49bdde1a18"}
        ClassPathResource classPathResource = new ClassPathResource("static/pcre2test");
        String s = service.targetUpload("./pcre2test", classPathResource.getInputStream());
        System.out.println(s);
    }
    @Test
    public void taskCreate(){
        // {"success":true,"msg":"","task_id":"7c5d7df0-68d5-4935-aae7-00287976ae44"}
//        {"success":true,"msg":"","task_id":"a70f92e9-9b8c-4be3-974f-8b0efa253237"}
        TestCaseAITaskCreateReq req = new TestCaseAITaskCreateReq();
        req.setBinaryId("8711fd176b686eebcc4e3f49bdde1a18");
        req.setCustomPrompt("你是一个模糊测试专家，当前程序输入为文本例如 aa aaa 这种");
        req.setCount(10);
        String s = service.taskCreate(req);
        System.out.println(s);
    }
    @Test
    public void getTaskStatus(){
//        {"success":true,"msg":"","task_id":"7c5d7df0-68d5-4935-aae7-00287976ae44","status":"success"}
        String taskStatus = service.getTaskStatus("1e6e9694-5ae2-425e-ad80-9af266d33bea");
        System.out.println(taskStatus);
    }
    @Test
    public void getTaskLog(){
//        {"success":true,"msg":"","log":"智能体初始化中...\n"}
        String taskLog = service.getTaskLog("1e6e9694-5ae2-425e-ad80-9af266d33bea");
        System.out.println(taskLog);
    }
    @Test
    public void downloadSeed() throws IOException {
        File tempFile = File.createTempFile("seed", ".zip");
        FileOutputStream fos  = new FileOutputStream(tempFile);
        service.downloadTestCase("1e6e9694-5ae2-425e-ad80-9af266d33bea",fos);
        tempFile.deleteOnExit();
        fos.close();
        System.out.println();
    }
}
