Skip to content

Your lightweight open-source personal agent.

20.1 KB
Raw Blame
import json

import pytest

from nanobot.pairing import __all__ as pairing_all
from nanobot.pairing import store


def test_all_exports_are_importable():
    import nanobot.pairing as pkg

    for name in pairing_all:
        assert hasattr(pkg, name), f"{name} is in __all__ but not exported"


@pytest.fixture(autouse=True)
def _tmp_store(tmp_path, monkeypatch):
    path = tmp_path / "pairing.json"
    monkeypatch.setattr(store, "_store_path", lambda: path)


class TestGenerateCode:
    def test_format(self) -> None:
        code = store.generate_code("telegram", "123")
        assert len(code) == 9
        assert code[4] == "-"
        assert code.replace("-", "").isalnum()
        assert code.replace("-", "").isupper()

    def test_uniqueness(self) -> None:
        codes = {store.generate_code("telegram", str(i)) for i in range(20)}
        assert len(codes) == 20

    def test_reuses_active_code_for_same_sender(self) -> None:
        first = store.generate_code("telegram", "123")

        assert store.generate_code("telegram", "123") == first
        assert len(store.list_pending()) == 1

    def test_scopes_reused_codes_to_channel(self) -> None:
        telegram = store.generate_code("telegram", "123")
        discord = store.generate_code("discord", "123")

        assert telegram != discord
        assert len(store.list_pending()) == 2

    def test_ttl_expiration(self, monkeypatch) -> None:
        clock = {"now": 1_000.0}
        monkeypatch.setattr(store.time, "time", lambda: clock["now"])

        code = store.generate_code("telegram", "123", ttl=1)
        assert store.approve_code(code) == ("telegram", "123")

        code2 = store.generate_code("telegram", "456", ttl=0)
        clock["now"] += 0.1
        assert store.approve_code(code2) is None


class TestDelivery:
    def test_failed_reply_can_retry_without_replacing_code_or_approving(self):
        code = store.generate_code("feishu", "alice")
        assert "delivery_status" not in store.list_pending()[0]
        store.record_delivery(code, "sending")
        assert store.list_pending()[0]["delivery_status"] == "sending"
        store.record_delivery(code, "failed")
        assert store.list_pending()[0]["delivery_status"] == "failed"
        assert store.generate_code("feishu", "alice") == code
        store.record_delivery(code, "sent")
        store.record_delivery(code, "failed")
        assert store.list_pending()[0]["delivery_status"] == "sent"
        assert not store.is_approved("feishu", "alice")

    @pytest.mark.parametrize("action", [store.approve_code, store.deny_code])
    def test_late_delivery_does_not_revive_removed_request(self, action):
        code = store.generate_code("feishu", "alice")
        action(code)
        store.record_delivery(code, "sent")
        assert store.list_pending() == []

    def test_expired_reply_is_not_revived(self, monkeypatch):
        monkeypatch.setattr(store.time, "time", lambda: 1000)
        code = store.generate_code("feishu", "alice", ttl=10)
        monkeypatch.setattr(store.time, "time", lambda: 1011)
        store.record_delivery(code, "sent")
        assert store.list_pending() == []
        assert store.approve_code(code) is None

    def test_old_records_remain_usable(self):
        code = store.generate_code("feishu", "alice")
        data = store._load()
        data["pending"][code].pop("delivery_status", None)
        store._save(data)
        assert "delivery_status" not in store.list_pending()[0]
        store.record_delivery(code, "sent")
        assert store.approve_code(code) == ("feishu", "alice")

    def test_bookkeeping_failure_preserves_store(self, monkeypatch):
        code = store.generate_code("feishu", "alice")
        before = store._store_path().read_bytes()
        with monkeypatch.context() as m:
            def fail(_data):
                raise OSError("disk unavailable")
            m.setattr(store, "_save", fail)
            store.record_delivery(code, "sent")
        assert store._store_path().read_bytes() == before


