Files
buzz/mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzNotificationReplyTests.swift
Tom Brow 3689241672 feat(mobile): reply to a message from its iOS notification (#9439)
On iOS, a notification that shows a channel or DM message now offers
**Reply**. The user types a reply in the notification and sends it
without opening the app.

- The reply goes in a subthread under the notified message (its thread
root is the message's own root, or the message itself when top-level). A
Solo chat takes no thread replies, so there it goes at the chat's top
level.
- It always mentions the notified message's sender, so the person or
agent being answered is notified. Typed `@name` text stays plain, since
a notification has no mention autocomplete.
- It is sent by the app's own send path. iOS starts the Flutter engine
when it wakes the app in the background, and the reply reopens the relay
connection (closed after the background grace period), waiting a bounded
time for it before sending.
- A sent reply marks the conversation read **up to the notified
message** (newer messages stay unread) and clears that notification and
earlier ones from the same conversation.
- An unsent reply (offline, relay error, timeout, a channel the user
isn't in) keeps the typed text as a draft, after any draft already
there, and shows a "Reply not sent" notification that opens the
conversation.

For now only notifications from the community selected in the app offer
Reply: replying must not switch communities, and the app can only reach
the selected one. Forum posts and fallback notifications that show no
message offer none. The design and the options considered are in
`docs/adr/0004-notification-reply.md`.

### Before / after

Long-pressing a message notification (synthetic names and text). Before
has no reply field (captured by sending the notification without its new
reply category, which is how main presents it); after shows one with
Send.

| Before | After |
| --- | --- |
|
![before](https://raw.githubusercontent.com/block/buzz/df800599173cf3436af4d2c738447396a632fac7/pr-9439--before.png)
|
![after](https://raw.githubusercontent.com/block/buzz/df800599173cf3436af4d2c738447396a632fac7/pr-9439--after.png)
|

### Demo

With the app backgrounded past the relay grace period, replying from the
notification lands the reply in a subthread under the message:


https://github.com/user-attachments/assets/37df7e28-aa46-4521-938e-8af5faa77b83

### Regressions

Nothing existing changes unless the user taps Reply. Notifications gain
a category and a small reply context in `userInfo`; tapping a
notification still opens the conversation as before. The relay reconnect
only happens for a pending reply and then falls back to the normal
background disconnect, so idle background behavior and network use are
unchanged. Reply failures end in a draft and a "Reply not sent"
notification rather than touching other state.

### Validation

- End to end on an iOS 27 simulator against a real community: with the
app backgrounded past the relay grace period, replied from the
notification; the reply appeared in a subthread under the message,
mentioning its sender. The demo above was recorded before the review
fixes (session hold, community fence, origin-community drafts, composer
draft sync); the same run was repeated on the current head, 9b9c8b0, and
the reply again appeared in the subthread with no "Reply not sent"
notification.
- Human testing is waived for this repo's mobile client; the simulator
run above replaces it. Not yet exercised on a physical device or on a
cold launch (app not running).
- Added tests: a Dart journey covering top-level, in-thread and Solo
replies, another community (draft kept in that community), a failed
send, a channel the user isn't in (draft appended), a reply arriving
while running, an unreachable relay, and a community switch while
connecting (not sent, draft kept in the reply's community); a
relay-session test that a background-work hold keeps the grace
disconnect off, including one already waiting on cleanup, until released
or its limit; a composer step showing a draft appended elsewhere appears
in the mounted composer and survives its next edit; a relay-session test
for reconnecting a paused session and timing out (fails without the
fix); Swift tests for reply context encoding, NIP-10 root parsing
aligned with the app, and request validation.
- `relay_session.dart` was at its 1200-line cap, so the background
connect lives in a new part file and three existing test hooks moved
unchanged into another.
- Full `mobile-test`, `mobile-check` and BuzzPushKit `swift test` pass.

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-18 16:15:28 +00:00

277 lines
12 KiB
Swift

import Foundation
import Testing
@testable import BuzzPushKit
private let root = String(repeating: "a", count: 64)
private let parent = String(repeating: "b", count: 64)
private let sender = String(repeating: "C", count: 64)
private func event(kind: Int, tags: [[String]] = []) -> VerifiedNostrEvent {
VerifiedNostrEvent(
id: String(repeating: "d", count: 64),
pubkey: sender,
createdAt: 1_000,
kind: kind,
tags: tags,
content: "message",
sig: String(repeating: "e", count: 128)
)
}
@Test(arguments: [
// Top-level message: no thread root.
([[String]](), nil as String?),
// Direct reply to a thread head: the lone reply marker names the root.
([["e", root, "", "reply"]], root),
// Nested reply: the root marker wins over the direct parent.
([["e", root, "", "root"], ["e", parent, "", "reply"]], root),
// A malformed root marker falls back to the valid reply marker.
([["e", "not-hex", "", "root"], ["e", parent, "", "reply"]], parent),
// Uppercase IDs are normalized.
([["e", root.uppercased(), "", "root"], ["e", parent, "", "reply"]], root),
// A root marker without a valid reply marker is not a reply, as in the app.
([["e", root, "", "root"]], nil),
])
func `Reply context carries the sender and NIP-10 thread root`(
tags: [[String]], expectedRoot: String?
) throws {
let context = try #require(BuzzNotificationReplyContext.make(for: event(kind: 9, tags: tags)))
#expect(context.senderPubkey == sender.lowercased())
#expect(context.threadRootID == expectedRoot)
#expect(context.messageCreatedAt == 1_000)
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [BuzzNotificationReplyContext.userInfoKey: context.userInfoValue]) == context)
}
@Test(arguments: [45001, 45003, 7])
func `Forum posts and other kinds offer no reply`(kind: Int) {
#expect(BuzzNotificationReplyContext.make(for: event(kind: kind)) == nil)
}
@Test func `Reply context rejects unexpected or malformed fields`() {
let key = BuzzNotificationReplyContext.userInfoKey
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [key: ["sender_pubkey": "", "created_at": "1"]]) == nil)
#expect(BuzzNotificationReplyContext.decodeIfPresent(from: [key: ["sender_pubkey": "pk"]]) == nil)
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [key: ["sender_pubkey": "pk", "created_at": "soon"]]) == nil)
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [key: ["sender_pubkey": "pk", "created_at": "1", "extra": "x"]]) == nil)
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [key: ["sender_pubkey": "pk", "created_at": "1", "thread_root_id": ""]]) == nil)
}
@Test func `Resolution offers reply only for a channel message`() {
let community = PushLeaseCommunity(
id: "community", name: "Community", relayUrl: "https://relay.example.org",
pubkey: String(repeating: "f", count: 64), policies: [])
let inChannel = BuzzPushNotificationResolver.makeResolution(
event: event(kind: 9, tags: [["h", "channel"]]), community: community, profile: nil,
channel: nil)
#expect(inChannel?.replyContext?.senderPubkey == sender.lowercased())
let noChannel = BuzzPushNotificationResolver.makeResolution(
event: event(kind: 9), community: community, profile: nil, channel: nil)
#expect(noChannel != nil)
#expect(noChannel?.replyContext == nil)
}
@Test func `Reply request needs text, a target and a reply context`() {
let target = BuzzPushNavigationTarget(
eventID: parent, communityID: "community", channelID: "channel")
let context = BuzzNotificationReplyContext(senderPubkey: "pk", threadRootID: root, messageCreatedAt: 1)
let userInfo: [AnyHashable: Any] = [
BuzzPushNavigationTarget.userInfoKey: target.userInfoValue,
BuzzNotificationReplyContext.userInfoKey: context.userInfoValue,
]
let request = BuzzNotificationReplyRequest.make(
id: "1", text: "hi", userInfo: userInfo, threadIdentifier: "thread")
#expect(request?.target == target)
#expect(request?.context == context)
#expect(
BuzzNotificationReplyRequest.make(
id: "1", text: " \n", userInfo: userInfo, threadIdentifier: "thread") == nil)
#expect(
BuzzNotificationReplyRequest.make(
id: "1", text: "hi",
userInfo: [BuzzPushNavigationTarget.userInfoKey: target.userInfoValue],
threadIdentifier: "thread") == nil)
}
@Test func `Each buffered reply is taken once and finishes once`() {
let buffer = BuzzNotificationReplyBuffer()
let target = BuzzPushNavigationTarget(eventID: parent, communityID: "c", channelID: "ch")
let context = BuzzNotificationReplyContext(senderPubkey: "pk", messageCreatedAt: 1)
func request(_ id: String) -> BuzzNotificationReplyRequest {
BuzzNotificationReplyRequest(
id: id, text: id, target: target, context: context, threadIdentifier: "t")
}
var outcomes: [String: [BuzzNotificationReplyOutcome]] = [:]
for id in ["1", "2", "3", "4"] {
buffer.add(request(id)) { outcomes[id, default: []].append($0) }
}
#expect(buffer.take().map(\.id) == ["1", "2", "3", "4"])
#expect(buffer.take().isEmpty)
// A Solo chat keeps an expired reply's draft in the chat itself.
buffer.add(
BuzzNotificationReplyRequest(
id: "5", text: "5", target: target,
context: BuzzNotificationReplyContext(senderPubkey: "pk", messageCreatedAt: 1, inSolo: true),
threadIdentifier: "t")
) { outcomes["5", default: []].append($0) }
#expect(buffer.complete(id: "1", outcome: .sent))
// A deadline after the app reported the outcome changes nothing.
buffer.expire(id: "1", mayHavePublished: false)
#expect(!buffer.complete(id: "1", outcome: .sent))
buffer.complete(id: "2", outcome: .notSent(draftThreadRootID: "head"))
buffer.complete(id: "3", outcome: .unknown)
buffer.expire(id: "4", mayHavePublished: false)
buffer.expire(id: "5", mayHavePublished: false)
// One the app may already have published can't be called not sent.
buffer.add(request("6")) { outcomes["6", default: []].append($0) }
buffer.expire(id: "6", mayHavePublished: true)
#expect(!buffer.complete(id: "unknown", outcome: .sent))
#expect(
outcomes == [
"1": [.sent], "2": [.notSent(draftThreadRootID: "head")], "3": [.unknown],
// Expired, it opens the subthread under the notified message, where the
// app keeps its draft.
"4": [.notSent(draftThreadRootID: parent)], "5": [.notSent(draftThreadRootID: nil)],
"6": [.unknown],
])
}
@Test func `A reply stays journaled until reported, across a relaunch`() throws {
let suite = "reply-journal-\(UUID().uuidString)"
let defaults = try #require(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
let journal = BuzzNotificationReplyJournal(defaults: defaults)
journal.record(["id": "1", "text": "one"])
journal.record(["id": "2", "text": "two"])
#expect(journal.take().map { $0["id"] } == ["1", "2"])
// The app is already working on these.
#expect(journal.take().isEmpty)
journal.settle(id: "1")
journal.record(["id": "3", "text": "three"])
#expect(journal.take().map { $0["id"] } == ["3"])
// The app got as far as maybe publishing 3, so expiry reports it unknown.
#expect(!journal.isPublishing(id: "3"))
journal.markPublishing(id: "3")
#expect(journal.isPublishing(id: "3"))
#expect(!journal.isPublishing(id: "2"))
// The app was stopped before reporting 2 and 3, so a relaunch hands them
// over again, marked so the app never sends them; one recorded after the
// relaunch is fresh.
let relaunched = BuzzNotificationReplyJournal(defaults: defaults)
relaunched.record(["id": "4", "text": "four"])
#expect(
relaunched.take() == [
["id": "2", "text": "two", "recovered": "true"],
["id": "3", "text": "three", "publishing": "true", "recovered": "true"],
["id": "4", "text": "four"],
])
// The relaunch can still say that 3 may not have sent, then settle it.
#expect(relaunched.recorded(id: "3") == ["id": "3", "text": "three", "publishing": "true"])
#expect(relaunched.isPublishing(id: "3"))
for id in ["2", "3", "4"] { relaunched.settle(id: id) }
#expect(relaunched.recorded(id: "3") == nil)
#expect(BuzzNotificationReplyJournal(defaults: defaults).take().isEmpty)
}
@Test func `A journaled reply rebuilds the request it came from`() {
let request = BuzzNotificationReplyRequest(
id: "1", text: "hi",
target: BuzzPushNavigationTarget(
eventID: parent, communityID: "c", channelID: "ch", threadRootID: "root"),
context: BuzzNotificationReplyContext(
senderPubkey: "pk", threadRootID: "root", messageCreatedAt: 7, inSolo: true),
threadIdentifier: "t",
deadline: Date(timeIntervalSince1970: 1_700_000_000.5))
let rebuilt = BuzzNotificationReplyRequest(
journalArguments: request.journalArguments.merging(["recovered": "true"]) { _, new in new })
// The notification it came from, and its thread, are gone.
#expect(
rebuilt
== BuzzNotificationReplyRequest(
id: request.id, text: request.text, target: request.target, context: request.context,
threadIdentifier: "", deadline: request.deadline))
#expect(BuzzNotificationReplyRequest(journalArguments: ["id": "1", "text": "hi"]) == nil)
}
@Test func `Reply context carries whether the chat is Solo`() throws {
for inSolo in [true, false, nil] as [Bool?] {
let context = BuzzNotificationReplyContext(
senderPubkey: "pk", messageCreatedAt: 1, inSolo: inSolo)
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [BuzzNotificationReplyContext.userInfoKey: context.userInfoValue]) == context)
}
#expect(
BuzzNotificationReplyContext.decodeIfPresent(
from: [
BuzzNotificationReplyContext.userInfoKey: [
"sender_pubkey": "pk", "created_at": "1", "in_solo": "yes",
]
]) == nil)
}
@Test func `Selected community round-trips and clears`() throws {
let suite = "BuzzNotificationReplyTests.\(UUID().uuidString)"
let defaults = try #require(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
#expect(BuzzSelectedCommunity.id(in: defaults) == nil)
BuzzSelectedCommunity.set("community", in: defaults)
#expect(BuzzSelectedCommunity.id(in: defaults) == "community")
BuzzSelectedCommunity.set(nil, in: defaults)
#expect(BuzzSelectedCommunity.id(in: defaults) == nil)
}
@Test func `A sent reply clears the notifications its read marker covers`() {
let target = BuzzPushNavigationTarget(eventID: parent, communityID: "c", channelID: "ch")
func request(root: String?) -> BuzzNotificationReplyRequest {
BuzzNotificationReplyRequest(
id: "1", text: "hi", target: target,
context: BuzzNotificationReplyContext(
senderPubkey: "pk", threadRootID: root, messageCreatedAt: 10),
threadIdentifier: "conversation")
}
func notification(root: String?, at createdAt: Int) -> [AnyHashable: Any] {
[
BuzzNotificationReplyContext.userInfoKey: BuzzNotificationReplyContext(
senderPubkey: "pk", threadRootID: root, messageCreatedAt: createdAt
).userInfoValue
]
}
let rows: [(request: String?, notified: String?, createdAt: Int, cleared: Bool)] = [
// A top-level reply reads the whole conversation, threads included.
(nil, nil, 5, true),
(nil, "thread", 5, true),
(nil, "thread", 11, false),
// A reply in a thread reads only that thread.
("thread", "thread", 5, true),
("thread", nil, 5, false),
("thread", "other", 5, false),
]
for row in rows {
#expect(
request(root: row.request).clears(
threadIdentifier: "conversation",
userInfo: notification(root: row.notified, at: row.createdAt)) == row.cleared)
}
#expect(
!request(root: nil).clears(
threadIdentifier: "elsewhere", userInfo: notification(root: nil, at: 5)))
#expect(!request(root: nil).clears(threadIdentifier: "conversation", userInfo: [:]))
}