Files
buzz/mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/PushLeaseTests.swift
Tom Brow 4b21a10c4b fix: don't notify workflow owners through their attribution tag (#8694)
A workflow message names its owner in a marked p tag, ["p", owner, "", "owner"], for attribution. #p live, history and push matching, operator listeners, desktop, mobile and the iOS NSE treat that tag as attribution rather than a mention. A workflow that mentions its owner adds a plain p, so the owner is notified only when actually mentioned. NOSTR.md records the #p exception.

Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@squareup.com>
2026-10-29 15:21:56 -07:00

112 lines
3.8 KiB
Swift

import XCTest
@testable import BuzzPushKit
final class PushLeaseTests: XCTestCase {
private let mine = String(repeating: "a", count: 64)
private let other = String(repeating: "b", count: 64)
func testFilterBuildsQueryFromLeaseWithoutHardcodedKinds() {
let filter = PushLeaseFilter(
kinds: [7, 1059],
authors: [other],
pTags: [mine],
hTags: ["channel"],
eTags: [String(repeating: "c", count: 64)]
)
let query = filter.queryFilter(since: 1_000, limit: 10)
XCTAssertEqual(query["kinds"] as? [Int], [7, 1059])
XCTAssertEqual(query["authors"] as? [String], [other])
XCTAssertEqual(query["#p"] as? [String], [mine])
XCTAssertEqual(query["#h"] as? [String], ["channel"])
XCTAssertEqual(query["since"] as? Int, 1_000)
}
func testPushEligibleKindAbsentFromOldConstantMatchesLease() {
let event = makeEvent(kind: 1059, tags: [["p", mine]])
let policy = PushResolutionPolicy(
filter: PushLeaseFilter(kinds: [1059], pTags: [mine])
)
XCTAssertTrue(PushLeaseMatcher.matches(event: event, policy: policy))
}
func testIgnoreAndHellthreadSuppressionRejectCandidates() {
let ignored = makeEvent(kind: 9, pubkey: other, tags: [["p", mine]])
let ignorePolicy = PushResolutionPolicy(
filter: PushLeaseFilter(kinds: [9], pTags: [mine]),
ignore: [PushLeaseFilter(kinds: [9], authors: [other])]
)
XCTAssertFalse(
PushLeaseMatcher.matches(event: ignored, policy: ignorePolicy)
)
let hellthread = makeEvent(
kind: 9,
tags: (0..<21).map { ["p", String(format: "%064x", $0)] }
)
let suppressed = PushResolutionPolicy(
filter: PushLeaseFilter(kinds: [9], authors: [other]),
suppress: PushLeaseSuppression(pTagsMax: 20)
)
XCTAssertFalse(PushLeaseMatcher.matches(event: hellthread, policy: suppressed))
}
func testWorkflowOwnerAttributionIsNotAMentionOrCounted() {
let workflow = ["buzz:workflow", "true"]
let attribution = ["p", mine, "", "owner"]
let mentions = PushResolutionPolicy(
filter: PushLeaseFilter(kinds: [9], pTags: [mine])
)
XCTAssertFalse(
PushLeaseMatcher.matches(
event: makeEvent(kind: 9, tags: [attribution, workflow]), policy: mentions))
XCTAssertTrue(
PushLeaseMatcher.matches(
event: makeEvent(kind: 9, tags: [attribution, workflow, ["p", mine]]),
policy: mentions))
XCTAssertTrue(
PushLeaseMatcher.matches(event: makeEvent(kind: 9, tags: [attribution]), policy: mentions))
let atLimit = makeEvent(
kind: 9,
tags: [attribution, workflow] + (0..<20).map { ["p", String(format: "%064x", $0)] }
)
let suppressed = PushResolutionPolicy(
filter: PushLeaseFilter(kinds: [9], authors: [other]),
suppress: PushLeaseSuppression(pTagsMax: 20)
)
XCTAssertTrue(PushLeaseMatcher.matches(event: atLimit, policy: suppressed))
}
func testDecodesSnapshotContractFromDartShape() throws {
let json = """
{"communities":[{"id":"origin","name":"Team","relayUrl":"https://relay.example.com","pubkey":"\(mine)","policies":[{"filter":{"kinds":[9],"#p":["\(mine)"]},"ignore":[{"kinds":[9],"authors":["\(mine)"]}],"suppress":{"p_tags_max":20}}]}]}
"""
let snapshot = try JSONDecoder().decode(PushLeaseSnapshot.self, from: Data(json.utf8))
XCTAssertEqual(snapshot.communities.count, 1)
XCTAssertEqual(
snapshot.communities[0].policies.count,
1
)
}
private func makeEvent(
kind: Int,
pubkey: String? = nil,
tags: [[String]] = []
) -> VerifiedNostrEvent {
VerifiedNostrEvent(
id: String(repeating: "d", count: 64),
pubkey: pubkey ?? other,
createdAt: 1_000,
kind: kind,
tags: tags,
content: "message",
sig: String(repeating: "e", count: 128)
)
}
}