class TestFormatPairingReply:
    def test_points_owner_to_webui_with_command_fallback(self) -> None:
        reply = store.format_pairing_reply("ABCD-EFGH")

        assert "nanobot WebUI" in reply
        assert "ABCD-EFGH" in reply
        assert "/pairing approve ABCD-EFGH" in reply
        assert "private nanobot" in reply
        assert "approved users" not in reply


class TestApproveDeny:
    def test_approve_moves_to_approved(self) -> None:
        code = store.generate_code("telegram", "123")
        assert store.is_approved("telegram", "123") is False

        result = store.approve_code(code)
        assert result == ("telegram", "123")
        assert store.is_approved("telegram", "123") is True
        assert store.get_approved("telegram") == ["123"]

    def test_deny_removes_pending(self) -> None:
        code = store.generate_code("telegram", "123")
        assert store.deny_code(code) is True
        assert store.approve_code(code) is None

    def test_deny_unknown_returns_false(self) -> None:
        assert store.deny_code("UNKNOWN") is False

    def test_approve_expired_returns_none(self, monkeypatch) -> None:
        clock = {"now": 1_000.0}
        monkeypatch.setattr(store.time, "time", lambda: clock["now"])

        code = store.generate_code("telegram", "123", ttl=0)
        clock["now"] += 0.1
        assert store.approve_code(code) is None


class TestRevoke:
    def test_revoke_removes_sender(self) -> None:
        code = store.generate_code("telegram", "123")
        store.approve_code(code)
        assert store.is_approved("telegram", "123") is True

        assert store.revoke("telegram", "123") is True
        assert store.is_approved("telegram", "123") is False
        assert store.get_approved("telegram") == []

    def test_revoke_unknown_returns_false(self) -> None:
        assert store.revoke("telegram", "999") is False

    def test_clear_channel_removes_approved_and_pending(self) -> None:
        code = store.generate_code("telegram", "123")
        store.approve_code(code)
        store.generate_code("telegram", "456")
        store.generate_code("discord", "789")

        assert store.clear_channel("telegram") == {"approved": 1, "pending": 1}

        assert store.is_approved("telegram", "123") is False
        pending = store.list_pending()
        assert [item["channel"] for item in pending] == ["discord"]

    def test_clear_channel_unknown_returns_zero_counts(self) -> None:
        assert store.clear_channel("telegram") == {"approved": 0, "pending": 0}


class TestListPending:
    def test_empty(self) -> None:
        assert store.list_pending() == []

    def test_shows_pending(self) -> None:
        store.generate_code("telegram", "123")
        store.generate_code("discord", "456")
        pending = store.list_pending()
        assert len(pending) == 2
        channels = {p["channel"] for p in pending}
        assert channels == {"telegram", "discord"}

    def test_expired_not_listed(self, monkeypatch) -> None:
        clock = {"now": 1_000.0}
        monkeypatch.setattr(store.time, "time", lambda: clock["now"])

        store.generate_code("telegram", "123", ttl=0)
        clock["now"] += 0.1
        assert store.list_pending() == []


