#!/usr/bin/env python3
"""
Test SPEC-TOKEN-002 append_entry token optimization.
Tests the new format without requiring full MCP server restart.
"""
import sys
from pathlib import Path
# Add MCP_SPINE root to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from scribe_mcp.utils.response import ResponseFormatter
def test_append_entry_optimization():
"""Test the optimized append_entry readable format."""
formatter = ResponseFormatter()
# Test case 1: Simple entry with default metadata only
data_simple = {
'ok': True,
'written_line': '[ℹ️] [2026-01-07 23:10:45 UTC] [Agent: CoderAgent] [Project: test_project] Simple test message | priority=low; log_type=progress; content_type=log',
'path': '/home/austin/projects/MCP_SPINE/scribe_mcp/.scribe/docs/dev_plans/test_project/PROGRESS_LOG.md',
'meta': {}
}
# Test case 2: Entry with custom metadata
data_with_meta = {
'ok': True,
'written_line': '[✅] [2026-01-07 23:10:45 UTC] [Agent: CoderAgent-TOKEN002] [Project: test_project] Implemented feature X successfully | spec=SPEC-TOKEN-002; phase=implementation; confidence=0.95; priority=low; log_type=progress; content_type=log',
'path': '/home/austin/projects/MCP_SPINE/scribe_mcp/.scribe/docs/dev_plans/test_project/PROGRESS_LOG.md',
'meta': {'spec': 'SPEC-TOKEN-002', 'phase': 'implementation', 'confidence': 0.95}
}
# Test case 3: Entry with reasoning block
data_with_reasoning = {
'ok': True,
'written_line': '[ℹ️] [2026-01-07 14:34:25 UTC] [Agent: ResearchAgent] [Project: research_project] Investigation complete | phase=research; priority=low; log_type=progress; content_type=log',
'path': '/home/austin/.scribe/docs/dev_plans/research_project/PROGRESS_LOG.md',
'meta': {
'reasoning': {
'why': 'Need to understand append_entry structure',
'what': 'Analyzed return values, usage patterns',
'how': 'Read source code, traced execution paths'
}
}
}
# Test case 4: Entry with unicode
data_with_unicode = {
'ok': True,
'written_line': '[ℹ️] [2026-01-07 23:10:45 UTC] [Agent: TestAgent] [Project: test_project] Unicode test message | unicode_test=日本語🎯; test_type=unicode; priority=low; log_type=progress; content_type=log',
'path': '/home/austin/.scribe/docs/dev_plans/test_project/PROGRESS_LOG.md',
'meta': {'unicode_test': '日本語🎯', 'test_type': 'unicode'}
}
print("=" * 80)
print("SPEC-TOKEN-002 APPEND_ENTRY OPTIMIZATION TEST")
print("=" * 80)
print()
print("Test Case 1: Simple entry (default metadata only)")
print("-" * 80)
result1 = formatter.format_readable_append_entry(data_simple)
print(result1)
print()
print(f"Token count estimate: ~{len(result1.split())} words")
print()
print("Test Case 2: Entry with custom metadata")
print("-" * 80)
result2 = formatter.format_readable_append_entry(data_with_meta)
print(result2)
print()
print(f"Token count estimate: ~{len(result2.split())} words")
print()
print("Test Case 3: Entry with reasoning block")
print("-" * 80)
result3 = formatter.format_readable_append_entry(data_with_reasoning)
print(result3)
print()
print(f"Token count estimate: ~{len(result3.split())} words")
print()
print("Test Case 4: Entry with unicode")
print("-" * 80)
result4 = formatter.format_readable_append_entry(data_with_unicode)
print(result4)
print()
print(f"Token count estimate: ~{len(result4.split())} words")
print()
print("=" * 80)
print("OPTIMIZATION SUMMARY")
print("=" * 80)
print("✅ Removed: 'Entry written to progress log (project_name)' prefix")
print("✅ Shortened: Timestamp from full ISO to HH:MM UTC")
print("✅ Removed: Bracketed labels [ℹ️], [Agent:], [Project:]")
print("✅ Simplified: Path to just filename (PROGRESS_LOG.md)")
print("✅ Filtered: Default metadata (priority=low, log_type=progress, content_type=log)")
print()
print("Expected token reduction: ~37% (from 136 to 85 tokens)")
print()
if __name__ == '__main__':
test_append_entry_optimization()