156 lines
5.2 KiB
Python
Executable File
156 lines
5.2 KiB
Python
Executable File
"""
|
|
Tests for SEE — Symbolic Execution Envelope.
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
|
|
|
from execute_compressed.see import (
|
|
SymbolicExecutionEnvelope,
|
|
wrap_code,
|
|
unwrap_envelope,
|
|
execute_with_envelope,
|
|
)
|
|
|
|
passed = 0
|
|
failed = 0
|
|
|
|
|
|
def test(name: str, ok: bool):
|
|
global passed, failed
|
|
if ok:
|
|
passed += 1
|
|
print(f" ✅ PASS: {name}")
|
|
else:
|
|
failed += 1
|
|
print(f" ❌ FAIL: {name}")
|
|
|
|
|
|
print("=" * 60)
|
|
print("SEE — Symbolic Execution Envelope Tests")
|
|
print("=" * 60)
|
|
|
|
# Test 1: Build envelope
|
|
code = b"print('hello world')"
|
|
envelope = SymbolicExecutionEnvelope.build(code=code, glyph_ids=["G001"])
|
|
test("Build envelope", envelope.code == code)
|
|
test("Envelope has glyph_ids", envelope.glyph_ids == ["G001"])
|
|
test("Envelope has integrity_hash", len(envelope.integrity_hash) == 32)
|
|
test("Envelope has invocation_id", len(envelope.invocation_id) > 0)
|
|
test("Envelope built_at > 0", envelope.built_at > 0)
|
|
|
|
# Test 2: Default values
|
|
env2 = SymbolicExecutionEnvelope.build(code=b"test")
|
|
test("Default glyph_ids is empty list", env2.glyph_ids == [])
|
|
test("Default manifest is empty dict", env2.manifest == {})
|
|
test("Default mode is symbolic", env2.mode == "symbolic")
|
|
|
|
# Test 3: Integrity verification
|
|
test("Integrity passes for unmodified envelope", envelope.verify_integrity())
|
|
env2.code = b"tampered"
|
|
test("Integrity fails for tampered envelope", not env2.verify_integrity())
|
|
|
|
# Test 4: Resonance map from glyph_context top-level keys
|
|
env3 = SymbolicExecutionEnvelope.build(
|
|
code=b"test",
|
|
glyph_ids=["G001"],
|
|
glyph_context={"G001": {"resonance_weight": 0.85}},
|
|
)
|
|
test("Resonance map has G001", "G001" in env3.resonance_map)
|
|
test("Resonance weight from top-level context", abs(env3.resonance_map["G001"] - 0.85) < 0.001)
|
|
|
|
# Test 4b: Resonance map from nested glyphs key
|
|
env3b = SymbolicExecutionEnvelope.build(
|
|
code=b"test",
|
|
glyph_ids=["G001"],
|
|
glyph_context={"glyphs": {"G001": {"resonance_weight": 0.75}}},
|
|
)
|
|
test("Resonance map from nested glyphs", "G001" in env3b.resonance_map)
|
|
test("Resonance weight from nested", abs(env3b.resonance_map["G001"] - 0.75) < 0.001)
|
|
|
|
# Test 5: wrap_code convenience
|
|
env4 = wrap_code(b"hello", glyph_ids=["G015", "G042"])
|
|
test("wrap_code returns SymbolicExecutionEnvelope", isinstance(env4, SymbolicExecutionEnvelope))
|
|
test("wrap_code preserves code", env4.code == b"hello")
|
|
test("wrap_code preserves glyph_ids", env4.glyph_ids == ["G015", "G042"])
|
|
|
|
# Test 6: unwrap_envelope
|
|
code_out, context_out, glyph_ids_out = unwrap_envelope(envelope)
|
|
test("unwrap returns code bytes", code_out == b"print('hello world')")
|
|
test("unwrap returns glyph_ids", glyph_ids_out == ["G001"])
|
|
test("unwrap context has mode", context_out["mode"] == "symbolic")
|
|
test("unwrap context has resonance_map", "resonance_map" in context_out)
|
|
|
|
# Test 7: resolve_glyph_context
|
|
test("resolve_glyph_context returns None when no context set",
|
|
envelope.resolve_glyph_context("G001") is None)
|
|
test("resolve_glyph_context returns None for unknown",
|
|
envelope.resolve_glyph_context("G999") is None)
|
|
# Test with explicit glyph context
|
|
ctx_with_data = env3.resolve_glyph_context("G001")
|
|
test("resolve_glyph_context with glyph_context data",
|
|
ctx_with_data is not None and ctx_with_data["glyph_id"] == "G001")
|
|
|
|
# Test 8: Glyph context from nested structure
|
|
env5 = SymbolicExecutionEnvelope.build(
|
|
code=b"test",
|
|
glyph_ids=["G001"],
|
|
glyph_context={
|
|
"glyphs": {
|
|
"G001": {"resonance_weight": 0.9, "name": "Ledo"},
|
|
}
|
|
},
|
|
)
|
|
resolved = env5.resolve_glyph_context("G001")
|
|
test("resolve_glyph_context works with nested glyphs",
|
|
resolved is not None and resolved["glyph_id"] == "G001")
|
|
|
|
# Test 9: to_dict serialization
|
|
d = envelope.to_dict()
|
|
test("to_dict has code_size", d["code_size"] == len(code))
|
|
test("to_dict has glyph_ids", d["glyph_ids"] == ["G001"])
|
|
test("to_dict has integrity_hash", d["integrity_hash"] == envelope.integrity_hash)
|
|
|
|
# Test 10: execute_with_envelope - integrity failure
|
|
tampered = SymbolicExecutionEnvelope(
|
|
code=b"tampered",
|
|
manifest={},
|
|
glyph_context={},
|
|
glyph_ids=[],
|
|
resonance_map={},
|
|
mode="symbolic",
|
|
epoch=None,
|
|
invocation_id="bad",
|
|
chain_label=None,
|
|
integrity_hash="00000000000000000000000000000000",
|
|
built_at=0.0,
|
|
metadata={},
|
|
)
|
|
result = execute_with_envelope(tampered)
|
|
test("execute_with_envelope detects tampering",
|
|
"integrity" in result.get("diagnostics", {}).get("error", ""))
|
|
|
|
# Test 11: execute_with_envelope with valid code
|
|
try:
|
|
env_valid = SymbolicExecutionEnvelope.build(
|
|
code=b"Hello from SEE envelope test",
|
|
glyph_ids=["G001"],
|
|
metadata={"invocation_id": "test-001"},
|
|
)
|
|
result = execute_with_envelope(env_valid)
|
|
has_output = bool(result.get("output_text"))
|
|
test("execute_with_envelope returns output", has_output)
|
|
except Exception as e:
|
|
test(f"execute_with_envelope did not crash ({e})", False)
|
|
|
|
# Summary
|
|
print()
|
|
print("=" * 60)
|
|
print(f"Results: {passed} passed, {failed} failed, {passed + failed} total")
|
|
if failed == 0:
|
|
print("✅ ALL SEE TESTS PASSED")
|
|
else:
|
|
print(f"❌ {failed} TEST(S) FAILED")
|
|
sys.exit(0 if failed == 0 else 1)
|