File size: 6,554 Bytes
32bd232
 
 
 
 
 
 
 
 
 
 
 
 
e8e2c75
 
32bd232
 
 
e8e2c75
 
 
 
32bd232
 
 
 
8127776
 
 
 
 
32bd232
8127776
24c3f5a
 
32bd232
 
8127776
7b59407
 
 
 
 
 
8127776
 
 
 
 
 
 
 
 
 
 
11ad76e
8127776
 
 
 
 
 
 
11ad76e
24c3f5a
 
 
8127776
 
 
 
 
 
24c3f5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32bd232
24c3f5a
d818021
 
 
 
 
 
 
 
45fb1c1
 
d818021
 
45fb1c1
 
 
d818021
 
 
 
 
 
45fb1c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Comprehension quiz Back button + the 'YOU round' wording fix."""
import os
os.environ["RELOAD"] = "0"
os.environ["STORAGE_SECRET"] = "test"
os.environ["PORT"] = "8091"

import pytest
import web_app
from nicegui.testing import User


def test_you_round_wording_is_fixed():
    """Unit test checking that the wording fix is in place (no app context needed)."""
    questionnaires_path = os.path.join(os.path.dirname(__file__), "questionnaires.py")
    with open(questionnaires_path, "r") as f:
        content = f.read()
    # Verify "YOU round" exists in comp_who_self prompt
    assert 'prompt="On a YOU round, who gets the credits you earn?"' in content
    # Verify the "YOUR round" bug is genuinely absent (two separate assertions,
    # not one or-joined pseudo-assertion satisfiable by either half)
    assert "YOUR round" not in content
    assert 'prompt="On a YOUR round' not in content


@pytest.mark.module_under_test(web_app)
async def test_comprehension_back_returns_to_previous_question(user: User) -> None:
    """Integration test: drive UI through Back button on question 2.

    Answers Q1 (Submit → Continue), verifies Back button appears on Q2,
    clicks Back, and confirms view returns to Q1. Tests that the actual
    Back button wiring in _ask_mc and _comp_round's loop logic work end-to-end."""
    import asyncio
    await user.open("/?dev=1&start=quiz&role=c&theme=harvest&pid=SMOKE_quiz_back2")

    # Step 1: Start quiz
    user.find("Start quiz").click()
    await asyncio.sleep(0.1)

    # answer question 1 (click the correct option to select it)
    # Taken from the item itself rather than hard-coded: the option text embeds
    # the rest credit, so a change to REST_CREDIT used to break this test.
    import questionnaires as _Q
    _item = _Q.comp_items()[0]
    user.find(_item["options"][_item["correct"]][0]).click()
    await asyncio.sleep(0.1)

    # Submit the answer
    user.find("Submit").click()
    await asyncio.sleep(0.1)

    # Click Continue to move to question 2
    user.find("Continue").click()
    await asyncio.sleep(0.1)

    # Question 2 should now be shown with a Back button (n > 0)
    await user.should_see("Quiz (2/")
    await user.should_see("Back")

    # Click the Back button to return to question 1
    user.find("Back").click()
    await asyncio.sleep(0.1)

    # Verify we're back on question 1 of N (the critical assertion: proves Back worked)
    await user.should_see("Quiz (1/")


async def test_comp_round_back_navigation():
    """Unit test: verify _comp_round correctly handles Back button flow.

    Mocks _ask_mc to simulate user selecting option on Q1, going back from Q2,
    re-answering Q1, and completing Q2. Tests the while-loop rewind logic
    (decrement n, pop answer, continue) inside _comp_round."""
    from unittest.mock import MagicMock, patch

    # Mock items for the quiz (simplified versions of comp_items)
    items = [
        {
            "key": "q1", "qid": "Q1",
            "prompt": "Question 1?",
            "options": [("Option A", "A"), ("Option B", "B")],
            "correct": 0,
            "explain": "Correct!"
        },
        {
            "key": "q2", "qid": "Q2",
            "prompt": "Question 2?",
            "options": [("Option C", "C"), ("Option D", "D")],
            "correct": 1,
            "explain": "That's right!"
        },
    ]

    # Import the functions to test
    from questionnaires import _comp_round, _ask_mc

    # Mock container and _ask_mc to simulate user interactions
    mock_container = MagicMock()

    # Simulate: Answer Q1 correctly, then go back, then answer both again
    call_sequence = [
        (0, False),  # Q1: select option 0, don't go back
        (1, True),   # Q2: select option 1, then go back
        (0, False),  # Q1 again: select option 0, don't go back
        (1, False),  # Q2: select option 1, don't go back
    ]
    call_iter = iter(call_sequence)

    async def mock_ask_mc(container, header, item, can_back=False):
        """Mock _ask_mc that simulates the user interaction sequence."""
        choice, went_back = next(call_iter)
        return choice, went_back

    # Replace _ask_mc with our mock
    with patch('questionnaires._ask_mc', side_effect=mock_ask_mc):
        result = await _comp_round(mock_container, 1, items)

    # Verify results: should have 2 questions answered correctly
    # The sequence was: Q1(0), Q2(1, back), Q1(0), Q2(1) = 2 items in final result
    assert len(result) == 2, f"Expected 2 results, got {len(result)}"
    assert result[0]["key"] == "q1"
    assert result[0]["choice"] == 0
    assert result[1]["key"] == "q2"
    assert result[1]["choice"] == 1

    print("Back navigation test passed: _comp_round correctly handled going back on Q2")


@pytest.mark.module_under_test(web_app)
async def test_back_from_nasa_tlx_returns_to_fatigue_post(user: User) -> None:
    await user.open("/?dev=1&start=end&role=c&theme=harvest&pid=SMOKE_back_chain")
    import asyncio
    # ?dev=1&start=end lands on Fatigue_post first (no Back there by design)
    await user.should_see("How tired do you feel right now?")
    # the slider hasn't been touched, so Continue now triggers the
    # Qualtrics-style "are you sure" confirmation dialog (ported from sl-cu)
    user.find("Continue").click()
    await asyncio.sleep(0.1)
    await user.should_see("Continue anyway")
    user.find("Continue anyway").click()
    await asyncio.sleep(0.1)
    # now on NASA_TLX -- it should have a Back button
    await user.should_see("Back")
    user.find("Back").click()
    await asyncio.sleep(0.1)
    # should be back on Fatigue_post
    await user.should_see("How tired do you feel right now?")


@pytest.mark.module_under_test(web_app)
async def test_slider_battery_skips_confirm_dialog_once_touched(user: User) -> None:
    """If every slider has been touched, Continue proceeds immediately -- no dialog."""
    from nicegui import ui
    await user.open("/?dev=1&start=end&role=c&theme=harvest&pid=SMOKE_touched_slider")
    import asyncio
    await user.should_see("How tired do you feel right now?")
    slider = next(iter(user.find(kind=ui.slider).elements))
    with user.client:
        slider.value = 70   # triggers on_value_change -> touched[key] = True, same as a real drag
    await asyncio.sleep(0.05)
    user.find("Continue").click()
    await asyncio.sleep(0.1)
    # no confirmation dialog -- straight through to NASA_TLX
    await user.should_see("Back")
    assert not user._gather_elements("Continue anyway", None, None, None)