Skip to content
Repos

blackcat

Python 12.9 KB298 lines Opens in site

Directory Raw Download

"""Committed tests for blackcat AW limit + compaction (stdlib, no network)."""
# retoor <retoor@molodetz.nl>

import os
import tempfile
import unittest
from unittest import mock

from blackcat.agent import BlackCatBot, resolve_context_limit
from blackcat.client import DeepSeekClient, DeepSeekError
from blackcat.fstate import FileState, estimate_tokens
from blackcat.tools import FileTools


def _bot(**kwargs):
    kwargs.setdefault("provider", "echo")
    kwargs.setdefault("system", "t")
    return BlackCatBot(**kwargs)


class TestEstimate(unittest.TestCase):
    def test_chars_per_token(self):
        self.assertEqual(estimate_tokens(""), 1)
        self.assertEqual(estimate_tokens("abcd"), 1)
        self.assertEqual(estimate_tokens("x" * 400), 100)


class TestHistory(unittest.IsolatedAsyncioTestCase):
    async def test_prompts_accumulate_messages(self):
        async with _bot() as bot:
            self.assertEqual(await bot.prompt("hi"), "echo: hi")
            self.assertEqual(await bot.prompt("again"), "echo: again")
            self.assertEqual(len(bot.messages), 4)
            self.assertEqual(bot.messages[0]["role"], "user")

    async def test_report_carries_context(self):
        async with _bot() as bot:
            await bot.prompt("hi")
            status = bot.report()["context"]
        self.assertEqual(status["limit"], 1000000)
        self.assertFalse(status["summary"])
        self.assertEqual(status["messages"], 2)

    async def test_wire_strips_meta(self):
        async with _bot() as bot:
            bot.messages.append({"role": "user", "tool": "read",
                                 "tool_ok": True, "content": "x"})
            wire = bot._wire_messages()
        self.assertEqual(wire, [{"role": "user", "content": "x"}])


class TestRunTool(unittest.IsolatedAsyncioTestCase):
    def setUp(self):
        self.tmp = tempfile.TemporaryDirectory()
        self.addCleanup(self.tmp.cleanup)
        tools = FileTools(FileState(self.tmp.name))
        self.bot = BlackCatBot(provider="echo", system="t", tools=tools)
        self.addAsyncCleanup(self.bot.aclose)

    async def test_unknown_tool_recorded(self):
        out = await self.bot.run_tool("nope")
        self.assertFalse(out["ok"])
        self.assertEqual(out["code"], "unknown_tool")
        kinds = [f["kind"] for f in self.bot.transcript]
        self.assertIn("tool", kinds)

    async def test_read_flows_to_messages(self):
        with open(os.path.join(self.tmp.name, "f.txt"), "w") as fh:
            fh.write("hello\n")
        out = await self.bot.run_tool("read", path="f.txt")
        self.assertTrue(out["ok"])
        last = self.bot.messages[-1]
        self.assertEqual(last["tool"], "read")
        self.assertTrue(last["tool_ok"])
        self.assertIn("hello", last["content"])

    async def test_bulky_args_abbreviated(self):
        with open(os.path.join(self.tmp.name, "f.txt"), "w") as fh:
            fh.write("a\n")
        await self.bot.run_tool("read", path="f.txt")
        await self.bot.run_tool("edit", path="f.txt", old_string="a" * 500,
                          new_string="b")
        frame = self.bot.transcript[-1]
        self.assertEqual(frame["args"]["old_string"], "<500 chars>")
        self.assertEqual(frame["args"]["new_string"], "<1 chars>")


