package com.ruoyi.generator.service.front;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

import java.util.Date;
import org.junit.Test;

public class AiTaskRetryPolicyTest
{
    @Test
    public void retryableFailuresCanRetryBeforeMaxAttempts()
    {
        AiTaskRetryPolicy policy = new AiTaskRetryPolicy();

        assertTrue(policy.canRetry("DeepSeek request timeout", 1, 3));
        assertTrue(policy.canRetry("429 too many requests", 1, 3));
        assertTrue(policy.canRetry("DeepSeek unavailable", 2, 3));
        assertTrue(policy.canRetry("HTTP 502 bad gateway", 2, 3));
    }

    @Test
    public void nonRetryableFailuresDoNotRetry()
    {
        AiTaskRetryPolicy policy = new AiTaskRetryPolicy();

        assertFalse(policy.canRetry("配额不足", 1, 3));
        assertFalse(policy.canRetry("项目不存在或无权限", 1, 3));
        assertFalse(policy.canRetry("Business action references missing field", 1, 3));
    }

    @Test
    public void maxAttemptsStopsRetry()
    {
        AiTaskRetryPolicy policy = new AiTaskRetryPolicy();

        assertFalse(policy.canRetry("DeepSeek request timeout", 3, 3));
    }

    @Test
    public void nextRetryTimeUsesBackoffSequence()
    {
        AiTaskRetryPolicy policy = new AiTaskRetryPolicy();
        Date now = new Date(1_000L);

        assertEquals(new Date(31_000L), policy.nextRetryTime(1, now));
        assertEquals(new Date(121_000L), policy.nextRetryTime(2, now));
        assertEquals(new Date(301_000L), policy.nextRetryTime(3, now));
        assertEquals(new Date(301_000L), policy.nextRetryTime(9, now));
    }
}
