package com.atlassian.bitbucket.mesh.throttle;

import org.junit.Test;

import java.time.Duration;

import static org.junit.Assert.assertEquals;

public class ThrottlingOutputUtilsTest {

    @Test
    public void testIntermediateZerosIncluded() {
        assertEquals("9d0h2m", ThrottlingOutputUtils.toHumanDuration(
                Duration.ofDays(9L).plusMinutes(2L).plusSeconds(3L).plusMillis(99L).toMillis()));
    }

    @Test
    public void testMillisIncludedWhenLessThanOneMinute() {
        //Milliseconds should only be included if the total time is less than 1 minute
        assertEquals("1m0s", ThrottlingOutputUtils.toHumanDuration(
                Duration.ofMinutes(1L).plusMillis(598L).toMillis()));
        assertEquals("59s598ms", ThrottlingOutputUtils.toHumanDuration(
                Duration.ofSeconds(59L).plusMillis(598L).toMillis()));
    }

    @Test
    public void testMostSignificantUnitsSkippedIfZero() {
        assertEquals("12m4s", ThrottlingOutputUtils.toHumanDuration(
                Duration.ofMinutes(12L).plusSeconds(4L).plusMillis(598L).toMillis()));
    }

    @Test
    public void testSecondsIncludedWhenLessThanOneDay() {
        //Seconds should only be included if the total time is less than 1 hour
        assertEquals("1d0h0m", ThrottlingOutputUtils.toHumanDuration(
                Duration.ofDays(1L).plusSeconds(12L).plusMillis(598L).toMillis()));
        assertEquals("23h0m12s", ThrottlingOutputUtils.toHumanDuration(
                Duration.ofHours(23L).plusSeconds(12L).plusMillis(598L).toMillis()));
    }
}