package com.ruoyi.web.modules.screen.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("ScreenLoginRsaSupport")
class ScreenLoginRsaSupportTest
{
    private static final String PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH"
            + "nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";
    private static final String PRIVATE_KEY = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY"
            + "7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN"
            + "PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA"
            + "kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow"
            + "cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv"
            + "DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh"
            + "YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3"
            + "UP8iWi1Qw0Y=";

    @Test
    @DisplayName("加解密往返")
    void encryptDecrypt_roundTrip()
    {
        String uuid = UUID.randomUUID().toString();
        String cipher = ScreenLoginRsaSupport.encrypt(uuid, PUBLIC_KEY);
        assertNotNull(cipher);
        assertEquals(uuid, ScreenLoginRsaSupport.decrypt(cipher, PRIVATE_KEY));
    }

    @Test
    @DisplayName("UUID 格式校验")
    void isValidUuid()
    {
        assertTrue(ScreenLoginRsaSupport.isValidUuid("550e8400-e29b-41d4-a716-446655440000"));
        assertFalse(ScreenLoginRsaSupport.isValidUuid("not-a-uuid"));
        assertFalse(ScreenLoginRsaSupport.isValidUuid(null));
    }

    @Test
    @DisplayName("解密后非 UUID")
    void decrypt_notUuid()
    {
        String cipher = ScreenLoginRsaSupport.encrypt("hello", PUBLIC_KEY);
        assertFalse(ScreenLoginRsaSupport.isValidUuid(ScreenLoginRsaSupport.decrypt(cipher, PRIVATE_KEY)));
    }
}