class TestHandlePairingCommand:
    def test_list_empty(self) -> None:
        reply = store.handle_pairing_command("telegram", "list")
        assert reply == "No pending pairing requests."

    def test_list_pending(self) -> None:
        store.generate_code("telegram", "123")
        reply = store.handle_pairing_command("telegram", "list")
        assert "Pending pairing requests:" in reply
        assert "telegram" in reply
        assert "123" in reply

    def test_approve(self) -> None:
        code = store.generate_code("telegram", "123")
        reply = store.handle_pairing_command("telegram", f"approve {code}")
        assert "Approved" in reply
        assert "123" in reply
        assert store.is_approved("telegram", "123") is True

    @pytest.mark.parametrize(
        "command, expected",
        [
            pytest.param("approve BAD-CODE", "Invalid or expired", id="approve_invalid"),
            pytest.param("approve", "Usage:", id="approve_no_arg"),
            pytest.param("deny BAD-CODE", "not found", id="deny_unknown"),
            pytest.param("revoke 999", "was not in the approved list", id="revoke_unknown"),
            pytest.param("revoke", "Usage:", id="revoke_no_arg"),
            pytest.param("foo", "Unknown pairing command", id="unknown_subcommand"),
        ],
    )
    def test_pairing_command_errors(self, command, expected) -> None:
        reply = store.handle_pairing_command("telegram", command)
        assert expected in reply

    def test_deny(self) -> None:
        code = store.generate_code("telegram", "123")
        reply = store.handle_pairing_command("telegram", f"deny {code}")
        assert "Denied" in reply
        assert store.approve_code(code) is None

    def test_revoke_current_channel(self) -> None:
        code = store.generate_code("telegram", "123")
        store.approve_code(code)
        reply = store.handle_pairing_command("telegram", "revoke 123")
        assert "Revoked" in reply
        assert store.is_approved("telegram", "123") is False

    def test_revoke_other_channel(self) -> None:
        code = store.generate_code("discord", "456")
        store.approve_code(code)
        reply = store.handle_pairing_command("telegram", "revoke discord 456")
        assert "Revoked" in reply
        assert store.is_approved("discord", "456") is False

    def test_default_to_list(self) -> None:
        store.generate_code("telegram", "123")
        reply = store.handle_pairing_command("telegram", "")
        assert "Pending pairing requests:" in reply


class TestNonStringSenderId:
    def test_numeric_sender_id_round_trip(self) -> None:
        code = store.generate_code("telegram", 12345)
        assert store.approve_code(code) == ("telegram", "12345")
        assert store.is_approved("telegram", 12345) is True
        assert store.is_approved("telegram", "12345") is True
        assert store.get_approved("telegram") == ["12345"]
        assert store.revoke("telegram", 12345) is True
        assert store.is_approved("telegram", "12345") is False

    def test_hand_edited_numeric_pending_does_not_corrupt_approved_set(self) -> None:
        store._store_path().write_text(
            '{"approved": {"telegram": ["111"]}, '
            '"pending": {"ABCD-EFGH": {"channel": "telegram", "sender_id": 222, '
            '"created_at": 1000.0, "expires_at": 9999999999.0}}}',
            encoding="utf-8",
        )
        assert store.approve_code("ABCD-EFGH") == ("telegram", "222")
        assert store.is_approved("telegram", 222) is True
        store.generate_code("telegram", 333)
        assert store.get_approved("telegram") == ["111", "222"]

    def test_numeric_id_in_hand_edited_store(self) -> None:
        store._store_path().write_text(
            '{"approved": {"telegram": [12345]}, "pending": {}}',
            encoding="utf-8",
        )
        assert store.is_approved("telegram", "12345") is True
        assert store.is_approved("telegram", 12345) is True
        assert store.revoke("telegram", 12345) is True
        assert store.is_approved("telegram", "12345") is False


class TestStoreDurability:
    def test_corruption_recovery(self, tmp_path, monkeypatch) -> None:
        path = tmp_path / "pairing.json"
        path.write_text("not json{", encoding="utf-8")
        monkeypatch.setattr(store, "_store_path", lambda: path)

        assert store.list_pending() == []
        assert store.is_approved("telegram", "123") is False


def test_load_treats_null_approved_channel_list_as_empty(tmp_path, monkeypatch):
    """Null approved channel lists must not crash pairing checks.

    JSON stores can contain ``"telegram": null`` after partial edits; treat
    that like an empty allow-list, matching corrupt-JSON reset behavior.
    """
    path = tmp_path / "pairing.json"
    path.write_text(
        '{"approved": {"telegram": null, "discord": ["456"]}, "pending": {}}',
        encoding="utf-8",
    )
    monkeypatch.setattr(store, "_store_path", lambda: path)
    assert store.is_approved("telegram", "123") is False
    assert store.is_approved("discord", "456") is True
    assert store.get_approved("telegram") == []


