discourse/spec/system/nested_category_default_spec.rb
Mark VanLandingham 8feeaa5d69
FEATURE: First iteration of nested replies (#38888)
### Super high level description:
Adds a nested/threaded view for Discourse topics, allowing posts to be
displayed as an indented reply tree instead of the default flat
chronological stream.

  Backend:
- New /n/:slug/:topic_id routes serving roots, children (paginated), and
context (ancestor chain) endpoints
- TreeLoader recursively fetches reply trees with configurable max
depth, Sort supports top/new/old ordering
- NestedViewPostStat caches per-post reply counts (direct + total
descendants, whisper-aware) with a backfill job for existing data
  - NestedTopic model tracks per-topic opt-in and pinned post

  Frontend:
- Recursive <NestedPost> / <NestedPostChildren> components with
lazy-load expansion, cloaking, and scroll tracking
- NestedViewCache service preserves expansion state and scroll position
across back/forward navigation (15 entries, 10min TTL)
- Context view for deep-linking to a specific post with its ancestor
chain
  - Floating actions bar, sort selector, real-time MessageBus updates

Site settings (hidden): nested_replies_enabled, nested_replies_default,
nested_replies_default_sort, nested_replies_max_depth,
nested_replies_cap_nesting_depth, nested_replies_toggle_mode_groups,
plus a per-category default override.

Co-authored-by: Rafael Silva <xfalcox@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Sérgio Saquetim <saquetim@discourse.org>
2026-04-16 08:06:44 -05:00

137 lines
5 KiB
Ruby

# frozen_string_literal: true
require_relative "../support/nested_replies_helpers"
RSpec.describe "Nested view category default" do
include NestedRepliesHelpers
fab!(:admin)
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:category)
fab!(:nested_category) { Fabricate(:category, name: "Nested Category") }
fab!(:topic) { Fabricate(:topic, user: user, category: nested_category) }
fab!(:op) { Fabricate(:post, topic: topic, user: user, post_number: 1) }
fab!(:reply) { Fabricate(:post, topic: topic, user: Fabricate(:user), raw: "A reply") }
let(:nested_view) { PageObjects::Pages::NestedView.new }
let(:category_page) { PageObjects::Pages::Category.new }
let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
before do
SiteSetting.nested_replies_enabled = true
SiteSetting.enable_simplified_category_creation = false
nested_category.category_setting.update!(nested_replies_default: true)
Fabricate(:nested_topic, topic: topic)
end
describe "category settings UI" do
before { sign_in(admin) }
it "allows admin to enable nested view default for a category" do
unchecked_category = Fabricate(:category, name: "Unchecked Category")
category_page.visit_settings(unchecked_category)
expect(page).to have_css(
".enable-nested-replies-default input[type='checkbox']:not(:checked)",
visible: :all,
)
find(".enable-nested-replies-default label.checkbox-label").click
category_page.save_settings
expect(page).to have_current_path(%r{/c/#{unchecked_category.slug}})
unchecked_category.reload
expect(unchecked_category.nested_replies_default).to eq(true)
end
it "shows checkbox as checked when category has nested default enabled" do
category_page.visit_settings(nested_category)
expect(page).to have_css(
".enable-nested-replies-default input[type='checkbox']:checked",
visible: :all,
)
end
context "with simplified category creation" do
before { SiteSetting.enable_simplified_category_creation = true }
it "allows admin to enable nested view default for a category" do
unchecked_category = Fabricate(:category, name: "Unchecked Category")
category_page.visit_settings(unchecked_category)
form.field("category_setting.nested_replies_default").toggle
category_page.save_settings
unchecked_category.reload
expect(unchecked_category.nested_replies_default).to eq(true)
end
it "shows checkbox as checked when category has nested default enabled" do
category_page.visit_settings(nested_category)
expect(form.field("category_setting.nested_replies_default")).to be_checked
end
end
end
describe "topic redirect" do
before { sign_in(user) }
it "redirects to nested view when visiting a topic URL directly" do
page.visit("/t/#{topic.slug}/#{topic.id}")
expect(page).to have_current_path(%r{/n/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_nested_view
end
it "redirects to nested view when clicking a topic from the category page" do
page.visit("/c/#{nested_category.slug}/#{nested_category.id}")
find(".topic-list-item .raw-topic-link[data-topic-id='#{topic.id}']").click
expect(page).to have_current_path(%r{/n/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_nested_view
end
it "does not redirect topics in categories without nested default" do
normal_topic = Fabricate(:topic, user: user, category: category)
Fabricate(:post, topic: normal_topic, user: user, post_number: 1)
page.visit("/t/#{normal_topic.slug}/#{normal_topic.id}")
expect(page).to have_current_path(%r{/t/#{normal_topic.slug}/#{normal_topic.id}})
expect(nested_view).to have_no_nested_view
end
it "respects ?flat=1 to force flat view even in nested-default category" do
page.visit("/t/#{topic.slug}/#{topic.id}?flat=1")
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(page).to have_current_path(/flat=1/)
expect(nested_view).to have_no_nested_view
end
it "does not redirect to nested when navigating within flat view (e.g. topic timeline)" do
page.visit("/t/#{topic.slug}/#{topic.id}?flat=1")
expect(nested_view).to have_no_nested_view
# Simulate the exact code path the topic timeline uses:
# topic.urlForPostNumber() → DiscourseURL.routeTo()
# This exercises the topic-url-for-post-number transformer which rewrites
# URLs to /nested/ for nested-default categories.
page.execute_script(<<~JS)
(function() {
var topic = Discourse.lookup("controller:topic").model;
var url = topic.urlForPostNumber(#{reply.post_number});
require("discourse/lib/url").default.routeTo(url);
})();
JS
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_no_nested_view
end
end
end