Spaces:
Running
Running
Download test_back_navigation.py from princessamygdala/HarvestForTwo: direct link, hf CLI and curl.
- Browser
- Download file 6.55 kB
-
https://huggingface.co/spaces/princessamygdala/HarvestForTwo/resolve/main/test_back_navigation.py
- Command line
-
hf download hf://spaces/princessamygdala/HarvestForTwo/test_back_navigation.py
-
curl -L -o test_back_navigation.py https://huggingface.co/spaces/princessamygdala/HarvestForTwo/resolve/main/test_back_navigation.py
6.55 kB
| """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 | |
| 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") | |
| 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?") | |
| 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) | |