class TestCompaction(unittest.IsolatedAsyncioTestCase):
    async def test_warn_gate_fires_once(self):
        async with _bot(context_limit=100) as bot:
            bot._usage_input = 76
            await bot._maybe_compact()
            warns = [f for f in bot.transcript
                     if f.get("event") == "warn"]
            self.assertEqual(len(warns), 1)
            await bot._maybe_compact()
            warns = [f for f in bot.transcript
                     if f.get("event") == "warn"]
            self.assertEqual(len(warns), 1)

    async def test_manual_compact_clears_old_tools_only(self):
        async with _bot(keep_recent_tools=2) as bot:
            for i in range(4):
                bot.messages.append({"role": "user", "tool": "read",
                                     "tool_ok": True,
                                     "content": "result %d" % i})
            info = await bot.compact(reason="manual")
            self.assertTrue(info["ok"])
            self.assertEqual(info["tier"], "clear")
            self.assertEqual(info["cleared"], 2)
            self.assertEqual(len(bot.messages), 4)
            self.assertEqual(bot.messages[-1]["content"], "result 3")
            self.assertTrue(bot.messages[0]["content"]["cleared"])

    async def test_auto_compact_summarizes(self):
        async with _bot(context_limit=200, keep_recent_tokens=40) as bot:
            bot.note("decision: use tabs")
            await bot.prompt("please summarize " + "x" * 800)
            compacts = [f for f in bot.transcript
                        if f["kind"] == "compact"]
            self.assertTrue(compacts)
            self.assertTrue(bot.context_status()["summary"])
            self.assertIn("Messages condensed", bot._summary)
            self.assertIn("decision: use tabs", bot._summary)
            self.assertEqual(bot.system, "t")
            self.assertTrue(bot.messages)
            self.assertEqual(bot.messages[-1]["role"], "assistant")

    async def test_memory_flush_writes_notes(self):
        with tempfile.TemporaryDirectory() as tmp:
            mem = os.path.join(tmp, "MEMORY.md")
            async with _bot(context_limit=200, keep_recent_tokens=40,
                      memory_path=mem) as bot:
                bot.note("decision: use tabs")
                await bot.prompt("go " + "y" * 800)
            with open(mem, encoding="utf-8") as fh:
                body = fh.read()
            self.assertIn("decision: use tabs", body)

    async def test_headdrop_when_nothing_to_summarize(self):
        async with _bot(context_limit=50, keep_recent_tokens=100000) as bot:
            for i in range(10):
                bot.messages.append({"role": "user",
                                     "content": "m%d" % i})
            bot._usage_input = 1000
            info = await bot.compact(reason="manual")
            self.assertEqual(info["tier"], "headdrop")
            self.assertEqual(info["dropped_messages"], 2)
            self.assertEqual(len(bot.messages), 8)

    async def test_reentrant_compact_noops(self):
        async with _bot() as bot:
            bot._compacting = True
            try:
                info = await bot.compact()
            finally:
                bot._compacting = False
            self.assertFalse(info["ok"])
            self.assertEqual(info["code"], "busy")

    async def test_llm_summary_path_mocked(self):
        bot = BlackCatBot(provider="deepseek", system="t", api_key="k",
                      context_limit=100, keep_recent_tokens=10)
        for i in range(4):
            bot.messages.append({"role": "user",
                                 "content": "body %d " % i + "z" * 200})
        bot._usage_input = 1000
        payload = {"choices": [{"message": {"content": "CANNED"}}]}
        with mock.patch.object(
                DeepSeekClient, "chat",
                new=mock.AsyncMock(return_value=payload)) as chat:
            info = await bot.compact(reason="manual")
        self.assertEqual(info["tier"], "summary")
        self.assertEqual(bot._summary, "CANNED")
        sent = chat.call_args[0][0]
        self.assertIn("Files touched", sent[0]["content"])

    async def test_failed_summarizer_falls_back(self):
        bot = BlackCatBot(provider="deepseek", system="t", api_key="k",
                      context_limit=100, keep_recent_tokens=10)
        for i in range(5):
            bot.messages.append({"role": "user",
                                 "content": "body %d " % i + "z" * 200})
        bot._usage_input = 1000
        with mock.patch.object(
                DeepSeekClient, "chat",
                new=mock.AsyncMock(
                    side_effect=DeepSeekError("down"))):
            info = await bot.compact(reason="manual")
        self.assertEqual(info["tier"], "headdrop")


