/*
 * Copyright (C) 2026 SHX VPN
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package org.strongswan.android.logic.autoconnect

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.strongswan.android.logic.autoconnect.AutoConnectManager.Action
import org.strongswan.android.logic.autoconnect.AutoConnectManager.Config
import org.strongswan.android.logic.autoconnect.AutoConnectManager.NetworkKind

class AutoConnectManagerTest
{
	private val config = Config(
		enabled = true,
		profileUuid = "uuid-1",
		trustedSsids = setOf("Home", "Office"),
	)

	@Test
	fun cellularConnectsWhenVpnDown()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.CELLULAR, vpnActive = false, autoSession = false))
			.isEqualTo(Action.CONNECT)
	}

	@Test
	fun untrustedWifiConnectsWhenVpnDown()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.UNTRUSTED_WIFI, vpnActive = false, autoSession = false))
			.isEqualTo(Action.CONNECT)
	}

	@Test
	fun trustedWifiDisconnectsAutoSession()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.TRUSTED_WIFI, vpnActive = true, autoSession = true))
			.isEqualTo(Action.DISCONNECT)
	}

	@Test
	fun trustedWifiDisconnectsManualSession()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.TRUSTED_WIFI, vpnActive = true, autoSession = false))
			.isEqualTo(Action.DISCONNECT)
	}

	@Test
	fun unknownWifiKeepsExistingTunnel()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.UNKNOWN_WIFI, vpnActive = true, autoSession = true))
			.isEqualTo(Action.IGNORE)
	}

	@Test
	fun unknownWifiConnectsWhenVpnDown()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.UNKNOWN_WIFI, vpnActive = false, autoSession = false))
			.isEqualTo(Action.CONNECT)
	}

	@Test
	fun ignoresWhenVpnAlreadyActive()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, NetworkKind.CELLULAR, vpnActive = true, autoSession = false))
			.isEqualTo(Action.IGNORE)
	}

	@Test
	fun ignoresWhenDisabled()
	{
		val disabled = config.copy(enabled = false)
		assertThat(AutoConnectManager.evaluateDecision(disabled, NetworkKind.CELLULAR, vpnActive = false, autoSession = false))
			.isEqualTo(Action.IGNORE)
	}

	@Test
	fun ignoresWhenNoProfileSelected()
	{
		val noProfile = config.copy(profileUuid = null)
		assertThat(AutoConnectManager.evaluateDecision(noProfile, NetworkKind.CELLULAR, vpnActive = false, autoSession = false))
			.isEqualTo(Action.IGNORE)
	}

	@Test
	fun ignoresWhenNetworkUnknown()
	{
		assertThat(AutoConnectManager.evaluateDecision(config, null, vpnActive = false, autoSession = false))
			.isEqualTo(Action.IGNORE)
	}
}
