0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 09:44:06 +08:00
discourse/spec/lib/nested_replies/ancestor_walker_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

132 lines
4 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe NestedReplies do
fab!(:user)
fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:op) { Fabricate(:post, topic: topic, user: user, post_number: 1) }
before { SiteSetting.nested_replies_enabled = true }
def build_chain(depth)
posts = [op]
depth.times do |i|
reply_to = posts.last.post_number
reply_to = nil if reply_to == 1
posts << Fabricate(
:post,
topic: topic,
user: Fabricate(:user),
reply_to_post_number: reply_to,
)
end
posts
end
describe "basic walking" do
it "returns ancestors from start_post_number to root" do
chain = build_chain(4)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: chain[4].reply_to_post_number,
)
expect(results).to be_an(Array)
expect(results.length).to be >= 1
expect(results.first.depth).to eq(1)
end
it "returns empty array when start post does not exist" do
results = NestedReplies.walk_ancestors(topic_id: topic.id, start_post_number: 99_999)
expect(results).to be_empty
end
it "respects the limit parameter" do
chain = build_chain(5)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: chain.last.reply_to_post_number,
limit: 2,
)
expect(results.length).to be <= 2
end
end
describe "exclude_deleted option" do
it "skips deleted posts when exclude_deleted: true" do
chain = build_chain(3)
chain[2].update!(deleted_at: Time.current)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: chain[3].reply_to_post_number,
exclude_deleted: true,
)
post_numbers = results.map(&:post_number)
expect(post_numbers).not_to include(chain[2].post_number)
end
it "includes deleted posts when exclude_deleted: false" do
chain = build_chain(3)
chain[2].update!(deleted_at: Time.current)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: chain[3].reply_to_post_number,
exclude_deleted: false,
)
post_numbers = results.map(&:post_number)
expect(post_numbers).to include(chain[2].post_number)
end
end
describe "stop_at_op option" do
it "stops before OP when stop_at_op: true" do
reply_to_op = Fabricate(:post, topic: topic, user: user, reply_to_post_number: 1)
child =
Fabricate(:post, topic: topic, user: user, reply_to_post_number: reply_to_op.post_number)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: child.reply_to_post_number,
stop_at_op: true,
)
post_numbers = results.map(&:post_number)
expect(post_numbers).to include(reply_to_op.post_number)
expect(post_numbers).not_to include(1)
end
it "includes OP when stop_at_op: false" do
reply_to_op = Fabricate(:post, topic: topic, user: user, reply_to_post_number: 1)
child =
Fabricate(:post, topic: topic, user: user, reply_to_post_number: reply_to_op.post_number)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: child.reply_to_post_number,
stop_at_op: false,
)
post_numbers = results.map(&:post_number)
expect(post_numbers).to include(1)
end
end
describe "depth tracking" do
it "assigns correct depth values" do
chain = build_chain(3)
results =
NestedReplies.walk_ancestors(
topic_id: topic.id,
start_post_number: chain[3].reply_to_post_number,
)
expect(results.length).to eq(2)
result_by_depth = results.index_by(&:depth)
expect(result_by_depth[1].post_number).to eq(chain[2].post_number)
expect(result_by_depth[2].post_number).to eq(chain[1].post_number)
end
end
end