package com.ruoyi.framework.config;

import com.ruoyi.common.core.domain.model.LoginUser;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * 复现 Redis 实际用法：valueSerializer 使用 Object.class。
 */
@DisplayName("FastJson2 Redis Object.class 反序列化")
class FastJson2RedisReproduceTest
{
    private static final String USER_JSON =
            "{\"@type\":\"com.ruoyi.common.core.domain.model.LoginUser\",\"browser\":\"Chrome 148\","
                    + "\"deptId\":103L,\"expireTime\":1780482471190,\"ipaddr\":\"115.238.57.190\","
                    + "\"loginLocation\":\"XX XX\",\"loginTime\":1780480671190,\"os\":\"Windows10\","
                    + "\"permissions\":Set[\"*:*:*\"],\"token\":\"96dc6244-2273-489d-865f-36da1c8999bc\","
                    + "\"user\":{\"createBy\":\"admin\",\"createTime\":\"2026-05-09 10:26:46\","
                    + "\"delFlag\":\"0\",\"dept\":{\"ancestors\":\"0,100,101\",\"children\":[],"
                    + "\"deptId\":103L,\"deptName\":\"研发部门\",\"leader\":\"若依\",\"orderNum\":1,"
                    + "\"params\":{\"@type\":\"java.util.HashMap\"},\"parentId\":101L,\"status\":\"0\"},"
                    + "\"deptId\":103L,\"email\":\"ry@163.com\",\"loginDate\":\"2026-06-03 17:44:28\","
                    + "\"loginIp\":\"115.238.57.190\",\"nickName\":\"超级管理员\","
                    + "\"params\":{\"@type\":\"java.util.HashMap\"},\"phonenumber\":\"15888888888\","
                    + "\"pwdUpdateDate\":\"2026-05-09 10:26:46\",\"remark\":\"管理员\","
                    + "\"roles\":[{\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"flag\":false,"
                    + "\"menuCheckStrictly\":false,\"params\":{\"@type\":\"java.util.HashMap\"},"
                    + "\"roleId\":1L,\"roleKey\":\"admin\",\"roleName\":\"超级管理员\",\"roleSort\":1,"
                    + "\"status\":\"0\"}],\"sex\":\"1\",\"status\":\"0\",\"userId\":1L,\"userName\":\"admin\"},"
                    + "\"userId\":1L,\"username\":\"admin\"}";

    @Test
    @DisplayName("Object.class 与 RedisConfig 一致")
    void deserializeAsObjectClass()
    {
        FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
        Object raw = assertDoesNotThrow(() -> serializer.deserialize(USER_JSON.getBytes(FastJson2JsonRedisSerializer.DEFAULT_CHARSET)));
        assertNotNull(raw);
        assertNotNull(((LoginUser) raw).getUser());
    }
}
