package tech.easyflow.ai.chattime.availability;

import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.entity.Bot;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.core.runtime.ChatChannel;
import tech.easyflow.core.runtime.ChatRuntimeContext;

import java.math.BigInteger;

/**
 * {@link ChatTimeToolAvailabilityContext} 单元测试。
 *
 * @author Codex
 * @since 2026-05-10
 */
public class ChatTimeToolAvailabilityContextTest {

    /**
     * 登录用户快照应被显式绑定到聊天运行时上下文。
     */
    @Test
    public void bindLoggedInSnapshotShouldAttachContextToRuntimeExt() {
        ChatRuntimeContext runtimeContext = new ChatRuntimeContext();
        runtimeContext.setChannel(ChatChannel.ADMIN);
        runtimeContext.setSessionId(BigInteger.valueOf(2001));
        LoginAccount loginAccount = new LoginAccount();
        loginAccount.setId(BigInteger.valueOf(12));
        Bot bot = new Bot();
        bot.setId(BigInteger.valueOf(99));

        ChatTimeToolAvailabilityContext.bindLoggedInSnapshot(runtimeContext, loginAccount, bot);

        ChatTimeToolAvailabilityContext chatTimeContext = ChatTimeToolAvailabilityContext.fromRuntimeContext(runtimeContext);
        Assert.assertNotNull(chatTimeContext);
        Assert.assertEquals(BigInteger.valueOf(12), chatTimeContext.getLoginAccount().getId());
        Assert.assertEquals(BigInteger.valueOf(99), chatTimeContext.getBot().getId());
        Assert.assertEquals(ChatChannel.ADMIN, chatTimeContext.getChatChannel());
        Assert.assertEquals(BigInteger.valueOf(2001), chatTimeContext.getSessionId());
    }

    /**
     * 匿名或缺失账号快照时不应绑定聊天态权限上下文。
     */
    @Test
    public void bindLoggedInSnapshotShouldIgnoreAnonymousAccount() {
        ChatRuntimeContext runtimeContext = new ChatRuntimeContext();
        runtimeContext.setChannel(ChatChannel.USER_CENTER);
        runtimeContext.setSessionId(BigInteger.valueOf(3001));
        LoginAccount loginAccount = new LoginAccount();
        loginAccount.setId(BigInteger.ZERO);

        ChatTimeToolAvailabilityContext.bindLoggedInSnapshot(runtimeContext, loginAccount, new Bot());

        Assert.assertNull(ChatTimeToolAvailabilityContext.fromRuntimeContext(runtimeContext));
    }
}
