From 10679fa3e6d23766e03565b8237d7135624b67e1 Mon Sep 17 00:00:00 2001 From: Taksh Date: Sun, 2 Aug 2026 09:32:32 +0530 Subject: [PATCH] refactor: extract BridgeStatusSummary formatter with tests Move bridge status string assembly into a testable helper used by App Info. --- .../Gateway/BridgeStatusSummary.swift | 63 +++++++++++++++++++ bitchat/Views/AppInfoView.swift | 23 ++----- .../Services/BridgeStatusSummaryTests.swift | 23 +++++++ 3 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 bitchat/Services/Gateway/BridgeStatusSummary.swift create mode 100644 bitchatTests/Services/BridgeStatusSummaryTests.swift diff --git a/bitchat/Services/Gateway/BridgeStatusSummary.swift b/bitchat/Services/Gateway/BridgeStatusSummary.swift new file mode 100644 index 00000000..3737fe4c --- /dev/null +++ b/bitchat/Services/Gateway/BridgeStatusSummary.swift @@ -0,0 +1,63 @@ +// +// BridgeStatusSummary.swift +// bitchat +// +// This is free and unencumbered software released into the public domain. +// For more information, see +// + +import Foundation + +/// One-line mesh bridge status for the settings pane. +enum BridgeStatusSummary { + static func formatted( + enabled: Bool, + cell: String?, + bridgedCount: Int, + nearbyOnly: Bool + ) -> String { + if !enabled { + return String( + localized: "app_info.settings.bridge.status.off", + defaultValue: "bridge off — only radio-range mesh traffic", + comment: "Bridge status line when the mesh bridge toggle is off" + ) + } + let cellPart = cell.map { + String( + format: String( + localized: "app_info.settings.bridge.status.cell", + defaultValue: "rendezvous #%@", + comment: "Bridge status fragment showing rendezvous cell; %@ is geohash" + ), + locale: .current, + $0 + ) + } ?? String( + localized: "app_info.settings.bridge.status.no_cell", + defaultValue: "no rendezvous cell", + comment: "Bridge status fragment when no cell is active" + ) + let peoplePart = String( + format: String( + localized: "app_info.settings.bridge.status.people", + defaultValue: "%lld people via bridge", + comment: "Bridge status fragment counting bridged participants; %lld is count" + ), + locale: .current, + bridgedCount + ) + let composePart = nearbyOnly + ? String( + localized: "app_info.settings.bridge.status.compose_nearby", + defaultValue: "compose: nearby only", + comment: "Bridge status fragment when outgoing mesh messages stay on radio" + ) + : String( + localized: "app_info.settings.bridge.status.compose_bridged", + defaultValue: "compose: bridged", + comment: "Bridge status fragment when outgoing mesh messages cross the bridge" + ) + return [cellPart, peoplePart, composePart].joined(separator: " · ") + } +} diff --git a/bitchat/Views/AppInfoView.swift b/bitchat/Views/AppInfoView.swift index 9c029cba..29d1aafe 100644 --- a/bitchat/Views/AppInfoView.swift +++ b/bitchat/Views/AppInfoView.swift @@ -79,25 +79,12 @@ struct AppInfoView: View { } static let bridgeNoCell = String(localized: "app_info.settings.bridge.no_cell", defaultValue: "no rendezvous cell yet — needs location access or a nearby bridge peer", comment: "Caption under the mesh bridge toggle when the bridge is on but has no geohash cell to meet on") static func bridgeStatusSummary(enabled: Bool, cell: String?, bridgedCount: Int, nearbyOnly: Bool) -> String { - if !enabled { - return String(localized: "app_info.settings.bridge.status.off", defaultValue: "bridge off — only radio-range mesh traffic", comment: "Bridge status line when the mesh bridge toggle is off") - } - let cellPart = cell.map { - String( - format: String(localized: "app_info.settings.bridge.status.cell", defaultValue: "rendezvous #%@", comment: "Bridge status fragment showing rendezvous cell; %@ is geohash"), - locale: .current, - $0 - ) - } ?? String(localized: "app_info.settings.bridge.status.no_cell", defaultValue: "no rendezvous cell", comment: "Bridge status fragment when no cell is active") - let peoplePart = String( - format: String(localized: "app_info.settings.bridge.status.people", defaultValue: "%lld people via bridge", comment: "Bridge status fragment counting bridged participants; %lld is count"), - locale: .current, - bridgedCount + BridgeStatusSummary.formatted( + enabled: enabled, + cell: cell, + bridgedCount: bridgedCount, + nearbyOnly: nearbyOnly ) - let composePart = nearbyOnly - ? String(localized: "app_info.settings.bridge.status.compose_nearby", defaultValue: "compose: nearby only", comment: "Bridge status fragment when outgoing mesh messages stay on radio") - : String(localized: "app_info.settings.bridge.status.compose_bridged", defaultValue: "compose: bridged", comment: "Bridge status fragment when outgoing mesh messages cross the bridge") - return [cellPart, peoplePart, composePart].joined(separator: " · ") } // Moved from LocationChannelsSheet; keys unchanged. (The former diff --git a/bitchatTests/Services/BridgeStatusSummaryTests.swift b/bitchatTests/Services/BridgeStatusSummaryTests.swift new file mode 100644 index 00000000..742b0831 --- /dev/null +++ b/bitchatTests/Services/BridgeStatusSummaryTests.swift @@ -0,0 +1,23 @@ +// +// BridgeStatusSummaryTests.swift +// bitchatTests +// +// This is free and unencumbered software released into the public domain. +// For more information, see +// + +import Testing +@testable import bitchat + +struct BridgeStatusSummaryTests { + @Test func offStateDoesNotMentionBridgePeople() { + let text = BridgeStatusSummary.formatted(enabled: false, cell: "u4pruy", bridgedCount: 3, nearbyOnly: false) + #expect(text.lowercased().contains("bridge off")) + } + + @Test func enabledStateIncludesCellAndCount() { + let text = BridgeStatusSummary.formatted(enabled: true, cell: "u4pruy", bridgedCount: 2, nearbyOnly: true) + #expect(text.contains("u4pruy")) + #expect(text.contains("nearby")) + } +}