import pytest from emails import pop_account, has_accounts, mark_done, mark_failed, _parse_line import emails as em def test_parse_line(): acc = _parse_line("user@example.com:pass123") assert acc is not None assert acc.email == "user@example.com" assert acc.password == "pass123" def test_parse_line_with_colon_in_password(): acc = _parse_line("user@example.com:pass:with:colons") assert acc is not None assert acc.password == "pass:with:colons" def test_parse_line_empty(): assert _parse_line("") is None assert _parse_line(" ") is None assert _parse_line("# comment") is None def test_parse_line_malformed(): assert _parse_line("no-colon-here") is None @pytest.mark.asyncio async def test_has_accounts_true(tmp_path, monkeypatch): f = tmp_path / "emails.txt" f.write_text("a@b.com:pass1\nc@d.com:pass2\n") monkeypatch.setattr(em, "EMAILS_FILE", f) assert await has_accounts() is True @pytest.mark.asyncio async def test_has_accounts_false(tmp_path, monkeypatch): f = tmp_path / "emails.txt" f.write_text("") monkeypatch.setattr(em, "EMAILS_FILE", f) assert await has_accounts() is False @pytest.mark.asyncio async def test_has_accounts_missing_file(tmp_path, monkeypatch): monkeypatch.setattr(em, "EMAILS_FILE", tmp_path / "nope.txt") assert await has_accounts() is False @pytest.mark.asyncio async def test_pop_account_removes_from_file(tmp_path, monkeypatch): emails_file = tmp_path / "emails.txt" emails_file.write_text("a@b.com:pass1\nc@d.com:pass2\ne@f.com:pass3\n") monkeypatch.setattr(em, "EMAILS_FILE", emails_file) monkeypatch.setattr(em, "DATA_DIR", tmp_path) monkeypatch.setattr(em, "DONE_FILE", tmp_path / "done.txt") monkeypatch.setattr(em, "FAILED_FILE", tmp_path / "failed.txt") acc = await pop_account() assert acc is not None assert acc.email == "a@b.com" remaining = emails_file.read_text() assert "a@b.com" not in remaining assert "c@d.com" in remaining @pytest.mark.asyncio async def test_pop_account_empty(tmp_path, monkeypatch): f = tmp_path / "emails.txt" f.write_text("") monkeypatch.setattr(em, "EMAILS_FILE", f) monkeypatch.setattr(em, "DATA_DIR", tmp_path) monkeypatch.setattr(em, "DONE_FILE", tmp_path / "done.txt") monkeypatch.setattr(em, "FAILED_FILE", tmp_path / "failed.txt") assert await pop_account() is None @pytest.mark.asyncio async def test_pop_account_missing_file(tmp_path, monkeypatch): monkeypatch.setattr(em, "EMAILS_FILE", tmp_path / "nope.txt") assert await pop_account() is None @pytest.mark.asyncio async def test_mark_done(tmp_path, monkeypatch): done_file = tmp_path / "done.txt" monkeypatch.setattr(em, "DATA_DIR", tmp_path) monkeypatch.setattr(em, "DONE_FILE", done_file) await mark_done("test@example.com") content = done_file.read_text() assert "test@example.com" in content @pytest.mark.asyncio async def test_mark_failed(tmp_path, monkeypatch): failed_file = tmp_path / "failed.txt" monkeypatch.setattr(em, "DATA_DIR", tmp_path) monkeypatch.setattr(em, "FAILED_FILE", failed_file) await mark_failed("test@example.com") content = failed_file.read_text() assert "test@example.com" in content @pytest.mark.asyncio async def test_pop_all_accounts(tmp_path, monkeypatch): f = tmp_path / "emails.txt" f.write_text("a@b.com:pass1\n") monkeypatch.setattr(em, "EMAILS_FILE", f) monkeypatch.setattr(em, "DATA_DIR", tmp_path) monkeypatch.setattr(em, "DONE_FILE", tmp_path / "done.txt") monkeypatch.setattr(em, "FAILED_FILE", tmp_path / "failed.txt") acc1 = await pop_account() assert acc1.email == "a@b.com" acc2 = await pop_account() assert acc2 is None assert await has_accounts() is False