Files
fizzy/test/models/notifier/event_notifier_test.rb
T
Kevin McConnell 59dd8ca549 Make IDs more time-sortable in tests
In order for model ordering to work as expected in tests, we need to
keep two properties:

- Fixtures are all created in the past
- Models sort in the order that they were created

This allows us to do things like this:

    post cards_path, params: { ... }
    created_card = Card.last

When using UUIDv7 PKs rather than sequential integers, we have to make
sure a couple of things happen in order for this still to be true:

- Fixtures should generate deterministic IDs that translate to UUIDs
  that would have been created in the past (i.e. before today)
- Newly created objects must have enough precision in their timestamps
  so that they sort in the order they were created, and their random
  component doesn't come into play.

To solve this, we use the deterministic numeric ID as a number of
milliseconds after an early year. And we ensure that the new timestamps
we create have sub-millisecond precision.
2025-11-17 09:12:40 -05:00

104 lines
3.4 KiB
Ruby

require "test_helper"
class Notifier::EventNotifierTest < ActiveSupport::TestCase
test "for returns the matching notifier class for the event" do
assert_kind_of Notifier::CardEventNotifier, Notifier.for(events(:logo_published))
end
test "generate does not create notifications if the event was system-generated" do
cards(:logo).drafted!
events(:logo_published).update!(creator: User.system)
assert_no_difference -> { Notification.count } do
Notifier.for(events(:logo_published)).notify
end
end
test "creates a notification for each watcher, other than the event creator (events)" do
notifications = Notifier.for(events(:layout_commented)).notify
assert_equal [ users(:kevin) ], notifications.map(&:user)
end
test "creates a notification for each watcher (mentions)" do
notifications = Notifier.for(events(:layout_commented)).notify
assert_equal [ users(:kevin) ], notifications.map(&:user)
end
test "does not create a notification for access-only users" do
boards(:writebook).access_for(users(:kevin)).access_only!
notifications = Notifier.for(events(:layout_commented)).notify
assert_equal [ users(:kevin) ], notifications.map(&:user)
end
test "links to the card" do
boards(:writebook).access_for(users(:kevin)).watching!
Notifier.for(events(:logo_published)).notify
assert_equal cards(:logo), Notification.last.source.eventable
end
test "assignment events only create a notification for the assignee" do
boards(:writebook).access_for(users(:jz)).watching!
boards(:writebook).access_for(users(:kevin)).watching!
notifications = Notifier.for(events(:logo_assignment_jz)).notify
assert_equal [ users(:jz) ], notifications.map(&:user)
end
test "assignment events do not notify users who are access-only for the board" do
boards(:writebook).access_for(users(:jz)).watching!
events(:logo_assignment_jz).update! creator: users(:jz)
notifications = Notifier.for(events(:logo_assignment_jz)).notify
assert_empty notifications
end
test "assignment events do not notify you if you assigned yourself" do
boards(:writebook).access_for(users(:david)).watching!
notifications = Notifier.for(events(:logo_assignment_david)).notify
assert_empty notifications
end
test "create notifications on publish for mentionees" do
users(:kevin).mentioned_by(users(:david), at: cards(:logo))
assert_difference -> { users(:kevin).notifications.count }, +1 do
Notifier.for(events(:logo_published)).notify
end
end
test "don'create notifications on publish for mentionees that are not watching" do
users(:kevin).mentioned_by(users(:david), at: cards(:logo))
cards(:logo).unwatch_by(users(:kevin))
assert_difference -> { users(:kevin).notifications.count }, +1 do
Notifier.for(events(:logo_published)).notify
end
end
test "don't create notifications on comment for mentionees" do
users(:david).mentioned_by(users(:kevin), at: cards(:layout))
assert_no_difference -> { users(:david).notifications.count } do
Notifier.for(events(:layout_commented)).notify
end
end
test "assignment events notify assignees regardless of involvement level" do
boards(:writebook).access_for(users(:jz)).access_only!
notifications = Notifier.for(events(:logo_assignment_jz)).notify
assert_equal [ users(:jz) ], notifications.map(&:user)
end
end