<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Unit\DTO;

use Drupal\Tests\UnitTestCase;
use Drupal\flowdrop\DTO\ExecutionEdge;

/**
 * Tests the ExecutionEdge job-metadata record.
 *
 * @coversDefaultClass \Drupal\flowdrop\DTO\ExecutionEdge
 * @group flowdrop
 */
class ExecutionEdgeTest extends UnitTestCase {

  /**
   * @covers ::fromMetadata
   * @covers ::getSource
   * @covers ::getTarget
   * @covers ::getSourceHandle
   * @covers ::getTargetHandle
   * @covers ::isTrigger
   * @covers ::isLoopback
   * @covers ::isTool
   * @covers ::getBranchName
   * @covers ::getEdgeId
   */
  public function testReadsSnakeCaseMetadata(): void {
    $edge = ExecutionEdge::fromMetadata([
      'source' => 'a.1',
      'target' => 'b.1',
      'source_handle' => 'a.1-output-True',
      'target_handle' => 'b.1-input-message',
      'is_trigger' => TRUE,
      'is_loopback' => FALSE,
      'is_tool' => FALSE,
      'branch_name' => 'True',
      'edge_id' => 'e1',
    ]);

    $this->assertSame('a.1', $edge->getSource());
    $this->assertSame('b.1', $edge->getTarget());
    $this->assertSame('a.1-output-True', $edge->getSourceHandle());
    $this->assertSame('b.1-input-message', $edge->getTargetHandle());
    $this->assertTrue($edge->isTrigger());
    $this->assertFalse($edge->isLoopback());
    $this->assertFalse($edge->isTool());
    $this->assertSame('True', $edge->getBranchName());
    $this->assertSame('e1', $edge->getEdgeId());
  }

  /**
   * @covers ::fromMetadata
   */
  public function testDefaultsForMissingKeys(): void {
    $edge = ExecutionEdge::fromMetadata([]);

    $this->assertSame('', $edge->getSource());
    $this->assertSame('', $edge->getTarget());
    $this->assertSame('', $edge->getSourceHandle());
    $this->assertSame('', $edge->getTargetHandle());
    $this->assertFalse($edge->isTrigger());
    $this->assertFalse($edge->isLoopback());
    $this->assertFalse($edge->isTool());
    $this->assertSame('', $edge->getBranchName());
    $this->assertSame('', $edge->getEdgeId());
  }

  /**
   * @covers ::toMetadata
   * @covers ::fromMetadata
   */
  public function testRoundTripsThroughSnakeMetadata(): void {
    $snake = [
      'source' => 'a.1',
      'target' => 'b.1',
      'source_handle' => 'a.1-output-True',
      'target_handle' => 'b.1-input-message',
      'is_trigger' => TRUE,
      'is_loopback' => FALSE,
      'is_tool' => TRUE,
      'branch_name' => 'True',
      'edge_id' => 'e1',
    ];

    $this->assertSame($snake, ExecutionEdge::fromMetadata($snake)->toMetadata());
  }

}
