package com.liquidnet.service.goblin.test;

import com.kuaidi100.sdk.utils.SignUtils;
import com.liquidnet.service.goblin.constant.GoblinExpressCompanyConst;
import com.liquidnet.service.goblin.dto.vo.GoblinExpressCompanyVo;
import com.liquidnet.service.goblin.express.kuaidi100.Kuaidi100Client;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.List;

/**
 * 快递100相关：不依赖真实 customer/key 的纯逻辑单测。
 * 真接口冒烟需配置账号后另跑联调（见类末注释）。
 */
public class Kuaidi100ClientTest {

    @Test
    public void verifyCallbackSign_okWhenSaltMatches() {
        Kuaidi100Client client = new Kuaidi100Client();
        ReflectionTestUtils.setField(client, "salt", "test-salt");

        String param = "{\"status\":\"polling\",\"lastResult\":{}}";
        String sign = SignUtils.sign(param + "test-salt");

        Assert.assertTrue(client.verifyCallbackSign(param, sign));
    }

    @Test
    public void verifyCallbackSign_rejectWrongSign() {
        Kuaidi100Client client = new Kuaidi100Client();
        ReflectionTestUtils.setField(client, "salt", "test-salt");

        Assert.assertFalse(client.verifyCallbackSign("{\"a\":1}", "deadbeef"));
    }

    @Test
    public void verifyCallbackSign_allowEmptySignWhenNoSalt() {
        Kuaidi100Client client = new Kuaidi100Client();
        ReflectionTestUtils.setField(client, "salt", "");

        Assert.assertTrue(client.verifyCallbackSign("{\"a\":1}", null));
        Assert.assertTrue(client.verifyCallbackSign("{\"a\":1}", ""));
    }

    @Test
    public void isConfigured_requiresCustomerAndKey() {
        Kuaidi100Client client = new Kuaidi100Client();
        ReflectionTestUtils.setField(client, "customer", "");
        ReflectionTestUtils.setField(client, "key", "k");
        Assert.assertFalse(client.isConfigured());

        ReflectionTestUtils.setField(client, "customer", "c");
        ReflectionTestUtils.setField(client, "key", "k");
        Assert.assertTrue(client.isConfigured());
    }

    @Test
    public void commonExpressList_putsDebangSfJdFirst() {
        List<GoblinExpressCompanyVo> list = GoblinExpressCompanyConst.COMMON_LIST;
        Assert.assertTrue(list.size() >= 4);
        Assert.assertEquals("debangkuaidi", list.get(0).getCode());
        Assert.assertEquals("debangwuliu", list.get(1).getCode());
        Assert.assertEquals("shunfeng", list.get(2).getCode());
        Assert.assertEquals("jd", list.get(3).getCode());
    }

    /*
     * 真接口联调（需配置 liquidnet.express.kuaidi100.customer/key，建议本地手动跑）：
     *
     * 1) AutoNum：client.autoComDetail("运单号")
     * 2) QueryTrackMap：client.queryMapTrack(com, num, phone后四位, from, to)
     * 3) SubscribeWithMap：需公网 callbackurl，否则推送收不到
     *
     * 顺丰/京东等强校验手机号，phone 必传。
     */
}
