Skip to content

Commit ab5d27d

Browse files
committed
Fix PM seed refresh tracking
1 parent 31f2bdc commit ab5d27d

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

lib/termcourse/ui.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,12 +2352,13 @@ def maybe_refresh_pm_unread_count(now: Time.now)
23522352

23532353
def refresh_notification_unread_count
23542354
data = with_errors { @client.notification_totals }
2355-
return false unless data.is_a?(Hash)
2355+
return { notification: false, pm: false } unless data.is_a?(Hash)
23562356

23572357
count = data["unread_notifications"].to_i
23582358
@notification_unread_count = [count, 0].max
23592359
@live_updates&.set_unread_notification_count(@notification_unread_count)
23602360

2361+
pm_refreshed = false
23612362
pm_count =
23622363
if data.key?("unread_personal_messages")
23632364
data["unread_personal_messages"].to_i
@@ -2367,8 +2368,9 @@ def refresh_notification_unread_count
23672368
unless pm_count.nil?
23682369
@pm_unread_count = [pm_count, 0].max
23692370
@live_updates&.set_pm_unread_count(@pm_unread_count)
2371+
pm_refreshed = true
23702372
end
2371-
true
2373+
{ notification: true, pm: pm_refreshed }
23722374
end
23732375

23742376
def maybe_refresh_notification_unread_count(now: Time.now)
@@ -2379,7 +2381,7 @@ def maybe_refresh_notification_unread_count(now: Time.now)
23792381
return if @last_notification_unread_refresh_at && (now - @last_notification_unread_refresh_at) < @notification_unread_refresh_interval
23802382

23812383
refreshed = refresh_notification_unread_count
2382-
@last_notification_unread_refresh_at = now if refreshed
2384+
@last_notification_unread_refresh_at = now if refreshed[:notification]
23832385
end
23842386

23852387
def maybe_refresh_status_counts(now: Time.now)
@@ -2415,8 +2417,8 @@ def consume_topic_list_refresh_request(filter:, top_period:)
24152417

24162418
def seed_status_counts_from_server(now: Time.now)
24172419
notification_refreshed = refresh_notification_unread_count
2418-
pm_refreshed = @pm_unread_count.to_i > 0 ? true : refresh_pm_unread_count
2419-
@last_notification_unread_refresh_at = now if notification_refreshed
2420+
pm_refreshed = notification_refreshed[:pm] || refresh_pm_unread_count
2421+
@last_notification_unread_refresh_at = now if notification_refreshed[:notification]
24202422
@last_pm_unread_refresh_at = now if pm_refreshed
24212423
end
24222424

test/ui_caching_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,24 @@ def test_maybe_refresh_status_counts_resyncs_once_after_live_reconnect
292292
assert_equal 2, @ui.instance_variable_get(:@pm_unread_count)
293293
end
294294

295+
def test_seed_status_counts_from_server_refreshes_pm_when_notification_refresh_did_not_seed_pm
296+
@ui.instance_variable_set(:@pm_unread_count, 3)
297+
@ui.define_singleton_method(:refresh_notification_unread_count) { { notification: false, pm: false } }
298+
299+
pm_refreshed = false
300+
@ui.define_singleton_method(:refresh_pm_unread_count) do
301+
pm_refreshed = true
302+
true
303+
end
304+
305+
now = Time.utc(2026, 3, 15, 12, 0, 0)
306+
@ui.send(:seed_status_counts_from_server, now: now)
307+
308+
assert_equal true, pm_refreshed
309+
assert_nil @ui.instance_variable_get(:@last_notification_unread_refresh_at)
310+
assert_equal now, @ui.instance_variable_get(:@last_pm_unread_refresh_at)
311+
end
312+
295313
def test_mark_notification_read_decrements_local_and_live_unread_counts
296314
live_updates = FakeLiveUpdates.new(4, [], 0)
297315
notification = { "id" => 99, "read" => false }

test/ui_renderer_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,11 @@ def test_refresh_notification_unread_count_preserves_existing_count_on_failed_re
501501
@ui.instance_variable_set(:@notification_unread_count, 6)
502502
@ui.define_singleton_method(:with_errors) { nil }
503503

504-
@ui.send(:refresh_notification_unread_count)
504+
result = @ui.send(:refresh_notification_unread_count)
505505

506506
assert_equal 6, @ui.instance_variable_get(:@notification_unread_count)
507507
assert_equal 6, live_updates.unread_notification_count
508+
assert_equal false, result[:notification]
508509
end
509510

510511
def test_unread_notification_count_uses_live_zero_value

0 commit comments

Comments
 (0)