<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Unit\DTO\JsApi;

use Drupal\Tests\UnitTestCase;
use Drupal\flowdrop\DTO\JsApi\Node;

/**
 * Tests the JsApi transport Node value object.
 *
 * @coversDefaultClass \Drupal\flowdrop\DTO\JsApi\Node
 * @group flowdrop
 */
class NodeTest extends UnitTestCase {

  /**
   * @covers ::fromArray
   * @covers ::getId
   * @covers ::getType
   * @covers ::getPosition
   * @covers ::getData
   * @covers ::getLabel
   * @covers ::getConfig
   * @covers ::getMetadata
   */
  public function testReadsCamelCaseTransportKeys(): void {
    $node = Node::fromArray([
      'id' => 'chat_model.1',
      'type' => 'universalNode',
      'position' => ['x' => 12, 'y' => 34],
      'data' => [
        'label' => 'Chat Model',
        'config' => ['temperature' => 0.7],
        'metadata' => ['executor_plugin' => 'chat_model'],
      ],
    ]);

    $this->assertSame('chat_model.1', $node->getId());
    $this->assertSame('universalNode', $node->getType());
    $this->assertSame(['x' => 12.0, 'y' => 34.0], $node->getPosition());
    $this->assertSame('Chat Model', $node->getLabel());
    $this->assertSame(['temperature' => 0.7], $node->getConfig());
    $this->assertSame(['executor_plugin' => 'chat_model'], $node->getMetadata());
  }

  /**
   * @covers ::fromArray
   */
  public function testDefaultsForMissingKeys(): void {
    $node = Node::fromArray([]);

    $this->assertSame('', $node->getId());
    $this->assertSame('', $node->getType());
    $this->assertNull($node->getPosition());
    $this->assertNull($node->getLabel());
    $this->assertSame([], $node->getConfig());
    $this->assertSame([], $node->getMetadata());
    $this->assertSame([], $node->getData());
  }

  /**
   * @covers ::toArray
   * @covers ::getPosition
   */
  public function testPositionCoercesToFloats(): void {
    $node = Node::fromArray(['position' => ['x' => '5', 'y' => '7']]);
    $this->assertSame(['x' => 5.0, 'y' => 7.0], $node->getPosition());
  }

}
