|
| 1 | +"""Tests for workflow encoding configuration lifecycle.""" |
| 2 | + |
| 3 | +import gc |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import SecretStr |
| 7 | + |
| 8 | +from mistralai.client import Mistral |
| 9 | +from mistralai.client._hooks.workflow_encoding_hook import ( |
| 10 | + _workflow_configs, |
| 11 | + _ENCODING_CONFIG_ID_ATTR, |
| 12 | + configure_workflow_encoding, |
| 13 | +) |
| 14 | +from mistralai.extra.workflows import ( |
| 15 | + WorkflowEncodingConfig, |
| 16 | + PayloadEncryptionConfig, |
| 17 | + PayloadEncryptionMode, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def encryption_config() -> WorkflowEncodingConfig: |
| 23 | + """Create a test encryption config.""" |
| 24 | + return WorkflowEncodingConfig( |
| 25 | + payload_encryption=PayloadEncryptionConfig( |
| 26 | + mode=PayloadEncryptionMode.FULL, |
| 27 | + main_key=SecretStr("0" * 64), # 256-bit key in hex |
| 28 | + ) |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_payload_encoder_cleanup_on_client_gc(encryption_config: WorkflowEncodingConfig): |
| 33 | + """Test that PayloadEncoder is cleaned up when client is garbage collected.""" |
| 34 | + initial_config_count = len(_workflow_configs) |
| 35 | + |
| 36 | + # Create client and configure encoding |
| 37 | + client = Mistral(api_key="test-key") |
| 38 | + configure_workflow_encoding( |
| 39 | + encryption_config, |
| 40 | + namespace="test-namespace", |
| 41 | + sdk_config=client.sdk_configuration, |
| 42 | + ) |
| 43 | + |
| 44 | + # Verify config was added |
| 45 | + config_id = getattr(client.sdk_configuration, _ENCODING_CONFIG_ID_ATTR) |
| 46 | + assert config_id is not None |
| 47 | + assert config_id in _workflow_configs |
| 48 | + assert len(_workflow_configs) == initial_config_count + 1 |
| 49 | + |
| 50 | + # Delete client and force garbage collection |
| 51 | + del client |
| 52 | + gc.collect() |
| 53 | + |
| 54 | + # Verify config was cleaned up |
| 55 | + assert config_id not in _workflow_configs |
| 56 | + assert len(_workflow_configs) == initial_config_count |
| 57 | + |
| 58 | + |
| 59 | +def test_multiple_clients_independent_configs(encryption_config: WorkflowEncodingConfig): |
| 60 | + """Test that multiple clients have independent configs.""" |
| 61 | + initial_config_count = len(_workflow_configs) |
| 62 | + |
| 63 | + # Create two clients with different namespaces |
| 64 | + client1 = Mistral(api_key="test-key-1") |
| 65 | + client2 = Mistral(api_key="test-key-2") |
| 66 | + |
| 67 | + configure_workflow_encoding( |
| 68 | + encryption_config, |
| 69 | + namespace="namespace-1", |
| 70 | + sdk_config=client1.sdk_configuration, |
| 71 | + ) |
| 72 | + configure_workflow_encoding( |
| 73 | + encryption_config, |
| 74 | + namespace="namespace-2", |
| 75 | + sdk_config=client2.sdk_configuration, |
| 76 | + ) |
| 77 | + |
| 78 | + # Verify both configs exist |
| 79 | + config_id1 = getattr(client1.sdk_configuration, _ENCODING_CONFIG_ID_ATTR) |
| 80 | + config_id2 = getattr(client2.sdk_configuration, _ENCODING_CONFIG_ID_ATTR) |
| 81 | + assert config_id1 != config_id2 |
| 82 | + assert len(_workflow_configs) == initial_config_count + 2 |
| 83 | + |
| 84 | + # Verify namespaces are independent |
| 85 | + assert _workflow_configs[config_id1].namespace == "namespace-1" |
| 86 | + assert _workflow_configs[config_id2].namespace == "namespace-2" |
| 87 | + |
| 88 | + # Delete first client |
| 89 | + del client1 |
| 90 | + gc.collect() |
| 91 | + |
| 92 | + # First config should be cleaned up, second should remain |
| 93 | + assert config_id1 not in _workflow_configs |
| 94 | + assert config_id2 in _workflow_configs |
| 95 | + assert len(_workflow_configs) == initial_config_count + 1 |
| 96 | + |
| 97 | + # Delete second client |
| 98 | + del client2 |
| 99 | + gc.collect() |
| 100 | + |
| 101 | + # Both configs should be cleaned up |
| 102 | + assert config_id2 not in _workflow_configs |
| 103 | + assert len(_workflow_configs) == initial_config_count |
| 104 | + |
| 105 | + |
| 106 | +def test_reconfigure_same_client(encryption_config: WorkflowEncodingConfig): |
| 107 | + """Test that reconfiguring the same client updates the config.""" |
| 108 | + client = Mistral(api_key="test-key") |
| 109 | + |
| 110 | + # Initial configuration |
| 111 | + configure_workflow_encoding( |
| 112 | + encryption_config, |
| 113 | + namespace="namespace-v1", |
| 114 | + sdk_config=client.sdk_configuration, |
| 115 | + ) |
| 116 | + |
| 117 | + config_id = getattr(client.sdk_configuration, _ENCODING_CONFIG_ID_ATTR) |
| 118 | + assert _workflow_configs[config_id].namespace == "namespace-v1" |
| 119 | + |
| 120 | + # Reconfigure with different namespace |
| 121 | + configure_workflow_encoding( |
| 122 | + encryption_config, |
| 123 | + namespace="namespace-v2", |
| 124 | + sdk_config=client.sdk_configuration, |
| 125 | + ) |
| 126 | + |
| 127 | + # Should use same config_id but updated namespace |
| 128 | + assert getattr(client.sdk_configuration, _ENCODING_CONFIG_ID_ATTR) == config_id |
| 129 | + assert _workflow_configs[config_id].namespace == "namespace-v2" |
| 130 | + |
| 131 | + # Cleanup |
| 132 | + del client |
| 133 | + gc.collect() |
| 134 | + assert config_id not in _workflow_configs |
0 commit comments