forked from HAL9000/cleveragents-core
164 lines
5.7 KiB
Python
164 lines
5.7 KiB
Python
"""Step definitions for JSON sanitization unit tests."""
|
|
|
|
import json
|
|
|
|
from behave import given, then, when
|
|
|
|
from cleveragents.core.application import ReactiveCleverAgentsApp
|
|
|
|
|
|
@given("I have an application with JSON sanitizer")
|
|
def step_given_app_with_sanitizer(context):
|
|
"""Create a minimal app instance with sanitization method."""
|
|
context.app = ReactiveCleverAgentsApp.__new__(ReactiveCleverAgentsApp)
|
|
|
|
|
|
@given("a valid JSON string with file and content")
|
|
def step_given_valid_json(context):
|
|
"""Set up valid JSON string."""
|
|
context.json_string = '{"file": "test.txt", "content": "Hello World"}'
|
|
context.original_json = context.json_string
|
|
|
|
|
|
@given("a JSON string with literal newlines in content")
|
|
def step_given_json_with_newlines(context):
|
|
"""Set up JSON with literal newlines."""
|
|
context.json_string = '{"file": "test.txt", "content": "Line 1\nLine 2\nLine 3"}'
|
|
|
|
|
|
@given("a JSON string with multiple consecutive newlines")
|
|
def step_given_json_with_multiple_newlines(context):
|
|
"""Set up JSON with multiple consecutive newlines."""
|
|
context.json_string = '{"file": "paper.txt", "content": "Title\n\nIntroduction\n\nConclusion"}'
|
|
|
|
|
|
@given("a JSON string with complex multi-section blockchain content")
|
|
def step_given_json_with_blockchain(context):
|
|
"""Set up JSON with complex blockchain content."""
|
|
context.json_string = """{"file": "blockchain.txt", "content": "Title: Blockchain Technology
|
|
|
|
I. Introduction
|
|
Blockchain is a revolutionary technology...
|
|
|
|
II. Technical Details
|
|
The blockchain works by...
|
|
|
|
III. Conclusion
|
|
In conclusion, blockchain represents..."}"""
|
|
|
|
|
|
@given("a JSON string with literal tabs")
|
|
def step_given_json_with_tabs(context):
|
|
"""Set up JSON with tabs."""
|
|
context.json_string = '{"file": "test.txt", "content": "Item 1:\tValue 1\nItem 2:\tValue 2"}'
|
|
|
|
|
|
@given("a JSON string with carriage returns")
|
|
def step_given_json_with_carriage_returns(context):
|
|
"""Set up JSON with carriage returns."""
|
|
context.json_string = '{"file": "test.txt", "content": "Line 1\r\nLine 2\r\nLine 3"}'
|
|
|
|
|
|
@given("a JSON string with mixed control characters")
|
|
def step_given_json_with_mixed_chars(context):
|
|
"""Set up JSON with mixed control characters."""
|
|
context.json_string = '{"file": "test.txt", "content": "Title\n\tSection 1\r\n\tSection 2"}'
|
|
|
|
|
|
@given("a JSON string with empty content")
|
|
def step_given_json_with_empty_content(context):
|
|
"""Set up JSON with empty content."""
|
|
context.json_string = '{"file": "empty.txt", "content": ""}'
|
|
|
|
|
|
@given("a JSON string with escaped quotes in content")
|
|
def step_given_json_with_quotes(context):
|
|
"""Set up JSON with escaped quotes."""
|
|
context.json_string = '{"file": "test.txt", "content": "He said \\"Hello\\""}'
|
|
|
|
|
|
@given("a JSON string with very long content")
|
|
def step_given_json_with_long_content(context):
|
|
"""Set up JSON with very long content."""
|
|
long_content = (
|
|
"Section 1\n" + ("This is a long paragraph. " * 100) + "\n\nSection 2\n" + ("Another paragraph. " * 100)
|
|
)
|
|
context.json_string = f'{{"file": "long.txt", "content": "{long_content}"}}'
|
|
|
|
|
|
@given("a JSON string with special characters")
|
|
def step_given_json_with_special_chars(context):
|
|
"""Set up JSON with special characters."""
|
|
context.json_string = '{"file": "test.txt", "content": "Math: x + y = z, Cost: $100"}'
|
|
|
|
|
|
@given("a JSON string with unicode characters")
|
|
def step_given_json_with_unicode(context):
|
|
"""Set up JSON with unicode characters."""
|
|
context.json_string = '{"file": "test.txt", "content": "Hello 世界 🌍"}'
|
|
|
|
|
|
@when("I sanitize the JSON string")
|
|
def step_when_sanitize_json(context):
|
|
"""Sanitize the JSON string."""
|
|
context.sanitized_json = context.app._sanitize_json_string(context.json_string)
|
|
|
|
|
|
@then("the JSON should be unchanged")
|
|
def step_then_json_unchanged(context):
|
|
"""Verify JSON is unchanged."""
|
|
assert context.sanitized_json == context.original_json
|
|
|
|
|
|
@then("the JSON should parse successfully")
|
|
def step_then_json_parses(context):
|
|
"""Verify JSON can be parsed."""
|
|
try:
|
|
context.parsed_json = json.loads(context.sanitized_json)
|
|
assert True
|
|
except json.JSONDecodeError as e:
|
|
raise AssertionError(f"JSON failed to parse: {e}")
|
|
|
|
|
|
@then("the content should contain all original lines")
|
|
def step_then_content_has_lines(context):
|
|
"""Verify content contains all lines."""
|
|
assert "Line 1" in context.parsed_json["content"]
|
|
assert "Line 2" in context.parsed_json["content"]
|
|
assert "Line 3" in context.parsed_json["content"]
|
|
|
|
|
|
@then("the content should contain all blockchain sections")
|
|
def step_then_content_has_blockchain_sections(context):
|
|
"""Verify blockchain sections are present."""
|
|
assert "Title: Blockchain Technology" in context.parsed_json["content"]
|
|
assert "I. Introduction" in context.parsed_json["content"]
|
|
assert "II. Technical Details" in context.parsed_json["content"]
|
|
assert "III. Conclusion" in context.parsed_json["content"]
|
|
|
|
|
|
@then("the content should be empty")
|
|
def step_then_content_empty(context):
|
|
"""Verify content is empty."""
|
|
assert context.parsed_json["content"] == ""
|
|
|
|
|
|
@then("the content should be longer than {length:d} characters")
|
|
def step_then_content_length(context, length):
|
|
"""Verify content length."""
|
|
assert len(context.parsed_json["content"]) > length
|
|
|
|
|
|
@then("special characters should be preserved")
|
|
def step_then_special_chars_preserved(context):
|
|
"""Verify special characters are preserved."""
|
|
assert "x + y = z" in context.parsed_json["content"]
|
|
assert "$100" in context.parsed_json["content"]
|
|
|
|
|
|
@then("unicode characters should be preserved")
|
|
def step_then_unicode_preserved(context):
|
|
"""Verify unicode characters are preserved."""
|
|
assert "世界" in context.parsed_json["content"]
|
|
assert "🌍" in context.parsed_json["content"]
|