semantics: complete DEF-to-region migration, fix regressions
- Convert legacy [DEF🆔Type] anchors to #region/#endregion across 329 files
- Reinstate _normalize_timestamp_value in sql_generator.py
- Fix MarkerLogger→logger migration in events.py (molecular CoT markers)
- Fix dataset_review orchestrator dependencies (_build_execution_snapshot)
- Fix config_manager stale-record deletion (moved to save path only)
- Add 77 missing [/DEF:] closers in 5 unbalanced test files
- Update assistant_chat.integration.test.js for #region format
- Apply molecular-cot-logging markers (REASON/REFLECT/EXPLORE) via logger.* methods
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
# #region test_llm_provider [C:3] [TYPE Module] [SEMANTICS tests, llm-provider, encryption, contract]
|
||||
# @BRIEF Contract testing for LLMProviderService and EncryptionManager
|
||||
# @RELATION VERIFIES -> [src.services.llm_provider:Module]
|
||||
# #endregion test_llm_provider
|
||||
# [DEF:test_llm_provider:Module]
|
||||
# @RELATION: VERIFIES -> [src.services.llm_provider:Module]
|
||||
# @COMPLEXITY: 3
|
||||
# @SEMANTICS: tests, llm-provider, encryption, contract
|
||||
# @PURPOSE: Contract testing for LLMProviderService and EncryptionManager
|
||||
# [/DEF:test_llm_provider:Module]
|
||||
|
||||
import pytest
|
||||
import os
|
||||
@@ -12,18 +14,18 @@ from src.services.llm_provider import EncryptionManager, LLMProviderService
|
||||
from src.models.llm import LLMProvider
|
||||
from src.plugins.llm_analysis.models import LLMProviderConfig, LLMProviderType
|
||||
|
||||
# #region _test_encryption_key_fixture [TYPE Global]
|
||||
# @BRIEF Ensure encryption-dependent provider tests run with a valid Fernet key.
|
||||
# @RELATION DEPENDS_ON -> [pytest:Module]
|
||||
# [DEF:_test_encryption_key_fixture:Global]
|
||||
# @PURPOSE: Ensure encryption-dependent provider tests run with a valid Fernet key.
|
||||
# @RELATION: DEPENDS_ON -> [pytest:Module]
|
||||
os.environ.setdefault("ENCRYPTION_KEY", Fernet.generate_key().decode())
|
||||
# #endregion _test_encryption_key_fixture
|
||||
# [/DEF:_test_encryption_key_fixture:Global]
|
||||
|
||||
|
||||
# @TEST_CONTRACT: EncryptionManagerModel -> Invariants
|
||||
# @TEST_INVARIANT: symmetric_encryption
|
||||
# #region test_encryption_cycle [TYPE Function]
|
||||
# @BRIEF Verify EncryptionManager round-trip encryption/decryption invariant for non-empty secrets.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_encryption_cycle:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Verify EncryptionManager round-trip encryption/decryption invariant for non-empty secrets.
|
||||
def test_encryption_cycle():
|
||||
"""Verify encrypted data can be decrypted back to original string."""
|
||||
manager = EncryptionManager()
|
||||
@@ -34,12 +36,12 @@ def test_encryption_cycle():
|
||||
|
||||
|
||||
# @TEST_EDGE: empty_string_encryption
|
||||
# #endregion test_encryption_cycle
|
||||
# [/DEF:test_encryption_cycle:Function]
|
||||
|
||||
|
||||
# #region test_empty_string_encryption [TYPE Function]
|
||||
# @BRIEF Verify EncryptionManager preserves empty-string payloads through encrypt/decrypt cycle.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_empty_string_encryption:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Verify EncryptionManager preserves empty-string payloads through encrypt/decrypt cycle.
|
||||
def test_empty_string_encryption():
|
||||
manager = EncryptionManager()
|
||||
original = ""
|
||||
@@ -48,12 +50,12 @@ def test_empty_string_encryption():
|
||||
|
||||
|
||||
# @TEST_EDGE: decrypt_invalid_data
|
||||
# #endregion test_empty_string_encryption
|
||||
# [/DEF:test_empty_string_encryption:Function]
|
||||
|
||||
|
||||
# #region test_decrypt_invalid_data [TYPE Function]
|
||||
# @BRIEF Ensure decrypt rejects invalid ciphertext input by raising an exception.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_decrypt_invalid_data:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Ensure decrypt rejects invalid ciphertext input by raising an exception.
|
||||
def test_decrypt_invalid_data():
|
||||
manager = EncryptionManager()
|
||||
with pytest.raises(Exception):
|
||||
@@ -61,48 +63,50 @@ def test_decrypt_invalid_data():
|
||||
|
||||
|
||||
# @TEST_FIXTURE: mock_db_session
|
||||
# #endregion test_decrypt_invalid_data
|
||||
# [/DEF:test_decrypt_invalid_data:Function]
|
||||
|
||||
|
||||
# #region mock_db [C:1] [TYPE Fixture]
|
||||
# @BRIEF MagicMock(spec=Session) fixture providing a constrained DB session double for LLMProviderService tests.
|
||||
# @INVARIANT Chained calls beyond Session spec create unconstrained intermediate mocks; only top-level query/add/commit are spec-enforced.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:mock_db:Fixture]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @COMPLEXITY: 1
|
||||
# @PURPOSE: MagicMock(spec=Session) fixture providing a constrained DB session double for LLMProviderService tests.
|
||||
# @INVARIANT: Chained calls beyond Session spec create unconstrained intermediate mocks; only top-level query/add/commit are spec-enforced.
|
||||
@pytest.fixture
|
||||
def mock_db():
|
||||
# @RISK: query() returns unconstrained MagicMock — chain beyond query() has no spec protection. Consider create_autospec(Session) for full chain safety.
|
||||
return MagicMock(spec=Session)
|
||||
|
||||
|
||||
# #endregion mock_db
|
||||
# [/DEF:mock_db:Fixture]
|
||||
|
||||
|
||||
# #region service [C:1] [TYPE Fixture]
|
||||
# @BRIEF LLMProviderService fixture wired to mock_db for provider CRUD tests.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:service:Fixture]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @COMPLEXITY: 1
|
||||
# @PURPOSE: LLMProviderService fixture wired to mock_db for provider CRUD tests.
|
||||
@pytest.fixture
|
||||
def service(mock_db):
|
||||
return LLMProviderService(db=mock_db)
|
||||
|
||||
|
||||
# #endregion service
|
||||
# [/DEF:service:Fixture]
|
||||
|
||||
|
||||
# #region test_get_all_providers [TYPE Function]
|
||||
# @BRIEF Verify provider list retrieval issues query/all calls on the backing DB session.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_get_all_providers:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Verify provider list retrieval issues query/all calls on the backing DB session.
|
||||
def test_get_all_providers(service, mock_db):
|
||||
service.get_all_providers()
|
||||
mock_db.query.assert_called()
|
||||
mock_db.query().all.assert_called()
|
||||
|
||||
|
||||
# #endregion test_get_all_providers
|
||||
# [/DEF:test_get_all_providers:Function]
|
||||
|
||||
|
||||
# #region test_create_provider [TYPE Function]
|
||||
# @BRIEF Ensure provider creation persists entity and stores API key in encrypted form.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_create_provider:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Ensure provider creation persists entity and stores API key in encrypted form.
|
||||
def test_create_provider(service, mock_db):
|
||||
config = LLMProviderConfig(
|
||||
provider_type=LLMProviderType.OPENAI,
|
||||
@@ -123,12 +127,12 @@ def test_create_provider(service, mock_db):
|
||||
assert EncryptionManager().decrypt(provider.api_key) == "sk-test"
|
||||
|
||||
|
||||
# #endregion test_create_provider
|
||||
# [/DEF:test_create_provider:Function]
|
||||
|
||||
|
||||
# #region test_get_decrypted_api_key [TYPE Function]
|
||||
# @BRIEF Verify service decrypts stored provider API key for an existing provider record.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_get_decrypted_api_key:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Verify service decrypts stored provider API key for an existing provider record.
|
||||
def test_get_decrypted_api_key(service, mock_db):
|
||||
# Setup mock provider
|
||||
encrypted_key = EncryptionManager().encrypt("secret-value")
|
||||
@@ -139,23 +143,23 @@ def test_get_decrypted_api_key(service, mock_db):
|
||||
assert key == "secret-value"
|
||||
|
||||
|
||||
# #endregion test_get_decrypted_api_key
|
||||
# [/DEF:test_get_decrypted_api_key:Function]
|
||||
|
||||
|
||||
# #region test_get_decrypted_api_key_not_found [TYPE Function]
|
||||
# @BRIEF Verify missing provider lookup returns None instead of attempting decryption.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_get_decrypted_api_key_not_found:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Verify missing provider lookup returns None instead of attempting decryption.
|
||||
def test_get_decrypted_api_key_not_found(service, mock_db):
|
||||
mock_db.query().filter().first.return_value = None
|
||||
assert service.get_decrypted_api_key("missing") is None
|
||||
|
||||
|
||||
# #endregion test_get_decrypted_api_key_not_found
|
||||
# [/DEF:test_get_decrypted_api_key_not_found:Function]
|
||||
|
||||
|
||||
# #region test_update_provider_ignores_masked_placeholder_api_key [TYPE Function]
|
||||
# @BRIEF Ensure masked placeholder API keys do not overwrite previously encrypted provider secrets.
|
||||
# @RELATION BINDS_TO -> [test_llm_provider:Module]
|
||||
# [DEF:test_update_provider_ignores_masked_placeholder_api_key:Function]
|
||||
# @RELATION: BINDS_TO -> [test_llm_provider:Module]
|
||||
# @PURPOSE: Ensure masked placeholder API keys do not overwrite previously encrypted provider secrets.
|
||||
def test_update_provider_ignores_masked_placeholder_api_key(service, mock_db):
|
||||
existing_encrypted = EncryptionManager().encrypt("secret-value")
|
||||
mock_provider = LLMProvider(
|
||||
@@ -186,4 +190,4 @@ def test_update_provider_ignores_masked_placeholder_api_key(service, mock_db):
|
||||
assert updated.is_active is False
|
||||
|
||||
|
||||
# #endregion test_update_provider_ignores_masked_placeholder_api_key
|
||||
# [/DEF:test_update_provider_ignores_masked_placeholder_api_key:Function]
|
||||
|
||||
Reference in New Issue
Block a user