package com.ruoyi.framework.config;

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

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

@DisplayName("FastJson2JsonRedisSerializer")
class FastJson2JsonRedisSerializerTest
{
    private static final String LEGACY_LOGIN_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("反序列化含 admin/flag 字段的历史 LoginUser 缓存")
    void deserializeLegacyLoginUserWithAdminField()
    {
        FastJson2JsonRedisSerializer<LoginUser> serializer = new FastJson2JsonRedisSerializer<>(LoginUser.class);
        LoginUser loginUser = serializer.deserialize(LEGACY_LOGIN_USER_JSON.getBytes(FastJson2JsonRedisSerializer.DEFAULT_CHARSET));
        assertNotNull(loginUser);
        assertEquals(1L, loginUser.getUserId());
        assertNotNull(loginUser.getUser());
        assertEquals("admin", loginUser.getUser().getUserName());
        assertEquals("96dc6244-2273-489d-865f-36da1c8999bc", loginUser.getToken());
    }

    @Test
    @DisplayName("反序列化含 admin 的旧版 LoginUser 缓存")
    void deserializeLegacyLoginUserWithAdminOnly()
    {
        String json =
                "{\"@type\":\"com.ruoyi.common.core.domain.model.LoginUser\",\"deptId\":103L,"
                        + "\"permissions\":Set[\"*:*:*\"],\"token\":\"legacy-token\","
                        + "\"user\":{\"admin\":true,\"userId\":1L,\"userName\":\"admin\","
                        + "\"roles\":[{\"admin\":true,\"roleId\":1L,\"roleKey\":\"admin\","
                        + "\"flag\":false,\"deptCheckStrictly\":false,\"menuCheckStrictly\":false}]},"
                        + "\"userId\":1L,\"username\":\"admin\"}";
        FastJson2JsonRedisSerializer<LoginUser> serializer = new FastJson2JsonRedisSerializer<>(LoginUser.class);
        LoginUser loginUser = serializer.deserialize(json.getBytes(FastJson2JsonRedisSerializer.DEFAULT_CHARSET));
        assertNotNull(loginUser);
        assertEquals("admin", loginUser.getUser().getUserName());
    }
}
