<commit_before># frozen_string_literal: true

require("test_helper")

# Controller tests for nucleotide sequences
class SequenceControllerTest < FunctionalTestCase
  def test_list_sequences
    get_with_dump(:list_sequences)
    assert(:success)
  end

  def test_sequence_search
    get(:sequence_search, pattern: Sequence.last.id)
    assert_redirected_to(Sequence.last.show_link_args)

    get(:sequence_search, pattern: "ITS")
    assert(:success)
  end

  def test_observation_index
    obs = observations(:locally_sequenced_obs)
    get(:observation_index, id: obs.id)
    assert(:success)

    obs = observations(:genbanked_obs)
    get(:observation_index, id: obs.id)
    assert(:success)
  end

  def test_index_sequence
    obs = observations(:genbanked_obs)
    query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs)
    results = query.results
    assert_operator(results.count, :>, 3)
    q = query.id.alphabetize

    get(:index_sequence, q: q, id: results[2].id)
    assert_response(:success)

    get(:next_sequence, q: q, id: results[1].id)
    assert_redirected_to(results[2].show_link_args.merge(q: q))

    get(:prev_sequence, q: q, id: results[2].id)
    assert_redirected_to(results[1].show_link_args.merge(q: q))
  end

  def test_show_sequence
    # Prove sequence displayed if called with id of sequence in db
    sequence = sequences(:local_sequence)
    get_with_dump(:show_sequence, id: sequence.id)
    assert_response(:success)

    # Prove index displayed if called with id of sequence not in db
    get(:show_sequence, id: 666)
    assert_redirected_to(action: :index_sequence)
  end

  def test_create_sequence_get
    # choose an obs not owned by Rolf (`requires_login` will login Rolf)
    obs   = observations(:minimal_unknown_obs)
    owner = obs.user

    # Prove method requires login
    requires_login(:create_sequence, id: obs.id)

    # Prove logged-in user can add Sequence to someone else's Observation
    login("zero")
    get(:create_sequence, id: obs.id)
    assert_response(:success)

    # Prove Observation owner can add Sequence<commit_msg>Turn SequenceControllerTest green<commit_after># frozen_string_literal: true

require("test_helper")

# Controller tests for nucleotide sequences
class SequenceControllerTest < FunctionalTestCase
  def test_list_sequences
    get_with_dump(:list_sequences)
    assert(:success)
  end

  def test_sequence_search
    login
    get(:sequence_search, pattern: Sequence.last.id)
    assert_redirected_to(Sequence.last.show_link_args)

    get(:sequence_search, pattern: "ITS")
    assert(:success)
  end

  def test_observation_index
    obs = observations(:locally_sequenced_obs)
    get(:observation_index, id: obs.id)
    assert(:success)

    obs = observations(:genbanked_obs)
    get(:observation_index, id: obs.id)
    assert(:success)
  end

  def test_index_sequence
    login
    obs = observations(:genbanked_obs)
    query = Query.lookup_and_save(:Sequence, :for_observation, observation: obs)
    results = query.results
    assert_operator(results.count, :>, 3)
    q = query.id.alphabetize

    get(:index_sequence, q: q, id: results[2].id)
    assert_response(:success)

    get(:next_sequence, q: q, id: results[1].id)
    assert_redirected_to(results[2].show_link_args.merge(q: q))

    get(:prev_sequence, q: q, id: results[2].id)
    assert_redirected_to(results[1].show_link_args.merge(q: q))
  end

  def test_show_sequence
    login
    # Prove sequence displayed if called with id of sequence in db
    sequence = sequences(:local_sequence)
    get_with_dump(:show_sequence, id: sequence.id)
    assert_response(:success)

    # Prove index displayed if called with id of sequence not in db
    get(:show_sequence, id: 666)
    assert_redirected_to(action: :index_sequence)
  end

  def test_create_sequence_get
    # choose an obs not owned by Rolf (`requires_login` will login Rolf)
    obs   = observations(:minimal_unknown_obs)
    owner = obs.user

    # Prove method requires login
    requires_login(:create_sequence, id: obs.id)

    # Prove logged-in user can add Sequence to someone else's Observation
    login("zero")
    get(:create_sequence, id: obs.id)
    assert_response(:success)

    # Prove Observation owner can add Sequence
{'fraction_non_alphanumeric': 0.09299065420560748, 'fraction_numerical': 0.004205607476635514, 'mean_word_length': 4.220731707317073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1360158', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1311, 'n_words': 326}