forked from kay/RouterOS
Neuer "Einrichten"-Tab führt durch Internet-Anschluss (DHCP/statisch/ PPPoE) und Heimnetzwerk+DHCP-Server, zeigt vor dem Anwenden eine Klartext-Übersicht (optional mit den exakten RouterOS-Befehlen) und erstellt automatisch eine Sicherung, bevor Änderungen geschrieben werden. Änderungen laufen über beide Transporte: CLI-Zeile für SSH, JSON-POST für REST — beide aus einem gemeinsamen RouterOSCommand gebaut. RouterOS-CLI-Syntax ist Standard und langjährig stabil, aber nicht gegen ein echtes Gerät verifiziert; deshalb die Detailanzeige im Übersichtsschritt vor dem Anwenden. ConnectionService ist jetzt der einzige App-weite Zustand (ersetzt SessionStore) und wird explizit an alle drei Tabs durchgereicht. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HReLXMbmPvtQ23p1iWiJNW
80 lines
2.7 KiB
Swift
80 lines
2.7 KiB
Swift
import XCTest
|
|
@testable import RouterOSAssistant
|
|
|
|
private final class MockTransport: RouterOSTransport {
|
|
let kind: RouterOSTransportKind
|
|
var connectError: Error?
|
|
var deviceInfo = RouterDeviceInfo(boardName: "Mock", routerOSVersion: "7.0", architecture: "arm64", uptime: "1h")
|
|
|
|
init(kind: RouterOSTransportKind, connectError: Error? = nil) {
|
|
self.kind = kind
|
|
self.connectError = connectError
|
|
}
|
|
|
|
func connect() async throws {
|
|
if let connectError { throw connectError }
|
|
}
|
|
|
|
func fetchDeviceInfo() async throws -> RouterDeviceInfo { deviceInfo }
|
|
func fetchInterfaces() async throws -> [NetworkInterface] { [] }
|
|
func apply(_ command: RouterOSCommand) async throws {}
|
|
func disconnect() async {}
|
|
}
|
|
|
|
@MainActor
|
|
final class ConnectionServiceTests: XCTestCase {
|
|
private let credentials = RouterOSCredentials(host: "192.168.88.1", username: "admin", password: "")
|
|
|
|
func testFallsBackToSSHWhenRestFails() async {
|
|
let service = ConnectionService()
|
|
let sshTransport = MockTransport(kind: .ssh)
|
|
|
|
await service.connect(
|
|
with: credentials,
|
|
makeRestTransport: { MockTransport(kind: .rest, connectError: RouterOSError.transportUnavailable("kein REST")) },
|
|
makeSSHTransport: { sshTransport }
|
|
)
|
|
|
|
XCTAssertEqual(service.state, .connected(kind: .ssh))
|
|
}
|
|
|
|
func testUsesRestWhenAvailable() async {
|
|
let service = ConnectionService()
|
|
|
|
await service.connect(
|
|
with: credentials,
|
|
makeRestTransport: { MockTransport(kind: .rest) },
|
|
makeSSHTransport: { MockTransport(kind: .ssh) }
|
|
)
|
|
|
|
XCTAssertEqual(service.state, .connected(kind: .rest))
|
|
}
|
|
|
|
func testUntrustedCertificateAsksForConfirmationInsteadOfFallingBackToSSH() async {
|
|
let service = ConnectionService()
|
|
|
|
await service.connect(
|
|
with: credentials,
|
|
makeRestTransport: { MockTransport(kind: .rest, connectError: RouterOSError.untrustedCertificate(fingerprint: "AA:BB")) },
|
|
makeSSHTransport: { MockTransport(kind: .ssh) }
|
|
)
|
|
|
|
XCTAssertEqual(service.state, .needsCertificateConfirmation(fingerprint: "AA:BB"))
|
|
}
|
|
|
|
func testFailsWhenBothTransportsFail() async {
|
|
let service = ConnectionService()
|
|
|
|
await service.connect(
|
|
with: credentials,
|
|
makeRestTransport: { MockTransport(kind: .rest, connectError: RouterOSError.transportUnavailable("kein REST")) },
|
|
makeSSHTransport: { MockTransport(kind: .ssh, connectError: RouterOSError.transportUnavailable("kein SSH")) }
|
|
)
|
|
|
|
guard case .failed = service.state else {
|
|
XCTFail("Erwarteter Zustand .failed, war \(service.state)")
|
|
return
|
|
}
|
|
}
|
|
}
|