Files
fizzy/saas/test/models/notification/push_target/native_test.rb
T
Rosa Gutierrez 0948533da7 Rename push to process on PushTarget for clearer semantics
"Push to target" reads naturally - we push the notification to the
target. "Target processes" also makes sense - the target receives
and handles the notification in its own way.

- Add class method PushTarget.process(notification) that instantiates
  and calls the instance method
- Rename instance method from push to process
- Add private push_to helper in Pushable for readable iteration

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-25 19:31:13 +01:00

203 lines
7.1 KiB
Ruby

require "test_helper"
class Notification::PushTarget::NativeTest < ActiveSupport::TestCase
setup do
@user = users(:kevin)
@identity = @user.identity
@notification = notifications(:logo_published_kevin)
# Ensure user has no web push subscriptions (we want to test native push independently)
@user.push_subscriptions.delete_all
end
test "payload category returns assignment for card_assigned" do
notification = notifications(:logo_assignment_kevin)
assert_equal "assignment", notification.payload.category
end
test "payload category returns comment for comment_created" do
notification = notifications(:layout_commented_kevin)
assert_equal "comment", notification.payload.category
end
test "payload category returns mention for mentions" do
notification = notifications(:logo_card_david_mention_by_jz)
assert_equal "mention", notification.payload.category
end
test "payload category returns card for other card events" do
assert_equal "card", @notification.payload.category
end
test "pushes to native devices when user has devices" do
stub_push_services
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
assert_native_push_delivery(count: 1) do
Notification::PushTarget::Native.new(@notification).process
end
end
test "does not push when user has no devices" do
@identity.devices.delete_all
assert_no_native_push_delivery do
Notification::PushTarget::Native.new(@notification).process
end
end
test "does not push when creator is system user" do
stub_push_services
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
@notification.update!(creator: users(:system))
assert_no_native_push_delivery do
Notification::PushTarget::Native.new(@notification).process
end
end
test "pushes to multiple devices" do
stub_push_services
@identity.devices.delete_all
@identity.devices.create!(token: "token1", platform: "apple", name: "iPhone")
@identity.devices.create!(token: "token2", platform: "google", name: "Pixel")
assert_native_push_delivery(count: 2) do
Notification::PushTarget::Native.new(@notification).process
end
end
test "native notification includes required fields" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_not_nil native.title
assert_not_nil native.body
assert_equal "default", native.sound
end
test "native notification sets thread_id from card" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_equal @notification.card.id, native.thread_id
end
test "native notification sets high_priority for assignments" do
notification = notifications(:logo_assignment_kevin)
notification.user.identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
native = push.send(:native_notification)
assert native.high_priority
end
test "native notification sets high_priority for mentions" do
notification = notifications(:logo_card_david_mention_by_jz)
notification.user.identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
native = push.send(:native_notification)
assert native.high_priority
end
test "native notification sets normal priority for comments" do
notification = notifications(:layout_commented_kevin)
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
native = push.send(:native_notification)
assert_not native.high_priority
end
test "native notification sets normal priority for other card events" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_not native.high_priority
end
test "native notification includes apple-specific fields" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_equal 1, native.apple_data.dig(:aps, :"mutable-content")
assert_not_nil native.apple_data.dig(:aps, :category)
end
test "native notification sets time-sensitive interruption level for assignments" do
notification = notifications(:logo_assignment_kevin)
notification.user.identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
native = push.send(:native_notification)
assert_equal "time-sensitive", native.apple_data.dig(:aps, :"interruption-level")
end
test "native notification sets time-sensitive interruption level for mentions" do
notification = notifications(:logo_card_david_mention_by_jz)
notification.user.identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
native = push.send(:native_notification)
assert_equal "time-sensitive", native.apple_data.dig(:aps, :"interruption-level")
end
test "native notification sets active interruption level for comments" do
notification = notifications(:layout_commented_kevin)
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
native = push.send(:native_notification)
assert_equal "active", native.apple_data.dig(:aps, :"interruption-level")
end
test "native notification sets active interruption level for other card events" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_equal "active", native.apple_data.dig(:aps, :"interruption-level")
end
test "native notification sets android notification to nil for data-only" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_nil native.google_data.dig(:android, :notification)
end
test "native notification includes data payload" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
native = push.send(:native_notification)
assert_not_nil native.data[:url]
assert_equal @notification.account.external_account_id, native.data[:account_id]
assert_equal @notification.creator.name, native.data[:creator_name]
end
end