<commit_before>require File.expand_path('../../test_helper', __FILE__)
require 'ticket_sharing/comment'

class TicketSharing::CommentTest < MiniTest::Unit::TestCase

  def test_should_initialize_from_a_hash
    hash = { 'uuid' => '63f127' }
    comment = TicketSharing::Comment.new(hash)
    assert_equal('63f127', comment.uuid)
  end

  def test_should_serialize_author
    comment = TicketSharing::Comment.new(valid_comment_attributes)
    comment.author = TicketSharing::Actor.new('uuid' => 'Actor123', 'name' => 'The Actor')

    json = comment.to_json
    hash = TicketSharing::JsonSupport.decode(json)

    assert_equal('The Actor', hash['author']['name'])
    assert_equal('Actor123', hash['author']['uuid'])
  end

  def test_should_serialize_attachments
    attachment = TicketSharing::Attachment.new('url' => 'http://example.com/')
    comment = TicketSharing::Comment.new(valid_comment_attributes)
    comment.attachments = [attachment]

    json = comment.to_json

    # Convert the json back to a hash just for easier assertions
    hash = TicketSharing::JsonSupport.decode(json)
    assert_equal('http://example.com/', hash['attachments'].first['url'])
  end

  def test_should_parse_from_json
    attributes = valid_comment_attributes
    json = TicketSharing::JsonSupport.encode(attributes)

    comment = TicketSharing::Comment.parse(json)
    assert_equal(attributes['uuid'], comment.uuid)
    assert_equal(attributes['body'], comment.body)
    assert_equal(attributes['authored_at'], comment.authored_at.to_time)
  end

  def test_should_parse_author_from_json
    attributes = valid_comment_attributes
    attributes['author'] = {
        'uuid' => 'Actor123',
        'name' => 'The Actor'
      }

    json = TicketSharing::JsonSupport.encode(attributes)
    comment = TicketSharing::Comment.parse(json)

    assert_equal('Actor123', comment.author.uuid)
    assert_equal('The Actor', comment.author.name)
  end

  def test_should_parse_attachments_from_json
    hash = { 'attachments' => [{ 'url' => 'http://example.com/foo.jpg' }] }
    json = TicketSharing::JsonSupport.encode(hash)

    parsed_comment = TicketSharing::Comment.parse(json)
    assert_equal('http://example.com/foo.jpg', parsed_comment.attachments.first.url)
  end

  def test_parse_should_not_blow_up_when_there_are_no_attachments
    json = TicketSharing::JsonSupport.encode({ 'attachments' => nil })
    assert parsed_comment = TicketSharing::Comment.parse(json)
  end

  def test_a_comment_that_does_not_specify_its_publicity_should_be_public
    comment = TicketSharing::Comment.new
    assert comment.public?
  end

  def test_a_comment_explicitly_set_to_private_should_not_be_public
    comment = TicketSharing::Comment.new({
      'public' => false
    })
    assert !comment.public?
  end

  def test_a_comment_that_does_not_specify_its_rich_format_should_not_be_rich
    comment = TicketSharing::Comment.new
    assert !comment.rich?
  end

  def test_a_comment_explicitly_set_to_private_should_not_be_public
    comment = TicketSharing::Comment.new({
      'rich' => true
    })
    assert comment.rich?
  end

  def test_storing_authored_at
    now = Time.now
    comment = TicketSharing::Comment.new('authored_at' => now)
    assert_equal(now, comment.authored_at.to_time)
  end

  def test_should_serialize_custom_fields
    custom_fields = {
      'foo' => 'bar',
      'one' => 2,
      'three' => [4, 5, 6],
      'hash' => { 'key' => 'value' },
      'array' => [{'id' => 'abc', :url => "http://foo.bar/resources/1"},
                  {'id' => 'efg', :url => "http://foo.bar/resources/2"}]
    }

    comment = TicketSharing::Comment.new('custom_fields' => custom_fields)
<commit_msg>Add test<commit_after>require File.expand_path('../../test_helper', __FILE__)
require 'ticket_sharing/comment'

