package com.ruoyi.web.modules.industryservice.research.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.research.config.ResearchOpenApiProperties;
import com.ruoyi.web.modules.industryservice.research.domain.dto.ResearchTokenVo;

@DisplayName("科研开放接口 Token")
class ResearchOpenApiTokenServiceTest
{
    private ResearchOpenApiProperties properties;

    private ResearchOpenApiTokenService tokenService;

    @BeforeEach
    void setUp()
    {
        properties = new ResearchOpenApiProperties();
        properties.setEnabled(true);
        properties.setAppKey("demo-key");
        properties.setAppSecret("demo-secret");
        properties.setTokenSecret("unit-test-research-secret-key-32bytes!");
        properties.setTokenExpireMinutes(60);
        tokenService = new ResearchOpenApiTokenService(properties);
    }

    @Test
    @DisplayName("合法凭证签发与校验")
    void issueAndValidate()
    {
        ResearchTokenVo token = tokenService.issueToken("demo-key", "demo-secret");
        tokenService.validateToken(token.getAccessToken());
        assertEquals("Bearer", token.getTokenType());
        assertEquals(3600L, token.getExpiresIn());
    }

    @Test
    @DisplayName("错误密钥拒绝")
    void rejectBadSecret()
    {
        assertThrows(ServiceException.class, () -> tokenService.issueToken("demo-key", "wrong"));
    }
}
