import XCTest @testable import RouterOSAssistant @MainActor final class ExpertViewModelTests: XCTestCase { private func makeSchema() -> RouterOSMenuSchema { RouterOSMenuSchema( menuPath: "/ip route", restPath: "ip/route", category: .routing, displayName: "Test", summary: "", explanation: "", fields: [ RouterOSFieldSchema(key: "comment", label: "Kommentar", kind: .text, help: ""), RouterOSFieldSchema(key: "distance", label: "Distanz", kind: .int, help: "") ] ) } func testClearingACuratedFieldSendsExplicitEmptyValue() { let viewModel = ExpertViewModel(connectionService: ConnectionService()) viewModel.selectedSchema = makeSchema() viewModel.startEditing(RouterOSMenuItem(id: "*1", fields: ["comment": "bridge"])) viewModel.formValues["comment"] = "" XCTAssertEqual(viewModel.pendingCommand?.arguments["comment"], "") } func testUnchangedUncuratedFieldIsNotResent() { let viewModel = ExpertViewModel(connectionService: ConnectionService()) viewModel.selectedSchema = makeSchema() viewModel.startEditing(RouterOSMenuItem(id: "*1", fields: ["comment": "", "immediate-gw": "192.168.88.1"])) viewModel.formValues["comment"] = "bridge" XCTAssertNil(viewModel.pendingCommand?.arguments["immediate-gw"]) XCTAssertEqual(viewModel.pendingCommand?.arguments["comment"], "bridge") } func testChangedUncuratedFieldIsSent() { let viewModel = ExpertViewModel(connectionService: ConnectionService()) viewModel.selectedSchema = makeSchema() viewModel.startEditing(RouterOSMenuItem(id: "*1", fields: ["comment": "", "note": "old"])) viewModel.extraFields = [.init(key: "note", value: "new")] XCTAssertEqual(viewModel.pendingCommand?.arguments["note"], "new") } /// A dynamic/connected route's distance=0 is only valid as-is — RouterOS rejects it if /// resent verbatim on a `set` ("value of distance out of range (1...255)"), even when the /// value itself hasn't changed. Editing only the comment must not resend it. func testUnchangedCuratedFieldIsNotResentEvenIfOutOfNormalRange() { let viewModel = ExpertViewModel(connectionService: ConnectionService()) viewModel.selectedSchema = makeSchema() viewModel.startEditing(RouterOSMenuItem(id: "*1", fields: ["comment": "", "distance": "0"])) viewModel.formValues["comment"] = "bridge" XCTAssertNil(viewModel.pendingCommand?.arguments["distance"]) XCTAssertEqual(viewModel.pendingCommand?.arguments["comment"], "bridge") } }