discourse/spec/lib/nested_replies/sort_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

51 lines
1.6 KiB
Ruby

# frozen_string_literal: true
RSpec.describe NestedReplies::Sort do
describe ".valid?" do
it "accepts top, new, old" do
expect(described_class.valid?("top")).to eq(true)
expect(described_class.valid?("new")).to eq(true)
expect(described_class.valid?("old")).to eq(true)
end
it "rejects unknown algorithms" do
expect(described_class.valid?("random")).to eq(false)
expect(described_class.valid?("")).to eq(false)
end
end
describe ".sort_in_memory" do
let(:posts) do
[
Struct.new(:like_count, :post_number, :created_at).new(5, 2, 3.days.ago),
Struct.new(:like_count, :post_number, :created_at).new(1, 3, 1.day.ago),
Struct.new(:like_count, :post_number, :created_at).new(10, 4, 2.days.ago),
]
end
it "sorts by top (like_count desc, post_number asc tiebreaker)" do
sorted = described_class.sort_in_memory(posts, "top")
expect(sorted.map(&:post_number)).to eq([4, 2, 3])
end
it "sorts by new (created_at desc)" do
sorted = described_class.sort_in_memory(posts, "new")
expect(sorted.map(&:post_number)).to eq([3, 4, 2])
end
it "sorts by old (post_number asc)" do
sorted = described_class.sort_in_memory(posts, "old")
expect(sorted.map(&:post_number)).to eq([2, 3, 4])
end
it "raises on invalid algorithm" do
expect { described_class.sort_in_memory(posts, "random") }.to raise_error(ArgumentError)
end
end
describe ".sql_order_expression" do
it "raises on invalid algorithm" do
expect { described_class.sql_order_expression("bogus") }.to raise_error(ArgumentError)
end
end
end