package com.ruoyi.generator.service.front;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Field;
import org.junit.Test;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import com.ruoyi.generator.config.DeepSeekProperties;

public class HttpDeepSeekClientTest
{
    @Test
    public void requestFactoryUsesDefaultSslVerificationByDefault() throws Exception
    {
        HttpDeepSeekClient client = client(false);

        SimpleClientHttpRequestFactory factory = client.requestFactory(1000);

        assertSame(SimpleClientHttpRequestFactory.class, factory.getClass());
    }

    @Test
    public void requestFactoryCanSkipSslVerificationWhenEnabled() throws Exception
    {
        HttpDeepSeekClient client = client(true);

        SimpleClientHttpRequestFactory factory = client.requestFactory(1000);

        assertNotEquals(SimpleClientHttpRequestFactory.class, factory.getClass());
    }

    @Test
    public void skipSslVerifyDefaultsToFalse()
    {
        DeepSeekProperties properties = new DeepSeekProperties();

        assertFalse(properties.isSkipSslVerify());
        properties.setSkipSslVerify(true);
        assertTrue(properties.isSkipSslVerify());
        properties.setSkipSslVerify(null);
        assertFalse(properties.isSkipSslVerify());
    }

    @Test
    public void deepSeekTimeoutDefaultsToLongRunningGeneration()
    {
        assertEquals(Integer.valueOf(180000), new DeepSeekProperties().getTimeout());
    }

    private HttpDeepSeekClient client(boolean skipSslVerify) throws Exception
    {
        HttpDeepSeekClient client = new HttpDeepSeekClient();
        DeepSeekProperties properties = new DeepSeekProperties();
        properties.setSkipSslVerify(skipSslVerify);
        setField(client, "deepSeekProperties", properties);
        return client;
    }

    private void setField(Object target, String name, Object value) throws Exception
    {
        Field field = target.getClass().getDeclaredField(name);
        field.setAccessible(true);
        field.set(target, value);
    }
}
