#!/usr/bin/python3
#
# Copyright (C) 2026 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import time

from anacondalib import INSTALLER_VM_MEMORY_MB, VirtInstallMachineCase, run_on_vm_setups
from installer import Installer
from network import COCKPIT_CHECKPOINT_ROLLBACK_TIME, SYSROOT_PATH, Network
from progress import Progress
from testlib import nondestructive, test_main  # pylint: disable=import-error
from utils import check_cockpit_module_js_error


class TestNetworkDestructive(VirtInstallMachineCase):
    provision = {"machine1": {"memory_mb": INSTALLER_VM_MEMORY_MB}}

    def testCockpitConnectionsPreInstall(self):
        """
        Description:
            Test network configuration modifies profiles correctly.
            A pre-installation phase variant - a shorter test assuming the connections
            will be passed to the system correctly at the end of installation.

        Expected results:
            - Expected connection profiles in installer with expected values
            - Network configuration modifies correct profiles
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        n = Network(b, m)

        i.open()

        iface = n.get_the_iface()
        n.preinstall_connection_test(i, iface)

    @run_on_vm_setups("", "bootopts-net1")
    def testConnections(self):
        """
        Description:
            Test network configuration modifies profiles correctly.

        Expected results:
            - Expected connection profiles in installer with expected values
            - Network configuration modifies correct profiles
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        n = Network(b, m)

        i.open()

        iface = n.get_the_iface()

        if self.vm_setup == "bootopts-net1":
            configured = True
        else:
            configured = False
        n.preinstall_connection_test(i, iface, configured=configured)

        i.begin_installation(needs_confirmation=False)
        p = Progress(b)
        p.wait_done()

        con_name = iface
        # Check profiles in installed system
        n.check_con_profile_files(con_name, 1, root=SYSROOT_PATH, file_name=con_name)
        # Check there is only single persistent profile
        n.check_con_profile_files("", 1, root=SYSROOT_PATH)

    # FIXME add "" vm_setup back when
    # https://github.com/cockpit-project/cockpit/issues/22926 is fixed
    #@run_on_vm_setups("", "bootopts-net1")
    @run_on_vm_setups("bootopts-net1")
    def testWizardCockpitConfigurationPreInstall(self):
        """
        Description:
            Test configuration of settings of a device from a wizard page.

        Expected results:
            - Change breaking connection raises expected confirmation dialog
            - The configured values are correctly applied to connection profile and device.
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        n = Network(b, m)

        i.open()

        i.reach(i.steps.NETWORK)

        iface = n.get_the_iface()

        n.disable_ipv4_on_iface_wizard(iface)

        i.next()
        i.back()

        # After editing, therefore persisting, the automatic default connection
        # is updated by Cockpit (COCKPIT-1750)
        con_name = iface

        mtu_value = "1600"
        n.set_mtu_on_iface_wizard(iface, mtu_value)

        n.check_con_settings([
            [con_name, "802-3-ethernet.mtu", mtu_value, None],
        ])

        i.next()
        i.back()

        dns_server = "8.8.8.8"
        n.add_dns_server_to_iface_wizard(iface, dns_server)

        n.check_con_settings([
            [con_name, "ipv4.dns", dns_server, None],
        ])

        # Check that the checkpoint does not fail silently, ie check after
        # checkpoint timeout (INSTALLER-4594)
        time.sleep(COCKPIT_CHECKPOINT_ROLLBACK_TIME+2)

        n.check_con_settings([
            [con_name, "802-3-ethernet.mtu", mtu_value, None],
        ])

        n.check_con_settings([
            [con_name, "ipv4.dns", dns_server, None],
        ])


@nondestructive
class TestNetwork(VirtInstallMachineCase):
    def testCockpitJsErrorHandling(self):
        """
        Description:
            Test that the installer can handle JS errors in the network module
            and that it does not crash the installer

        Setup:
            - Generate a JS error in the network module

        Expected results:
            - The installer should not crash
            - The installer should show a critical error dialog
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        n = Network(b, m)

        i.open()
        i.reach(i.steps.NETWORK)
        n.enter_network()

        check_cockpit_module_js_error(b, "Network")


if __name__ == '__main__':
    test_main()
