<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_dashboard\Unit\Service;

use Drupal\Tests\UnitTestCase;
use Drupal\native_observability_dashboard\Service\MetricValueFormatter;

/**
 * Covers formatting rules used by dashboard KPI widgets.
 */
final class MetricValueFormatterTest extends UnitTestCase {

  /**
   * Verifies formatting for millisecond-scale durations.
   */
  public function testFormatDurationInMilliseconds(): void {
    $formatter = new MetricValueFormatter();

    $formatted = $formatter->formatDuration(245.0);

    $this->assertSame([
      'formatted_value' => '245',
      'unit' => 'ms',
    ], $formatted);
  }

  /**
   * Verifies formatting for second-scale durations.
   */
  public function testFormatDurationInSeconds(): void {
    $formatter = new MetricValueFormatter();

    $formatted = $formatter->formatDuration(1530.0);

    $this->assertSame([
      'formatted_value' => '1.53',
      'unit' => 's',
    ], $formatted);
  }

  /**
   * Verifies normalized percentage formatting.
   */
  public function testFormatPercentage(): void {
    $formatter = new MetricValueFormatter();

    $formatted = $formatter->formatPercentage(12.3456);

    $this->assertSame([
      'formatted_value' => '12.35',
      'unit' => '%',
    ], $formatted);
  }

  /**
   * Verifies generic number formatting with custom precision.
   */
  public function testFormatNumber(): void {
    $formatter = new MetricValueFormatter();

    $formatted = $formatter->formatNumber(1234.5678, 1);

    $this->assertSame([
      'formatted_value' => '1,234.6',
      'unit' => '',
    ], $formatted);
  }

}
