forked from kay/RouterOS
M17-M19: Bekannte Router, Live-Traffic-Anzeige, Übersicht-Animation+Drag
M17: "Bekannte Router" im Verbinden-Tab (SavedRouter/SavedRoutersStore), Standort-Freitextfeld, Scroll-Cap ab 4 Einträgen. Bugfix: Umbenennen- TextField steckte in einem sich selbst deaktivierenden Button. M18: Live-Traffic-Punkt an Interfaces (InterfaceTrafficMonitor, eigene SSH-Verbindung, monitor-traffic-Polling). Dabei zwei reale CLI-Parser-Bugs gefunden und gefixt: running/disabled-Flags werden als Buchstaben vor dem ersten Feld codiert, nicht als key=value; monitor-traffic liefert "50.7kbps" statt einer reinen Zahl. M19: Übersicht-Tab — animierte Flussrichtung auf allen Verbindungslinien (TimelineView+dashPhase), frei verschiebbare Knoten mit Live-folgenden Linien, Zurücksetzen-Button. Zusätzlich (noch nicht live getestet, nur Build+Unit-Tests grün): LAN-Port-Konflikt-Prüfung im Einrichten-Assistenten mit doppelter Sicherheitsbestätigung, "Fertig"-Button nach erfolgreichem Anwenden. 82 Tests grün. HANDOFF.md/README.md/Manual.md/CHATLOG.md aktualisiert. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YDmUd93KxsYGr2kLTotWnG
This commit is contained in:
@@ -5,6 +5,11 @@ private final class MockTransport: RouterOSTransport {
|
||||
let kind: RouterOSTransportKind
|
||||
var connectError: Error?
|
||||
var deviceInfo = RouterDeviceInfo(boardName: "Mock", routerOSVersion: "7.0", architecture: "arm64", uptime: "1h")
|
||||
/// Canned `fetchMenuItems` responses, keyed by menu path — lets a test simulate whatever the
|
||||
/// router currently reports for `/interface bridge port`, `/ip address`, etc. without a real
|
||||
/// connection. Menu paths not present here return an empty list, same as a genuinely empty
|
||||
/// menu on the router.
|
||||
var menuItemsByPath: [String: [RouterOSMenuItem]] = [:]
|
||||
|
||||
init(kind: RouterOSTransportKind, connectError: Error? = nil) {
|
||||
self.kind = kind
|
||||
@@ -18,7 +23,7 @@ private final class MockTransport: RouterOSTransport {
|
||||
func fetchDeviceInfo() async throws -> RouterDeviceInfo { deviceInfo }
|
||||
func fetchInterfaces() async throws -> [NetworkInterface] { [] }
|
||||
func fetchFirewallRuleCounts() async throws -> FirewallRuleCounts { FirewallRuleCounts(filterRuleCount: 0, natRuleCount: 0) }
|
||||
func fetchMenuItems(menuPath: String, restPath: String) async throws -> [RouterOSMenuItem] { [] }
|
||||
func fetchMenuItems(menuPath: String, restPath: String) async throws -> [RouterOSMenuItem] { menuItemsByPath[menuPath] ?? [] }
|
||||
func fetchFieldValues(menuPath: String, restPath: String, whereField: String, whereValue: String, returnField: String) async throws -> Set<String> { [] }
|
||||
func apply(_ command: RouterOSCommand) async throws {}
|
||||
func disconnect() async {}
|
||||
@@ -79,4 +84,61 @@ final class ConnectionServiceTests: XCTestCase {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - checkPortConflict (Setup wizard LAN step pre-check)
|
||||
|
||||
private func connectedService(menuItemsByPath: [String: [RouterOSMenuItem]]) async -> ConnectionService {
|
||||
let service = ConnectionService()
|
||||
let transport = MockTransport(kind: .rest)
|
||||
transport.menuItemsByPath = menuItemsByPath
|
||||
await service.connect(with: credentials, makeRestTransport: { transport }, makeSSHTransport: { transport })
|
||||
return service
|
||||
}
|
||||
|
||||
func testCheckPortConflictReturnsNilWhenPortIsFree() async throws {
|
||||
let service = await connectedService(menuItemsByPath: [:])
|
||||
let conflict = try await service.checkPortConflict(interfaceName: "ether4")
|
||||
XCTAssertNil(conflict)
|
||||
}
|
||||
|
||||
func testCheckPortConflictNeverFlagsTheSharedBridgeItself() async throws {
|
||||
// "bridge" is this app's own shared-LAN interface, not "someone else's" configuration —
|
||||
// even if it happens to already carry addresses (the normal case), it must never be
|
||||
// reported as a conflict.
|
||||
let service = await connectedService(menuItemsByPath: [
|
||||
"/ip address": [RouterOSMenuItem(id: "*1", fields: ["interface": "bridge", "address": "192.168.88.1/24"])]
|
||||
])
|
||||
let conflict = try await service.checkPortConflict(interfaceName: "bridge")
|
||||
XCTAssertNil(conflict)
|
||||
}
|
||||
|
||||
func testCheckPortConflictDetectsBridgeMembership() async throws {
|
||||
let service = await connectedService(menuItemsByPath: [
|
||||
"/interface bridge port": [RouterOSMenuItem(id: "*1", fields: ["interface": "ether4", "bridge": "bridge"])]
|
||||
])
|
||||
let conflict = try await service.checkPortConflict(interfaceName: "ether4")
|
||||
XCTAssertEqual(conflict, PortConflict(interfaceName: "ether4", reasons: [.bridgeMember(bridgeName: "bridge")]))
|
||||
}
|
||||
|
||||
func testCheckPortConflictDetectsExistingAddressDhcpClientAndPppoeClientTogether() async throws {
|
||||
let service = await connectedService(menuItemsByPath: [
|
||||
"/ip address": [RouterOSMenuItem(id: "*1", fields: ["interface": "ether1", "address": "10.0.0.1/24"])],
|
||||
"/ip dhcp-client": [RouterOSMenuItem(id: "*2", fields: ["interface": "ether1"])],
|
||||
"/interface pppoe-client": [RouterOSMenuItem(id: "*3", fields: ["interface": "ether1"])]
|
||||
])
|
||||
let conflict = try await service.checkPortConflict(interfaceName: "ether1")
|
||||
XCTAssertEqual(conflict, PortConflict(
|
||||
interfaceName: "ether1",
|
||||
reasons: [.hasAddresses(["10.0.0.1/24"]), .dhcpClient, .pppoeClient]
|
||||
))
|
||||
}
|
||||
|
||||
func testCheckPortConflictIgnoresEntriesForOtherInterfaces() async throws {
|
||||
let service = await connectedService(menuItemsByPath: [
|
||||
"/interface bridge port": [RouterOSMenuItem(id: "*1", fields: ["interface": "ether2", "bridge": "bridge"])],
|
||||
"/ip address": [RouterOSMenuItem(id: "*2", fields: ["interface": "ether2", "address": "192.168.88.1/24"])]
|
||||
])
|
||||
let conflict = try await service.checkPortConflict(interfaceName: "ether5")
|
||||
XCTAssertNil(conflict)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import XCTest
|
||||
@testable import RouterOSAssistant
|
||||
|
||||
final class PortConflictTests: XCTestCase {
|
||||
func testBridgeMembershipProducesNoResolutionCommand() {
|
||||
// DhcpServerCommandBuilder already unconditionally detaches any bridge membership —
|
||||
// repeating it here would just be a harmless duplicate at best, so it's deliberately
|
||||
// excluded from this list.
|
||||
let conflict = PortConflict(interfaceName: "ether4", reasons: [.bridgeMember(bridgeName: "bridge")])
|
||||
XCTAssertTrue(conflict.resolutionCommands().isEmpty)
|
||||
}
|
||||
|
||||
func testExistingAddressIsRemovedByInterfaceMatch() {
|
||||
let conflict = PortConflict(interfaceName: "ether4", reasons: [.hasAddresses(["10.0.0.1/24"])])
|
||||
let commands = conflict.resolutionCommands()
|
||||
|
||||
XCTAssertEqual(commands.count, 1)
|
||||
XCTAssertEqual(commands[0].menuPath, "/ip address")
|
||||
XCTAssertEqual(commands[0].operation, .remove(matchField: "interface", matchValue: "ether4"))
|
||||
}
|
||||
|
||||
func testDhcpClientAndPppoeClientAreBothRemoved() {
|
||||
let conflict = PortConflict(interfaceName: "ether1", reasons: [.dhcpClient, .pppoeClient])
|
||||
let commands = conflict.resolutionCommands()
|
||||
|
||||
XCTAssertEqual(commands.count, 2)
|
||||
XCTAssertEqual(commands[0].menuPath, "/ip dhcp-client")
|
||||
XCTAssertEqual(commands[0].operation, .remove(matchField: "interface", matchValue: "ether1"))
|
||||
XCTAssertEqual(commands[1].menuPath, "/interface pppoe-client")
|
||||
XCTAssertEqual(commands[1].operation, .remove(matchField: "interface", matchValue: "ether1"))
|
||||
}
|
||||
}
|
||||
@@ -18,21 +18,54 @@ final class RouterOSCliParserTests: XCTestCase {
|
||||
XCTAssertEqual(info.uptime, "1w2d3h4m5s")
|
||||
}
|
||||
|
||||
/// "running"/"disabled" are never present as `key=value` pairs in real `/interface print
|
||||
/// terse` output — confirmed live (2026-09-15, hEX/RouterOS 7.x) against a router with a mix
|
||||
/// of running, bridge-slave, and (synthetically added here) disabled ports:
|
||||
/// 0 R name=ether1 ... (running)
|
||||
/// 2 S name=ether3 ... (not running, bridge slave)
|
||||
/// RouterOS encodes them as single-letter flags in a fixed-width column before the first
|
||||
/// key=value pair instead ("X" = disabled, "R" = running, "S" = slave). An earlier version of
|
||||
/// this parser (and this test) assumed invented `running=`/`disabled=` keys that never
|
||||
/// actually appear — every interface's `running` silently read as `false` as a result.
|
||||
func testParseInterfaces() {
|
||||
let raw = """
|
||||
0 R name="ether1" type="ether" mtu=1500 mac-address="AA:BB:CC:DD:EE:01" running=true disabled=no
|
||||
1 name="ether2" type="ether" mtu=1500 mac-address="AA:BB:CC:DD:EE:02" running=false disabled=yes
|
||||
0 R name=ether1 type=ether mac-address=AA:BB:CC:DD:EE:01
|
||||
1 S name=ether2 type=ether mac-address=AA:BB:CC:DD:EE:02
|
||||
2 X name=ether3 type=ether mac-address=AA:BB:CC:DD:EE:03
|
||||
"""
|
||||
|
||||
let interfaces = RouterOSCliParser.parseInterfaces(raw)
|
||||
|
||||
XCTAssertEqual(interfaces.count, 2)
|
||||
XCTAssertEqual(interfaces.count, 3)
|
||||
XCTAssertEqual(interfaces[0].name, "ether1")
|
||||
XCTAssertTrue(interfaces[0].running)
|
||||
XCTAssertFalse(interfaces[0].disabled)
|
||||
XCTAssertEqual(interfaces[1].name, "ether2")
|
||||
XCTAssertFalse(interfaces[1].running)
|
||||
XCTAssertTrue(interfaces[1].disabled)
|
||||
XCTAssertFalse(interfaces[1].disabled)
|
||||
XCTAssertEqual(interfaces[2].name, "ether3")
|
||||
XCTAssertFalse(interfaces[2].running)
|
||||
XCTAssertTrue(interfaces[2].disabled)
|
||||
}
|
||||
|
||||
/// The exact live output pasted by the user (2026-09-15, hEX) that surfaced the flag-column
|
||||
/// bug — a mix of running/slave/plain ports plus the router's own bridge and loopback.
|
||||
func testParseInterfacesMatchesLiveHexOutput() {
|
||||
let raw = """
|
||||
0 R name=ether1 default-name=ether1 type=ether mtu=1500 actual-mtu=1500 l2mtu=1596 max-l2mtu=2026 vrf=main mac-address=F4:1E:57:1B:37:D5 last-link-up-time=2026-09-15 20:19:19 link-downs=0
|
||||
1 RS name=ether2 default-name=ether2 type=ether mtu=1500 actual-mtu=1500 l2mtu=1596 max-l2mtu=2026 vrf=main mac-address=F4:1E:57:1B:37:D6 last-link-up-time=2026-09-15 20:19:19 link-downs=0
|
||||
2 S name=ether3 default-name=ether3 type=ether mtu=1500 actual-mtu=1500 l2mtu=1596 max-l2mtu=2026 vrf=main mac-address=F4:1E:57:1B:37:D7 link-downs=0
|
||||
3 R name=ether4 default-name=ether4 type=ether mtu=1500 actual-mtu=1500 l2mtu=1596 max-l2mtu=2026 vrf=main mac-address=F4:1E:57:1B:37:D8 last-link-up-time=2026-09-15 20:19:19 link-downs=0
|
||||
4 S name=ether5 default-name=ether5 type=ether mtu=1500 actual-mtu=1500 l2mtu=1596 max-l2mtu=2026 vrf=main mac-address=F4:1E:57:1B:37:D9 link-downs=0
|
||||
5 R comment=defconf name=bridge type=bridge mtu=auto actual-mtu=1500 l2mtu=1596 vrf=main mac-address=F4:1E:57:1B:37:D6 last-link-up-time=2026-09-15 20:19:27 link-downs=0
|
||||
6 R name=lo type=loopback mtu=65536 actual-mtu=65536 vrf=main mac-address=00:00:00:00:00:00 last-link-up-time=2026-09-15 20:19:15 link-downs=0
|
||||
"""
|
||||
|
||||
let interfaces = RouterOSCliParser.parseInterfaces(raw)
|
||||
|
||||
XCTAssertEqual(interfaces.map(\.name), ["ether1", "ether2", "ether3", "ether4", "ether5", "bridge", "lo"])
|
||||
XCTAssertEqual(interfaces.map(\.running), [true, true, false, true, false, true, true])
|
||||
XCTAssertTrue(interfaces.allSatisfy { !$0.disabled })
|
||||
}
|
||||
|
||||
/// Regression test for a real bug found on a physical hEX device: its SSH output didn't
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import XCTest
|
||||
@testable import RouterOSAssistant
|
||||
|
||||
/// RouterOS' `/interface monitor-traffic <name> once` reports throughput as human-formatted
|
||||
/// strings with a unit suffix, never a bare integer — confirmed live (2026-09-15, hEX/RouterOS
|
||||
/// 7.x): "50.7kbps", "34.0kbps". The original implementation did `Int(fields[...] ?? "0") ?? 0`,
|
||||
/// which silently parsed every real value to 0 (a numeric string followed by letters isn't a
|
||||
/// valid `Int`), so the traffic indicator never showed as active regardless of real traffic.
|
||||
final class SSHTransportTrafficParsingTests: XCTestCase {
|
||||
func testParsesKbpsWithDecimalPoint() {
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("50.7kbps"), 50_700)
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("34.0kbps"), 34_000)
|
||||
}
|
||||
|
||||
func testParsesMbpsAndGbps() {
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("1.5Mbps"), 1_500_000)
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("2Gbps"), 2_000_000_000)
|
||||
}
|
||||
|
||||
/// "kbps" itself ends with "bps" — the plain "bps" suffix must not be matched first, or
|
||||
/// "50.7kbps" would parse as if it were "50.7" followed by a stray "k".
|
||||
func testDoesNotConfuseKbpsWithPlainBps() {
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("500bps"), 500)
|
||||
}
|
||||
|
||||
func testIdleReportsZero() {
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("0"), 0)
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("0bps"), 0)
|
||||
}
|
||||
|
||||
func testMissingOrUnparsableFallsBackToZero() {
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond(nil), 0)
|
||||
XCTAssertEqual(SSHTransport.parseBitsPerSecond("n/a"), 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import XCTest
|
||||
@testable import RouterOSAssistant
|
||||
|
||||
final class SavedRoutersStoreTests: XCTestCase {
|
||||
/// A dedicated suite name per test avoids bleeding state between tests / real app defaults.
|
||||
private func makeStore() -> SavedRoutersStore {
|
||||
let suiteName = "SavedRoutersStoreTests.\(UUID().uuidString)"
|
||||
let defaults = UserDefaults(suiteName: suiteName)!
|
||||
return SavedRoutersStore(defaults: defaults)
|
||||
}
|
||||
|
||||
func testFirstSuccessfulConnectionAddsEntryWithDefaultName() {
|
||||
let store = makeStore()
|
||||
let routers = store.recordSuccessfulConnection(host: "192.168.88.1", username: "admin", defaultName: "hEX")
|
||||
|
||||
XCTAssertEqual(routers.count, 1)
|
||||
XCTAssertEqual(routers[0].host, "192.168.88.1")
|
||||
XCTAssertEqual(routers[0].username, "admin")
|
||||
XCTAssertEqual(routers[0].name, "hEX")
|
||||
XCTAssertEqual(routers[0].location, "")
|
||||
}
|
||||
|
||||
func testUpdateLocationRoundTrip() {
|
||||
let store = makeStore()
|
||||
store.recordSuccessfulConnection(host: "192.168.88.1", username: "admin", defaultName: "hEX")
|
||||
let id = store.load()[0].id
|
||||
|
||||
let updated = store.updateLocation(id, to: "Keller, Serverschrank")
|
||||
XCTAssertEqual(updated.first?.location, "Keller, Serverschrank")
|
||||
// Renaming afterwards must not clobber the location, and vice versa.
|
||||
let renamed = store.rename(id, to: "Hauptrouter")
|
||||
XCTAssertEqual(renamed.first?.name, "Hauptrouter")
|
||||
XCTAssertEqual(renamed.first?.location, "Keller, Serverschrank")
|
||||
}
|
||||
|
||||
/// A list saved by an earlier app version (before `location` existed) must keep loading
|
||||
/// instead of the whole list silently vanishing — see `SavedRouter.init(from:)`.
|
||||
func testDecodingAnEntryWithoutALocationFieldDefaultsToEmpty() {
|
||||
let suiteName = "SavedRoutersStoreTests.\(UUID().uuidString)"
|
||||
let defaults = UserDefaults(suiteName: suiteName)!
|
||||
let legacyJSON = """
|
||||
[{"id":"\(UUID().uuidString)","host":"192.168.88.1","username":"admin","name":"hEX","lastConnectedAt":0}]
|
||||
"""
|
||||
defaults.set(Data(legacyJSON.utf8), forKey: "RouterOSAssistant.SavedRouters")
|
||||
|
||||
let store = SavedRoutersStore(defaults: defaults)
|
||||
let routers = store.load()
|
||||
|
||||
XCTAssertEqual(routers.count, 1)
|
||||
XCTAssertEqual(routers[0].name, "hEX")
|
||||
XCTAssertEqual(routers[0].location, "")
|
||||
}
|
||||
|
||||
func testReconnectingToKnownRouterUpdatesRecencyNotName() {
|
||||
let store = makeStore()
|
||||
store.recordSuccessfulConnection(host: "192.168.88.1", username: "admin", defaultName: "hEX")
|
||||
store.rename(store.load()[0].id, to: "Büro-Router")
|
||||
|
||||
// A later connection reports a possibly-different board name (e.g. after a hardware
|
||||
// swap behind the same IP) — the user's own rename must survive regardless.
|
||||
let routers = store.recordSuccessfulConnection(host: "192.168.88.1", username: "admin", defaultName: "RB750Gr3")
|
||||
|
||||
XCTAssertEqual(routers.count, 1)
|
||||
XCTAssertEqual(routers[0].name, "Büro-Router")
|
||||
}
|
||||
|
||||
func testDifferentUsernameOnSameHostIsATwoEntries() {
|
||||
let store = makeStore()
|
||||
store.recordSuccessfulConnection(host: "192.168.88.1", username: "admin", defaultName: "hEX")
|
||||
let routers = store.recordSuccessfulConnection(host: "192.168.88.1", username: "gast", defaultName: "hEX")
|
||||
|
||||
XCTAssertEqual(routers.count, 2)
|
||||
}
|
||||
|
||||
func testRenameAndRemoveRoundTrip() {
|
||||
let store = makeStore()
|
||||
store.recordSuccessfulConnection(host: "192.168.88.1", username: "admin", defaultName: "hEX")
|
||||
let id = store.load()[0].id
|
||||
|
||||
let renamed = store.rename(id, to: "Wohnzimmer")
|
||||
XCTAssertEqual(renamed.first?.name, "Wohnzimmer")
|
||||
|
||||
let removed = store.remove(id)
|
||||
XCTAssertTrue(removed.isEmpty)
|
||||
XCTAssertTrue(store.load().isEmpty)
|
||||
}
|
||||
|
||||
func testLoadOrdersMostRecentlyConnectedFirst() {
|
||||
let store = makeStore()
|
||||
store.recordSuccessfulConnection(host: "10.0.0.1", username: "admin", defaultName: "Router A")
|
||||
store.recordSuccessfulConnection(host: "10.0.0.2", username: "admin", defaultName: "Router B")
|
||||
// Reconnecting to the first one should move it back to the front.
|
||||
let routers = store.recordSuccessfulConnection(host: "10.0.0.1", username: "admin", defaultName: "Router A")
|
||||
|
||||
XCTAssertEqual(routers.first?.host, "10.0.0.1")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user