package tech.easyflow.chatlog.service.impl;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import tech.easyflow.chatlog.domain.dto.ChatHistoryPage;
import tech.easyflow.chatlog.domain.dto.ChatMessageRecord;
import tech.easyflow.chatlog.domain.dto.ChatSessionSummary;
import tech.easyflow.chatlog.domain.query.ChatPageQuery;
import tech.easyflow.chatlog.repository.analyticaldb.ChatAnalyticalDBRepository;
import tech.easyflow.chatlog.service.ChatSessionExtensionDispatcher;
import tech.easyflow.chatlog.service.ChatSessionQueryService;

import java.math.BigInteger;
import java.util.List;

/**
 * {@link ChatHistoryQueryServiceImpl} 归档历史安全投影测试。
 */
public class ChatHistoryQueryServiceImplTest {

    /**
     * 验证 UserCenter/Admin 归档历史返回前统一执行一次会话批量投影。
     */
    @Test
    public void shouldProjectAnalyticalHistoryOnce() {
        ChatAnalyticalDBRepository repository = Mockito.mock(ChatAnalyticalDBRepository.class);
        ChatSessionQueryService sessionQueryService = Mockito.mock(ChatSessionQueryService.class);
        ChatSessionExtensionDispatcher dispatcher = Mockito.mock(ChatSessionExtensionDispatcher.class);
        ChatHistoryPage page = new ChatHistoryPage();
        page.setRecords(List.of(new ChatMessageRecord()));
        ChatSessionSummary summary = new ChatSessionSummary();
        Mockito.when(repository.queryHistory(Mockito.eq(BigInteger.ONE), Mockito.any())).thenReturn(page);
        Mockito.when(sessionQueryService.getSessionSummary(BigInteger.ONE)).thenReturn(summary);
        ChatHistoryQueryServiceImpl service =
                new ChatHistoryQueryServiceImpl(repository, sessionQueryService, dispatcher);

        Assert.assertSame(page, service.queryHistoryMessages(BigInteger.ONE, new ChatPageQuery()));

        Mockito.verify(dispatcher, Mockito.times(1)).projectMessages(summary, page.getRecords());
    }
}
