package com.tzld.piaoquan.api.service.contentplatform.impl;

import com.tzld.piaoquan.api.common.enums.contentplatform.DemandMatchMethodEnum;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 曝光下发过滤纯逻辑单测(不依赖 Spring/DB)。
 * 语义见 {@link ContentPlatformPlanServiceImpl#passesExposureFilter(Long, Long)}。
 */
public class ContentPlatformPlanServiceImplExposureFilterTest {

    /** 阈值为 null:过滤关闭,任何值(含 NULL)都放行。 */
    @Test
    public void thresholdNull_passesAll() {
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(null, null));
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(0L, null));
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(999999L, null));
    }

    /** 阈值 <=0:过滤关闭,全放行。 */
    @Test
    public void thresholdNonPositive_passesAll() {
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(null, 0L));
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(1L, 0L));
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(null, -1L));
    }

    /** 无曝光数据(NULL,如「场景已看视频」池)在阈值开启时仍放行——核心豁免语义。 */
    @Test
    public void nullExposure_alwaysPasses_whenThresholdOn() {
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(null, 2000L));
    }

    /** 有曝光数据:严格大于阈值才放行,边界值(==阈值)被砍。 */
    @Test
    public void hasExposure_strictlyGreaterThanThreshold() {
        assertFalse(ContentPlatformPlanServiceImpl.passesExposureFilter(1000L, 2000L));
        assertFalse(ContentPlatformPlanServiceImpl.passesExposureFilter(2000L, 2000L)); // 边界:==2000 砍掉
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(2001L, 2000L));
        assertTrue(ContentPlatformPlanServiceImpl.passesExposureFilter(50000L, 2000L));
    }

    /** 搜索命中场景已看视频时,与列表下发一致:不按 rov>=0.03,改用 sceneSumRov>=0.03。 */
    @Test
    public void searchSceneVideo_usesSceneSumRovThreshold() {
        assertTrue(ContentPlatformPlanServiceImpl.passesSearchDemandRovFilter(
                DemandMatchMethodEnum.SCENE.getValue(), 0.01d, 0.03d, 0.03d, 0.03d));
        assertFalse(ContentPlatformPlanServiceImpl.passesSearchDemandRovFilter(
                DemandMatchMethodEnum.SCENE.getValue(), 0.01d, 0.029d, 0.03d, 0.03d));
        assertFalse(ContentPlatformPlanServiceImpl.passesSearchDemandRovFilter(
                DemandMatchMethodEnum.SCENE.getValue(), 0d, 0.05d, 0.03d, 0.03d));
    }

    /** 搜索命中非场景已看视频时,仍沿用 demandMinRov。 */
    @Test
    public void searchNonSceneVideo_usesDemandMinRovThreshold() {
        assertFalse(ContentPlatformPlanServiceImpl.passesSearchDemandRovFilter(
                DemandMatchMethodEnum.VECTOR_SIMILARITY.getValue(), 0.029d, 0.2d, 0.03d, 0.03d));
        assertTrue(ContentPlatformPlanServiceImpl.passesSearchDemandRovFilter(
                DemandMatchMethodEnum.VECTOR_SIMILARITY.getValue(), 0.03d, null, 0.03d, 0.03d));
    }
}
