package com.foxx.androidcast.network;

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

import org.junit.Test;

import java.util.TimeZone;

public class BackendNtpSyncTest {
    @Test
    public void parseBeTimeZone_parsesPlus0200() {
        TimeZone tz = BackendNtpSync.parseBeTimeZone("+0200");
        assertEquals(2 * 60 * 60 * 1000, tz.getOffset(0L));
    }

    @Test
    public void shouldSync_trueAboveThreeMinutes() {
        assertTrue(BackendNtpSync.shouldSync(BackendNtpSync.SYNC_THRESHOLD_MS + 1L));
        assertTrue(BackendNtpSync.shouldSync(-BackendNtpSync.SYNC_THRESHOLD_MS - 1L));
    }

    @Test
    public void shouldSync_falseWithinThreeMinutes() {
        assertFalse(BackendNtpSync.shouldSync(BackendNtpSync.SYNC_THRESHOLD_MS));
        assertFalse(BackendNtpSync.shouldSync(30_000L));
        assertFalse(BackendNtpSync.shouldSync(-120_000L));
    }

    @Test
    public void computeSkewMs_usesRequestMidpoint() {
        long skew = BackendNtpSync.computeSkewMs(1_000L, 1_200L, 2L);
        assertEquals(2_000L - 1_100L, skew);
    }
}
