"""Smoke tests for offline breach-log pipeline (no network deps)."""
import sys
import unittest
from pathlib import Path

SCRIPTS = Path(__file__).parent.parent / "scripts"
sys.path.insert(0, str(SCRIPTS))

from aggregate_breach import (
    parse_format_a, parse_format_b, parse_format_c, parse_tsv,
    detect_and_parse, _is_obfuscated_noise,
)
from intake_local import derive_source_id, guess_password
from tg_bot_harvest import extract_config


class TestFormatA(unittest.TestCase):
    def test_soft_host_login_password(self):
        text = "Soft: Chrome\nHost: https://example.com\nLogin: user1\nPassword: pass1\n"
        creds = parse_format_a(text)
        self.assertEqual(len(creds), 1)
        self.assertEqual(creds[0], ("https://example.com", "user1", "pass1"))

    def test_obfuscated_noise_filtered(self):
        text = "Soft: Chrome\nHost: https://x.com\nLogin: kir3inf\nPassword: pass\n"
        creds = parse_format_a(text)
        self.assertEqual(len(creds), 0)


class TestFormatB(unittest.TestCase):
    def test_soft_url_user_pass(self):
        text = "SOFT: Edge\nURL: https://b.com\nUSER: u2\nPASS: p2\n"
        creds = parse_format_b(text)
        self.assertEqual(creds[0], ("https://b.com", "u2", "p2"))


class TestFormatC(unittest.TestCase):
    def test_browser_url_username_password(self):
        text = "URL      : https://c.com\nUsername : u3\nPassword : p3\n"
        creds = parse_format_c(text)
        self.assertEqual(creds[0], ("https://c.com", "u3", "p3"))


class TestTSV(unittest.TestCase):
    def test_tsv_parse(self):
        text = "https://d.com\tu4\tp4\n"
        creds = parse_tsv(text)
        self.assertEqual(creds[0], ("https://d.com", "u4", "p4"))


class TestSourceIDDerivation(unittest.TestCase):
    def test_pixelcloud(self):
        self.assertEqual(derive_source_id("6226_05.07.2026_@PIXELCLOUD3.rar"), "PIXELCLOUD3-6226")

    def test_beetraffic(self):
        self.assertEqual(derive_source_id("@beetraffic 2000 MIX 05-07-2026.zip"), "BEETRAFFIC-2000")

    def test_ayankouji(self):
        self.assertEqual(derive_source_id("AYANKOUJI PRIVATE #528 PART 4.zip"), "AYANKOUJI-528")


class TestPasswordGuess(unittest.TestCase):
    def test_pixelcloud(self):
        self.assertEqual(guess_password("6226_05.07.2026_@PIXELCLOUD3.rar"), "@PIXELCLOUD3")

    def test_unknown(self):
        self.assertIsNone(guess_password("random_archive.rar"))


class TestTgBotHarvest(unittest.TestCase):
    FAKE_TOKEN = "1234567890:AAEhBOweik6ad9r_QXMENQjcrGbqCr4K-0a"
    FAKE_TOKEN2 = "987654321:BBFhCPxfjl7be0s_SZNEQrkhsDs5L-1bXYZ"

    def test_ascii_token_and_chat_id(self):
        blob = (
            b"\x00\x01MZ junk \x90\x90"
            + f"https://api.telegram.org/bot{self.FAKE_TOKEN}/sendMessage?chat_id=123456789&text=".encode()
            + b"\x00\x00 padding"
        )
        pairs = extract_config(blob)
        self.assertEqual(pairs, [{"token": self.FAKE_TOKEN, "chat_id": "123456789"}])

    def test_utf16le_token(self):
        blob = b"\xff" + self.FAKE_TOKEN2.encode("utf-16-le") + b"\x00\x00"
        pairs = extract_config(blob)
        self.assertEqual([p["token"] for p in pairs], [self.FAKE_TOKEN2])
        self.assertEqual(pairs[0]["chat_id"], "")

    def test_no_false_positive_on_short_secret(self):
        blob = b"token=12345:tooshorthere and 12345678901:toolongtoolongtoolongtoolongtoolongx"
        self.assertEqual(extract_config(blob), [])

    def test_chat_id_not_attributed_without_telegram_context(self):
        blob = b'{"chat_id": "99887766"}\x00\x00' + self.FAKE_TOKEN.encode() + b"\x00"
        pairs = extract_config(blob)
        self.assertEqual(pairs, [{"token": self.FAKE_TOKEN, "chat_id": ""}])


if __name__ == "__main__":
    unittest.main()
