61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import io
|
|
import tarfile
|
|
import unittest
|
|
|
|
from agent_builder.tools import (
|
|
_assert_current_agent_project,
|
|
_assert_current_a2a_init_template,
|
|
_render_a2a_init_template,
|
|
)
|
|
|
|
|
|
class TemplateInitTests(unittest.TestCase):
|
|
def test_render_a2a_init_template_matches_sdk_starter(self) -> None:
|
|
files = _render_a2a_init_template(
|
|
"research-agent",
|
|
description="Research helper",
|
|
)
|
|
|
|
self.assertEqual(set(files), {"agent.py", "a2a.yaml", "requirements.txt"})
|
|
self.assertIn('name = "research-agent"', files["agent.py"])
|
|
self.assertIn('description = "Research helper"', files["agent.py"])
|
|
self.assertIn("LLMProvisioning.CALLER_PROVIDED", files["agent.py"])
|
|
self.assertIn("ctx.llm", files["agent.py"])
|
|
self.assertIn("create_deep_agent", files["agent.py"])
|
|
self.assertIn("name: research-agent", files["a2a.yaml"])
|
|
self.assertIn("entrypoint: agent:ResearchAgent", files["a2a.yaml"])
|
|
self.assertIn("deepagents>=0.5.0", files["requirements.txt"])
|
|
|
|
def test_render_a2a_init_template_rejects_invalid_names(self) -> None:
|
|
with self.assertRaises(ValueError):
|
|
_render_a2a_init_template("Bad Name")
|
|
|
|
def test_stale_sdk_template_is_rejected(self) -> None:
|
|
with self.assertRaisesRegex(RuntimeError, "stale"):
|
|
_assert_current_a2a_init_template({
|
|
"agent.py": "from a2a_pack import A2AAgent",
|
|
"requirements.txt": "httpx>=0.27",
|
|
})
|
|
|
|
def test_stale_agent_project_is_rejected(self) -> None:
|
|
bundle = _tarball({
|
|
"agent.py": "from a2a_pack import A2AAgent\n",
|
|
"requirements.txt": "httpx>=0.27\n",
|
|
})
|
|
|
|
with self.assertRaisesRegex(RuntimeError, "agent project is stale"):
|
|
_assert_current_agent_project(bundle)
|
|
|
|
|
|
def _tarball(files: dict[str, str]) -> bytes:
|
|
buf = io.BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
|
|
for path, text in files.items():
|
|
body = text.encode("utf-8")
|
|
info = tarfile.TarInfo(path)
|
|
info.size = len(body)
|
|
tf.addfile(info, io.BytesIO(body))
|
|
return buf.getvalue()
|