from pathlib import Path


ROOT = Path(__file__).parents[1]
DASHBOARD = ROOT / "frontend" / "dashboard.js"
INDEX = ROOT / "frontend" / "index.html"
TIMER = ROOT / "frontend" / "today-timer.js"


def test_quick_capture_wires_timer_interruption_to_all_exit_paths():
    source = DASHBOARD.read_text()
    timer_source = TIMER.read_text()
    html = INDEX.read_text()

    assert 'id="today-capture-interruption"' in html
    assert 'id="today-capture-interruption-label"' in html
    assert "view.beginCapture();" in timer_source
    assert "timerView.transferCapture()" in source
    assert "timerView.finishCapture()" in source
    assert "result ? 'Save & return to Today' : 'Save to Drafts'" in timer_source
    assert "if (!capture)" in source


def test_capture_save_returns_only_after_durable_admission():
    source = DASHBOARD.read_text()

    saved = source.index("const savedCapture = await unfiledCaptures.save(captureDraft);")
    returned = source.index("timerView.finishCapture()", saved)
    failure = source.index("} catch (error) {", saved)
    assert saved < returned < failure
