"""Tests for DCC helper — TDD: tests before implementation.""" import pytest import time from i2p_apps.i2ptunnel.dcc import DCCHelper, DCCTransfer class TestDCCTransfer: def test_transfer_creation(self): t = DCCTransfer( remote_dest="A" * 516, local_port=12345, dcc_type="SEND", filename="test.txt", ) assert t.remote_dest == "A" * 516 assert t.local_port == 12345 assert t.dcc_type == "SEND" assert t.filename == "test.txt" def test_transfer_not_expired(self): t = DCCTransfer( remote_dest="A" * 516, local_port=12345, dcc_type="SEND", expires_at=time.monotonic() + 1800, ) assert t.is_expired is False def test_transfer_expired(self): t = DCCTransfer( remote_dest="A" * 516, local_port=12345, dcc_type="SEND", expires_at=time.monotonic() - 1, ) assert t.is_expired is True class TestDCCHelper: def test_creation(self): h = DCCHelper(local_b32="test.b32.i2p", max_transfers=5) assert h._max_transfers == 5 assert len(h._transfers) == 0 def test_parse_dcc_send(self): h = DCCHelper(local_b32="test.b32.i2p") parts = h._parse_dcc("DCC SEND file.txt 2130706433 1234 5678") assert parts is not None assert parts["type"] == "SEND" assert parts["filename"] == "file.txt" assert parts["port"] == "1234" def test_parse_dcc_chat(self): h = DCCHelper(local_b32="test.b32.i2p") parts = h._parse_dcc("DCC CHAT chat 2130706433 1234") assert parts is not None assert parts["type"] == "CHAT" def test_parse_invalid(self): h = DCCHelper(local_b32="test.b32.i2p") assert h._parse_dcc("not a dcc command") is None def test_max_transfers_enforced(self): h = DCCHelper(local_b32="test.b32.i2p", max_transfers=2) h._transfers["t1"] = DCCTransfer("d1", 10001, "SEND", expires_at=time.monotonic() + 1800) h._transfers["t2"] = DCCTransfer("d2", 10002, "SEND", expires_at=time.monotonic() + 1800) assert h._can_add_transfer() is False def test_cleanup_expired(self): h = DCCHelper(local_b32="test.b32.i2p", max_transfers=2) h._transfers["t1"] = DCCTransfer("d1", 10001, "SEND", expires_at=time.monotonic() - 1) h._transfers["t2"] = DCCTransfer("d2", 10002, "SEND", expires_at=time.monotonic() + 1800) h._cleanup_expired() assert len(h._transfers) == 1 assert "t2" in h._transfers def test_rewrite_outbound_send(self): h = DCCHelper(local_b32="mysite.b32.i2p") result = h.rewrite_outbound("DCC SEND file.txt 2130706433 1234 5678") assert result is not None assert "mysite.b32.i2p" in result assert "file.txt" in result