def test_load_treats_null_approved_and_pending_maps_as_empty(tmp_path, monkeypatch):
    path = tmp_path / "pairing.json"
    path.write_text(
        '{"approved": null, "pending": null}',
        encoding="utf-8",
    )
    monkeypatch.setattr(store, "_store_path", lambda: path)
    assert store.is_approved("telegram", "123") is False
    assert store.list_pending() == []
    assert store.get_approved("telegram") == []


@pytest.mark.parametrize(
    ("field", "value"),
    [("approved", "corrupt"), ("pending", ["corrupt"])],
)
def test_load_treats_non_object_approved_and_pending_maps_as_empty(
    tmp_path, monkeypatch, field, value
):
    path = tmp_path / "pairing.json"
    payload = {"approved": {}, "pending": {}}
    payload[field] = value
    path.write_text(json.dumps(payload), encoding="utf-8")
    monkeypatch.setattr(store, "_store_path", lambda: path)

    assert store.is_approved("telegram", "123") is False
    assert store.list_pending() == []


@pytest.mark.parametrize("payload", ["null", "[]", "true"])
def test_load_treats_non_object_store_as_empty(tmp_path, monkeypatch, payload):
    path = tmp_path / "pairing.json"
    path.write_text(payload, encoding="utf-8")
    monkeypatch.setattr(store, "_store_path", lambda: path)
    assert store.list_pending() == []
    assert store.is_approved("telegram", "123") is False


def test_list_pending_skips_null_pending_entries(tmp_path, monkeypatch):
    path = tmp_path / "pairing.json"
    path.write_text(
        '{"approved": {}, "pending": {"ABCD-EFGH": null}}',
        encoding="utf-8",
    )
    monkeypatch.setattr(store, "_store_path", lambda: path)
    assert store.list_pending() == []
    assert store.clear_channel("telegram") == {"approved": 0, "pending": 0}


def test_pending_gc_drops_malformed_entries(tmp_path, monkeypatch):
    path = tmp_path / "pairing.json"
    path.write_text(
        '{"approved": {}, "pending": {'
        '"bad-expiry": {"channel": "telegram", "sender_id": "123", "expires_at": null},'
        '"missing-sender": {"channel": "telegram", "expires_at": 9999999999}'
        "}}",
        encoding="utf-8",
    )
    monkeypatch.setattr(store, "_store_path", lambda: path)
    assert store.list_pending() == []


def _fail_reads_of(monkeypatch, path):
    """Make reads of *path* raise like a transiently locked/busy file."""
    import builtins
    from pathlib import Path

    real_open = builtins.open

    def flaky_open(file, mode="r", *args, **kwargs):
        try:
            same = Path(file) == path
        except TypeError:
            same = False
        if same and "r" in mode and "+" not in mode:
            raise PermissionError(13, "temporarily locked", str(path))
        return real_open(file, mode, *args, **kwargs)

    monkeypatch.setattr(builtins, "open", flaky_open)


class TestTransientReadFailure:

    def test_generate_code_does_not_wipe_approvals(self, tmp_path, monkeypatch):
        """An unapproved DM during a read blip previously erased every approval.

        _load treated OSError like corruption and returned an empty store;
        generate_code then unconditionally saved it, overwriting pairing.json
        with no approved senders.
        """
        code = store.generate_code("telegram", "123")
        store.approve_code(code)

        with monkeypatch.context() as m:
            _fail_reads_of(m, store._store_path())
            with pytest.raises(OSError):
                store.generate_code("telegram", "stranger")

        assert store.is_approved("telegram", "123") is True

    def test_reads_fail_closed_without_crashing(self, tmp_path, monkeypatch):
        code = store.generate_code("telegram", "123")
        store.approve_code(code)

        with monkeypatch.context() as m:
            _fail_reads_of(m, store._store_path())
            assert store.is_approved("telegram", "123") is False
            assert store.list_pending() == []
            assert store.get_approved("telegram") == []

        assert store.is_approved("telegram", "123") is True

    def test_approve_command_reports_store_unavailable(self, tmp_path, monkeypatch):
        code = store.generate_code("telegram", "123")

        with monkeypatch.context() as m:
            _fail_reads_of(m, store._store_path())
            reply = store.handle_pairing_command("telegram", f"approve {code}")

        assert "unavailable" in reply.lower()
        assert store.approve_code(code) == ("telegram", "123")


