<?php

declare(strict_types=1);

namespace Drupal\Tests\native_observability_execution\Unit\Service;

use Drupal\Tests\UnitTestCase;
use Drupal\native_observability_execution\Service\ExecutionSpanStack;

/**
 * Covers the in-memory execution span stack contract.
 *
 * These tests intentionally focus on public behavior only. The stack is a very
 * small but important primitive because several runtime decorators rely on it
 * for per-request span aggregation before persistence or reporting happens.
 */
final class ExecutionSpanStackTest extends UnitTestCase {

  /**
   * Verifies that added spans are returned in insertion order.
   */
  public function testAddAndAllPreserveCollectedSpans(): void {
    $stack = new ExecutionSpanStack();

    $first = ['name' => 'bootstrap', 'duration_ms' => 11];
    $second = ['name' => 'controller', 'duration_ms' => 27];

    $stack->add($first);
    $stack->add($second);

    $this->assertSame([$first, $second], $stack->all());
  }

  /**
   * Verifies that reset clears the current in-memory dataset.
   */
  public function testResetClearsCollectedSpans(): void {
    $stack = new ExecutionSpanStack();
    $stack->add(['name' => 'temporary']);

    $stack->reset();

    $this->assertSame([], $stack->all());
  }

}
