"""Test: verify cache survives multiple analyze calls and produces consistent results."""
import sys
sys.path.insert(0, 'c:\\src\\____GitProjects\\SXXXXXXX_PyUcc')

from pathlib import Path
import tempfile
import hashlib

# Create test file
test_content = '''"""Test file."""
def func():
    return 42
'''

temp_file = Path(tempfile.mktemp(suffix=".py"))
temp_file.write_text(test_content, encoding='utf-8')
content_hash = hashlib.md5(test_content.encode()).hexdigest()

print(f"Test file: {temp_file}")
print(f"Content hash: {content_hash}")
print()

from pyucc.core.countings_impl import analyze_file_counts, _COUNTING_CACHE

# First call - should miss cache and run pygount
print("=" * 60)
print("CALL 1: Should be CACHE MISS")
print("=" * 60)
result1 = analyze_file_counts(temp_file)
print(f"Result: code={result1['code_lines']}, comment={result1['comment_lines']}, blank={result1['blank_lines']}")
print(f"Cache size: {len(_COUNTING_CACHE)}")
print()

# Second call - should hit cache
print("=" * 60)
print("CALL 2: Should be CACHE HIT")
print("=" * 60)
result2 = analyze_file_counts(temp_file)
print(f"Result: code={result2['code_lines']}, comment={result2['comment_lines']}, blank={result2['blank_lines']}")
print(f"Cache size: {len(_COUNTING_CACHE)}")
print()

# Verify results are identical
print("=" * 60)
print("VERIFICATION")
print("=" * 60)
if result1 == result2:
    print("✅ Results are IDENTICAL")
else:
    print("❌ Results DIFFER:")
    for key in result1:
        if result1[key] != result2[key]:
            print(f"  {key}: {result1[key]} != {result2[key]}")

# Check cache content
print()
print(f"Cache contains {len(_COUNTING_CACHE)} entries")
if content_hash in _COUNTING_CACHE:
    cached = _COUNTING_CACHE[content_hash]
    print(f"✅ Content hash {content_hash[:8]} found in cache")
    print(f"   Cached: code={cached['code_lines']}, comment={cached['comment_lines']}, blank={cached['blank_lines']}")
else:
    print(f"❌ Content hash {content_hash[:8]} NOT in cache!")

# Cleanup
temp_file.unlink()

print()
print("CONCLUSION:")
print("If both calls produce identical results and cache works,")
print("then the problem is NOT in the counting logic itself.")
