test_fromconfig.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """Tests for lambdagent.fromconfig compiler."""
  2. import os
  3. import json
  4. import tempfile
  5. import pytest
  6. import yaml
  7. from lambdagent.fromconfig import from_config, lint_config, to_lambda_expr, describe_config
  8. from lambdagent.fromconfig.errors import SchemaError
  9. from lambdagent.fromconfig.schema import validate_schema
  10. from lambdagent.primitives import Lam, Loop, Compose
  11. from lambdagent.extensions import Memory, Guard, Route
  12. def _write_config(config):
  13. f = tempfile.NamedTemporaryFile(mode='w', suffix='.yml', delete=False, encoding='utf-8')
  14. yaml.dump(config, f, allow_unicode=True)
  15. f.close()
  16. return f.name
  17. def test_compile_simple():
  18. path = _write_config({"type": "simple", "systemPrompt": "Hello", "model": {"name": "test"}})
  19. try:
  20. term = from_config(path)
  21. assert isinstance(term, Lam)
  22. finally:
  23. os.unlink(path)
  24. def test_compile_chain():
  25. path = _write_config({
  26. "type": "chain", "model": {"name": "test"},
  27. "chain": {"steps": [
  28. {"name": "s1", "prompt": "Step 1"},
  29. {"name": "s2", "prompt": "Step 2"},
  30. ]}
  31. })
  32. try:
  33. term = from_config(path)
  34. assert isinstance(term, Compose)
  35. assert len(term.stages) == 2
  36. finally:
  37. os.unlink(path)
  38. def test_compile_react():
  39. path = _write_config({
  40. "type": "react", "systemPrompt": "Think",
  41. "model": {"name": "test"},
  42. "react": {"maxSteps": 5},
  43. "mcp": {"localTools": ["terminate"]},
  44. })
  45. try:
  46. term = from_config(path)
  47. assert isinstance(term, Loop)
  48. assert term.max_steps == 5
  49. finally:
  50. os.unlink(path)
  51. def test_compile_with_memory():
  52. path = _write_config({
  53. "type": "simple", "systemPrompt": "Hello",
  54. "model": {"name": "test"},
  55. "memory": {"enabled": True, "strategy": "local", "size": 10, "ttl": 3600},
  56. })
  57. try:
  58. term = from_config(path)
  59. assert isinstance(term, Memory)
  60. finally:
  61. os.unlink(path)
  62. def test_compile_with_guard():
  63. path = _write_config({
  64. "type": "simple", "systemPrompt": "Hello",
  65. "model": {"name": "test"},
  66. "guard": {"validator": "len(x) > 5", "retry": 1},
  67. })
  68. try:
  69. term = from_config(path)
  70. assert isinstance(term, Guard)
  71. finally:
  72. os.unlink(path)
  73. def test_schema_validation_missing_type():
  74. errors = validate_schema({})
  75. assert any(e[0] == "ERROR" for e in errors)
  76. def test_lint_no_terminate():
  77. results = lint_config({
  78. "type": "react", "systemPrompt": "x",
  79. "react": {"maxSteps": 10},
  80. "mcp": {"localTools": []},
  81. })
  82. rules = [r.rule for r in results]
  83. assert any("L004" in r for r in rules)
  84. def test_lint_clean():
  85. results = lint_config({
  86. "type": "react", "systemPrompt": "x",
  87. "model": {"name": "test"},
  88. "react": {"maxSteps": 10},
  89. "mcp": {"localTools": ["terminate"]},
  90. })
  91. errors = [r for r in results if r.level == "ERROR"]
  92. assert len(errors) == 0
  93. def test_to_lambda_expr():
  94. expr = to_lambda_expr({"type": "simple", "name": "test", "systemPrompt": "hi"})
  95. assert "test" in expr
  96. assert "lambda" in expr
  97. def test_describe_config():
  98. desc = describe_config({"type": "react", "name": "agent", "react": {"maxSteps": 10},
  99. "mcp": {"onlineTool": {"s": ["search"]}, "localTools": ["terminate"]}})
  100. assert "Y_10" in desc or "Loop" in desc