package com.liquidnet.common.third.secureaccess.util;

import org.junit.jupiter.api.Test;

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

class NiaPidUtilsTest {

    private static final String PID_1 =
            "Al7Y07Qo4P5eHosb4ADGQC4ou83HI+U6tgsUXC3uQYlZMDAwMDA3MTIyMDI2MDYyMzE5MTg1MjBGAiEAt5CKWHwrjso5x8i4Vl5cNiSCZAFwQefMXHyPoNIV4IMCIQCV1/pVvwyOlF6I6qLywzXHCGDmT6OVnBPX3+ZyMBWYjg==";
    private static final String PID_2 =
            "Al7Y07Qo4P5eHosb4ADGQC4ou83HI+U6tgsUXC3uQYlZMDAwMDA3MTIyMDI2MDYyNDExNTMxNDBFAiBfpnywTM/7efM4tAK5GglCWG39yMmFdYpfZDFRIpg30gIhAKkCuggZx2KoW0Uf1xy2BmF7OdBlCEqjlzujwUBq1ZDU";

    private static final String OPEN_ID_B64 =
            "XtjTtCjg/l4eixvgAMZALii7zccj5Tq2CxRcLe5BiVkwMDAwMDcxMg==";

    @Test
    void openId_shouldBeStandardBase64AndStableAcrossAuths() {
        String openId1 = NiaPidUtils.extractOpenId(PID_1);
        String openId2 = NiaPidUtils.extractOpenId(PID_2);
        assertEquals(openId1, openId2);
        assertEquals(56, openId1.length());
        assertEquals(OPEN_ID_B64, openId1);
    }

    @Test
    void openId_roundTrip() {
        String openId = NiaPidUtils.extractOpenId(PID_1);
        byte[] bytes = NiaPidUtils.toIdentifierBytes(openId);
        assertNotNull(bytes);
        assertEquals(openId, NiaPidUtils.encodeOpenId(bytes));
    }
}
