mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-24 22:45:19 +00:00
* Empty-mesh liveliness: nearby conversations, echoes, wave action, dead drops, radar The empty mesh timeline was a dead end: a grey zero and "nobody in range yet". This turns it into a live surface and gives the app pull when the mesh wakes up: - Nearest conversation: background geohash sampling now tracks actual chat messages (not just presence) per regional channel; the empty state surfaces the busiest nearby conversation with a preview, one tap to join (GeohashChatActivityTracker, fed from GeoPresenceTracker). - Echoes: the carried 6h store-and-forward window renders as dimmed "heard here earlier" rows at launch (new Transport collectArchivedPublicMessages -> GossipSyncManager snapshot, decoded with signature-derived nicknames; content-identity dedup guards against re-synced duplicates). - Wave: the "bitchatters nearby" notification gains a "wave" quick action that broadcasts a mesh 👋 straight from the notification, even backgrounded (first UNNotificationCategory in the app). - Dead drops: /drop pins a note to the current building geohash as a kind-1 location note with a 24h NIP-40 expiry; expired notes are now dropped client-side at ingest; the notices sheet shows "fades in Xh"; a "location notes" toggle plus location-permission controls live in app info (also fixes the duplicated Voice section). - Radar: an ambient sonar animation shows the radio scanning, with a privacy-safe daily tally ("N devices passed within range today" via salted per-day hashes) and a "notes left here" hint that opens the notices geo tab. All new user-facing strings ship in all 29 locales. 1403 tests green, including new suites for the activity tracker, sightings tally, and note expiry/drop publishing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Review round: app-info polish, urgent/expiry parity, centered radar, pin fill, Codex P2s - App info: LOCATION header uppercased like sibling sections; location notes and live voice descriptions shortened (29 locales); redundant "location access granted" line removed. - Notices parity: urgent + expiry controls now show on the geo tab too; the bridged Nostr note carries ["t","urgent"] and NIP-40 so relay-side readers see both; urgent parsed back from incoming notes. - Radar moved from the top of the empty state to the center of the chat area, below the help text (empty state fills the visible height). - Header pin fills whenever the scope has notices (was: only unseen), and Nostr-only nearby notes now light it too. - Codex P2 fixes: notification completion deferred until the wave action is handled (background suspension dropped the send); NIP-40 notes now prune on a timer when they expire while displayed; the location-notes kill switch retargets the nearby-notes counter immediately. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Hide macOS segmented picker's built-in label in notices composer (duplicate 'expires in') Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Geo notes: permanent (∞) expiry default, urgent stays mesh-only - Geo expiry picker gains ∞ as the default: a permanent note posts as a pure relay note (no NIP-40 tag, no mesh-board copy — a board copy must fade within days, contradicting the ∞ the user picked). 1/3/7d keep the board + bridged-note path with NIP-40. - The notes manager is now owned by the notices sheet (not the list) so the composer local-echoes ∞ notes into the list; it revives via refresh() after a tab-switch cancel, and its expiry-prune timer survives cancel (weak self, dies with the instance). - Urgent toggle returns to mesh-only per review — notes are ambient; the read-side urgent-tag parse stays so tagged notes still render. - macOS: hide the segmented picker's built-in label (duplicate "expires in"). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Clearing the mesh timeline dismisses echoes for good; tighter divider copy Triple-tap /clear emptied the timeline but the next launch re-seeded "heard here earlier" from the persisted archive. A MeshEchoSettings watermark now records the clear; only messages heard after it come back (the archive itself still carries everything for peers' sync). The echo dedup keys reset with it, and panic wipe drops the watermark. Divider copy tightened to "heard here earlier · last 6h" (29 locales). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Echoes visual polish: tinted history block, radar-captioned tally, ambient footer Device-test feedback on the echoes screen: - Archived echoes now sit on a subtle tinted background (secondary at 8%) in addition to the dim, so "heard here earlier" reads as one distinct block; the divider carries the echo ID prefix to join it. - "N devices passed within range today" moves out of the narration lines to sit centered under the radar as its caption. - When the timeline holds only echoes/system lines, a compact ambient footer (small radar + tally + live hints) renders below the history instead of the whole ambient layer vanishing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Notes strip persists above the mesh chat; leaner empty-state narration - The 📍 "notes left here" line was empty-state-only, so starting a conversation hid it. It is now a tappable strip pinned above the mesh timeline whenever unexpired notes exist at this place (opens the notices geo tab); the nearby-notes counter runs for the whole mesh timeline, not just the empty state. - Empty state narration drops "nobody in range yet..." (the radar and the sightings caption already say it) and the nearby-conversation hint moves below the help line instead of splitting the narration. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Radar means searching: hide the sweep once mesh peers are connected or reachable Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
343 lines
12 KiB
Swift
343 lines
12 KiB
Swift
import Testing
|
|
import Combine
|
|
import Foundation
|
|
@testable import bitchat
|
|
|
|
@MainActor
|
|
struct LocationNotesManagerTests {
|
|
@Test
|
|
func subscribeWithoutRelays_setsNoRelaysState() {
|
|
var subscribeCalled = false
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in [] },
|
|
subscribe: { _, _, _, _, _ in
|
|
subscribeCalled = true
|
|
},
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in try NostrIdentity.generate() },
|
|
now: { Date() }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
|
|
#expect(subscribeCalled == false)
|
|
#expect(manager.state == .noRelays)
|
|
#expect(manager.initialLoadComplete)
|
|
#expect(manager.errorMessage == String(localized: "location_notes.error.no_relays"))
|
|
}
|
|
|
|
@Test
|
|
func noRelays_resubscribesWhenDirectoryRefreshes() async throws {
|
|
var relays: [String] = []
|
|
var subscribeCount = 0
|
|
let directorySubject = PassthroughSubject<Void, Never>()
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in relays },
|
|
subscribe: { _, _, _, _, _ in
|
|
subscribeCount += 1
|
|
},
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in try NostrIdentity.generate() },
|
|
now: { Date() },
|
|
relayDirectoryUpdates: directorySubject.eraseToAnyPublisher()
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
#expect(manager.state == .noRelays)
|
|
#expect(subscribeCount == 0)
|
|
|
|
// Directory loads later (e.g. remote fetch finished after Tor came up).
|
|
relays = ["wss://relay.one"]
|
|
directorySubject.send(())
|
|
|
|
let deadline = Date().addingTimeInterval(1.0)
|
|
while manager.state == .noRelays && Date() < deadline {
|
|
try await Task.sleep(nanoseconds: 10_000_000)
|
|
}
|
|
|
|
#expect(subscribeCount == 1)
|
|
#expect(manager.state == .loading)
|
|
#expect(manager.errorMessage == nil)
|
|
}
|
|
|
|
@Test
|
|
func sendWithoutRelays_surfacesNoRelaysError() {
|
|
var sendCalled = false
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in [] },
|
|
subscribe: { _, _, _, _, _ in },
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in sendCalled = true },
|
|
deriveIdentity: { _ in throw TestError.shouldNotDerive },
|
|
now: { Date() }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
manager.send(content: "hello", nickname: "tester")
|
|
|
|
#expect(sendCalled == false)
|
|
#expect(manager.state == .noRelays)
|
|
#expect(manager.errorMessage == String(localized: "location_notes.error.no_relays"))
|
|
}
|
|
|
|
@Test func subscribeUsesGeoRelaysAndAppendsNotes() throws {
|
|
var relaysCaptured: [String] = []
|
|
var storedHandler: ((NostrEvent) -> Void)?
|
|
var storedEOSE: (() -> Void)?
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { filter, id, relays, handler, eose in
|
|
#expect(filter.kinds == [1])
|
|
#expect(!id.isEmpty)
|
|
relaysCaptured = relays
|
|
storedHandler = handler
|
|
storedEOSE = eose
|
|
},
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in throw TestError.shouldNotDerive },
|
|
now: { Date() }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
#expect(relaysCaptured == ["wss://relay.one"])
|
|
#expect(manager.state == .loading)
|
|
|
|
let identity = try NostrIdentity.generate()
|
|
let event = NostrEvent(
|
|
pubkey: identity.publicKeyHex,
|
|
createdAt: Date(),
|
|
kind: .textNote,
|
|
tags: [["g", "u4pruydq"]],
|
|
content: "hi"
|
|
)
|
|
let signed = try event.sign(with: identity.schnorrSigningKey())
|
|
storedHandler?(signed)
|
|
storedEOSE?()
|
|
|
|
#expect(manager.state == .ready)
|
|
#expect(manager.notes.count == 1)
|
|
#expect(manager.notes.first?.content == "hi")
|
|
}
|
|
|
|
@Test
|
|
func setGeohash_invalidValueIsIgnored() {
|
|
var subscribeCount = 0
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, _, _, _, _ in
|
|
subscribeCount += 1
|
|
},
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in try NostrIdentity.generate() },
|
|
now: { Date() }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
manager.setGeohash("not-valid")
|
|
|
|
#expect(manager.geohash == "u4pruydq")
|
|
#expect(subscribeCount == 1)
|
|
}
|
|
|
|
@Test
|
|
func refreshAndCancel_manageSubscriptions() {
|
|
var subscribeIDs: [String] = []
|
|
var unsubscribedIDs: [String] = []
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, id, _, _, _ in
|
|
subscribeIDs.append(id)
|
|
},
|
|
unsubscribe: { id in
|
|
unsubscribedIDs.append(id)
|
|
},
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in try NostrIdentity.generate() },
|
|
now: { Date() }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
manager.refresh()
|
|
manager.cancel()
|
|
|
|
#expect(subscribeIDs.count == 2)
|
|
#expect(unsubscribedIDs.count == 2)
|
|
#expect(manager.state == .idle)
|
|
#expect(manager.errorMessage == nil)
|
|
}
|
|
|
|
@Test
|
|
func send_successCreatesLocalEchoAndClearsError() throws {
|
|
var sentEvents: [NostrEvent] = []
|
|
let identity = try NostrIdentity.generate()
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, _, _, _, _ in },
|
|
unsubscribe: { _ in },
|
|
sendEvent: { event, _ in
|
|
sentEvents.append(event)
|
|
},
|
|
deriveIdentity: { _ in identity },
|
|
now: { Date(timeIntervalSince1970: 123_456) }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
manager.send(content: " hello note ", nickname: "Builder")
|
|
|
|
#expect(sentEvents.count == 1)
|
|
#expect(manager.state == .ready)
|
|
#expect(manager.errorMessage == nil)
|
|
#expect(manager.notes.first?.content == "hello note")
|
|
#expect(manager.notes.first?.displayName.hasPrefix("Builder#") == true)
|
|
}
|
|
|
|
@Test
|
|
func send_failureFormatsErrorMessageAndClearErrorRemovesIt() {
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, _, _, _, _ in },
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in throw TestError.shouldNotDerive },
|
|
now: { Date() }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
manager.send(content: "hello", nickname: "Builder")
|
|
|
|
#expect(manager.errorMessage?.isEmpty == false)
|
|
|
|
manager.clearError()
|
|
|
|
#expect(manager.errorMessage == nil)
|
|
}
|
|
|
|
@Test
|
|
func ingestDropsExpiredNotesAndKeepsUnexpiredOnes() throws {
|
|
var storedHandler: ((NostrEvent) -> Void)?
|
|
let now = Date(timeIntervalSince1970: 1_700_000_000)
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, _, _, handler, _ in
|
|
storedHandler = handler
|
|
},
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in throw TestError.shouldNotDerive },
|
|
now: { now }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
let identity = try NostrIdentity.generate()
|
|
|
|
let expired = NostrEvent(
|
|
pubkey: identity.publicKeyHex,
|
|
createdAt: now.addingTimeInterval(-3600),
|
|
kind: .textNote,
|
|
tags: [["g", "u4pruydq"], ["expiration", String(Int(now.timeIntervalSince1970) - 60)]],
|
|
content: "gone"
|
|
)
|
|
storedHandler?(try expired.sign(with: identity.schnorrSigningKey()))
|
|
|
|
let live = NostrEvent(
|
|
pubkey: identity.publicKeyHex,
|
|
createdAt: now.addingTimeInterval(-3600),
|
|
kind: .textNote,
|
|
tags: [["g", "u4pruydq"], ["expiration", String(Int(now.timeIntervalSince1970) + 3600)]],
|
|
content: "still here"
|
|
)
|
|
storedHandler?(try live.sign(with: identity.schnorrSigningKey()))
|
|
|
|
#expect(manager.notes.count == 1)
|
|
#expect(manager.notes.first?.content == "still here")
|
|
#expect(manager.notes.first?.expiresAt == Date(timeIntervalSince1970: TimeInterval(Int(now.timeIntervalSince1970) + 3600)))
|
|
}
|
|
|
|
@Test
|
|
func postDrop_sendsExpiringNoteToGeoRelays() throws {
|
|
var sentEvents: [NostrEvent] = []
|
|
let now = Date(timeIntervalSince1970: 1_700_000_000)
|
|
let identity = try NostrIdentity.generate()
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, _, _, _, _ in },
|
|
unsubscribe: { _ in },
|
|
sendEvent: { event, _ in sentEvents.append(event) },
|
|
deriveIdentity: { _ in identity },
|
|
now: { now }
|
|
)
|
|
|
|
let posted = LocationNotesManager.postDrop(
|
|
content: " the coffee here is great ",
|
|
nickname: "scout",
|
|
geohash: "u4pruydq",
|
|
dependencies: deps
|
|
)
|
|
|
|
#expect(posted)
|
|
#expect(sentEvents.count == 1)
|
|
let event = try #require(sentEvents.first)
|
|
#expect(event.kind == NostrProtocol.EventKind.textNote.rawValue)
|
|
#expect(event.content == "the coffee here is great")
|
|
#expect(event.tags.contains(["g", "u4pruydq"]))
|
|
let expiration = event.tags.first { $0.first == "expiration" }?.last
|
|
let expected = Int(now.addingTimeInterval(TransportConfig.locationDropExpirySeconds).timeIntervalSince1970)
|
|
#expect(expiration == String(expected))
|
|
}
|
|
|
|
@Test
|
|
func postDrop_failsWithoutRelays() {
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in [] },
|
|
subscribe: { _, _, _, _, _ in },
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in throw TestError.shouldNotDerive },
|
|
now: { Date() }
|
|
)
|
|
|
|
#expect(!LocationNotesManager.postDrop(content: "hi", nickname: "x", geohash: "u4pruydq", dependencies: deps))
|
|
}
|
|
|
|
@Test
|
|
func pruneExpiredNotes_dropsNotesWhoseExpiryPassed() throws {
|
|
var storedHandler: ((NostrEvent) -> Void)?
|
|
var currentNow = Date(timeIntervalSince1970: 1_700_000_000)
|
|
let deps = LocationNotesDependencies(
|
|
relayLookup: { _, _ in ["wss://relay.one"] },
|
|
subscribe: { _, _, _, handler, _ in
|
|
storedHandler = handler
|
|
},
|
|
unsubscribe: { _ in },
|
|
sendEvent: { _, _ in },
|
|
deriveIdentity: { _ in throw TestError.shouldNotDerive },
|
|
now: { currentNow }
|
|
)
|
|
|
|
let manager = LocationNotesManager(geohash: "u4pruydq", dependencies: deps)
|
|
let identity = try NostrIdentity.generate()
|
|
let note = NostrEvent(
|
|
pubkey: identity.publicKeyHex,
|
|
createdAt: currentNow,
|
|
kind: .textNote,
|
|
tags: [["g", "u4pruydq"], ["expiration", String(Int(currentNow.timeIntervalSince1970) + 60)]],
|
|
content: "short lived"
|
|
)
|
|
storedHandler?(try note.sign(with: identity.schnorrSigningKey()))
|
|
#expect(manager.notes.count == 1)
|
|
|
|
currentNow = currentNow.addingTimeInterval(120)
|
|
manager.pruneExpiredNotes()
|
|
|
|
#expect(manager.notes.isEmpty)
|
|
}
|
|
|
|
private enum TestError: Error {
|
|
case shouldNotDerive
|
|
}
|
|
}
|