test: fix cache validator tests to account for new hash_status field and side effects

This commit is contained in:
Will Miao
2026-03-18 21:10:56 +08:00
parent 7e87ec9521
commit 75dccaef87
2 changed files with 19 additions and 10 deletions

View File

@@ -91,17 +91,21 @@ class CacheEntryValidator:
errors: List[str] = [] errors: List[str] = []
repaired = False repaired = False
# If auto_repair is on, we work on a copy. If not, we still need a safe way to check fields.
working_entry = dict(entry) if auto_repair else entry working_entry = dict(entry) if auto_repair else entry
# First, ensure hash_status is present as it's used to validate sha256 # Determine effective hash_status for validation logic
hash_status = working_entry.get('hash_status') hash_status = entry.get('hash_status')
if hash_status is None: if hash_status is None:
if auto_repair:
working_entry['hash_status'] = 'completed' working_entry['hash_status'] = 'completed'
repaired = True repaired = True
hash_status = 'completed' hash_status = 'completed'
for field_name, (default_value, is_required) in cls.CORE_FIELDS.items(): for field_name, (default_value, is_required) in cls.CORE_FIELDS.items():
value = working_entry.get(field_name) # Get current value from the original entry to avoid side effects during validation
value = entry.get(field_name)
# Check if field is missing or None # Check if field is missing or None
if value is None: if value is None:
@@ -146,11 +150,10 @@ class CacheEntryValidator:
# Special validation: sha256 must not be empty for required field # Special validation: sha256 must not be empty for required field
# BUT allow empty sha256 when hash_status is pending (lazy hash calculation) # BUT allow empty sha256 when hash_status is pending (lazy hash calculation)
sha256 = working_entry.get('sha256', '') sha256 = working_entry.get('sha256', '')
# Re-fetch hash_status in case it was changed during loop # Use the effective hash_status we determined earlier
current_hash_status = working_entry.get('hash_status', 'completed')
if not sha256 or (isinstance(sha256, str) and not sha256.strip()): if not sha256 or (isinstance(sha256, str) and not sha256.strip()):
# Allow empty sha256 for lazy hash calculation (checkpoints) # Allow empty sha256 for lazy hash calculation (checkpoints)
if current_hash_status != 'pending': if hash_status != 'pending':
errors.append("Required field 'sha256' is empty") errors.append("Required field 'sha256' is empty")
# Cannot repair empty sha256 - entry is invalid # Cannot repair empty sha256 - entry is invalid
return ValidationResult( return ValidationResult(
@@ -164,8 +167,13 @@ class CacheEntryValidator:
if isinstance(sha256, str): if isinstance(sha256, str):
normalized_sha = sha256.lower().strip() normalized_sha = sha256.lower().strip()
if normalized_sha != sha256: if normalized_sha != sha256:
if auto_repair:
working_entry['sha256'] = normalized_sha working_entry['sha256'] = normalized_sha
repaired = True repaired = True
else:
# If not auto-repairing, we don't consider case difference as a "critical error"
# that invalidates the entry, but we also don't mark it repaired.
pass
# Determine if entry is valid # Determine if entry is valid
# Entry is valid if no critical required field errors remain after repair # Entry is valid if no critical required field errors remain after repair

View File

@@ -194,6 +194,7 @@ class TestCacheHealthMonitor:
'preview_nsfw_level': 0, 'preview_nsfw_level': 0,
'notes': '', 'notes': '',
'usage_tips': '', 'usage_tips': '',
'hash_status': 'completed',
} }
incomplete_entry = { incomplete_entry = {
'file_path': '/models/test2.safetensors', 'file_path': '/models/test2.safetensors',