From 3d9493606934be7da193d40642b34abe998360d8 Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Mon, 19 Oct 2026 00:11:44 +0200 Subject: [PATCH 1/2] Show the latest page when a refresh misses more than one After the cable reconnects, or a page hidden for over a minute comes back, the room asks for what was posted since the newest message it has. The refresh answered with the first page of that, appended. With more than a page posted meanwhile, the rest never arrived: the page still counted itself up to date, so nothing asked for more, and the next live message landed right after that first page, with the gap above it unmarked until a reload. When more than a page was posted since, the refresh now sends the latest page in place of the messages shown, the way reloading the room does, and older messages load on scrolling up as they do after a reload. The page keeps what the latest page can't know about: a message that arrived live while the refresh was on its way, and our own messages still being sent or that failed to send, whose text is nowhere else. A reader of older messages, from a search result or scrolled up past what the page holds, keeps their place instead, and still gets the edits and boosts of the messages on screen. The latest page loads when they return to it, as it already does. Co-Authored-By: Claude Opus 5.5 --- app/controllers/rooms/refreshes_controller.rb | 13 +++- .../controllers/messages_controller.js | 25 ++++++ app/models/message/pagination.rb | 3 +- .../rooms/refreshes/show.turbo_stream.erb | 12 ++- .../rooms/refreshes_controller_test.rb | 42 ++++++++++ test/system/catching_up_test.rb | 77 +++++++++++++++++++ 6 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 test/system/catching_up_test.rb diff --git a/app/controllers/rooms/refreshes_controller.rb b/app/controllers/rooms/refreshes_controller.rb index 2c2171ee9..fbb53a294 100644 --- a/app/controllers/rooms/refreshes_controller.rb +++ b/app/controllers/rooms/refreshes_controller.rb @@ -4,8 +4,17 @@ class Rooms::RefreshesController < ApplicationController before_action :set_last_updated_at def show - @new_messages = @room.messages.with_presentation.page_created_since(@last_updated_at) - @updated_messages = @room.messages.without(@new_messages).with_presentation.page_updated_since(@last_updated_at) + # Appending only the first page of what was posted meanwhile would leave the rest out for good: the page counts + # itself up to date, and the next live message lands right after that first page. So when more was posted than a + # page holds, the latest page takes the place of the messages shown, as reloading the room would. + if @room.messages.created_since(@last_updated_at).paged? + @last_page = @room.messages.with_presentation.last_page + # A reader of older messages keeps them in place of the latest page, and still gets what changed on them. + @updated_messages = @room.messages.where(created_at: ..@last_updated_at).with_presentation.page_updated_since(@last_updated_at) + else + @new_messages = @room.messages.with_presentation.page_created_since(@last_updated_at) + @updated_messages = @room.messages.without(@new_messages).with_presentation.page_updated_since(@last_updated_at) + end end private diff --git a/app/javascript/controllers/messages_controller.js b/app/javascript/controllers/messages_controller.js index 905e42686..a237028fb 100644 --- a/app/javascript/controllers/messages_controller.js +++ b/app/javascript/controllers/messages_controller.js @@ -62,15 +62,19 @@ export default class extends Controller { if (target === this.messagesTarget.id) { const render = event.detail.render const upToDate = this.#paginator.upToDate + const replacing = event.detail.newStream.getAttribute("action") === "update" if (upToDate) { event.detail.render = async (streamElement) => { + const kept = replacing ? this.#messagesUnknownTo(streamElement) : [] + const didScroll = await this.#scrollManager.autoscroll(false, async () => { await render(streamElement) await nextEventLoopTick() this.#positionLastMessage() this.#playSoundForLastMessage() + this.messagesTarget.append(...kept) this.#paginator.trimExcessMessages(true) }) if (!didScroll) { @@ -78,6 +82,9 @@ export default class extends Controller { } } } else { + // Older messages are on screen, so the latest page would only take the reader's place. Returning to the + // latest loads it anyway. + if (replacing) event.detail.render = () => {} this.latestTarget.hidden = false } } @@ -204,6 +211,24 @@ export default class extends Controller { return followingMessage } + // A refresh that missed more than a page replaces the messages with the latest one. That page can't know about + // messages that arrived live after it was rendered, or about our own still being sent or that failed to send, whose + // text is nowhere else: those stay. + #messagesUnknownTo(streamElement) { + const page = Array.from(streamElement.templateElement.content.querySelectorAll(".message")) + const pageIds = new Set(page.map((message) => message.id)) + const pageClientMessageIds = new Set(page.map((message) => message.dataset.clientMessageId)) + const pageEnd = Math.max(0, ...page.map((message) => this.#sortValue(message))) + + return Array.from(this.messagesTarget.children).filter((element) => { + if (element.dataset.pendingMessage) { + return !pageClientMessageIds.has(element.dataset.clientMessageId) + } else { + return element.dataset.messageId && !pageIds.has(element.id) && this.#sortValue(element) >= pageEnd + } + }) + } + #sortValue(node) { return (node && parseInt(node.dataset.sortValue)) || 0 } diff --git a/app/models/message/pagination.rb b/app/models/message/pagination.rb index 4e3d74337..e1faec521 100644 --- a/app/models/message/pagination.rb +++ b/app/models/message/pagination.rb @@ -35,7 +35,8 @@ def preload_associations(records) scope :page_before, ->(message) { before(message).last_page } scope :page_after, ->(message) { after(message).first_page } - scope :page_created_since, ->(time) { where("created_at > ?", time).first_page } + scope :created_since, ->(time) { where("created_at > ?", time) } + scope :page_created_since, ->(time) { created_since(time).first_page } # Sorting on +created_at, which no index covers, has SQLite find the few messages updated since # through (room_id, updated_at) and sort just those, instead of walking the whole room by # (room_id, created_at) looking for them. diff --git a/app/views/rooms/refreshes/show.turbo_stream.erb b/app/views/rooms/refreshes/show.turbo_stream.erb index d2d485f1e..7556c5f6f 100644 --- a/app/views/rooms/refreshes/show.turbo_stream.erb +++ b/app/views/rooms/refreshes/show.turbo_stream.erb @@ -1,6 +1,12 @@ -<%= turbo_stream.append dom_id(@room, :messages) do %> - <%= render partial: "messages/message", collection: @new_messages, cached: ->(message) { message_fragment_cache_key(message) } %> -<% end if @new_messages.any? %> +<% if @last_page %> + <%= turbo_stream.update dom_id(@room, :messages) do %> + <%= render partial: "messages/message", collection: @last_page, cached: ->(message) { message_fragment_cache_key(message) } %> + <% end %> +<% else %> + <%= turbo_stream.append dom_id(@room, :messages) do %> + <%= render partial: "messages/message", collection: @new_messages, cached: ->(message) { message_fragment_cache_key(message) } %> + <% end if @new_messages.any? %> +<% end %> <% @updated_messages.each do |message| %> <%= turbo_stream.replace dom_id(message), partial: "messages/message", locals: { message: message } %> diff --git a/test/controllers/rooms/refreshes_controller_test.rb b/test/controllers/rooms/refreshes_controller_test.rb index 392d03dfa..42032a5c8 100644 --- a/test/controllers/rooms/refreshes_controller_test.rb +++ b/test/controllers/rooms/refreshes_controller_test.rb @@ -29,4 +29,46 @@ class Rooms::RefreshesControllerTest < ActionDispatch::IntegrationTest assert_select "template", count: 1 end end + + test "refresh appends a whole page of new messages" do + missed = post_messages(Message::PAGE_SIZE) + + get room_refresh_url(rooms(:hq), format: :turbo_stream), params: { since: 10.minutes.ago.to_fs(:epoch) } + + assert_response :success + assert_select "turbo-stream[action='update']", count: 0 + assert_equal missed.size, missed.count { |message| css_select("turbo-stream[action='append'] #" + dom_id(message)).any? } + end + + test "refresh replaces the messages with the latest page when more than a page was posted" do + travel_to 1.day.ago do + @old_message = rooms(:hq).messages.create!(creator: users(:jason), body: "Old message", client_message_id: "old") + end + travel_to(1.minute.ago) { @old_message.touch } + missed = post_messages(Message::PAGE_SIZE + 5) + + get room_refresh_url(rooms(:hq), format: :turbo_stream), params: { since: 10.minutes.ago.to_fs(:epoch) } + + assert_response :success + assert_select "turbo-stream[action='append']", count: 0 + assert_select "turbo-stream[action='update'][target='#{dom_id(rooms(:hq), :messages)}']" do + assert_select "[data-message-id]", count: Message::PAGE_SIZE + missed.last(Message::PAGE_SIZE).each { |message| assert_select "#" + dom_id(message) } + end + + # Only for a message the page already had: someone reading older messages keeps them on screen. + assert_select "turbo-stream[action='replace']", count: 1 + assert_select "turbo-stream[action='replace'][target='#{dom_id(@old_message)}']" + end + + private + def post_messages(count) + base = 5.minutes.ago + + count.times.map do |i| + travel_to(base + i.seconds) do + rooms(:hq).messages.create!(creator: users(:jason), body: "Missed #{i}", client_message_id: "missed-#{i}") + end + end + end end diff --git a/test/system/catching_up_test.rb b/test/system/catching_up_test.rb new file mode 100644 index 000000000..7e267b382 --- /dev/null +++ b/test/system/catching_up_test.rb @@ -0,0 +1,77 @@ +require "application_system_test_case" + +class CatchingUpTest < ApplicationSystemTestCase + setup do + sign_in "jz@37signals.com" + end + + test "after missing more than a page, the room shows the latest page with nothing left out in between" do + join_room rooms(:designers) + + miss_messages 45 + reconnect + + assert_message_text "Missed 44", wait: 30 + sleep 3 # let a second refresh, if any, land + assert_equal (5..44).to_a, missed_messages_shown + assert_selector ".message:last-child .message__body", text: "Missed 44" + end + + test "replacing the messages keeps the ones the latest page can't know about" do + join_room rooms(:designers) + + # One that arrived live after the page was rendered, and one of ours that failed to send. + append_to_messages <<~HTML +
Arrived live
+
Unsent words
+ HTML + + miss_messages 45 + reconnect + + assert_message_text "Missed 44", wait: 30 + last_three = all("##{dom_id(rooms(:designers), :messages)} > .message").last(3).map(&:text) + assert_match "Missed 44", last_three.first + assert_equal [ "Arrived live", "Unsent words" ], last_three.last(2) + end + + test "a reader of older messages keeps their place when more than a page was missed" do + # Enough history after the message that the end of the page around it stays below the fold. + miss_messages 60, label: "Earlier" + visit room_at_message_url(rooms(:designers), messages(:first)) + wait_for_cable_connection + dismiss_pwa_install_prompt + + miss_messages 45 + reconnect + + assert_selector ".message-area__return-to-latest", visible: true, wait: 30 + assert_selector ".search-highlight", text: messages(:first).plain_text_body + assert_no_message_text "Missed 44" + end + + private + # Created without the controller, so none of them is broadcast: this browser misses them all live. + def miss_messages(count, label: "Missed") + count.times do |i| + rooms(:designers).messages.create!(creator: users(:kevin), body: "#{label} #{i}", client_message_id: "#{label.downcase}-#{i}") + end + end + + # Without a prompt reconnect, Action Cable's monitor notices the dropped socket only after its stale threshold. + def reconnect + ActionCable.server.remote_connections.where(current_user: users(:jz)).disconnect(reconnect: true) + end + + def missed_messages_shown + all(".message__body", text: /Missed \d+/).map { |body| body.text[/Missed (\d+)/, 1].to_i } + end + + def append_to_messages(html) + page.execute_script("document.getElementById(arguments[0]).insertAdjacentHTML('beforeend', arguments[1])", dom_id(rooms(:designers), :messages), html) + end + + def assert_no_message_text(text) + assert_no_selector ".message__body", text: text + end +end From c7b4b4be633b8443bae198bf56e2cd3aea3a5861 Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Mon, 19 Oct 2026 00:56:08 +0200 Subject: [PATCH 2/2] Load what a refresh missed past one page instead of replacing the list Replacing the messages with the latest page dropped whatever the page held that the latest page didn't: a message being edited, one queued to render, the place of someone reading a page up. Each fix for one of those left another open. The list now only grows. The refresh appends the first page of what was missed, as it always has, and says when there's more. The page then counts itself behind and loads the rest in order as its end comes into view, the way it already loads newer pages after a search result. Until it has caught up, a live message is held instead of appended after a gap, and added once the list reaches its end. A message appended live just before a refresh, and so above the ones the refresh missed, moves into order when the page that has it arrives. Co-Authored-By: Claude Opus 5.5 --- app/controllers/rooms/refreshes_controller.rb | 16 ++---- .../controllers/messages_controller.js | 53 +++++++++--------- app/javascript/models/message_paginator.js | 5 ++ .../rooms/refreshes/show.turbo_stream.erb | 11 +--- .../rooms/refreshes_controller_test.rb | 23 +++----- test/system/catching_up_test.rb | 55 +++++++++++-------- 6 files changed, 82 insertions(+), 81 deletions(-) diff --git a/app/controllers/rooms/refreshes_controller.rb b/app/controllers/rooms/refreshes_controller.rb index fbb53a294..6b7eb30a7 100644 --- a/app/controllers/rooms/refreshes_controller.rb +++ b/app/controllers/rooms/refreshes_controller.rb @@ -4,17 +4,11 @@ class Rooms::RefreshesController < ApplicationController before_action :set_last_updated_at def show - # Appending only the first page of what was posted meanwhile would leave the rest out for good: the page counts - # itself up to date, and the next live message lands right after that first page. So when more was posted than a - # page holds, the latest page takes the place of the messages shown, as reloading the room would. - if @room.messages.created_since(@last_updated_at).paged? - @last_page = @room.messages.with_presentation.last_page - # A reader of older messages keeps them in place of the latest page, and still gets what changed on them. - @updated_messages = @room.messages.where(created_at: ..@last_updated_at).with_presentation.page_updated_since(@last_updated_at) - else - @new_messages = @room.messages.with_presentation.page_created_since(@last_updated_at) - @updated_messages = @room.messages.without(@new_messages).with_presentation.page_updated_since(@last_updated_at) - end + @new_messages = @room.messages.with_presentation.page_created_since(@last_updated_at) + @updated_messages = @room.messages.without(@new_messages).with_presentation.page_updated_since(@last_updated_at) + # More was posted than one page holds. The page is told so, and loads the rest as it does when scrolling down, + # instead of counting itself up to date after the first page. + @more_messages = @room.messages.created_since(@last_updated_at).paged? end private diff --git a/app/javascript/controllers/messages_controller.js b/app/javascript/controllers/messages_controller.js index a237028fb..6544a3604 100644 --- a/app/javascript/controllers/messages_controller.js +++ b/app/javascript/controllers/messages_controller.js @@ -14,6 +14,7 @@ export default class extends Controller { #paginator #formatter #scrollManager + #heldMessages = [] // Lifecycle @@ -62,19 +63,21 @@ export default class extends Controller { if (target === this.messagesTarget.id) { const render = event.detail.render const upToDate = this.#paginator.upToDate - const replacing = event.detail.newStream.getAttribute("action") === "update" if (upToDate) { - event.detail.render = async (streamElement) => { - const kept = replacing ? this.#messagesUnknownTo(streamElement) : [] + // A refresh that missed more than it sends: the rest loads in order as the end of the list comes into view. + if (event.detail.newStream.hasAttribute("incomplete")) { + this.#paginator.upToDate = false + this.latestTarget.hidden = false + } + event.detail.render = async (streamElement) => { const didScroll = await this.#scrollManager.autoscroll(false, async () => { await render(streamElement) await nextEventLoopTick() this.#positionLastMessage() this.#playSoundForLastMessage() - this.messagesTarget.append(...kept) this.#paginator.trimExcessMessages(true) }) if (!didScroll) { @@ -82,9 +85,10 @@ export default class extends Controller { } } } else { - // Older messages are on screen, so the latest page would only take the reader's place. Returning to the - // latest loads it anyway. - if (replacing) event.detail.render = () => {} + // The end of the list isn't the latest message, so one appended there would leave a gap above it. It waits + // until the list has caught up. + this.#heldMessages.push(event.detail.newStream.templateContent) + event.detail.render = () => {} this.latestTarget.hidden = false } } @@ -128,6 +132,7 @@ export default class extends Controller { #allContentViewed() { this.latestTarget.hidden = true + this.#appendHeldMessages() } @@ -155,6 +160,22 @@ export default class extends Controller { async #ensureUpToDate() { if (!this.#paginator.upToDate) { await this.#paginator.resetToLastPage() + this.#appendHeldMessages() + } + } + + // Messages that arrived while the list was catching up, once it has. Only those past its end: the others it fetched on + // the way, or they sit above the latest page and load on scrolling up. + #appendHeldMessages() { + let end = Math.max(0, ...Array.from(this.messagesTarget.children, (element) => this.#sortValue(element))) + + for (const fragment of this.#heldMessages.splice(0)) { + for (const message of fragment.querySelectorAll(".message")) { + if (this.#sortValue(message) > end) { + this.messagesTarget.append(message) + end = this.#sortValue(message) + } + } } } @@ -211,24 +232,6 @@ export default class extends Controller { return followingMessage } - // A refresh that missed more than a page replaces the messages with the latest one. That page can't know about - // messages that arrived live after it was rendered, or about our own still being sent or that failed to send, whose - // text is nowhere else: those stay. - #messagesUnknownTo(streamElement) { - const page = Array.from(streamElement.templateElement.content.querySelectorAll(".message")) - const pageIds = new Set(page.map((message) => message.id)) - const pageClientMessageIds = new Set(page.map((message) => message.dataset.clientMessageId)) - const pageEnd = Math.max(0, ...page.map((message) => this.#sortValue(message))) - - return Array.from(this.messagesTarget.children).filter((element) => { - if (element.dataset.pendingMessage) { - return !pageClientMessageIds.has(element.dataset.clientMessageId) - } else { - return element.dataset.messageId && !pageIds.has(element.id) && this.#sortValue(element) >= pageEnd - } - }) - } - #sortValue(node) { return (node && parseInt(node.dataset.sortValue)) || 0 } diff --git a/app/javascript/models/message_paginator.js b/app/javascript/models/message_paginator.js index 771698b06..c300c0fc0 100644 --- a/app/javascript/models/message_paginator.js +++ b/app/javascript/models/message_paginator.js @@ -162,6 +162,11 @@ export default class MessagePaginator { const lastNewElement = page.lastElementChild keepScroll(this.#container, top, () => { + // A message appended live just before a refresh sits above the ones the refresh missed. Keep the copy in order. + for (const message of page.querySelectorAll(".message")) { + this.#container.querySelector(`#${CSS.escape(message.id)}`)?.remove() + } + insertHTMLFragment(page, this.#container, top) // Ensure formatting is correct over page boundaries diff --git a/app/views/rooms/refreshes/show.turbo_stream.erb b/app/views/rooms/refreshes/show.turbo_stream.erb index 7556c5f6f..c97a90650 100644 --- a/app/views/rooms/refreshes/show.turbo_stream.erb +++ b/app/views/rooms/refreshes/show.turbo_stream.erb @@ -1,11 +1,6 @@ -<% if @last_page %> - <%= turbo_stream.update dom_id(@room, :messages) do %> - <%= render partial: "messages/message", collection: @last_page, cached: ->(message) { message_fragment_cache_key(message) } %> - <% end %> -<% else %> - <%= turbo_stream.append dom_id(@room, :messages) do %> - <%= render partial: "messages/message", collection: @new_messages, cached: ->(message) { message_fragment_cache_key(message) } %> - <% end if @new_messages.any? %> +<% if @new_messages.any? %> + <%= turbo_stream_action_tag :append, target: dom_id(@room, :messages), incomplete: (true if @more_messages), + template: render(partial: "messages/message", formats: :html, collection: @new_messages, cached: ->(message) { message_fragment_cache_key(message) }) %> <% end %> <% @updated_messages.each do |message| %> diff --git a/test/controllers/rooms/refreshes_controller_test.rb b/test/controllers/rooms/refreshes_controller_test.rb index 42032a5c8..a663d66ec 100644 --- a/test/controllers/rooms/refreshes_controller_test.rb +++ b/test/controllers/rooms/refreshes_controller_test.rb @@ -30,35 +30,28 @@ class Rooms::RefreshesControllerTest < ActionDispatch::IntegrationTest end end - test "refresh appends a whole page of new messages" do + test "refresh appends a whole page of new messages as all there is" do missed = post_messages(Message::PAGE_SIZE) get room_refresh_url(rooms(:hq), format: :turbo_stream), params: { since: 10.minutes.ago.to_fs(:epoch) } assert_response :success - assert_select "turbo-stream[action='update']", count: 0 - assert_equal missed.size, missed.count { |message| css_select("turbo-stream[action='append'] #" + dom_id(message)).any? } + assert_select "turbo-stream[action='append'][incomplete]", count: 0 + assert_select "turbo-stream[action='append'][target='#{dom_id(rooms(:hq), :messages)}']" do + missed.each { |message| assert_select "#" + dom_id(message) } + end end - test "refresh replaces the messages with the latest page when more than a page was posted" do - travel_to 1.day.ago do - @old_message = rooms(:hq).messages.create!(creator: users(:jason), body: "Old message", client_message_id: "old") - end - travel_to(1.minute.ago) { @old_message.touch } + test "refresh says when more was posted than the page it appends" do missed = post_messages(Message::PAGE_SIZE + 5) get room_refresh_url(rooms(:hq), format: :turbo_stream), params: { since: 10.minutes.ago.to_fs(:epoch) } assert_response :success - assert_select "turbo-stream[action='append']", count: 0 - assert_select "turbo-stream[action='update'][target='#{dom_id(rooms(:hq), :messages)}']" do + assert_select "turbo-stream[action='append'][incomplete][target='#{dom_id(rooms(:hq), :messages)}']" do assert_select "[data-message-id]", count: Message::PAGE_SIZE - missed.last(Message::PAGE_SIZE).each { |message| assert_select "#" + dom_id(message) } + missed.first(Message::PAGE_SIZE).each { |message| assert_select "#" + dom_id(message) } end - - # Only for a message the page already had: someone reading older messages keeps them on screen. - assert_select "turbo-stream[action='replace']", count: 1 - assert_select "turbo-stream[action='replace'][target='#{dom_id(@old_message)}']" end private diff --git a/test/system/catching_up_test.rb b/test/system/catching_up_test.rb index 7e267b382..477dff20f 100644 --- a/test/system/catching_up_test.rb +++ b/test/system/catching_up_test.rb @@ -5,55 +5,66 @@ class CatchingUpTest < ApplicationSystemTestCase sign_in "jz@37signals.com" end - test "after missing more than a page, the room shows the latest page with nothing left out in between" do + test "after missing more than a page, the room shows every missed message once, in order" do join_room rooms(:designers) + missed = miss_messages(45) - miss_messages 45 + # A copy of one of them, appended live just before the refresh, out of order. + append_to_messages <<~HTML +
Early copy
+ HTML reconnect assert_message_text "Missed 44", wait: 30 - sleep 3 # let a second refresh, if any, land - assert_equal (5..44).to_a, missed_messages_shown - assert_selector ".message:last-child .message__body", text: "Missed 44" + assert_equal (0..44).to_a, missed_messages_shown + assert_no_message_text "Early copy" end - test "replacing the messages keeps the ones the latest page can't know about" do + test "catching up leaves a message being edited as it is" do join_room rooms(:designers) - - # One that arrived live after the page was rendered, and one of ours that failed to send. - append_to_messages <<~HTML -
Arrived live
-
Unsent words
- HTML + within_message messages(:third) do + reveal_message_actions + find(".message__edit-btn").click + end + assert_selector "##{dom_id(messages(:third))} .message__body-content--editing" miss_messages 45 reconnect assert_message_text "Missed 44", wait: 30 - last_three = all("##{dom_id(rooms(:designers), :messages)} > .message").last(3).map(&:text) - assert_match "Missed 44", last_three.first - assert_equal [ "Arrived live", "Unsent words" ], last_three.last(2) + assert_selector "##{dom_id(messages(:third))} .message__body-content--editing" end - test "a reader of older messages keeps their place when more than a page was missed" do + test "a message that arrives while the list isn't at its end waits, then shows in order" do # Enough history after the message that the end of the page around it stays below the fold. - miss_messages 60, label: "Earlier" + earlier = miss_messages(60, label: "Earlier") visit room_at_message_url(rooms(:designers), messages(:first)) wait_for_cable_connection dismiss_pwa_install_prompt - miss_messages 45 - reconnect + # One older than the latest page, as if it had been held for long, and one just now. + Turbo::StreamsChannel.broadcast_append_to rooms(:designers), :messages, target: dom_id(rooms(:designers), :messages), html: <<~HTML +
Held for long
+
Arrived live
+ HTML - assert_selector ".message-area__return-to-latest", visible: true, wait: 30 + assert_selector ".message-area__return-to-latest", visible: true, wait: 10 + assert_no_message_text "Arrived live" assert_selector ".search-highlight", text: messages(:first).plain_text_body - assert_no_message_text "Missed 44" + + # Sending one brings the list to its end first, and the held message comes before ours. + send_message "Mine" + + last_two = all("##{dom_id(rooms(:designers), :messages)} > .message").last(2).map(&:text) + assert_equal "Arrived live", last_two.first + assert_match "Mine", last_two.last + assert_no_message_text "Held for long" end private # Created without the controller, so none of them is broadcast: this browser misses them all live. def miss_messages(count, label: "Missed") - count.times do |i| + count.times.map do |i| rooms(:designers).messages.create!(creator: users(:kevin), body: "#{label} #{i}", client_message_id: "#{label.downcase}-#{i}") end end