Files
buzz/mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzPushConversationResolverTests.swift
Tom Brow 58696ff577 fix(mobile): show the channel name as the notification subtitle (#9173)
On Apple Watch, a Buzz notification showed the community name ("buzz")
under the sender, taking a line of the small display without saying
where the message came from. iPhone never showed it, because iOS draws
the banner from the communication intent and drops `content.subtitle`.
The Watch renders the plain fields.

The subtitle now carries the conversation name, the same one the intent
uses for its group name:

- Team channels and forums: `#channel`
- Your own Solo chats: the bare name, no `#`
- DMs, and channels not yet in the presentation cache: no subtitle

The name logic moves into one helper so the subtitle and the iPhone
group name can't drift apart. A channel that's known but isn't eligible
for the communication style (for example, an unverified roster) now gets
`#channel` as its plain-banner subtitle in place of the community name.
Losing the community name means the Watch no longer tells communities
apart. That seems like the right trade for most people, who are in one
community.

### Validation

- BuzzPushKit `swift test`: all 110 XCTest and 69 Swift Testing cases
pass. Updated the resolver assertions: `#General` for a cached stream,
both with and without communication styling, and no subtitle for DMs and
uncached channels.
- Not yet exercised on a device or Watch.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 06:59:13 -08:00

269 lines
8.8 KiB
Swift

import Foundation
import XCTest
@testable import BuzzPushKit
extension BuzzPushNotificationResolverTests {
func testOpenChannelOutsiderUsesEveryVerifiedMemberAsARecipient() throws {
let message = try Self.signedEvent(
privateKey: Self.profilePrivateKey,
createdAt: Self.now,
kind: 9,
tags: [["h", Self.channelID]],
content: "Hello from an open-channel guest"
)
let relayPubkey = try Self.pubkey(for: Self.relayPrivateKey)
let snapshot = BuzzPushPresentationCacheSnapshot(
channels: [
BuzzPushCachedChannel(
communityID: "community-id",
relayOrigin: "https://relay.example",
channelID: Self.channelID,
relayMetadataPubkey: relayPubkey,
displayName: "General",
channelType: "stream",
memberCount: 2,
memberDigests: Self.memberDigests([Self.ownPubkey, relayPubkey]),
membershipEventID: "membership-event",
membershipEventCreatedAt: Self.now,
membershipCachedAt: Self.now,
eventID: "channel-event",
eventCreatedAt: Self.now,
cachedAt: Self.now
)
]
)
URLProtocolStub.handler = { request in
Self.response(request, status: 200, data: try JSONEncoder().encode([message]))
}
let result = try XCTUnwrap(
resolve(
makeResolver(
communitiesData: try snapshotData([community(relayMetadataPubkey: relayPubkey)]),
presentationCacheData: try JSONEncoder().encode(snapshot),
now: Date(timeIntervalSince1970: TimeInterval(Self.now))
)
)
)
XCTAssertEqual(result.conversationDisplayName, "#General")
XCTAssertEqual(result.subtitle, "#General")
XCTAssertEqual(result.conversationRecipientCount, 2)
}
func testMissingCurrentUserInVerifiedRosterUsesOrdinaryPresentation() throws {
let message = try Self.signedEvent(
privateKey: Self.profilePrivateKey,
createdAt: Self.now,
kind: 9,
tags: [["h", Self.channelID]],
content: "Do not fabricate a group"
)
let relayPubkey = try Self.pubkey(for: Self.relayPrivateKey)
let snapshot = BuzzPushPresentationCacheSnapshot(
channels: [
BuzzPushCachedChannel(
communityID: "community-id",
relayOrigin: "https://relay.example",
channelID: Self.channelID,
relayMetadataPubkey: relayPubkey,
displayName: "General",
channelType: "stream",
memberCount: 1,
memberDigests: Self.memberDigests([message.pubkey]),
membershipEventID: "membership-event",
membershipEventCreatedAt: Self.now,
membershipCachedAt: Self.now,
eventID: "channel-event",
eventCreatedAt: Self.now,
cachedAt: Self.now
)
]
)
URLProtocolStub.handler = { request in
Self.response(request, status: 200, data: try JSONEncoder().encode([message]))
}
let result = try XCTUnwrap(
resolve(
makeResolver(
communitiesData: try snapshotData([community(relayMetadataPubkey: relayPubkey)]),
presentationCacheData: try JSONEncoder().encode(snapshot),
now: Date(timeIntervalSince1970: TimeInterval(Self.now))
)
)
)
XCTAssertNil(result.conversationDisplayName)
XCTAssertNil(result.conversationRecipientCount)
XCTAssertEqual(result.subtitle, "#General")
}
func testPartialMembershipRefreshDoesNotReuseCachedChannel() throws {
let message = try Self.signedEvent(
privateKey: Self.profilePrivateKey,
createdAt: Self.now,
kind: 9,
tags: [["h", Self.channelID]],
content: "Refresh an evicted roster"
)
let relayPubkey = try Self.pubkey(for: Self.relayPrivateKey)
let snapshot = BuzzPushPresentationCacheSnapshot(
channels: [
BuzzPushCachedChannel(
communityID: "community-id",
relayOrigin: "https://relay.example",
channelID: Self.channelID,
relayMetadataPubkey: relayPubkey,
displayName: "General",
channelType: "stream",
memberCount: 2,
memberDigests: nil,
membershipEventID: "evicted-membership",
membershipEventCreatedAt: Self.now - 1,
membershipCachedAt: Self.now,
eventID: "channel-event",
eventCreatedAt: Self.now,
cachedAt: Self.now
)
]
)
let refreshedMembership = try Self.signedEvent(
privateKey: Self.relayPrivateKey,
createdAt: Self.now,
kind: 39_002,
tags: [
["d", Self.channelID],
["p", Self.ownPubkey],
["p", message.pubkey],
]
)
URLProtocolStub.handler = { request in
if URLProtocolStub.requests.count == 1 {
return Self.response(request, status: 200, data: try JSONEncoder().encode([message]))
}
XCTAssertEqual(request.timeoutInterval, 60)
return Self.response(
request,
status: 200,
data: try JSONEncoder().encode([refreshedMembership])
)
}
let result = try XCTUnwrap(
resolve(
makeResolver(
communitiesData: try snapshotData([community(relayMetadataPubkey: relayPubkey)]),
presentationCacheData: try JSONEncoder().encode(snapshot),
now: Date(timeIntervalSince1970: TimeInterval(Self.now))
)
)
)
// Only membership was returned. Do not pair it with the cached channel.
XCTAssertNil(result.conversationDisplayName)
XCTAssertNil(result.conversationRecipientCount)
XCTAssertEqual(URLProtocolStub.requests.count, 4)
}
func testTwoPersonDMUsesDirectCommunicationPresentation() throws {
let message = try Self.signedEvent(
privateKey: Self.profilePrivateKey,
createdAt: Self.now,
kind: 9,
tags: [["h", Self.channelID]],
content: "Direct message"
)
let relayPubkey = try Self.pubkey(for: Self.relayPrivateKey)
let snapshot = BuzzPushPresentationCacheSnapshot(
channels: [
BuzzPushCachedChannel(
communityID: "community-id",
relayOrigin: "https://relay.example",
channelID: Self.channelID,
relayMetadataPubkey: relayPubkey,
displayName: "DM",
channelType: "dm",
memberCount: 2,
memberDigests: Self.memberDigests([Self.ownPubkey, message.pubkey]),
membershipEventID: "membership-event",
membershipEventCreatedAt: Self.now,
membershipCachedAt: Self.now,
eventID: "channel-event",
eventCreatedAt: Self.now,
cachedAt: Self.now
)
]
)
URLProtocolStub.handler = { request in
Self.response(request, status: 200, data: try JSONEncoder().encode([message]))
}
let result = try XCTUnwrap(
resolve(
makeResolver(
communitiesData: try snapshotData([community(relayMetadataPubkey: relayPubkey)]),
presentationCacheData: try JSONEncoder().encode(snapshot),
now: Date(timeIntervalSince1970: TimeInterval(Self.now))
)
)
)
XCTAssertNil(result.conversationDisplayName)
XCTAssertEqual(result.conversationRecipientCount, 1)
}
func testGroupDMWithoutVerifiedDisplayLabelUsesOrdinaryPresentation() throws {
let message = try Self.signedEvent(
privateKey: Self.profilePrivateKey,
createdAt: Self.now,
kind: 9,
tags: [["h", Self.channelID]],
content: "Group direct message"
)
let relayPubkey = try Self.pubkey(for: Self.relayPrivateKey)
let snapshot = BuzzPushPresentationCacheSnapshot(
channels: [
BuzzPushCachedChannel(
communityID: "community-id",
relayOrigin: "https://relay.example",
channelID: Self.channelID,
relayMetadataPubkey: relayPubkey,
displayName: "DM",
channelType: "dm",
memberCount: 3,
memberDigests: Self.memberDigests([
Self.ownPubkey,
message.pubkey,
relayPubkey,
]),
membershipEventID: "membership-event",
membershipEventCreatedAt: Self.now,
membershipCachedAt: Self.now,
eventID: "channel-event",
eventCreatedAt: Self.now,
cachedAt: Self.now
)
]
)
URLProtocolStub.handler = { request in
Self.response(request, status: 200, data: try JSONEncoder().encode([message]))
}
let result = try XCTUnwrap(
resolve(
makeResolver(
communitiesData: try snapshotData([community(relayMetadataPubkey: relayPubkey)]),
presentationCacheData: try JSONEncoder().encode(snapshot),
now: Date(timeIntervalSince1970: TimeInterval(Self.now))
)
)
)
XCTAssertNil(result.conversationDisplayName)
XCTAssertNil(result.conversationRecipientCount)
XCTAssertNil(result.subtitle)
}
}