package com.easyagents.federation.sql.runtime;

import java.time.Duration;
import org.junit.Assert;
import org.junit.Test;

/**
 * 查询统一截止时间的单调时钟边界测试。
 */
public class QueryDeadlineTest {

    /**
     * 验证 nanoTime 为负时仍保留有限查询时限。
     */
    @Test
    public void shouldKeepFiniteDeadlineWhenNanoTimeIsNegative() {
        Assert.assertEquals(
            15L,
            QueryDeadline.deadlineAfter(-5L, Duration.ofNanos(20L))
        );
    }

    /**
     * 验证单调时钟跨 Long 上界时按补码回绕，差值语义保持正确。
     */
    @Test
    public void shouldAllowNanoTimeDeadlineToWrap() {
        long now = Long.MAX_VALUE - 5L;
        long deadline = QueryDeadline.deadlineAfter(now, Duration.ofNanos(10L));

        Assert.assertEquals(Long.MIN_VALUE + 4L, deadline);
        Assert.assertEquals(10L, deadline - now);
    }
}
