package com.tzld.piaoquan.recommend.server.service.funnel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Map;

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

class FunnelAggregatorTest {

    @Test
    void step3NeverContainsVideoFilteredOutAtStep2() {
        FunnelContext context = new FunnelContext();
        RecallVideoEntry kept = entry(1, true, SelectKind.SELF);
        RecallVideoEntry filtered = entry(2, false, SelectKind.SELF);
        context.getStages123RecallByStrategy().put("p1", Arrays.asList(kept, filtered));

        Map<String, String> logItem = FunnelAggregator.toLogItem(context);
        JSONObject step3 = JSON.parseObject(logItem.get("step_3_truncated"));
        JSONArray videos = step3.getJSONArray("p1");

        assertEquals(1, videos.size());
        assertEquals(1L, videos.getJSONObject(0).getLongValue("vid"));
    }

    private static RecallVideoEntry entry(long videoId, boolean filteredIn, SelectKind select) {
        RecallVideoEntry entry = new RecallVideoEntry(videoId, 0, 0);
        entry.setFilteredIn(filteredIn);
        entry.setIndexNewAfterFilter(filteredIn ? 0 : -1);
        entry.setSelect(select);
        entry.setTruncate(1);
        return entry;
    }
}
