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

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

import java.util.Arrays;
import com.ruoyi.common.exception.ServiceException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("EarTagCodec")
class EarTagCodecTest
{
    @Test
    @DisplayName("英文逗号拆分")
    void parseComma()
    {
        assertEquals(Arrays.asList("A", "B", "C"), EarTagCodec.parse("A,B,C"));
    }

    @Test
    @DisplayName("中文逗号")
    void parseChineseComma()
    {
        assertEquals(Arrays.asList("A", "B"), EarTagCodec.parse("A，B"));
    }

    @Test
    @DisplayName("trim 与空段")
    void parseTrim()
    {
        assertEquals(Arrays.asList("A", "B"), EarTagCodec.parse(" A , , B "));
    }

    @Test
    @DisplayName("去重拒绝")
    void duplicate()
    {
        assertThrows(ServiceException.class, () -> EarTagCodec.parse("E1,E1"));
    }

    @Test
    @DisplayName("单段超长")
    void tooLong()
    {
        String longTag = repeat('x', 33);
        assertThrows(ServiceException.class, () -> EarTagCodec.parse(longTag));
    }

    private static String repeat(char c, int n)
    {
        StringBuilder sb = new StringBuilder(n);
        for (int i = 0; i < n; i++)
        {
            sb.append(c);
        }
        return sb.toString();
    }
}
