Files
bitchat/bitchatTests/Services/UnifiedNoticesTests.swift
T
8415c52913 Unified notices: one pin, one sheet for board pins + location notes (#1392)
* Unified notices: merge board pins and location notes into one sheet

One pin icon in the header now opens a single Notices sheet with a
geo/mesh scope toggle, replacing the separate board, location-notes,
and mesh-only note buttons:

- geo tab: current geohash's notices — mesh-synced board posts merged
  and deduped with Nostr kind-1 location notes, with per-item mesh/net
  source badges. Scope follows the selected location channel, or the
  device's building geohash when chatting on mesh.
- mesh tab: mesh-local board only (fully offline).
- One composer: geo posts go to the board and bridge to Nostr (existing
  bridge), so mesh and internet see the same notice.
- Merged delete: tombstoning an own board post now also retracts the
  bridged Nostr copy via NIP-09 (new createDeleteEvent, bridged event
  ids tracked in BoardManager); own Nostr-only notes are deletable too.
- LocationNotesManager accepts any channel-precision geohash (1-12
  chars), not just building-level.

BoardView and LocationNotesView are superseded by NoticesView.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Notices round 2: honest composer, friendlier copy, new-pin chat alerts

- Urgent + expiry controls now appear on the mesh tab only: the bridged
  Nostr copy of a geo post carries neither, so relay-side readers would
  never see them. Geo posts default to non-urgent with 7-day expiry, and
  the bridged note now gets a NIP-40 expiration tag so honoring relays
  drop it in step with the board copy.
- Geo tab explainer reuses the original location-notes description
  (keeps its 29 existing translations); mesh tab gets a new plain-
  language description.
- New-pin chat alerts, fully local (no wire traffic): BoardStore fires
  postArrivals for posts newly accepted from the wire; BoardAlertsModel
  filters own posts, dedups by postID, and for urgent pins created
  within the last 30 minutes emits one system line into the matching
  timeline (geo pin -> that geohash's chat, mesh pin -> mesh chat),
  collapsing simultaneous arrivals into a count line.
- Routine pins light up the header: the pin icon tints orange whenever
  the current scope has notices at all, and fills (pin.fill) while
  unseen new pins are waiting; opening the sheet clears them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* i18n: translate the unified-notices strings into all 28 non-English locales

Adds the 13 new notices keys (sheet title, geo/mesh tabs, mesh
description, source badges, urgent alert lines, button tooltip and
accessibility strings) to the string catalog with translations for
every locale the app ships. The geo tab already reuses the fully
translated location_notes.description; this covers the rest. Insertion
preserves the catalog's case-insensitive key order, so the diff is
purely additive.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Address Codex review: panic-wipe reset, scoped badge clear, geohash-aware dedupe

- BoardStore.wipe() now emits didWipe; BoardAlertsModel subscribes and
  resets, so a panic wipe drops pending urgent lines (which could
  otherwise re-append pre-wipe content into chat after the collapse
  flush), unseen badge scopes, and handled-post history.
- Opening the notices sheet clears unseen badges only for the scopes it
  actually shows (mesh + current geo scope); pins for other geohash
  channels keep their badge until visited.
- LocationNotesManager.Note now retains the matched g tag, and the
  bridged-copy dedupe requires the note's geohash to equal the board
  post's — a same-text note from a neighboring cell is no longer
  swallowed as a duplicate.

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>
2026-07-07 17:40:12 +02:00

143 lines
4.7 KiB
Swift

//
// UnifiedNoticesTests.swift
// bitchatTests
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Foundation
import Testing
@testable import bitchat
struct UnifiedNoticesTests {
private let baseDate = Date(timeIntervalSince1970: 1_700_000_000)
private var baseMs: UInt64 { UInt64(baseDate.timeIntervalSince1970 * 1000) }
private func makePost(
content: String,
nickname: String = "alice",
createdAt: UInt64? = nil,
urgent: Bool = false
) -> BoardPostPacket {
BoardPostPacket(
postID: Data((0..<16).map { _ in UInt8.random(in: 0...255) }),
geohash: "9q8yy",
content: content,
authorSigningKey: Data(repeating: 1, count: 32),
authorNickname: nickname,
createdAt: createdAt ?? baseMs,
expiresAt: (createdAt ?? baseMs) + 24 * 60 * 60 * 1000,
flags: urgent ? BoardPostPacket.urgentFlag : 0,
signature: Data(repeating: 2, count: 64)
)
}
private func makeNote(
content: String,
nickname: String? = "alice",
createdAt: Date? = nil,
geohash: String = "9q8yy"
) -> LocationNotesManager.Note {
LocationNotesManager.Note(
id: UUID().uuidString,
pubkey: "ab" + UUID().uuidString.replacingOccurrences(of: "-", with: ""),
content: content,
createdAt: createdAt ?? baseDate,
nickname: nickname,
geohash: geohash
)
}
@Test
func merge_dropsBridgedCopyOfBoardPost() {
let post = makePost(content: "free couch on 5th")
let bridged = makeNote(content: "free couch on 5th", createdAt: baseDate.addingTimeInterval(30))
let merged = UnifiedNotices.merge(posts: [post], notes: [bridged])
#expect(merged.count == 1)
#expect(merged[0].isBoardPost)
}
@Test
func merge_keepsNoteWithSameContentOutsideWindow() {
let post = makePost(content: "water station here")
let oldNote = makeNote(
content: "water station here",
createdAt: baseDate.addingTimeInterval(-UnifiedNotices.bridgeDedupeWindow - 60)
)
let merged = UnifiedNotices.merge(posts: [post], notes: [oldNote])
#expect(merged.count == 2)
}
@Test
func merge_keepsSameTextNoteFromNeighborCell() {
// The notes subscription covers the center cell plus 8 neighbors; a
// matching note posted to a *neighbor* is not the bridged copy.
let post = makePost(content: "free couch on 5th")
let neighborNote = makeNote(content: "free couch on 5th", createdAt: baseDate.addingTimeInterval(30), geohash: "9q8yz")
let merged = UnifiedNotices.merge(posts: [post], notes: [neighborNote])
#expect(merged.count == 2)
}
@Test
func merge_keepsNoteFromDifferentAuthor() {
let post = makePost(content: "meetup at 6", nickname: "alice")
let note = makeNote(content: "meetup at 6", nickname: "bob")
let merged = UnifiedNotices.merge(posts: [post], notes: [note])
#expect(merged.count == 2)
}
@Test
func merge_sortsUrgentFirstThenNewest() {
let urgent = makePost(content: "road closed", createdAt: baseMs - 60_000, urgent: true)
let newerPost = makePost(content: "later post", createdAt: baseMs)
let note = makeNote(content: "a note", nickname: "carol", createdAt: baseDate.addingTimeInterval(30))
let merged = UnifiedNotices.merge(posts: [newerPost, urgent], notes: [note])
#expect(merged.map(\.content) == ["road closed", "a note", "later post"])
#expect(merged[0].isUrgent)
}
@Test
func merge_anonNicknamesMatchForDedupe() {
// Bridged posts from an empty nickname arrive as anon notes with no
// "n" tag; they must still dedupe against the anon board copy.
let post = makePost(content: "hello", nickname: "")
let bridged = makeNote(content: "hello", nickname: nil)
let merged = UnifiedNotices.merge(posts: [post], notes: [bridged])
#expect(merged.count == 1)
#expect(merged[0].isBoardPost)
#expect(merged[0].author == "anon")
}
@Test
func noticeItem_normalizesNoteDisplayName() {
let note = LocationNotesManager.Note(
id: "e1",
pubkey: "deadbeef",
content: "hi",
createdAt: baseDate,
nickname: "dave",
geohash: "9q8yy"
)
let item = NoticeItem(note: note)
#expect(item.author == "dave")
#expect(!item.isBoardPost)
#expect(!item.isUrgent)
}
}