chore: FSD stabilization, strict TDD enforcement, and unlearn mechanism
- implemented self-healing unlearn for Qdrant false positives - centralized testing logic in conftest - documented core rules, ai standards, and goap philosophy - purged old dev scratchpads
This commit is contained in:
125
tests/chaos/__init__.py
Normal file
125
tests/chaos/__init__.py
Normal file
@@ -0,0 +1,125 @@
|
||||
"""
|
||||
Shared fixtures and utilities for chaos engineering tests.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
|
||||
def generate_corrupted_xml(corruption_type: str) -> str:
|
||||
"""
|
||||
Generates intentionally corrupted XML to stress-test parsers.
|
||||
Each corruption type simulates a real-world failure mode.
|
||||
"""
|
||||
base_valid = (
|
||||
'<hierarchy rotation="0">'
|
||||
'<node index="0" text="" resource-id="com.instagram.android:id/action_bar_root" '
|
||||
'class="android.widget.FrameLayout" package="com.instagram.android" '
|
||||
'content-desc="" checkable="false" checked="false" clickable="false" '
|
||||
'enabled="true" focusable="false" focused="false" scrollable="false" '
|
||||
'long-clickable="false" password="false" selected="false" '
|
||||
'bounds="[0,0][1080,2400]">'
|
||||
'<node index="0" text="Like" resource-id="com.instagram.android:id/row_feed_button_like" '
|
||||
'class="android.widget.ImageView" package="com.instagram.android" '
|
||||
'content-desc="Like" checkable="false" checked="false" clickable="true" '
|
||||
'enabled="true" focusable="true" focused="false" scrollable="false" '
|
||||
'long-clickable="false" password="false" selected="false" '
|
||||
'bounds="[50,500][150,600]" />'
|
||||
'</node>'
|
||||
'</hierarchy>'
|
||||
)
|
||||
|
||||
generators = {
|
||||
"EMPTY_STRING": lambda: "",
|
||||
"NONE_VALUE": lambda: None,
|
||||
"TRUNCATED_MID_TAG": lambda: base_valid[:len(base_valid) // 2],
|
||||
"UNICODE_INJECTION": lambda: base_valid.replace(
|
||||
'text="Like"',
|
||||
'text="L̵̡̧̢̛̛̛̘̗̣̥̱̲̲̝̪̣̗̝̠̫̲̤̱̪̞̻̙̜̺̩̰̫̝̥̩̭̩̫̦̠̦̣̣̬̤̤̠̗̣̲̬̟̣̰̝̥̤̜̻̫̙̥̘̻̝̯̗̼̣̮̲̻̝̹̩̗̥̖̝̝̪̣̜̜̱̣̱̻̮̬̮̬̗̖̟̩̭̜̀̀̈̀̀̀̑́̀̀̆̈́̐̑̈̈́̈́̉̿̈̉̆̂̃̉̆̉̑̉̈̊̏̀̒̌̽̈́̃̓̏̏͋̾̈́́̄̊̈́̽̅̒̓̈̈́̆̈̐̓̋̏̃͑̋̊̅̿̌̇̎̀̀̀̕̕̕͘̕̕̕̕̕͘͜͝͝i̷ke"'
|
||||
),
|
||||
"MASSIVE_DOM_10K_NODES": lambda: _generate_massive_dom(10000),
|
||||
"ZERO_SIZE_BOUNDS": lambda: base_valid.replace(
|
||||
'bounds="[50,500][150,600]"',
|
||||
'bounds="[500,500][500,500]"'
|
||||
),
|
||||
"NEGATIVE_COORDINATES": lambda: base_valid.replace(
|
||||
'bounds="[50,500][150,600]"',
|
||||
'bounds="[-100,-200][50,100]"'
|
||||
),
|
||||
"MISSING_CLOSING_TAGS": lambda: (
|
||||
'<hierarchy rotation="0">'
|
||||
'<node text="Like" clickable="true" bounds="[50,500][150,600]">'
|
||||
'<node text="nested">'
|
||||
# Intentionally missing closing tags
|
||||
),
|
||||
"RECURSIVE_NESTING_500_DEEP": lambda: _generate_deep_nesting(500),
|
||||
"NULL_BYTES": lambda: base_valid.replace("Like", "Li\x00ke\x00"),
|
||||
"MALFORMED_BOUNDS": lambda: base_valid.replace(
|
||||
'bounds="[50,500][150,600]"',
|
||||
'bounds="NOT_A_BOUND"'
|
||||
),
|
||||
"ONLY_WHITESPACE": lambda: " \n\t\n ",
|
||||
"HTML_NOT_XML": lambda: "<html><body><div>Not XML at all</div></body></html>",
|
||||
"BINARY_GARBAGE": lambda: bytes(range(256)).decode("latin-1"),
|
||||
"EXTREMELY_LONG_TEXT": lambda: base_valid.replace(
|
||||
'text="Like"',
|
||||
f'text="{"A" * 100000}"'
|
||||
),
|
||||
}
|
||||
|
||||
generator = generators.get(corruption_type)
|
||||
if generator is None:
|
||||
raise ValueError(f"Unknown corruption type: {corruption_type}")
|
||||
return generator()
|
||||
|
||||
|
||||
def _generate_massive_dom(count: int) -> str:
|
||||
"""Generates a valid XML with 'count' nodes to test performance bounds."""
|
||||
parts = ['<hierarchy rotation="0">']
|
||||
for i in range(count):
|
||||
parts.append(
|
||||
f'<node index="{i}" text="item_{i}" '
|
||||
f'resource-id="com.instagram.android:id/item_{i}" '
|
||||
f'class="android.widget.TextView" '
|
||||
f'package="com.instagram.android" '
|
||||
f'clickable="true" bounds="[0,{i}][100,{i+50}]" />'
|
||||
)
|
||||
parts.append('</hierarchy>')
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _generate_deep_nesting(depth: int) -> str:
|
||||
"""Generates deeply nested XML to test recursion limits."""
|
||||
xml = '<hierarchy rotation="0">'
|
||||
for i in range(depth):
|
||||
xml += f'<node index="{i}" text="level_{i}" class="android.widget.FrameLayout" '
|
||||
xml += f'package="com.instagram.android" bounds="[0,0][1080,2400]">'
|
||||
# Close all tags
|
||||
for _ in range(depth):
|
||||
xml += '</node>'
|
||||
xml += '</hierarchy>'
|
||||
return xml
|
||||
|
||||
|
||||
# Valid XML fixtures for property tests
|
||||
VALID_FEED_XML = (
|
||||
'<hierarchy rotation="0">'
|
||||
'<node index="0" text="" resource-id="com.instagram.android:id/action_bar_root" '
|
||||
'class="android.widget.FrameLayout" package="com.instagram.android" '
|
||||
'content-desc="" clickable="false" bounds="[0,0][1080,2400]">'
|
||||
'<node index="0" text="" resource-id="com.instagram.android:id/row_feed_button_like" '
|
||||
'class="android.widget.ImageView" package="com.instagram.android" '
|
||||
'content-desc="Like" clickable="true" bounds="[50,500][150,600]" />'
|
||||
'<node index="1" text="" resource-id="com.instagram.android:id/row_feed_button_comment" '
|
||||
'class="android.widget.ImageView" package="com.instagram.android" '
|
||||
'content-desc="Comment" clickable="true" bounds="[200,500][300,600]" />'
|
||||
'<node index="2" text="Follow" resource-id="com.instagram.android:id/profile_header_follow_button" '
|
||||
'class="android.widget.Button" package="com.instagram.android" '
|
||||
'content-desc="Follow" clickable="true" bounds="[800,300][1000,380]" />'
|
||||
'<node index="3" text="" resource-id="com.instagram.android:id/feed_tab" '
|
||||
'class="android.widget.ImageView" package="com.instagram.android" '
|
||||
'content-desc="Home" clickable="true" selected="true" bounds="[0,2300][216,2400]" />'
|
||||
'<node index="4" text="" resource-id="com.instagram.android:id/search_tab" '
|
||||
'class="android.widget.ImageView" package="com.instagram.android" '
|
||||
'content-desc="Search and explore" clickable="true" bounds="[216,2300][432,2400]" />'
|
||||
'</node>'
|
||||
'</hierarchy>'
|
||||
)
|
||||
195
tests/chaos/test_chaos_network.py
Normal file
195
tests/chaos/test_chaos_network.py
Normal file
@@ -0,0 +1,195 @@
|
||||
"""
|
||||
Chaos Engineering: Network & Dependency Failure Tests.
|
||||
|
||||
Verifies that the bot degrades gracefully when external services
|
||||
(Qdrant, Ollama, OpenRouter) are unavailable, slow, or return errors.
|
||||
|
||||
Tesla's FSD doesn't crash if the map server is unreachable — neither should we.
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch, PropertyMock
|
||||
from tests.chaos import VALID_FEED_XML
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────
|
||||
# Qdrant Failure Tests
|
||||
# ──────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.chaos
|
||||
class TestQdrantFailure:
|
||||
"""Bot must survive total Qdrant outage."""
|
||||
|
||||
def test_telepathic_works_without_qdrant(self):
|
||||
"""TelepathicEngine must still resolve nodes via keyword fast-path when Qdrant is down."""
|
||||
with patch("GramAddict.core.qdrant_memory.QdrantBase.__init__", return_value=None), \
|
||||
patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=lambda: property(lambda self: False)):
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
TelepathicEngine._instance = None
|
||||
engine = TelepathicEngine.__new__(TelepathicEngine)
|
||||
engine.ui_memory = MagicMock()
|
||||
engine.ui_memory.is_connected = False
|
||||
engine.ui_memory.query_closest = MagicMock(return_value=None)
|
||||
engine.positive_memory = MagicMock()
|
||||
engine.positive_memory.is_connected = False
|
||||
engine.positive_memory.recall = MagicMock(return_value=None)
|
||||
engine._edge_model = None
|
||||
engine._edge_tokenizer = None
|
||||
|
||||
nodes = engine._extract_semantic_nodes(VALID_FEED_XML)
|
||||
# Should still find clickable nodes via structural parsing
|
||||
assert len(nodes) > 0
|
||||
TelepathicEngine._instance = None
|
||||
|
||||
def test_sae_recall_returns_none_without_qdrant(self):
|
||||
"""SAE episodic memory must return None (not crash) when Qdrant is down."""
|
||||
with patch("GramAddict.core.qdrant_memory.QdrantBase.__init__", return_value=None), \
|
||||
patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=lambda: property(lambda self: False)):
|
||||
from GramAddict.core.situational_awareness import SituationEpisodeDB
|
||||
db = SituationEpisodeDB()
|
||||
db._db = MagicMock()
|
||||
db._db.is_connected = False
|
||||
|
||||
result = db.recall("test_situation_signature")
|
||||
assert result is None
|
||||
|
||||
def test_sae_learn_silently_fails_without_qdrant(self):
|
||||
"""SAE learning must silently skip (not crash) when Qdrant is down."""
|
||||
with patch("GramAddict.core.qdrant_memory.QdrantBase.__init__", return_value=None), \
|
||||
patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=lambda: property(lambda self: False)):
|
||||
from GramAddict.core.situational_awareness import SituationEpisodeDB, EscapeAction
|
||||
db = SituationEpisodeDB()
|
||||
db._db = MagicMock()
|
||||
db._db.is_connected = False
|
||||
|
||||
action = EscapeAction("back", reason="test")
|
||||
# Must not raise
|
||||
db.learn("test_signature", action, True)
|
||||
|
||||
|
||||
def test_qdrant_timeout_doesnt_hang_extraction(self):
|
||||
"""If Qdrant queries time out, node extraction must still complete."""
|
||||
import time
|
||||
|
||||
with patch("GramAddict.core.qdrant_memory.QdrantBase.__init__", return_value=None), \
|
||||
patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=lambda: property(lambda self: False)):
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
TelepathicEngine._instance = None
|
||||
engine = TelepathicEngine.__new__(TelepathicEngine)
|
||||
engine.ui_memory = MagicMock()
|
||||
engine.ui_memory.is_connected = False
|
||||
engine.ui_memory.query_closest = MagicMock(side_effect=TimeoutError("Qdrant timeout"))
|
||||
engine.positive_memory = MagicMock()
|
||||
engine.positive_memory.is_connected = False
|
||||
engine.positive_memory.recall = MagicMock(side_effect=TimeoutError("Qdrant timeout"))
|
||||
engine._edge_model = None
|
||||
engine._edge_tokenizer = None
|
||||
|
||||
start = time.time()
|
||||
nodes = engine._extract_semantic_nodes(VALID_FEED_XML)
|
||||
elapsed = time.time() - start
|
||||
|
||||
assert elapsed < 5.0
|
||||
assert isinstance(nodes, list)
|
||||
TelepathicEngine._instance = None
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────
|
||||
# LLM (Ollama/OpenRouter) Failure Tests
|
||||
# ──────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.chaos
|
||||
class TestLLMFailure:
|
||||
"""Bot must survive LLM outages."""
|
||||
|
||||
def test_sae_perceive_defaults_to_normal_on_llm_failure(self):
|
||||
"""If LLM classification fails, SAE must default to NORMAL (safe fallback)."""
|
||||
from GramAddict.core.situational_awareness import SituationalAwarenessEngine, SituationType
|
||||
SituationalAwarenessEngine.reset()
|
||||
|
||||
device = MagicMock()
|
||||
device.app_id = "com.instagram.android"
|
||||
device.deviceV2 = MagicMock()
|
||||
device.deviceV2.info = {"screenOn": True}
|
||||
|
||||
sae = SituationalAwarenessEngine(device)
|
||||
sae.episodes = MagicMock()
|
||||
sae.episodes.recall = MagicMock(return_value=None)
|
||||
|
||||
with patch("GramAddict.core.qdrant_memory.ScreenMemoryDB") as MockScreenDB:
|
||||
mock_screen_db = MagicMock()
|
||||
mock_screen_db.get_screen_type = MagicMock(return_value=None)
|
||||
MockScreenDB.return_value = mock_screen_db
|
||||
|
||||
with patch("GramAddict.core.llm_provider.query_telepathic_llm", side_effect=ConnectionError("Ollama down")):
|
||||
result = sae.perceive(VALID_FEED_XML)
|
||||
# Must default to NORMAL, not crash
|
||||
assert result == SituationType.NORMAL
|
||||
|
||||
SituationalAwarenessEngine.reset()
|
||||
|
||||
def test_sae_escape_planning_defaults_to_back_on_llm_failure(self):
|
||||
"""If LLM escape planning fails, SAE must default to BACK press."""
|
||||
from GramAddict.core.situational_awareness import SituationalAwarenessEngine, SituationType
|
||||
SituationalAwarenessEngine.reset()
|
||||
|
||||
device = MagicMock()
|
||||
device.app_id = "com.instagram.android"
|
||||
device.deviceV2 = MagicMock()
|
||||
device.deviceV2.info = {"screenOn": True}
|
||||
|
||||
sae = SituationalAwarenessEngine(device)
|
||||
|
||||
with patch("GramAddict.core.llm_provider.query_llm", side_effect=ConnectionError("LLM down")):
|
||||
action = sae._plan_escape_via_llm(
|
||||
VALID_FEED_XML, "compressed_sig", SituationType.OBSTACLE_MODAL
|
||||
)
|
||||
assert action.action_type == "back"
|
||||
assert "failed" in action.reason.lower() or "default" in action.reason.lower()
|
||||
|
||||
SituationalAwarenessEngine.reset()
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────
|
||||
# Active Inference Resilience
|
||||
# ──────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.chaos
|
||||
class TestActiveInferenceChaos:
|
||||
"""Active Inference engine must survive edge cases."""
|
||||
|
||||
def test_evaluate_with_empty_history(self):
|
||||
"""Evaluating without any predictions must return True (no-op)."""
|
||||
from GramAddict.core.active_inference import ActiveInferenceEngine
|
||||
ai = ActiveInferenceEngine("test_user")
|
||||
assert ai.evaluate_prediction("<hierarchy/>") is True
|
||||
|
||||
def test_extreme_free_energy_doesnt_overflow(self):
|
||||
"""Repeated errors must not cause float overflow."""
|
||||
from GramAddict.core.active_inference import ActiveInferenceEngine
|
||||
ai = ActiveInferenceEngine("test_user")
|
||||
|
||||
for _ in range(1000):
|
||||
ai.predict_state(["nonexistent_element"])
|
||||
ai.evaluate_prediction("<hierarchy><node text='wrong'/></hierarchy>")
|
||||
|
||||
assert ai.free_energy < float('inf')
|
||||
assert ai.free_energy >= 0
|
||||
|
||||
def test_surprise_with_identical_prediction_is_zero(self):
|
||||
"""Perfect prediction (predicted == observed) must produce near-zero surprise."""
|
||||
from GramAddict.core.active_inference import ActiveInferenceEngine
|
||||
ai = ActiveInferenceEngine("test_user")
|
||||
ai.free_energy = 0.0
|
||||
|
||||
result = ai.calculate_surprise(1.0, 1.0)
|
||||
assert result < 0.1 # Near-zero free energy
|
||||
|
||||
def test_sleep_modifier_bounds(self):
|
||||
"""Sleep modifier must always be between 1.0 and 5.0."""
|
||||
from GramAddict.core.active_inference import ActiveInferenceEngine
|
||||
ai = ActiveInferenceEngine("test_user")
|
||||
|
||||
for policy in ["STABLE", "CAUTIOUS", "DORMANT"]:
|
||||
ai.policy = policy
|
||||
mod = ai.get_sleep_modifier()
|
||||
assert 1.0 <= mod <= 5.0
|
||||
213
tests/chaos/test_chaos_xml_corruption.py
Normal file
213
tests/chaos/test_chaos_xml_corruption.py
Normal file
@@ -0,0 +1,213 @@
|
||||
"""
|
||||
Chaos Engineering: XML Corruption Resilience Tests for TelepathicEngine + SAE.
|
||||
|
||||
Verifies that NEITHER engine crashes on any form of corrupted, truncated,
|
||||
adversarial, or garbage XML input. They must degrade gracefully (return None
|
||||
or empty lists) without raising unhandled exceptions.
|
||||
|
||||
These tests are the "crash barrier" of autonomous navigation — ensuring that
|
||||
no matter what Android dumps to us, the bot survives and recovers.
|
||||
"""
|
||||
import pytest
|
||||
import time
|
||||
from unittest.mock import MagicMock, patch
|
||||
from tests.chaos import generate_corrupted_xml
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────
|
||||
# Telepathic Engine Chaos Tests
|
||||
# ──────────────────────────────────────────────────
|
||||
|
||||
@pytest.fixture
|
||||
def telepathic_engine():
|
||||
"""Creates a real TelepathicEngine instance with mocked Qdrant."""
|
||||
with patch("GramAddict.core.qdrant_memory.QdrantBase.__init__", return_value=None), \
|
||||
patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=lambda: property(lambda self: False)):
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
TelepathicEngine._instance = None
|
||||
engine = TelepathicEngine.__new__(TelepathicEngine)
|
||||
engine.ui_memory = MagicMock()
|
||||
engine.ui_memory.is_connected = False
|
||||
engine.ui_memory.query_closest = MagicMock(return_value=None)
|
||||
engine.positive_memory = MagicMock()
|
||||
engine.positive_memory.is_connected = False
|
||||
engine.positive_memory.recall = MagicMock(return_value=None)
|
||||
engine._edge_model = None
|
||||
engine._edge_tokenizer = None
|
||||
yield engine
|
||||
TelepathicEngine._instance = None
|
||||
|
||||
|
||||
ALL_CORRUPTION_TYPES = [
|
||||
"EMPTY_STRING",
|
||||
"NONE_VALUE",
|
||||
"TRUNCATED_MID_TAG",
|
||||
"UNICODE_INJECTION",
|
||||
"MASSIVE_DOM_10K_NODES",
|
||||
"ZERO_SIZE_BOUNDS",
|
||||
"NEGATIVE_COORDINATES",
|
||||
"MISSING_CLOSING_TAGS",
|
||||
"RECURSIVE_NESTING_500_DEEP",
|
||||
"NULL_BYTES",
|
||||
"MALFORMED_BOUNDS",
|
||||
"ONLY_WHITESPACE",
|
||||
"HTML_NOT_XML",
|
||||
"BINARY_GARBAGE",
|
||||
"EXTREMELY_LONG_TEXT",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.chaos
|
||||
class TestTelepathicEngineChaos:
|
||||
"""Telepathic Engine must NEVER crash on corrupted XML."""
|
||||
|
||||
@pytest.mark.parametrize("corruption_type", ALL_CORRUPTION_TYPES)
|
||||
def test_extract_semantic_nodes_survives(self, telepathic_engine, corruption_type):
|
||||
"""Engine's XML parser must return empty list on any corruption."""
|
||||
xml = generate_corrupted_xml(corruption_type)
|
||||
|
||||
# Must NOT raise. May return empty list.
|
||||
if xml is None:
|
||||
# None input — directly test defense
|
||||
result = telepathic_engine._extract_semantic_nodes("")
|
||||
else:
|
||||
result = telepathic_engine._extract_semantic_nodes(xml)
|
||||
|
||||
assert isinstance(result, list)
|
||||
|
||||
@pytest.mark.parametrize("corruption_type", [
|
||||
"EMPTY_STRING", "NONE_VALUE", "TRUNCATED_MID_TAG",
|
||||
"MISSING_CLOSING_TAGS", "ONLY_WHITESPACE", "HTML_NOT_XML",
|
||||
"BINARY_GARBAGE",
|
||||
])
|
||||
def test_find_best_node_survives_garbage(self, telepathic_engine, corruption_type):
|
||||
"""find_best_node must return None on garbage XML, never crash."""
|
||||
xml = generate_corrupted_xml(corruption_type)
|
||||
if xml is None:
|
||||
xml = ""
|
||||
|
||||
result = telepathic_engine._find_best_node_inner(
|
||||
xml, "tap like button", min_confidence=0.82
|
||||
)
|
||||
# Must be None or a dict, never an exception
|
||||
assert result is None or isinstance(result, dict)
|
||||
|
||||
def test_unicode_injection_doesnt_corrupt_semantics(self, telepathic_engine):
|
||||
"""Zalgo text in nodes shouldn't crash semantic extraction."""
|
||||
xml = generate_corrupted_xml("UNICODE_INJECTION")
|
||||
nodes = telepathic_engine._extract_semantic_nodes(xml)
|
||||
# Should extract SOME nodes (the XML structure is valid)
|
||||
assert isinstance(nodes, list)
|
||||
# If nodes found, they should have valid coordinates
|
||||
for node in nodes:
|
||||
assert isinstance(node.get("x", 0), int)
|
||||
assert isinstance(node.get("y", 0), int)
|
||||
|
||||
def test_massive_dom_doesnt_hang(self, telepathic_engine):
|
||||
"""10K nodes must be parsed within 5 seconds — no infinite loops."""
|
||||
xml = generate_corrupted_xml("MASSIVE_DOM_10K_NODES")
|
||||
start = time.time()
|
||||
nodes = telepathic_engine._extract_semantic_nodes(xml)
|
||||
elapsed = time.time() - start
|
||||
|
||||
assert elapsed < 5.0, f"Parsing 10K nodes took {elapsed:.2f}s (limit: 5s)"
|
||||
assert isinstance(nodes, list)
|
||||
|
||||
def test_deep_nesting_doesnt_stackoverflow(self, telepathic_engine):
|
||||
"""500 levels of nesting must not cause stack overflow."""
|
||||
xml = generate_corrupted_xml("RECURSIVE_NESTING_500_DEEP")
|
||||
# This would crash Python's default recursion limit (1000) if
|
||||
# we used recursive parsing. ElementTree uses iterative parsing,
|
||||
# so it should survive.
|
||||
nodes = telepathic_engine._extract_semantic_nodes(xml)
|
||||
assert isinstance(nodes, list)
|
||||
|
||||
def test_null_bytes_stripped(self, telepathic_engine):
|
||||
"""Null bytes in text content must not cause parsing failures."""
|
||||
xml = generate_corrupted_xml("NULL_BYTES")
|
||||
nodes = telepathic_engine._extract_semantic_nodes(xml)
|
||||
assert isinstance(nodes, list)
|
||||
# Verify no null bytes leaked into node semantics
|
||||
for node in nodes:
|
||||
assert "\x00" not in node.get("semantic_string", "")
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────
|
||||
# SAE (Situational Awareness Engine) Chaos Tests
|
||||
# ──────────────────────────────────────────────────
|
||||
|
||||
@pytest.fixture
|
||||
def sae_engine():
|
||||
"""Creates a SAE instance with mocked device."""
|
||||
from GramAddict.core.situational_awareness import SituationalAwarenessEngine
|
||||
SituationalAwarenessEngine.reset()
|
||||
|
||||
device = MagicMock()
|
||||
device.app_id = "com.instagram.android"
|
||||
device.deviceV2 = MagicMock()
|
||||
device.deviceV2.info = {"screenOn": True}
|
||||
|
||||
engine = SituationalAwarenessEngine(device)
|
||||
|
||||
# Mock the episode DB to avoid Qdrant dependency
|
||||
engine.episodes = MagicMock()
|
||||
engine.episodes.recall = MagicMock(return_value=None)
|
||||
engine.episodes.learn = MagicMock()
|
||||
|
||||
yield engine
|
||||
SituationalAwarenessEngine.reset()
|
||||
|
||||
|
||||
@pytest.mark.chaos
|
||||
class TestSAEChaos:
|
||||
"""SAE perception must be bulletproof against XML corruption."""
|
||||
|
||||
@pytest.mark.parametrize("corruption_type", [
|
||||
"EMPTY_STRING", "TRUNCATED_MID_TAG", "MISSING_CLOSING_TAGS",
|
||||
"ONLY_WHITESPACE", "HTML_NOT_XML", "BINARY_GARBAGE",
|
||||
])
|
||||
def test_compress_xml_survives_garbage(self, sae_engine, corruption_type):
|
||||
"""XML compression must never crash, even on garbage."""
|
||||
xml = generate_corrupted_xml(corruption_type)
|
||||
if xml is None:
|
||||
xml = ""
|
||||
|
||||
result = sae_engine._compress_xml(xml)
|
||||
assert isinstance(result, str)
|
||||
assert len(result) > 0 # Should always return something
|
||||
|
||||
def test_compress_empty_returns_marker(self, sae_engine):
|
||||
"""Empty/None input must return 'EMPTY_SCREEN' sentinel."""
|
||||
assert sae_engine._compress_xml("") == "EMPTY_SCREEN"
|
||||
assert sae_engine._compress_xml(None) == "EMPTY_SCREEN"
|
||||
|
||||
@pytest.mark.parametrize("corruption_type", [
|
||||
"EMPTY_STRING", "TRUNCATED_MID_TAG", "BINARY_GARBAGE", "ONLY_WHITESPACE",
|
||||
])
|
||||
def test_perceive_survives_garbage(self, sae_engine, corruption_type):
|
||||
"""perceive() must return a valid SituationType on any input."""
|
||||
from GramAddict.core.situational_awareness import SituationType
|
||||
xml = generate_corrupted_xml(corruption_type)
|
||||
if xml is None:
|
||||
xml = ""
|
||||
|
||||
result = sae_engine.perceive(xml)
|
||||
assert isinstance(result, SituationType)
|
||||
|
||||
def test_compute_situation_hash_is_deterministic(self, sae_engine):
|
||||
"""Same XML must always produce the same hash."""
|
||||
xml = generate_corrupted_xml("UNICODE_INJECTION")
|
||||
compressed = sae_engine._compress_xml(xml)
|
||||
hash1 = sae_engine._compute_situation_hash(compressed)
|
||||
hash2 = sae_engine._compute_situation_hash(compressed)
|
||||
assert hash1 == hash2
|
||||
|
||||
def test_massive_dom_compression_is_bounded(self, sae_engine):
|
||||
"""10K nodes must be compressed to < 3000 chars (the cap)."""
|
||||
xml = generate_corrupted_xml("MASSIVE_DOM_10K_NODES")
|
||||
start = time.time()
|
||||
result = sae_engine._compress_xml(xml)
|
||||
elapsed = time.time() - start
|
||||
|
||||
assert len(result) <= 3000, f"Compressed output is {len(result)} chars (limit: 3000)"
|
||||
assert elapsed < 5.0, f"Compression took {elapsed:.2f}s"
|
||||
Reference in New Issue
Block a user