# test initial page load and static page elements
# Derek Fujimoto
# Mar 2026

from fixtures import *
import time

# check the page title
def test_page_title(page, users_added):

    @try_again
    def check():
        assert page.title() == 'Shift Schedule', \
            f"Expected page title to be 'Shift Schedule', got '{page.title()}'"
    check()

# check the main heading is present and visible
def test_header_present(page, users_added):

    @try_again
    def check():

        heading = page.locator('h1')
        assert heading.is_visible(), \
            f"Expected the <h1> heading to be visible on page load, but it was not displayed"
        assert heading.inner_text() == 'Shift Assignments', \
            f"Expected heading text to be 'Shift Assignments', got '{heading.inner_text()}'"
    check()

# check the schedule table body exists and contains rows
def test_schedule_table_present(page, users_added):

    @try_again
    def check():

        tbody = by_id(page, 'schedule_body')
        assert tbody.is_visible(), \
            f"Expected the schedule table body to be visible on page load, but it was not displayed"

        nrows = tbody.locator('tr').count()
        assert nrows > 0, \
            f"Expected at least one row in the schedule table body, got {nrows}"
    check()

# check the avail table body exists and contains rows
def test_avail_table_present(page, users_added):

    @try_again
    def check():

        tbody = by_id(page, 'avail_body')
        assert tbody.is_visible(), \
            f"Expected the avail table body to be visible on page load, but it was not displayed"

        nrows = tbody.locator('tr').count()
        assert nrows > 0, \
            f"Expected at least one row in the avail table body, got {nrows}"
    check()

# check that all toolbar buttons are present in the DOM
def test_toolbar_buttons_present(page, users_added):


    button_ids = [
        'addshifter_button',
        'editshifter_button',
        'download_button',
        'calendar_button',
        'settings_button',
        'undo_button',
        'redo_button',
    ]

    @try_again
    def check():

        for button_id in button_ids:
            button = by_id(page, button_id)
            assert button.count() > 0, \
                f"Expected toolbar button with id='{button_id}' to be present in the DOM, but it was not found"
            assert button.is_visible(), \
                f"Expected toolbar button '{button_id}' to be visible on page load, but it was not displayed"
    check()

# check that the "On shift now" label and its ODB-bound span are present
def test_on_shift_now_label_present(page, users_added):

    @try_again
    def check():
        # find the label containing the text
        label = page.locator('xpath=//label[contains(., "On shift now:")]').first
        assert label.is_visible(), \
            f"Expected the 'On shift now:' label to be visible on page load, but it was not displayed"

        # find the ODB-bound span inside it
        span = label.locator('.modbvalue')
        assert span.count() > 0, \
            f"Expected an ODB-bound span (class 'modbvalue') inside the 'On shift now:' label, but it was not found"

    check()
