<?php
namespace nur\sery\php\time;

use DateTimeZone;
use nulib\tests\TestCase;

class DelayTest extends TestCase {
  protected static function dt(string $datetime): DateTime {
    return new DateTime($datetime, new DateTimeZone("Indian/Reunion"));
  }

  function testDelay() {
    $from = self::dt("2024-04-05 09:15:23");

    $delay = new Delay(10, $from);
    self::assertEquals(self::dt("2024-04-05 09:15:33"), $delay->getDest());

    $delay = new Delay("10", $from);
    self::assertEquals(self::dt("2024-04-05 09:15:33"), $delay->getDest());

    $delay = new Delay("10s", $from);
    self::assertEquals(self::dt("2024-04-05 09:15:33"), $delay->getDest());

    $delay = new Delay("s", $from);
    self::assertEquals(self::dt("2024-04-05 09:15:24"), $delay->getDest());

    $delay = new Delay("5m", $from);
    self::assertEquals(self::dt("2024-04-05 09:20:00"), $delay->getDest());

    $delay = new Delay("5m0", $from);
    self::assertEquals(self::dt("2024-04-05 09:20:00"), $delay->getDest());

    $delay = new Delay("5m2", $from);
    self::assertEquals(self::dt("2024-04-05 09:20:02"), $delay->getDest());

    $delay = new Delay("m", $from);
    self::assertEquals(self::dt("2024-04-05 09:16:00"), $delay->getDest());

    $delay = new Delay("5h", $from);
    self::assertEquals(self::dt("2024-04-05 14:00:00"), $delay->getDest());

    $delay = new Delay("5h0", $from);
    self::assertEquals(self::dt("2024-04-05 14:00:00"), $delay->getDest());

    $delay = new Delay("5h2", $from);
    self::assertEquals(self::dt("2024-04-05 14:02:00"), $delay->getDest());

    $delay = new Delay("h", $from);
    self::assertEquals(self::dt("2024-04-05 10:00:00"), $delay->getDest());

    $delay = new Delay("5d", $from);
    self::assertEquals(self::dt("2024-04-10 05:00:00"), $delay->getDest());

    $delay = new Delay("5d2", $from);
    self::assertEquals(self::dt("2024-04-10 02:00:00"), $delay->getDest());

    $delay = new Delay("5d0", $from);
    self::assertEquals(self::dt("2024-04-10 00:00:00"), $delay->getDest());

    $delay = new Delay("d", $from);
    self::assertEquals(self::dt("2024-04-06 05:00:00"), $delay->getDest());

    $delay = new Delay("2w", $from);
    self::assertEquals(self::dt("2024-04-21 05:00:00"), $delay->getDest());

    $delay = new Delay("2w2", $from);
    self::assertEquals(self::dt("2024-04-21 02:00:00"), $delay->getDest());

    $delay = new Delay("2w0", $from);
    self::assertEquals(self::dt("2024-04-21 00:00:00"), $delay->getDest());

    $delay = new Delay("w", $from);
    self::assertEquals(self::dt("2024-04-07 05:00:00"), $delay->getDest());
  }

  function testElapsed() {
    $delay = new Delay(5);
    sleep(2);
    self::assertFalse($delay->isElapsed());
    sleep(5);
    self::assertTrue($delay->isElapsed());
  }
}
