#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)

# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit 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.
#
# Cockpit 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 Cockpit; If not, see <http://www.gnu.org/licenses/>.

import time

import testlib


@testlib.nondestructive
class TestMenu(testlib.MachineCase):

    def testDarkThemeSwitcher(self):
        b = self.browser

        def switch_style(style_class, prevPage="system", pixel_test=False):
            b.switch_to_top()
            b.click("#toggle-menu")
            if pixel_test:
                b.wait_visible("#toggle-menu-menu")
                b.assert_pixels_in_current_layout("#toggle-menu-menu", "shell-toggle-menu")
            b.click(f"#topnav {style_class}")
            b.enter_page(f"/{prevPage}")

        self.login_and_go("/system")
        switch_style("#dark", pixel_test=True)
        b._wait_present("html.pf-v5-theme-dark")

        switch_style("#light")
        b.wait_not_present("html.pf-v5-theme-dark")

        # Test overriding, switching only works on Chromium
        if b.cdp.browser.name == "chromium":
            # Light theme overrides browser defaults
            b._set_emulated_media_theme("dark")
            b.wait_not_present("html.pf-v5-theme-dark")

            switch_style("#auto")
            b._wait_present("html.pf-v5-theme-dark")

            b._set_emulated_media_theme("light")
            b.wait_not_present("html.pf-v5-theme-dark")

            switch_style("#dark")
            b._wait_present("html.pf-v5-theme-dark")

    def testBasic(self):
        b = self.browser
        m = self.machine

        # Add a link with a hash in it to test that this works
        m.execute("mkdir -p /usr/local/share/cockpit/systemd; cp -rp /usr/share/cockpit/systemd/* /usr/local/share/cockpit/systemd")
        m.execute(
            """sed -i '/"menu"/a "memory": { "label": "Memory", "path": "#/memory" },' /usr/local/share/cockpit/systemd/manifest.json""")
        self.addCleanup(m.execute, "rm -r /usr/local/share/cockpit")

        self.login_and_go("/system")

        b.switch_to_top()
        b.click('#toggle-docs')
        b.click('button:contains("About Web Console")')
        b.wait_visible('#about-cockpit-modal:contains("Cockpit is an interactive Linux server admin interface")')
        if not m.ostree_image:
            pkgname = "cockpit" if m.image == "arch" else "cockpit-bridge"
            b.wait_visible(f'#about-cockpit-modal:contains("{pkgname}")')
        b.click('.pf-v5-c-about-modal-box__close button')
        b.wait_not_present('#about-cockpit-modal')

        # Clicking inside the iframed pages should close the docs menu
        b.click("#toggle-docs")
        b.wait_visible("#toggle-docs-menu")
        b.assert_pixels("#toggle-docs-menu", "shell-docs-menu")
        b.enter_page("/system")
        b.focus("#overview main")
        b.switch_to_top()
        b.wait_not_present("#toggle-docs-menu")

        # Check that we can use a link with a hash in it
        b.click_system_menu("/system/#/memory")

        # Ensure that our tests pick up unhandled JS exceptions
        b.switch_to_top()
        b.go("/playground/exception")

        # Test that subpages are correctly shown in the navigation (twice - once that only one page is shown as active)
        b.wait_in_text("#host-apps .pf-m-current", "Development")

        b.enter_page("/playground/exception")
        b.wait_visible("button")
        with self.assertRaisesRegex(RuntimeError, "TypeError:.*undefined"):
            b.click("button")
            # Some round trips, one of which should update the deferred exception
            for _ in range(5):
                b.wait_visible("button")
                time.sleep(2)

        # UI should also show the crash
        b.switch_to_top()
        b.wait_visible("#navbar-oops")

        # normally called at the end of the test, should fail due to the oops
        with self.assertRaisesRegex(AssertionError, "Cockpit shows an Oops"):
            self.check_browser_errors()

        # don't actually fail this test
        b.allow_oops = True

    def testSessionTimeout(self):
        b = self.browser
        m = self.machine

        m.execute("printf '[Session]\nIdleTimeout = 1\n' >> /etc/cockpit/cockpit.conf")

        # does not time out immediately
        self.login_and_go()
        time.sleep(20)
        self.assertFalse(b.is_present("#session-timeout-modal"))
        b.wait_visible("#hosts-sel")

        # a mouse event resets the timer
        b.enter_page("/system")
        b.mouse("#system_information_hardware_text", "mousemove", 24, 24)
        b.switch_to_top()

        # 30s before the 1 min timeout the dialog pops up
        time.sleep(35)
        with b.wait_timeout(3):
            b.wait_visible("#session-timeout-modal")
            self.assertGreater(int(b.text("#session-timeout-modal .pf-v5-c-modal-box__body").split()[-2]), 15)
        # click on "Continue session"
        b.click("#session-timeout-modal footer button")
        b.wait_not_present("#session-timeout-modal")

        # now wait for timeout dialog again, but don't click; instead, wait for the full minute
        time.sleep(30)
        with b.wait_timeout(8):
            b.wait_popup("session-timeout-modal")
            self.assertGreater(int(b.text("#session-timeout-modal .pf-v5-c-modal-box__body").split()[-2]), 20)

        time.sleep(30)
        # that logs you out
        b.wait_visible("#login")
        b.wait_visible("#login-info-message")
        b.wait_text("#login-info-message", "You have been logged out due to inactivity.")

    def testKeyboardNavigation(self):
        b = self.browser
        self.login_and_go()

        # initially shows host switcher
        b.wait_visible("#host-toggle")
        self.assertEqual(b.eval_js("document.activeElement.tagName"), "BODY")
        # conceptually we want that, but we use CSS hacks (-999 offset and 1x1 size) to hide it
        # b.wait_not_visible(".skiplink")

        # press Tab once → "Skip to content" skiplink
        b.key("Tab")
        b.wait_js_cond("document.activeElement.classList.contains('skiplink')")
        self.assertEqual(b.eval_js("document.activeElement.textContent"), "Skip to content")
        self.assertEqual(b.eval_js("document.activeElement.getAttribute('href')"), "#content")
        b.key("Enter")
        b.wait_js_cond("document.activeElement.getAttribute('id') === 'content'")
        self.assertEqual(b.eval_js("document.activeElement.tagName"), "DIV")
        self.assertEqual(b.eval_js("document.activeElement.getAttribute('id')"), "content")

        # reset
        b.reload()
        b.wait_visible("#host-toggle")

        # press Tab twice → "Skip main navigation" skiplink
        b.key("Tab")
        self.assertEqual(b.eval_js("document.activeElement.textContent"), "Skip to content")
        b.key("Tab")
        self.assertEqual(b.eval_js("document.activeElement.textContent"), "Skip main navigation")
        b.key("Enter")
        b.wait_js_cond("document.activeElement.tagName === 'NAV'")
        self.assertEqual(b.eval_js("document.activeElement.getAttribute('id')"), "hosts-sel")

        # actually skips the page menu and goes on to top nav bar
        b.key("Tab")
        b.wait_js_cond("document.activeElement.getAttribute('id') === 'host-toggle'")
        b.key("Tab")
        if self.machine.ostree_image:
            b.wait_js_cond("document.activeElement.textContent === 'Help'")
        else:
            b.wait_js_cond("document.activeElement.textContent === 'Administrative access'")

        # reset
        b.reload()
        b.wait_visible("#host-toggle")

        # without skip links, tabbing goes through page menu
        b.key("Tab", repeat=5)
        # different browsers behave a bit different here -- Firefox is at "Overview", Chromium at "Logs"
        b.wait_js_cond("document.activeElement.classList.contains('pf-v5-c-nav__link')")


if __name__ == '__main__':
    testlib.test_main()