class TicketSharing::CommentTest < MiniTest::Unit::TestCase

  def test_should_initialize_from_a_hash
    hash = { 'uuid' => '63f127' }
    comment = TicketSharing::Comment.new(hash)
    assert_equal('63f127', comment.uuid)
  end

  def test_should_serialize_author
    comment = TicketSharing::Comment.new(valid_comment_attributes)
    comment.author = TicketSharing::Actor.new('uuid' => 'Actor123', 'name' => '<NAME>')

    json = comment.to_json
    hash = TicketSharing::JsonSupport.decode(json)

    assert_equal('The Actor', hash['author']['name'])
    assert_equal('Actor123', hash['author']['uuid'])
  end

  def test_should_serialize_attachments
    attachment = TicketSharing::Attachment.new('url' => 'http://example.com/')
    comment = TicketSharing::Comment.new(valid_comment_attributes)
    comment.attachments = [attachment]

    json = comment.to_json

    # Convert the json back to a hash just for easier assertions
    hash = TicketSharing::JsonSupport.decode(json)
    assert_equal('http://example.com/', hash['attachments'].first['url'])
  end

  def test_should_parse_from_json
    attributes = valid_comment_attributes
    json = TicketSharing::JsonSupport.encode(attributes)

    comment = TicketSharing::Comment.parse(json)
    assert_equal(attributes['uuid'], comment.uuid)
    assert_equal(attributes['body'], comment.body)
    assert_equal(attributes['authored_at'], comment.authored_at.to_time)
  end

  def test_should_parse_author_from_json
    attributes = valid_comment_attributes
    attributes['author'] = {
        'uuid' => 'Actor123',
        'name' => '<NAME>'
      }

    json = TicketSharing::JsonSupport.encode(attributes)
    comment = TicketSharing::Comment.parse(json)

    assert_equal('Actor123', comment.author.uuid)
    assert_equal('The Actor', comment.author.name)
  end

  def test_should_parse_attachments_from_json
    hash = { 'attachments' => [{ 'url' => 'http://example.com/foo.jpg' }] }
    json = TicketSharing::JsonSupport.encode(hash)

    parsed_comment = TicketSharing::Comment.parse(json)
    assert_equal('http://example.com/foo.jpg', parsed_comment.attachments.first.url)
  end

  def test_parse_should_not_blow_up_when_there_are_no_attachments
    json = TicketSharing::JsonSupport.encode({ 'attachments' => nil })
    assert parsed_comment = TicketSharing::Comment.parse(json)
  end

  def test_a_comment_that_does_not_specify_its_publicity_should_be_public
    comment = TicketSharing::Comment.new
    assert comment.public?
  end

  def test_a_comment_explicitly_set_to_private_should_not_be_public
    comment = TicketSharing::Comment.new({
      'public' => false
    })
    assert !comment.public?
  end

  def test_a_comment_that_does_not_specify_its_rich_format_should_not_be_rich
    comment = TicketSharing::Comment.new
    assert !comment.rich?
  end

  def test_a_comment_explicitly_set_to_rich_format_should_be_rich
    comment = TicketSharing::Comment.new({
      'rich' => true
    })
    assert comment.rich?
  end

  def test_storing_authored_at
    now = Time.now
    comment = TicketSharing::Comment.new('authored_at' => now)
    assert_equal(now, comment.authored_at.to_time)
  end

  def test_should_serialize_custom_fields
    custom_fields = {
      'foo' => 'bar',
      'one' => 2,
      'three' => [4, 5, 6],
      'hash' => { 'key' => 'value' },
      'array' => [{'id' => 'abc', :url => "http://foo.bar/resources/1"},
                  {'id' => 'efg', :url => "http://foo.bar/resources/2"}]
    }

    comment = TicketSharing::Comment.new('custom_fields' => custom_fields)

{'fraction_non_alphanumeric': 0.12691525423728814, 'fraction_numerical': 0.007593220338983051, 'mean_word_length': 4.283667621776504, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4367677', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2370, 'n_words': 381}