#!/usr/bin/python3
#
# Copyright (C) 2022 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.

import os
import sys

# import Cockpit's machinery for test VMs and its browser test API
TEST_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(TEST_DIR, "helpers"))
sys.path.append(os.path.join(TEST_DIR, "common"))
sys.path.append(os.path.join(os.path.dirname(TEST_DIR), "bots/machine"))

from installer import Installer
from testlib import MachineCase, test_main  # pylint: disable=import-error
from machine_install import VirtInstallMachine

class TestInstallationProgress(MachineCase):
    MachineCase.machine_class = VirtInstallMachine

    def testBasic(self):
        b = self.browser
        i = Installer(b, self.machine)

        i.open()

        for page in i.steps[:-1]:
            # with the pages basically empty of common elements (as those are provided by the top-level installer widget)
            # we at least iterate over them to check this works as expected
            i.wait_current_page(page)
            if page != i.steps.REVIEW:
                i.next()
            else:
                i.begin_installation()

        # the warning can take longer to show up than the regular wait_in_text() timeout
        with b.wait_timeout(300):
            b.wait_in_text(".pf-c-alert.pf-m-danger", "No such file or directory: 'systemd-machine-id-setup'")

        b.wait_in_text("#installation-progress-step-storage", "Storage configuration")

        b.wait_visible("#installation-progress-step-0[aria-label='completed step'] > div:first-child")
        b.wait_visible("#installation-progress-step-1[aria-label='completed step'] > div:first-child")
        b.wait_visible("#installation-progress-step-2[aria-label='current step'] > div:first-child")
        b.wait_visible("#installation-progress-step-3[aria-label='pending step'] > div:first-child")

        # Pixel test the failed progress step
        b.assert_pixels("#app", "installation-progress-step-fail", ignore=["#betanag-icon"])

if __name__ == '__main__':
    test_main()