class TestContextLimitResolution(unittest.IsolatedAsyncioTestCase):
    async def test_model_table(self):
        self.assertEqual(resolve_context_limit("deepseek-flash"), 1000000)
        self.assertEqual(resolve_context_limit("deepseek-chat"), 128000)
        self.assertEqual(resolve_context_limit("openrouter/auto"), 128000)
        self.assertEqual(resolve_context_limit(None), 128000)

    async def test_default_uses_model(self):
        async with _bot() as bot:
            self.assertEqual(bot.context_limit, 1000000)
        async with _bot(model="deepseek-chat") as bot:
            self.assertEqual(bot.context_limit, 128000)

    async def test_explicit_limit_wins(self):
        async with _bot(context_limit=200) as bot:
            self.assertEqual(bot.context_limit, 200)

    async def test_bad_limit_rejected(self):
        with self.assertRaises(ValueError):
            _bot(context_limit=0)


class TestTodoTool(unittest.IsolatedAsyncioTestCase):
    async def test_set_update_add_list(self):
        async with _bot() as bot:
            out = await bot.run_tool("todo", action="set",
                               items=["plan", "build", "verify"])
            self.assertTrue(out["ok"])
            self.assertEqual(out["counts"],
                             {"pending": 3, "in_progress": 0, "done": 0})
            out = await bot.run_tool("todo", action="update", index=1,
                               status="in_progress")
            self.assertTrue(out["ok"])
            out = await bot.run_tool("todo", action="update", index=2,
                               status="in_progress")
            states = [t["status"] for t in bot.todo_list()]
            self.assertEqual(states, ["pending", "in_progress", "pending"])
            await bot.run_tool("todo", action="update", index=2, status="done")
            await bot.run_tool("todo", action="add", items=["ship"])
            out = await bot.run_tool("todo", action="list")
            self.assertEqual(out["counts"],
                             {"pending": 3, "in_progress": 0, "done": 1})
            self.assertIn("[x] 2. build", out["rendered"])

    async def test_bad_calls(self):
        async with _bot() as bot:
            self.assertEqual((await bot.run_tool("todo", action="set",
                                          items="nope"))["code"], "bad_args")
            self.assertEqual((await bot.run_tool("todo", action="update",
                                          index=9,
                                          status="done"))["code"], "bad_args")
            self.assertEqual((await bot.run_tool("todo", action="bogus"))["code"],
                             "bad_args")

    async def test_todos_on_wire_and_survive_compaction(self):
        async with _bot(context_limit=100, keep_recent_tokens=10) as bot:
            await bot.run_tool("todo", action="set", items=["big task"])
            for i in range(5):
                bot.messages.append({"role": "user",
                                     "content": "body %d " % i + "z" * 200})
            bot._usage_input = 1000
            await bot.compact(reason="manual")
            wire = bot._build_wire()
            joined = "\n".join(str(m.get("content", "")) for m in wire)
            self.assertIn("Current task list", joined)
            self.assertIn("big task", joined)


class TestSessionPersistence(unittest.IsolatedAsyncioTestCase):
    async def test_roundtrip_restores_history_todos_summary(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = os.path.join(tmp, "s.json")
            async with _bot() as bot:
                await bot.prompt("hello")
                await bot.run_tool("todo", action="set", items=["step one"])
                await bot.save_session(path)
            async with _bot() as bot:
                info = await bot.load_session(path)
                self.assertTrue(info["ok"])
                self.assertEqual(info["messages"], 3)
                self.assertEqual(bot.messages[0]["content"], "hello")
                self.assertEqual(bot.todo_list()[0]["text"], "step one")
                self.assertEqual(await bot.prompt("again"), "echo: again")
                self.assertEqual(len(bot.messages), 5)

    async def test_model_mismatch_rejected(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = os.path.join(tmp, "s.json")
            async with _bot() as bot:
                await bot.save_session(path)
            async with _bot(model="deepseek-chat") as bot:
                with self.assertRaises(ValueError):
                    await bot.load_session(path)

    async def test_malformed_rejected(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = os.path.join(tmp, "s.json")
            with open(path, "w") as fh:
                fh.write('{"version": 99}')
            async with _bot() as bot:
                with self.assertRaises(ValueError):
                    await bot.load_session(path)


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