diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt index a9dbbd1b..6c69a5e5 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt @@ -168,6 +168,7 @@ private fun EntryPage(state: SetupWizardState, strings: Strings) { RadioOption( label = when (source) { WizardSource.NEW -> strings.wizardSourceLoginNew + WizardSource.TAKE -> state.loginDonor?.displayName.orEmpty() WizardSource.KEEP -> strings.wizardSourceLoginKeep(state.storedCallsign) }, selected = state.loginSource == source, @@ -183,6 +184,7 @@ private fun EntryPage(state: SetupWizardState, strings: Strings) { RadioOption( label = when (source) { WizardSource.NEW -> strings.wizardSourceStationNew + WizardSource.TAKE -> state.stationDonor?.displayName.orEmpty() WizardSource.KEEP -> strings.wizardSourceStationKeep(state.storedLocator) }, selected = state.stationSource == source, diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt index ea6a9ef8..ec9809f2 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardState.kt @@ -12,10 +12,30 @@ enum class WizardSource { /** Entered in the wizard, starting from the wizard's own empty values. */ NEW, + /** Taken from another, configured profile. */ + TAKE, + /** What the profile already holds. */ KEEP, } +/** + * Another profile the login or the station can be taken from. + * + * Its preferences are read from its file and never written: taking over fills the wizard's + * answers, and Save writes only into the active profile. + * + * @param prefs the other profile's preferences, already read from its file + */ +class WizardDonor(val profileId: String, val displayName: String, val prefs: ChatPreferences) { + + /** For the entry page's label. */ + val callsign: String = prefs.getStn_loginCallSign().orEmpty() + + /** For the entry page's label. */ + val locator: String = prefs.getStn_loginLocatorMainCat().orEmpty() +} + /** The pages, in the order they can appear. */ enum class WizardPage { ENTRY, PERSONAL, LOGIN, SITE, ANTENNA, LOG, GEAR, SUMMARY } @@ -52,8 +72,27 @@ enum class WizardBand( * depends on Compose. * * @param prefs the profile's preferences, already read from its file + * @param donors other configured profiles the login or the station can be taken from */ -class SetupWizardState(prefs: ChatPreferences) { +class SetupWizardState(prefs: ChatPreferences, val donors: List = emptyList()) { + + /** What the pages of the station group show of one profile's preferences. */ + private class StationSnapshot(prefs: ChatPreferences) { + val locator: String = prefs.getStn_loginLocatorMainCat().orEmpty() + val bands: Set = WizardBand.entries.filter { it.isActive(prefs) }.toSet() + val category: Int? = prefs.getLoginChatCategoryMain()?.categoryNumber + val secondChat: Boolean = prefs.isLoginToSecondChatEnabled() + val secondCategory: Int? = prefs.getLoginChatCategorySecond()?.categoryNumber + } + + /** Kept to rebuild the answers of the station group when its source changes. */ + private val ownPrefs: ChatPreferences = prefs + private val ownStation = StationSnapshot(prefs) + + /** What the own profile has on, whatever the answers are built from. */ + private val ownSwitches: Set = WizardIntegrations(prefs).let { + it.storedLogSwitches() + it.storedGearSwitches() + } private val loginConfigured = !LoginConfiguration.isUnconfigured(prefs) private val templateCredentials = LoginConfiguration.carriesTemplateCredentials(prefs) @@ -65,15 +104,12 @@ class SetupWizardState(prefs: ChatPreferences) { private val storedSecondName: String = prefs.getStn_loginNameSecondCat().orEmpty() /** The locator the profile holds, for the label of "leave unchanged". */ - val storedLocator: String = prefs.getStn_loginLocatorMainCat().orEmpty() - private val storedBands: Set = WizardBand.entries.filter { it.isActive(prefs) }.toSet() - private val storedCategory: Int? = prefs.getLoginChatCategoryMain()?.categoryNumber - private val storedSecondChat: Boolean = prefs.isLoginToSecondChatEnabled() - private val storedSecondCategory: Int? = prefs.getLoginChatCategorySecond()?.categoryNumber + val storedLocator: String = ownStation.locator - /** A login can only be kept where an operator entered one. */ + /** A login can be taken where another profile has one, and kept where an operator entered one. */ val loginSources: List = buildList { add(WizardSource.NEW) + if (donors.isNotEmpty()) add(WizardSource.TAKE) if (loginConfigured) add(WizardSource.KEEP) } @@ -83,6 +119,7 @@ class SetupWizardState(prefs: ChatPreferences) { */ val stationSources: List = buildList { add(WizardSource.NEW) + if (donors.isNotEmpty()) add(WizardSource.TAKE) if (!templateCredentials) add(WizardSource.KEEP) } @@ -95,8 +132,17 @@ class SetupWizardState(prefs: ChatPreferences) { var stationSource: WizardSource? by mutableStateOf(stationSources.singleOrNull()) private set - private var appliedLoginSource: WizardSource? = null - private var appliedStationSource: WizardSource? = null + /** The profile the login is taken from; decided with the source when there is only one. */ + var loginDonor: WizardDonor? by mutableStateOf(null) + private set + + /** The profile the station is taken from; decided with the source when there is only one. */ + var stationDonor: WizardDonor? by mutableStateOf(null) + private set + + /* A group is reloaded when its source or its donor changes, and only then. */ + private var appliedLogin: Pair = null to null + private var appliedStation: Pair = null to null /** The stored form of the language: a code, or empty for the system language. */ var language: String by mutableStateOf(prefs.getGuiOptions_language().orEmpty()) @@ -121,11 +167,16 @@ class SetupWizardState(prefs: ChatPreferences) { var secondCategory: Int? by mutableStateOf(null) - /** Antenna and power; always proposals from what is stored. */ - val antenna = WizardAntenna(prefs) + /** + * Antenna and power; always proposals from the profile the station comes from. Replaced + * when the station's source changes, so nothing typed under one choice shows under another. + */ + var antenna: WizardAntenna by mutableStateOf(WizardAntenna(prefs)) + private set - /** Log program, rotator and AirScout. */ - val integrations = WizardIntegrations(prefs) + /** Log program, rotator and AirScout; replaced like [antenna]. */ + var integrations: WizardIntegrations by mutableStateOf(WizardIntegrations(prefs)) + private set private var loginShown by mutableStateOf(false) @@ -148,44 +199,79 @@ class SetupWizardState(prefs: ChatPreferences) { } fun chooseLoginSource(source: WizardSource) { - if (source in loginSources) loginSource = source + if (source !in loginSources) return + loginSource = source + if (source == WizardSource.TAKE && loginDonor == null) loginDonor = donors.singleOrNull() } fun chooseStationSource(source: WizardSource) { - if (source in stationSources) stationSource = source + if (source !in stationSources) return + stationSource = source + if (source == WizardSource.TAKE && stationDonor == null) stationDonor = donors.singleOrNull() } + fun chooseLoginDonor(donor: WizardDonor) { + if (donor in donors) loginDonor = donor + } + + fun chooseStationDonor(donor: WizardDonor) { + if (donor in donors) stationDonor = donor + } + + /** A source is decided once it is chosen and, when it takes over, says from whom. */ + private fun decided(source: WizardSource?, donor: WizardDonor?): Boolean = + source != null && (source != WizardSource.TAKE || donor != null) + /** - * Loads the answers of a group whose source changed. A group whose source is the one - * already applied is left alone, so going back to the entry page costs nothing typed. + * Loads the answers of a group whose source or donor changed. A group whose choice is the + * one already applied is left alone, so going back to the entry page costs nothing typed. */ private fun applySources() { - if (loginSource != appliedLoginSource) { - val keep = loginSource == WizardSource.KEEP - callsign = if (keep) storedCallsign else "" - password = if (keep) storedPassword else "" - loginShown = !keep || !loginComplete - appliedLoginSource = loginSource + val login = loginSource to loginDonor.takeIf { loginSource == WizardSource.TAKE } + if (login != appliedLogin) { + when (loginSource) { + WizardSource.KEEP -> { + callsign = storedCallsign + password = storedPassword + } + WizardSource.TAKE -> { + callsign = loginDonor?.prefs?.getStn_loginCallSign().orEmpty() + password = loginDonor?.prefs?.getStn_loginPassword().orEmpty() + } + else -> { + callsign = "" + password = "" + } + } + // Shown when entered anew, and when what was kept or taken cannot be saved as it is. + loginShown = loginSource == WizardSource.NEW || !loginComplete + appliedLogin = login } - if (stationSource != appliedStationSource) { - val keep = stationSource == WizardSource.KEEP - locator = if (keep) storedLocator else "" - bands = if (keep) storedBands else emptySet() - category = if (keep) storedCategory else null - secondChat = keep && storedSecondChat - secondCategory = if (keep) storedSecondCategory else null - // A kept category is the operator's; the suggestion must not replace it. - categoryChosenByOperator = keep - siteShown = !keep || !stationComplete - antennaShown = !keep - logShown = !keep - gearShown = !keep - // A new station gets only what the operator says yes to. - if (keep) integrations.loadStored() else integrations.loadFresh() - // Proposals from the profile either way; what was typed under the other choice goes. - antenna.loadStored() - appliedStationSource = stationSource + val station = stationSource to stationDonor.takeIf { stationSource == WizardSource.TAKE } + if (station != appliedStation) { + val fresh = stationSource == WizardSource.NEW + val base = if (stationSource == WizardSource.TAKE) stationDonor?.prefs ?: ownPrefs else ownPrefs + val snapshot = if (base === ownPrefs) ownStation else StationSnapshot(base) + + locator = if (fresh) "" else snapshot.locator + bands = if (fresh) emptySet() else snapshot.bands + category = if (fresh) null else snapshot.category + secondChat = !fresh && snapshot.secondChat + secondCategory = if (fresh) null else snapshot.secondCategory + // A kept or taken category is an operator's; the suggestion must not replace it. + categoryChosenByOperator = !fresh + + siteShown = fresh || !stationComplete + antennaShown = fresh + logShown = fresh + gearShown = fresh + + // Built anew from the profile the station comes from; a new station gets only + // what the operator says yes to. + antenna = WizardAntenna(base) + integrations = WizardIntegrations(base).also { if (fresh) it.loadFresh() } + appliedStation = station } } @@ -271,7 +357,7 @@ class SetupWizardState(prefs: ChatPreferences) { val canGoNext: Boolean get() = when (page) { - WizardPage.ENTRY -> loginSource != null && stationSource != null + WizardPage.ENTRY -> decided(loginSource, loginDonor) && decided(stationSource, stationDonor) WizardPage.PERSONAL -> personalComplete WizardPage.LOGIN -> loginComplete WizardPage.SITE -> stationComplete @@ -341,14 +427,22 @@ class SetupWizardState(prefs: ChatPreferences) { get() = if (logShown) integrations.networked && integrations.clusterServer else integrations.storedClusterServer /** - * What was on in the profile and will be off after Save, for the summary. Pages that are - * not part of this run change nothing and contribute nothing. + * What is on in the own profile and will be off after Save, for the summary. Always + * against the own profile: with a station taken from another one, that is what the + * operator loses, whatever the other profile had. + * + * A page that is not part of the run contributes what its profile holds -- the own one + * for a station left unchanged, the donor's for a station taken over. */ val switchedOff: List - get() = buildList { - if (storedSecondChat && !secondChat) add(WizardSwitch.SECOND_CHAT) - if (logShown) addAll(integrations.switchedOffByLog()) - if (gearShown) addAll(integrations.switchedOffByGear()) + get() { + val after = buildSet { + if (secondChat) add(WizardSwitch.SECOND_CHAT) + addAll(if (logShown) integrations.answeredLogSwitches() else integrations.storedLogSwitches()) + addAll(if (gearShown) integrations.answeredGearSwitches() else integrations.storedGearSwitches()) + } + val before = ownSwitches + if (ownStation.secondChat) setOf(WizardSwitch.SECOND_CHAT) else emptySet() + return WizardSwitch.entries.filter { it in before && it !in after } } /** @@ -364,6 +458,8 @@ class SetupWizardState(prefs: ChatPreferences) { * Antenna, log program and equipment are written only for the pages that were part of * this run. * + * A station taken from another profile is copied whole before the answers are written. + * * @return false when a mandatory answer is missing -- nothing is touched then -- or when * the file could not be written */ @@ -381,6 +477,12 @@ class SetupWizardState(prefs: ChatPreferences) { prefs.setStn_loginCallSign(callsign.trim()) prefs.setStn_loginPassword(password) + // The whole station of the other profile first, also what no page asks about; the + // answers below then lie on top, so a taken value the operator changed wins. + if (stationSource == WizardSource.TAKE) { + stationDonor?.let { copyStation(it.prefs, prefs) } + } + prefs.setStn_loginLocatorMainCat(locator.trim()) WizardBand.entries.forEach { it.setActive(prefs, it in bands) } prefs.setLoginChatCategoryMain(ChatCategory(requireNotNull(category))) diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardIntegrations.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardIntegrations.kt index 2977320c..1ed7af5e 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardIntegrations.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardIntegrations.kt @@ -243,19 +243,37 @@ class WizardIntegrations(prefs: ChatPreferences) { } } - /** What the log page's answers switch off that the profile had on, in a fixed order. */ - fun switchedOffByLog(): List = buildList { - if (storedWintest && logProgram != WizardLogProgram.WINTEST) add(WizardSwitch.LOG_WINTEST) - if (storedUdp && logProgram != WizardLogProgram.UDP) add(WizardSwitch.LOG_UDP) - if (storedFile && logProgram != WizardLogProgram.FILE) add(WizardSwitch.LOG_FILE) - if ((storedUdpQrg || storedWintestQrg) && !qrgFollow) add(WizardSwitch.QRG_FOLLOW) - if (storedCluster && !(networked && clusterServer)) add(WizardSwitch.CLUSTER_SERVER) + /** The log-page switches the profile these answers were built from has on. */ + fun storedLogSwitches(): Set = buildSet { + if (storedWintest) add(WizardSwitch.LOG_WINTEST) + if (storedUdp) add(WizardSwitch.LOG_UDP) + if (storedFile) add(WizardSwitch.LOG_FILE) + if (storedQrgFollow) add(WizardSwitch.QRG_FOLLOW) + if (storedCluster) add(WizardSwitch.CLUSTER_SERVER) } - /** What the equipment page's answers switch off that the profile had on. */ - fun switchedOffByGear(): List = buildList { - if (storedRotator && !rotator) add(WizardSwitch.ROTATOR) - if (storedAirScout && !airScout) add(WizardSwitch.AIRSCOUT) + /** The equipment switches the profile these answers were built from has on. */ + fun storedGearSwitches(): Set = buildSet { + if (storedRotator) add(WizardSwitch.ROTATOR) + if (storedAirScout) add(WizardSwitch.AIRSCOUT) + } + + /** The log-page switches that are on once [writeLogTo] has run. */ + fun answeredLogSwitches(): Set = buildSet { + when (logProgram) { + WizardLogProgram.WINTEST -> add(WizardSwitch.LOG_WINTEST) + WizardLogProgram.UDP -> add(WizardSwitch.LOG_UDP) + WizardLogProgram.FILE -> add(WizardSwitch.LOG_FILE) + WizardLogProgram.NONE -> Unit + } + if (networked && qrgFollow) add(WizardSwitch.QRG_FOLLOW) + if (networked && clusterServer) add(WizardSwitch.CLUSTER_SERVER) + } + + /** The equipment switches that are on once [writeGearTo] has run. */ + fun answeredGearSwitches(): Set = buildSet { + if (rotator) add(WizardSwitch.ROTATOR) + if (airScout) add(WizardSwitch.AIRSCOUT) } private companion object { diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardDonorTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardDonorTest.kt new file mode 100644 index 00000000..247f4c3f --- /dev/null +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardDonorTest.kt @@ -0,0 +1,352 @@ +package kst4contest.view.compose.wizard + +import kst4contest.model.ChatCategory +import kst4contest.model.ChatPreferences +import org.junit.jupiter.api.Assertions.assertArrayEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption + +/** + * Taking the login or the station from another profile: what is offered, what the answers + * become, what the summary says against the own profile, and what reaches the file. + */ +class SetupWizardDonorTest(@param:TempDir private val directory: Path) { + + private fun prefs(configure: ChatPreferences.() -> Unit = {}): ChatPreferences = + ChatPreferences(Path.of("setup-wizard-donor-test-unused.xml")).apply(configure) + + /** Every integration switch stated, so no test hangs on a compiled-in default. */ + private fun ChatPreferences.integrationsOff() { + setLogsynch_wintestNetworkListenerEnabled(false) + setLogsynch_ucxUDPWkdCallListenerEnabled(false) + setLogsynch_fileBasedWkdCallInterpreterEnabled(false) + setTrxSynch_ucxLogUDPListenerEnabled(false) + setLogsynch_wintestQrgSyncEnabled(false) + setNotify_dxClusterServerEnabled(false) + setStn_pstRotatorEnabled(false) + setAirScout_asUDPListenerEnabled(false) + setTrxSynch_so2rEnabled(false) + setStn_pstRotator2Enabled(false) + } + + /** An empty profile, as "new profile" creates it: no login, the template's integrations on. */ + private fun emptyProfile() = prefs { + integrationsOff() + setStn_loginCallSign("") + setStn_loginPassword("") + setStn_loginLocatorMainCat("JO54ME") + setStn_bandActive144(true) + setLoginChatCategoryMain(ChatCategory(2)) + setLoginToSecondChatEnabled(true) + setLoginChatCategorySecond(ChatCategory(3)) + setLogsynch_ucxUDPWkdCallListenerEnabled(true) + setAirScout_asUDPListenerEnabled(true) + } + + private fun contestStation() = WizardDonor("contest", "Conteststation", prefs { + integrationsOff() + setStn_loginCallSign("DL0TEST") + setStn_loginPassword("secret") + setStn_loginLocatorMainCat("JO50AA") + setStn_bandActive144(true) + setStn_bandActive432(true) + setLoginChatCategoryMain(ChatCategory(2)) + setLoginToSecondChatEnabled(false) + setStn_antennaBeamWidthDeg(28.0) + setStn_qtfDefault(270.0) + setLogsynch_wintestNetworkListenerEnabled(true) + setLogsynch_wintestNetworkPort(9871) + setLogsynch_wintestNetworkStationNameOfKST("KST") + setLogsynch_wintestQrgSyncEnabled(true) + setStn_pstRotatorEnabled(true) + setStn_pstRotatorHost("127.0.0.1") + setStn_pstRotatorPort(12000) + }) + + private fun portable() = WizardDonor("portable", "Portabel", prefs { + integrationsOff() + setStn_loginCallSign("DL0TEST/P") + setStn_loginPassword("secret") + setStn_loginLocatorMainCat("JN58QA") + setStn_bandActive1240(true) + setLoginChatCategoryMain(ChatCategory(3)) + setLoginToSecondChatEnabled(false) + }) + + @Test + fun withoutDonorsNothingChanges() { + val state = SetupWizardState(emptyProfile()) + + assertEquals(listOf(WizardSource.NEW), state.loginSources) + assertEquals(listOf(WizardSource.NEW, WizardSource.KEEP), state.stationSources) + assertTrue(state.donors.isEmpty()) + } + + @Test + fun aDonorAddsTakeBetweenNewAndKeep() { + val state = SetupWizardState(emptyProfile(), listOf(contestStation())) + + assertEquals(listOf(WizardSource.NEW, WizardSource.TAKE), state.loginSources) + assertEquals(listOf(WizardSource.NEW, WizardSource.TAKE, WizardSource.KEEP), state.stationSources) + assertTrue(state.hasEntryPage) + assertNull(state.loginSource, "with a choice nothing is preselected") + } + + @Test + fun aSingleDonorIsChosenWithTheSource() { + val donor = contestStation() + val state = SetupWizardState(emptyProfile(), listOf(donor)) + + state.chooseLoginSource(WizardSource.TAKE) + state.chooseStationSource(WizardSource.NEW) + + assertEquals(donor, state.loginDonor, "one donor needs no second question") + assertTrue(state.canGoNext) + } + + @Test + fun severalDonorsNeedAChoice() { + val contest = contestStation() + val state = SetupWizardState(emptyProfile(), listOf(contest, portable())) + + state.chooseLoginSource(WizardSource.TAKE) + state.chooseStationSource(WizardSource.NEW) + assertNull(state.loginDonor) + assertFalse(state.canGoNext, "which profile is not said yet") + + state.chooseLoginDonor(contest) + assertTrue(state.canGoNext) + } + + @Test + fun aDonorThatIsNotOfferedIsNotAccepted() { + val state = SetupWizardState(emptyProfile(), listOf(contestStation())) + state.chooseLoginSource(WizardSource.TAKE) + val first = state.loginDonor + + state.chooseLoginDonor(portable()) + + assertEquals(first, state.loginDonor) + } + + @Test + fun takingTheLoginSkipsItsPageAndLeavesItChangeable() { + val state = SetupWizardState(emptyProfile(), listOf(contestStation())) + state.chooseLoginSource(WizardSource.TAKE) + state.chooseStationSource(WizardSource.KEEP) + state.next() + + assertEquals("DL0TEST", state.callsign) + assertEquals("secret", state.password) + assertFalse(state.pages.contains(WizardPage.LOGIN)) + + state.name = "Philipp" + state.next() + assertEquals(WizardPage.SUMMARY, state.page) + state.edit(WizardPage.LOGIN) + state.callsign = "DL0TEST/P" + state.next() + assertEquals("DL0TEST/P", state.callsign, "a taken callsign gets its /P") + } + + @Test + fun takingTheStationLoadsEverythingThePagesShow() { + val state = SetupWizardState(emptyProfile(), listOf(contestStation())) + state.chooseLoginSource(WizardSource.NEW) + state.chooseStationSource(WizardSource.TAKE) + state.next() + + assertEquals("JO50AA", state.locator) + assertEquals(setOf(WizardBand.B144, WizardBand.B432), state.bands) + assertEquals(2, state.category) + assertFalse(state.secondChat) + assertEquals(28.0, state.antenna.beamWidthDeg) + assertEquals(WizardLogProgram.WINTEST, state.integrations.logProgram) + assertTrue(state.integrations.rotator) + assertTrue(state.summaryQrgFollow) + assertEquals( + listOf(WizardPage.ENTRY, WizardPage.PERSONAL, WizardPage.LOGIN, WizardPage.SUMMARY), + state.pages, + "a taken station shows no page of its own", + ) + } + + @Test + fun takingAStationNamesWhatTheOwnProfileLoses() { + val state = SetupWizardState(emptyProfile(), listOf(contestStation())) + state.chooseLoginSource(WizardSource.NEW) + state.chooseStationSource(WizardSource.TAKE) + state.next() + + // The own profile had the second chat, the network listener and AirScout on; the + // donor has none of them. Win-Test and the rotator come new and are not "switched off". + assertEquals( + listOf(WizardSwitch.SECOND_CHAT, WizardSwitch.LOG_UDP, WizardSwitch.AIRSCOUT), + state.switchedOff, + ) + } + + @Test + fun aTakenStationKeepsSeveralLogSources() { + val several = WizardDonor("several", "Zwei Quellen", contestStation().prefs.apply { + setLogsynch_fileBasedWkdCallInterpreterEnabled(true) + }) + val state = SetupWizardState(emptyProfile(), listOf(several)) + state.chooseLoginSource(WizardSource.NEW) + state.chooseStationSource(WizardSource.TAKE) + state.next() + + assertFalse(state.logPageEditable, "the page knows one source and would switch the other off") + state.edit(WizardPage.LOG) + assertFalse(state.pages.contains(WizardPage.LOG)) + } + + @Test + fun changingTheDonorLoadsItsValues() { + val contest = contestStation() + val other = portable() + val state = SetupWizardState(emptyProfile(), listOf(contest, other)) + state.chooseLoginSource(WizardSource.NEW) + state.chooseStationSource(WizardSource.TAKE) + state.chooseStationDonor(contest) + state.next() + assertEquals("JO50AA", state.locator) + + state.back() + state.chooseStationDonor(other) + state.next() + + assertEquals("JN58QA", state.locator) + assertEquals(setOf(WizardBand.B1240), state.bands) + assertEquals(3, state.category) + assertEquals(WizardLogProgram.NONE, state.integrations.logProgram, "nothing of the first donor is left") + assertFalse(state.integrations.rotator) + } + + @Test + fun goingBackToNewDropsWhatWasTaken() { + val state = SetupWizardState(emptyProfile(), listOf(contestStation())) + state.chooseLoginSource(WizardSource.NEW) + state.chooseStationSource(WizardSource.TAKE) + state.next() + state.back() + state.chooseStationSource(WizardSource.NEW) + state.next() + + assertEquals("", state.locator) + assertTrue(state.bands.isEmpty()) + assertEquals(WizardLogProgram.NONE, state.integrations.logProgram) + assertFalse(state.integrations.rotator) + assertTrue(state.pages.contains(WizardPage.LOG)) + } + + // --- against real files ------------------------------------------------------------- + + private fun file(name: String, configure: ChatPreferences.() -> Unit): Path { + val path = directory.resolve(name) + ChatPreferences::class.java.getResourceAsStream(ChatPreferences.PREFERENCE_RESOURCE).use { template -> + Files.copy(requireNotNull(template), path, StandardCopyOption.REPLACE_EXISTING) + } + ChatPreferences(path).apply { + assertTrue(readPreferencesFromXmlFile()) + configure() + assertTrue(writePreferencesToXmlFile()) + } + return path + } + + private fun read(path: Path) = ChatPreferences(path).also { assertTrue(it.readPreferencesFromXmlFile()) } + + @Test + fun saveWritesTheWholeTakenStationAndTheTakenLoginIntoTheOwnFile() { + val donorFile = file("donor.xml") { + setStn_loginCallSign("DL0TEST") + setStn_loginPassword("secret") + setStn_loginLocatorMainCat("JO50AA") + setStn_bandActive144(true) + setLoginChatCategoryMain(ChatCategory(2)) + setLoginToSecondChatEnabled(false) + setStn_qtfDefault(270.0) + setTrxSynch_so2rEnabled(true) + setStn_pstRotator2Enabled(true) + setStn_pstRotator2Port(12020) + setLogsynch_wintestNetworkListenerEnabled(true) + setLogsynch_wintestNetworkStationNameOfWintestClient1("RUN") + } + val ownFile = file("own.xml") { + setStn_loginCallSign("") + setStn_loginPassword("") + setStn_on4kstServersDns("own.example") + } + val donorBytes = Files.readAllBytes(donorFile) + val own = read(ownFile) + val state = SetupWizardState(own, listOf(WizardDonor("contest", "Conteststation", read(donorFile)))) + state.chooseLoginSource(WizardSource.TAKE) + state.chooseStationSource(WizardSource.TAKE) + state.next() + state.name = "Philipp" + state.next() + assertEquals(WizardPage.SUMMARY, state.page) + + assertTrue(state.writeTo(own)) + + val restored = read(ownFile) + assertEquals("DL0TEST", restored.getStn_loginCallSign()) + assertEquals("secret", restored.getStn_loginPassword()) + assertEquals("Philipp", restored.getStn_loginNameMainCat()) + assertEquals("JO50AA", restored.getStn_loginLocatorMainCat()) + assertEquals(270.0, restored.getStn_qtfDefault(), "a station value the wizard never asks about") + assertTrue(restored.isTrxSynch_so2rEnabled()) + assertTrue(restored.isStn_pstRotator2Enabled()) + assertEquals(12020, restored.getStn_pstRotator2Port()) + assertTrue(restored.isLogsynch_wintestNetworkListenerEnabled()) + assertEquals("RUN", restored.getLogsynch_wintestNetworkStationNameOfWintestClient1()) + assertEquals("own.example", restored.getStn_on4kstServersDns(), "not part of the station") + assertArrayEquals(donorBytes, Files.readAllBytes(donorFile), "the donor's file was touched") + } + + @Test + fun aPageOpenedAfterTakingOverridesTheCopy() { + val donorFile = file("donor.xml") { + setStn_loginCallSign("DL0TEST") + setStn_loginPassword("secret") + setStn_loginLocatorMainCat("JO50AA") + setStn_bandActive144(true) + setLoginChatCategoryMain(ChatCategory(2)) + setLoginToSecondChatEnabled(false) + setStn_pstRotatorEnabled(true) + setStn_pstRotatorHost("127.0.0.1") + setStn_pstRotatorPort(12000) + } + val ownFile = file("own.xml") { + setStn_loginCallSign("") + setStn_loginPassword("") + } + val own = read(ownFile) + val state = SetupWizardState(own, listOf(WizardDonor("contest", "Conteststation", read(donorFile)))) + state.chooseLoginSource(WizardSource.TAKE) + state.chooseStationSource(WizardSource.TAKE) + state.next() + state.name = "Philipp" + state.next() + state.edit(WizardPage.GEAR) + state.integrations.rotator = false + state.next() + state.edit(WizardPage.SITE) + state.locator = "JN58QA" + state.next() + + assertTrue(state.writeTo(own)) + + val restored = read(ownFile) + assertFalse(restored.isStn_pstRotatorEnabled(), "the operator switched the taken rotator off") + assertEquals("JN58QA", restored.getStn_loginLocatorMainCat(), "the operator changed the taken locator") + } +} diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt index 760d0526..41cf7b33 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt @@ -252,30 +252,40 @@ class WizardIntegrationsTest { } @Test - fun theSwitchedOffListNamesWhatWasOnAndWillBeOff() { - val answers = WizardIntegrations(likeTheTemplate()) - answers.loadFresh() + fun theStoredSwitchesAreWhatTheProfileHasOn() { + val template = WizardIntegrations(likeTheTemplate()) assertEquals( - listOf(WizardSwitch.LOG_UDP, WizardSwitch.LOG_FILE, WizardSwitch.QRG_FOLLOW, WizardSwitch.CLUSTER_SERVER), - answers.switchedOffByLog(), + setOf(WizardSwitch.LOG_UDP, WizardSwitch.LOG_FILE, WizardSwitch.QRG_FOLLOW, WizardSwitch.CLUSTER_SERVER), + template.storedLogSwitches(), ) - assertEquals(listOf(WizardSwitch.AIRSCOUT), answers.switchedOffByGear()) + assertEquals(setOf(WizardSwitch.AIRSCOUT), template.storedGearSwitches()) - answers.chooseLogProgram(WizardLogProgram.UDP) - answers.qrgFollow = true - answers.airScout = true - assertEquals(listOf(WizardSwitch.LOG_FILE, WizardSwitch.CLUSTER_SERVER), answers.switchedOffByLog()) - assertEquals(emptyList(), answers.switchedOffByGear()) + val nothing = WizardIntegrations(allOff()) + assertEquals(emptySet(), nothing.storedLogSwitches()) + assertEquals(emptySet(), nothing.storedGearSwitches()) } @Test - fun nothingIsSwitchedOffOnAStationThatHadNothingOn() { - val answers = WizardIntegrations(allOff()) + fun theAnsweredSwitchesFollowThePages() { + val answers = WizardIntegrations(likeTheTemplate()) answers.loadFresh() - assertEquals(emptyList(), answers.switchedOffByLog()) - assertEquals(emptyList(), answers.switchedOffByGear()) + assertEquals(emptySet(), answers.answeredLogSwitches()) + assertEquals(emptySet(), answers.answeredGearSwitches()) + + answers.chooseLogProgram(WizardLogProgram.WINTEST) + answers.qrgFollow = true + answers.clusterServer = true + answers.rotator = true + + assertEquals( + setOf(WizardSwitch.LOG_WINTEST, WizardSwitch.QRG_FOLLOW, WizardSwitch.CLUSTER_SERVER), + answers.answeredLogSwitches(), + ) + assertEquals(setOf(WizardSwitch.ROTATOR), answers.answeredGearSwitches()) + // The stored side does not move with the answers. + assertEquals(setOf(WizardSwitch.AIRSCOUT), answers.storedGearSwitches()) } @Test