Skip to content

A platform for community discussion. Free, open, simple.

10.4 KB
Raw Blame
# frozen_string_literal: true

describe "Admin Dashboard Redesign | Search section" do
  fab!(:current_user, :admin)
  fab!(:moderator)
  fab!(:user)

  let(:dashboard) { PageObjects::Pages::AdminDashboard.new }

  before do
    SiteSetting.improved_crawler_detection = true
    AdminDashboardSectionConfiguration.update(
      [
        { id: "search", visible: true },
        { id: "highlights", visible: false },
        { id: "reports", visible: false },
        { id: "traffic", visible: false },
        { id: "engagement", visible: false },
      ],
      actor: current_user,
    )
    sign_in(current_user)
  end

  it "lets staff review search health, inspect tooltips, and drill into terms",
     time: Time.zone.local(2026, 5, 14, 12, 0, 0) do
    Fabricate.times(
      2,
      :clicked_search_log,
      term: "ruby",
      user: user,
      created_at: "2026-05-10 10:00",
    )
    Fabricate.times(3, :search_log, term: "ruby", user: user, created_at: "2026-05-10 11:00")
    Fabricate(:search_log, term: "ruby", user: user, created_at: "2026-04-20 10:00")

    Fabricate(
      :clicked_search_log,
      term: "markdown tables",
      user: user,
      created_at: "2026-04-20 11:00",
    )
    Fabricate.times(
      3,
      :search_log,
      term: "markdown tables",
      user: user,
      created_at: "2026-04-20 12:00",
    )
    Fabricate(:search_log, term: "markdown tables", user: user, created_at: "2026-05-03 10:00")

    Fabricate(:search_log, term: "discobot", user: user, created_at: "2026-04-20 13:00")

    Fabricate.times(
      6,
      :clicked_search_log,
      term: "ruby",
      user: user,
      created_at: "2026-03-20 10:00",
    )

    Fabricate(:search_log, term: "admin-search", user: current_user, created_at: "2026-05-10 08:00")
    Fabricate(:clicked_search_log, term: "ruby", user: moderator, created_at: "2026-05-10 08:30")
    Fabricate(
      :search_log,
      term: "admin-prior-search",
      user: current_user,
      created_at: "2026-03-20 08:00",
    )

    # Searches flagged as crawler traffic must be excluded from every metric. If they
    # were counted, "crawlerbot" would top trending and the no-result rate would spike.
    Fabricate.times(
      10,
      :search_log,
      term: "crawlerbot",
      crawler: true,
      created_at: "2026-05-10 09:00",
    )
    Fabricate.times(
      2,
      :clicked_search_log,
      term: "ruby",
      crawler: true,
      created_at: "2026-05-10 09:30",
    )

    dashboard.visit
    expect(dashboard).to have_section("search")

    search = dashboard.search

    expect(search).to have_headline(
      "The total number of searches has increased in the selected period",
      "Members are conducting more searches in your community, but the no-result rate has " \
        "increased. Review the content gaps to see what's missing.",
    )

    expect(search).to have_total_searches_kpi("12", improving_delta: "+100%")
    expect(search).to have_no_result_rate_kpi("8%", worsening_delta: "+8%")

    search.hover_total_searches_tooltip
    expect(search).to have_total_searches_tooltip(
      "The number of searches performed in your community.",
    )

    search.hover_no_result_rate_tooltip
    expect(search).to have_no_result_rate_tooltip(
      "The percentage of searches where members didn't click any result (0% CTR). " \
        "This indicates that members may be searching for content your community doesn't have.",
    )

    search.hover_trending_tooltip
    expect(search).to have_trending_tooltip("The most popular search terms in your community.")

    expect(search).to have_trending_rows(
      [
        { term: "ruby", searches: 6 },
        { term: "markdown tables", searches: 5 },
        { term: "discobot", searches: 1 },
      ],
    )
    expect(search).to have_no_trending_term("admin-search")
    expect(search).to have_no_trending_term("crawlerbot")

    expect(search).to have_content_gap_rows(
      [
        { term: "markdown tables", searches: 5, badge: "Poor match" },
        { term: "discobot", searches: 1, badge: "No match" },
      ],
    )

    search.hover_content_gap_badge("markdown tables")
    expect(search).to have_content_gap_badge_tooltip(
      "Search terms with a click-through rate (CTR) between 1-20%.",
    )

    search.hover_content_gap_badge("discobot")
    expect(search).to have_content_gap_badge_tooltip(
      "Search terms with a 0% click-through rate (CTR).",
    )

    dashboard.select_preset("last_7_days")

    expect(search).to have_headline(
      "Searches have increased and the no-result rate has declined in the selected period",
      "Members are conducting more searches in your community and are finding what they're " \
        "looking for more often.",
    )

    expect(search).to have_total_searches_kpi("5", improving_delta: "+400%")
    expect(search).to have_no_result_rate_kpi("0%", improving_delta: "-100%")

    search.click_trending_term("ruby")

    expect(page).to have_current_path("/admin/logs/search_logs/term", ignore_query: true)
    expect(Rack::Utils.parse_query(URI.parse(page.current_url).query)).to eq(
      "searchType" => "human_only",
      "period" => "weekly",
      "term" => "ruby",
    )

    dashboard.visit
    dashboard.select_preset("last_3_months")

    expect(dashboard.search).to have_headline(
      "The total number of searches has increased in the selected period",
      "Members are conducting more searches in your community, but the no-result rate has " \
        "increased. Review the content gaps to see what's missing.",
    )

    dashboard.search.click_content_gap_term("discobot")

    expect(page).to have_current_path("/search?q=discobot")
  end

  it "alerts staff when the no-result rate crosses the threshold",
     time: Time.zone.local(2026, 5, 14, 12, 0, 0) do
    Fabricate.times(5, :search_log, term: "ghost", user: user, created_at: "2026-05-10 10:00")
    Fabricate.times(
      5,
      :clicked_search_log,
      term: "ruby",
      user: user,
      created_at: "2026-05-10 11:00",
    )
    Fabricate.times(
      2,
      :search_log,
      term: "ghost",
      session_id: "browser-session",
      created_at: "2026-05-10 12:00",
    )

    dashboard.visit
    search = dashboard.search

    expect(search).to have_headline(
      "The total number of searches has increased in the selected period",
      "Members are conducting more searches in your community, but the no-result rate has " \
        "increased. Review the content gaps to see what's missing.",
    )
    expect(search).to have_total_searches_kpi("12")
    expect(search).to have_alert_no_result_rate_kpi("58%")
  end

  it "stays members-only and drills into member searches while crawler detection is disabled",
     time: Time.zone.local(2026, 5, 14, 12, 0, 0) do
    SiteSetting.improved_crawler_detection = false
    Fabricate.times(
      4,
      :clicked_search_log,
      term: "ruby",
      user: user,
      created_at: "2026-05-10 10:00",
    )
    Fabricate.times(2, :search_log, term: "crawlerbait", created_at: "2026-05-10 11:00")

    dashboard.visit
    search = dashboard.search

    expect(search).to have_total_searches_kpi("4")
    expect(search).to have_trending_rows([{ term: "ruby", searches: 4 }])
    expect(search).to have_no_trending_term("crawlerbait")

    search.click_trending_term("ruby")

    expect(Rack::Utils.parse_query(URI.parse(page.current_url).query)).to eq(
      "searchType" => "non_staff_only",
      "period" => "monthly",
      "term" => "ruby",
    )
  end

  it "shows staff a graceful empty state when no searches were logged" do
    dashboard.visit
    search = dashboard.search

    expect(search).to have_headline(
      "Not enough search activity to summarise yet.",
      "Pick a longer date range or come back once members have searched more.",
    )
    expect(search).to have_total_searches_kpi("0")
    expect(search).to have_no_result_rate_kpi("—")
    expect(search).to have_no_kpi_deltas
    expect(search).to have_trending_empty_state("No searches in this period.")
    expect(search).to have_content_gaps_empty_state("No content gaps in this period.")
  end

  it "tells staff when search logging is disabled instead of showing zeros" do
    SiteSetting.log_search_queries = false

    dashboard.visit
    search = dashboard.search

    expect(search).to have_logging_disabled_notice(
      "Search logging is disabled, so no search data is being collected. " \
        "Enable log search queries to start collecting it.",
    )
    expect(search).to have_no_kpis
  end

  it "asks moderators to contact an admin when search logging is disabled" do
    SiteSetting.log_search_queries = false
    sign_in(moderator)

    dashboard.visit

    expect(dashboard.search).to have_moderator_logging_disabled_notice(
      "Search logging is disabled, so no search data is being collected. " \
        "Ask an admin to enable log search queries to start collecting it.",
    )
  end

  it "shows staff search activity for a selected custom date range" do
    Fabricate(:clicked_search_log, term: "ruby", user: user, created_at: "2026-05-02 10:00")
    Fabricate.times(2, :search_log, term: "ruby", user: user, created_at: "2026-05-02 11:00")

    Fabricate.times(5, :search_log, term: "ruby", user: user, created_at: "2026-04-30 10:00")

    Fabricate(:search_log, term: "solo", user: user, created_at: "2026-04-25 10:00")

    dashboard.visit_with_query(range: "custom", start_date: "2026-05-01", end_date: "2026-05-03")
    search = dashboard.search

    expect(search).to have_headline(
      "The no-result rate has decreased in the selected period",
      "Members are finding what they're looking for, and total search volume has decreased.",
    )
    expect(search).to have_total_searches_kpi("3", worsening_delta: "-40%")
    expect(search).to have_no_result_rate_kpi("0%", improving_delta: "-100%")
    expect(search).to have_trending_rows([{ term: "ruby", searches: 3 }])

    search.click_trending_term("ruby")

    expect(page).to have_current_path("/admin/logs/search_logs/term", ignore_query: true)
    expect(Rack::Utils.parse_query(URI.parse(page.current_url).query)).to eq(
      "searchType" => "human_only",
      "period" => "all",
      "term" => "ruby",
    )

    dashboard.visit_with_query(range: "custom", start_date: "2026-04-25", end_date: "2026-04-25")

    expect(search).to have_headline(
      "The total number of searches has increased in the selected period",
      "Members are conducting more searches in your community, but the no-result rate has " \
        "increased. Review the content gaps to see what's missing.",
    )
  end
end