<commit_before>require 'test_helper'

class DashboardControllerTest < ActionController::TestCase

  def setup
    SysRouter.stubs(:base_path).returns(Rails.root.join("test/fixtures/sys"))
    OodFilesApp.any_instance.stubs(:favorite_paths).returns([Pathname.new("/fs/scratch/efranz")])
    OodAppkit.stubs(:clusters).returns([])
  end

  def teardown
    SysRouter.unstub(:base_path)
    OodFilesApp.any_instance.unstub(:favorite_paths)
    OodAppkit.unstub(:clusters)
  end

  def assert_divider(item)
    assert_equal "divider", item['class'], "li was supposed to be a divider"
  end

  def assert_header(item, title)
    assert_equal "dropdown-header", item['class'], "li was supposed to be a dropdown-header"
    assert_equal title, item.text.strip
  end

  test "should create Jobs dropdown" do

    get :index

    assert_select "li.dropdown[title='Jobs'] li" do |items|
      assert_select items[0], "a", "Active Jobs"
      assert_select items[1], "a", "My Jobs"
    end
  end

  test "should create Files dropdown" do
    get :index

    assert_select "li.dropdown[title='Files'] li" do |items|
      assert_select items[0], "a", "Home Directory"
      assert_divider items[1]
      assert_select items[2], "a", "/fs/scratch/efranz"
    end
  end

  test "should create Clusters dropdown" do
    get :index

    assert_select "li.dropdown[title='Clusters'] li" do |items|
      assert_select items[0], "a", "Shell Access"
      assert_select items[1], "a", "System Status" do |item|
        assert_equal "/apps/show/systemstatus", item.first['href']
      end

    end
  end

  test "should create Interactive Apps dropdown" do
    SysRouter.stubs(:base_path).returns(Rails.root.join("test/fixtures/sys_with_interactive_apps"))

    get :index

    assert_select "li.dropdown[title='Interactive Apps'] li" do |items|
      assert_select items[0], "a", "Interactive Sessions", "need to add Active Sessions to each menu that has batch connect apps"
      assert_divider items[1]

      # Apps and Desktops
      assert_header items[2], "Apps"
      assert_select items[3], "a", "Jupyter", "Jupyter link not in menu"
      assert_select items[4], "a", "Paraview", "Paraview link not in menu"
      assert_divider items[5]
      assert_header items[6], "Desktops"
      assert_select items[7], "a", "Desktops", "Desktops link not in menu" do |item|
        assert_equal "/batch_connect/sys/bc_desktop/session_contexts/new", item.first['href']
      end
    end

    SysRouter.unstub(:base_path)
  end
end<commit_msg>make test more readable<commit_after>require 'test_helper'

class DashboardControllerTest < ActionController::TestCase

  def setup
    SysRouter.stubs(:base_path).returns(Rails.root.join("test/fixtures/sys"))
    OodFilesApp.any_instance.stubs(:favorite_paths).returns([Pathname.new("/fs/scratch/efranz")])
    OodAppkit.stubs(:clusters).returns([])
  end

  def teardown
    SysRouter.unstub(:base_path)
    OodFilesApp.any_instance.unstub(:favorite_paths)
    OodAppkit.unstub(:clusters)
  end

  def dropdown_list(title)
    css_select("li.dropdown[title='#{title}'] ul")
  end

  # given a dropdown list, return the list items as an array of strings
  # with symbols for header or divider
  def dropdown_list_items(list)
    css_select(list, "li").map do |item|
      if item['class'] && item['class'].include?("divider")
        :divider
      elsif item['class'] && item['class'].include?("dropdown-header")
        { :header => item.text.strip }
      else
        item.text.strip
      end
    end
  end

  test "should create Jobs dropdown" do
    get :index

    dditems = dropdown_list_items(dropdown_list('Jobs'))
    assert dditems.any?, "dropdown list items not found"
    assert_equal ["Active Jobs", "My Jobs"], dditems
  end

  test "should create Files dropdown" do
    get :index

    dditems = dropdown_list_items(dropdown_list('Files'))
    assert dditems.any?, "dropdown list items not found"
    assert_equal [
      "Home Directory",
      :divider,
      "/fs/scratch/efranz"], dditems
  end

  test "should create Clusters dropdown" do
    get :index

    dd = dropdown_list('Clusters')
    dditems = dropdown_list_items(dd)

    assert dditems.any?, "dropdown list items not found"
    assert_equal ["Shell Access", "System Status"], dditems

    assert_select dd, "li a", "System Status" do |link|
      assert_equal "/apps/show/systemstatus", link.first['href'], "System Status link is incorrect"
    end
  end

  test "should create Interactive Apps dropdown" do
    SysRouter.stubs(:base_path).returns(Rails.root.join("test/fixtures/sys_with_interactive_apps"))

    get :index

    dd = dropdown_list('Interactive Apps')
    dditems = dropdown_list_items(dd)
    assert dditems.any?, "dropdown list items not found"
    assert_equal [
      "Interactive Sessions",
      :divider,
      {header: "Apps"},
      "Jupyter",
      "Paraview",
      :divider,
      {header: "Desktops"},
      "Desktops"], dditems

    assert_select dd, "li a", "Desktops" do |link|
      assert_equal "/batch_connect/sys/bc_desktop/session_contexts/new", link.first['href'], "Desktops link is incorrect"
    end

    SysRouter.unstub(:base_path)
  end
end
{'fraction_non_alphanumeric': 0.12023121387283237, 'fraction_numerical': 0.002890173410404624, 'mean_word_length': 3.6556053811659193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '3129263', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1654, 'n_words': 420}