class TestChangeNotifications:
    def test_changes_are_committed_and_unlocked_before_notification(self):
        snapshots = []

        def changed():
            unlocked = store._LOCK.acquire(blocking=False)
            if unlocked:
                store._LOCK.release()
                snapshots.append(store.list_pending())

        code = store.generate_code("telegram", "alice", on_change=changed)
        assert len(snapshots) == 1 and snapshots[-1][0]["code"] == code
        assert store.generate_code("telegram", "alice", on_change=changed) == code
        assert len(snapshots) == 1
        store.record_delivery(code, "sending", on_change=changed)
        assert snapshots[-1][0]["delivery_status"] == "sending"
        store.record_delivery(code, "sending", on_change=changed)
        assert len(snapshots) == 2
        store.record_delivery(code, "sent", on_change=changed)
        store.record_delivery(code, "failed", on_change=changed)
        assert len(snapshots) == 3 and snapshots[-1][0]["delivery_status"] == "sent"
        assert store.approve_code(code, on_change=changed) == ("telegram", "alice")
        assert snapshots[-1] == [] and len(snapshots) == 4
        assert store.approve_code(code, on_change=changed) is None
        assert not store.deny_code(code, on_change=changed)
        assert len(snapshots) == 4

    def test_commands_notify_only_after_changes(self):
        changes = []

        def changed():
            changes.append(json.loads(store._store_path().read_text()))

        code = store.generate_code("telegram", "alice")
        assert "Approved" in store.handle_pairing_command("cli", f"approve {code}", on_change=changed)
        assert changes[-1]["approved"] == {"telegram": ["alice"]}
        store.handle_pairing_command("cli", "list", on_change=changed)
        store.handle_pairing_command("cli", "approve invalid", on_change=changed)
        assert len(changes) == 1
        store.handle_pairing_command("cli", "revoke telegram alice", on_change=changed)
        store.handle_pairing_command("cli", "revoke telegram alice", on_change=changed)
        code = store.generate_code("telegram", "bob")
        store.handle_pairing_command("cli", f"deny {code}", on_change=changed)
        code = store.generate_code("telegram", "carol")
        store.handle_pairing_command("cli", f"approve {code}", on_change=changed)
        store.handle_pairing_command("cli", "revoke telegram carol", on_change=changed)
        assert len(changes) == 5 and changes[-1] == {"approved": {}, "pending": {}}

    def test_failed_persistence_does_not_notify(self, monkeypatch):
        code = store.generate_code("telegram", "alice")
        before = store._store_path().read_bytes()
        changes = []

        def failed_write(_data):
            raise OSError("fixture disk failure")

        monkeypatch.setattr(store, "_save", failed_write)
        with pytest.raises(OSError, match="disk failure"):
            store.generate_code("telegram", "bob", on_change=lambda: changes.append(True))
        store.record_delivery(code, "sent", on_change=lambda: changes.append(True))
        assert changes == [] and store._store_path().read_bytes() == before

    def test_notification_failure_does_not_report_persisted_mutation_as_failure(self):
        def failed_notify():
            raise RuntimeError("fixture observer failure")

        code = store.generate_code("telegram", "alice", on_change=failed_notify)
        assert store.list_pending()[0]["code"] == code
        assert store.approve_code(code, on_change=failed_notify) == ("telegram", "alice")
        assert store.is_approved("telegram", "alice")