from pathlib import Path

from src.frontend_bundle import FEATURE_SOURCES, build_frontend


ROOT = Path(__file__).parents[1]


def test_cross_device_unfiled_draft_sync_is_shipped_and_wired_to_user_flow():
    html = (ROOT / "frontend" / "index.html").read_text()
    dashboard = (ROOT / "frontend" / "dashboard.js").read_text()

    assert '<script src="static/unfiled-draft-sync.js"></script>' in html
    assert "static/unfiled-draft-sync.js" in FEATURE_SOURCES["today-timer"]
    assert "createUnfiledDraftSync(" in dashboard
    assert "unfiledCaptures, fetchReviewJson" in dashboard
    assert "Saved on this device · sync pending" in (ROOT / "frontend" / "unfiled-draft-sync.js").read_text()

    built = build_frontend(ROOT / "frontend")
    assert "unfiled-draft-sync.js" not in built.dashboard_html
    assert b"stackchain.unfiled-draft-sync.v1" in built.feature_bundles["today-timer"].runtime_bytes
