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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.web.modules.cart.constant.CartConstants;
import com.ruoyi.web.modules.cart.dto.CartItemRowDTO;
import com.ruoyi.web.modules.category.facade.ICategoryFacade;
import com.ruoyi.web.modules.goods.constant.GoodsConstants;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 购物车失效判定单元测试
 */
@ExtendWith(MockitoExtension.class)
class CartInvalidSupportTest
{
    @Mock
    private ICategoryFacade categoryFacade;

    @Test
    void resolveInvalidType_noneWhenAllValid()
    {
        CartItemRowDTO row = validRow();
        when(categoryFacade.isCategoryVisible(10L)).thenReturn(true);

        assertEquals(CartConstants.INVALID_NONE, CartInvalidSupport.resolveInvalidType(row, categoryFacade));
        assertTrue(CartInvalidSupport.isPurchasable(CartConstants.INVALID_NONE));
        assertNull(CartInvalidSupport.resolveInvalidMsg(CartConstants.INVALID_NONE, null));
    }

    @Test
    void resolveInvalidType_shopDeleted()
    {
        CartItemRowDTO row = validRow();
        row.setShopDelFlag(ShopConstants.DEL_DELETED);

        assertEquals(CartConstants.INVALID_SHOP_DELETED,
                CartInvalidSupport.resolveInvalidType(row, categoryFacade));
    }

    @Test
    void resolveInvalidType_offShelf()
    {
        CartItemRowDTO row = validRow();
        row.setGoodsStatus(GoodsConstants.STATUS_OFF_SHELF);

        assertEquals(CartConstants.INVALID_OFF_SHELF,
                CartInvalidSupport.resolveInvalidType(row, categoryFacade));
    }

    @Test
    void resolveInvalidType_outOfStock()
    {
        CartItemRowDTO row = validRow();
        row.setStock(0);
        when(categoryFacade.isCategoryVisible(10L)).thenReturn(true);

        assertEquals(CartConstants.INVALID_OUT_OF_STOCK,
                CartInvalidSupport.resolveInvalidType(row, categoryFacade));
    }

    private static CartItemRowDTO validRow()
    {
        CartItemRowDTO row = new CartItemRowDTO();
        row.setGoodsId(1L);
        row.setGoodsName("商品A");
        row.setGoodsStatus(GoodsConstants.STATUS_ON_SALE);
        row.setGoodsDelFlag(GoodsConstants.DEL_NORMAL);
        row.setShopStatus(ShopConstants.STATUS_OPEN);
        row.setShopDelFlag(ShopConstants.DEL_NORMAL);
        row.setCategoryId(10L);
        row.setStock(5);
        row.setQuantity(2);
        return row;
    }
}
