Inject notification/favorites singletons through coordinator contexts

NotificationService.shared and FavoritesPersistenceService.shared
accesses across nine coordinators/components consolidate into
ChatViewModel witnesses behind intent-named context members
(notifyPrivateMessage, notifyMention, favoriteRelationship(forNoiseKey:),
allFavoriteRelationships, postLocalNotification, ...). Singleton reach
from coordinators is now zero; nine new tests cover previously
untestable notification/favorites flows.

Also root-causes the long-flaky gift-wrap dedup test: parallel tests
share LocationChannelManager.shared, so channel-switch tests trigger
clearProcessedNostrEvents() on every live ChatViewModel, wiping the
dedup record mid-test. The invariant (forged-signature copies never
poison dedup) now lives as a deterministic NostrInboundPipeline
mock-context test; two Schnorr concurrency probes added along the way
stay as regression guards for the P256K shared-context assumptions.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
jack
2026-06-10 23:04:20 +02:00
co-authored by Claude Fable 5
parent 638f3f5005
commit cc76086615
19 changed files with 820 additions and 116 deletions
@@ -398,40 +398,6 @@ struct ChatViewModelNostrExtensionTests {
#expect(viewModel.privateChats.isEmpty)
}
@Test @MainActor
func handleNostrMessage_invalidSignatureDoesNotPoisonDedup() async throws {
let (viewModel, _) = makeTestableViewModel()
let sender = try NostrIdentity.generate()
let recipient = try #require(try viewModel.idBridge.getCurrentNostrIdentity())
let giftWrap = try NostrProtocol.createPrivateMessage(
content: "verify:noop",
recipientPubkey: recipient.publicKeyHex,
senderIdentity: sender
)
var invalidGiftWrap = giftWrap
invalidGiftWrap.sig = String(repeating: "0", count: 128)
// Verification and recording now happen on a detached task; give the
// invalid copy time to be verified-and-rejected, then assert it never
// entered the dedup set.
viewModel.handleNostrMessage(invalidGiftWrap)
try await Task.sleep(nanoseconds: 150_000_000)
#expect(!viewModel.deduplicationService.hasProcessedNostrEvent(giftWrap.id))
// Generous deadline: the detached verification task can be starved
// under parallel test load.
viewModel.handleNostrMessage(giftWrap)
var recorded = false
for _ in 0..<600 {
if viewModel.deduplicationService.hasProcessedNostrEvent(giftWrap.id) {
recorded = true
break
}
try await Task.sleep(nanoseconds: 10_000_000)
}
#expect(recorded)
}
@Test @MainActor
func switchLocationChannel_clearsNostrDedupCache() async {
let (viewModel, _) = makeTestableViewModel()