<commit_before>require_relative 'test_helper'

class ClientTest < Test::Unit::TestCase
  def setup
    @client = Wasd::Client.new(
      domain: "wasd.test",
      resolver_config: {nameserver_port: [["127.0.0.1", 53534]]}
    )
    @service = @client.service(name: "test")
  end

  def test_service_instances
    expected = [
      Wasd::Instance.new(@service, "Woop"),
      Wasd::Instance.new(@service, "Hello There"),
    ]
    with_test_dns_server do
      assert_equal expected, @client.service_instances(name: 'test')
    end
  end

  def test_instance_resolution
    instance = Wasd::Instance.new(@service, "Woop")
    expected = Wasd::ResolvedInstance.new(
      instance, [Wasd::Endpoint.new("woop.wasd.test", 49153, 0)],
      {1 => {"hello" => "there", "this" => "is=fun"},
       2 => {"second" => "version", "gosh" => "wow"}})

    with_test_dns_server do
      assert_equal expected, instance.resolve(@client)
    end
  end

  def test_service_resolution
    expected = Wasd::ResolvedService.new(@service, [
      Wasd::Endpoint.new("onlyone.wasd.test", 8001, 0)
    ])

    with_test_dns_server do
      assert_equal expected, @service.resolve(@client)
    end
  end

  def test_service_subtype_resolution
    expected = [
      Wasd::Instance.new(@service.with(subtype: 'cheese'), "Woop"),
    ]

    with_test_dns_server do
      assert_equal expected, @client.service_instances(name: 'test',
                                                       subtype: 'cheese')
    end
  end

  def test_instance_resolution_failure
    with_test_dns_server do
      assert_raises Wasd::NoEndpointsFound do
        @client.service_instance(description: "asdf", name: "qwer").resolve
      end
    end<commit_msg>Add test for resolving a subtyped instance<commit_after>require_relative 'test_helper'

class ClientTest < Test::Unit::TestCase
  def setup
    @client = Wasd::Client.new(
      domain: "wasd.test",
      resolver_config: {nameserver_port: [["127.0.0.1", 53534]]}
    )
    @service = @client.service(name: "test")
  end

  def test_service_instances
    expected = [
      Wasd::Instance.new(@service, "Woop"),
      Wasd::Instance.new(@service, "Hello There"),
    ]
    with_test_dns_server do
      assert_equal expected, @client.service_instances(name: 'test')
    end
  end

  def test_instance_resolution
    instance = Wasd::Instance.new(@service, "Woop")
    expected = Wasd::ResolvedInstance.new(
      instance, [Wasd::Endpoint.new("woop.wasd.test", 49153, 0)],
      {1 => {"hello" => "there", "this" => "is=fun"},
       2 => {"second" => "version", "gosh" => "wow"}})

    with_test_dns_server do
      assert_equal expected, instance.resolve(@client)
    end
  end

  def test_service_resolution
    expected = Wasd::ResolvedService.new(@service, [
      Wasd::Endpoint.new("onlyone.wasd.test", 8001, 0)
    ])

    with_test_dns_server do
      assert_equal expected, @service.resolve(@client)
    end
  end

  def test_service_subtype_resolution
    expected = [
      Wasd::Instance.new(@service.with(subtype: 'cheese'), "Woop"),
    ]

    with_test_dns_server do
      instances = @client.service_instances(name: 'test', subtype: 'cheese')
      assert_equal expected, instances

      endpoints = instances.first.resolve.endpoints
      assert_equal 1, endpoints.size
      assert_equal "woop.wasd.test", endpoints.first.host
    end
  end

  def test_instance_resolution_failure
    with_test_dns_server do
      assert_raises Wasd::NoEndpointsFound do
        @client.service_instance(description: "asdf", name: "qwer").resolve
      end
    end
{'fraction_non_alphanumeric': 0.12771890686001117, 'fraction_numerical': 0.013664249860568878, 'mean_word_length': 3.2855436081242533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5589118', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1155, 'n_words': 227}