"""
tests/core/organs/test_vault_organ.py — Vault Organ Test Suite

Tests for _4_vault.py — Stage 999: VAULT (Ledger & Commitment)

Target: 85%+ coverage for Vault organ
"""

import pytest


class TestVaultOrgan:
    """Test Vault organ - Stage 999 commitment and ledger operations."""

    @pytest.mark.asyncio
    async def test_vault_commit_basic(self):
        """Test basic vault commit operation."""
        from core.organs._4_vault import vault

        result = await vault(
            action="commit",
            session_id="test-vault-001",
            payload={"data": "test commit data"},
        )

        assert result["session_id"] == "test-vault-001"
        assert "commit_id" in result
        assert result["status"] == "committed"

    @pytest.mark.asyncio
    async def test_vault_seal_operation(self):
        """Test vault seal operation."""
        from core.organs._4_vault import vault

        result = await vault(
            action="seal",
            session_id="test-vault-002",
            payload={"final_state": "complete"},
        )

        assert result["session_id"] == "test-vault-002"
        assert result["sealed"] is True

    @pytest.mark.asyncio
    async def test_vault_retrieve(self):
        """Test vault retrieve operation."""
        from core.organs._4_vault import vault

        # First commit something
        commit_result = await vault(
            action="commit", session_id="test-vault-003", payload={"key": "value123"}
        )

        commit_id = commit_result["commit_id"]

        # Then retrieve it
        retrieve_result = await vault(
            action="retrieve", session_id="test-vault-003", commit_id=commit_id
        )

        assert retrieve_result["found"] is True
        assert retrieve_result["data"]["key"] == "value123"

    @pytest.mark.asyncio
    async def test_vault_list_commits(self):
        """Test listing vault commits."""
        from core.organs._4_vault import vault

        # Create multiple commits
        for i in range(3):
            await vault(action="commit", session_id="test-vault-004", payload={"index": i})

        # List commits
        result = await vault(action="list", session_id="test-vault-004")

        assert "commits" in result
        assert len(result["commits"]) >= 3

    @pytest.mark.asyncio
    async def test_vault_with_metadata(self):
        """Test vault commit with metadata."""
        from core.organs._4_vault import vault

        result = await vault(
            action="commit",
            session_id="test-vault-005",
            payload={"data": "test"},
            metadata={
                "author": "test-user",
                "tags": ["test", "vault"],
                "priority": "high",
            },
        )

        assert result["session_id"] == "test-vault-005"
        assert "commit_id" in result

    @pytest.mark.asyncio
    async def test_vault_empty_payload(self):
        """Test vault handles empty payload gracefully."""
        from core.organs._4_vault import vault

        result = await vault(action="commit", session_id="test-vault-006", payload={})

        assert result["session_id"] == "test-vault-006"
        assert result["status"] == "committed"

    @pytest.mark.asyncio
    async def test_vault_unicode_data(self):
        """Test vault handles unicode data."""
        from core.organs._4_vault import vault

        result = await vault(
            action="commit",
            session_id="test-vault-007",
            payload={"data": "Unicode test: 你好 🌍 ñ"},
        )

        assert result["session_id"] == "test-vault-007"

    @pytest.mark.asyncio
    async def test_vault_retrieve_nonexistent(self):
        """Test vault retrieve for non-existent commit."""
        from core.organs._4_vault import vault

        result = await vault(
            action="retrieve",
            session_id="test-vault-008",
            commit_id="nonexistent-id-12345",
        )

        assert result["found"] is False

    @pytest.mark.asyncio
    async def test_vault_large_payload(self):
        """Test vault handles large payloads."""
        from core.organs._4_vault import vault

        large_data = "x" * 10000  # 10KB of data

        result = await vault(
            action="commit",
            session_id="test-vault-009",
            payload={"large_field": large_data},
        )

        assert result["session_id"] == "test-vault-009"
        assert result["status"] == "committed"

    @pytest.mark.asyncio
    async def test_vault_verify_integrity(self):
        """Test vault integrity verification."""
        from core.organs._4_vault import vault

        commit_result = await vault(
            action="commit", session_id="test-vault-010", payload={"sensitive": "data"}
        )

        commit_id = commit_result["commit_id"]

        # Verify integrity
        verify_result = await vault(
            action="verify", session_id="test-vault-010", commit_id=commit_id
        )

        assert verify_result["valid"] is True
        assert verify_result["commit_id"] == commit_id

    def test_vault_aliases(self):
        """Test Vault function aliases."""
        from core.organs._4_vault import vault, commit, seal, retrieve

        assert vault is commit
        assert vault is seal
        assert vault is retrieve


class TestVaultEdgeCases:
    """Edge cases for Vault operations."""

    @pytest.mark.asyncio
    async def test_vault_multiple_sessions_isolated(self):
        """Test vault isolation between sessions."""
        from core.organs._4_vault import vault

        # Commit in session 1
        await vault(
            action="commit",
            session_id="session-a",
            payload={"secret": "session-a-data"},
        )

        # Commit in session 2
        await vault(
            action="commit",
            session_id="session-b",
            payload={"secret": "session-b-data"},
        )

        # List should only show session-a commits
        result_a = await vault(action="list", session_id="session-a")
        result_b = await vault(action="list", session_id="session-b")

        # Each session should only see its own commits
        for commit in result_a["commits"]:
            assert commit.get("session_id") != "session-b"

        for commit in result_b["commits"]:
            assert commit.get("session_id") != "session-a"

    @pytest.mark.asyncio
    async def test_vault_nested_data(self):
        """Test vault with deeply nested data structures."""
        from core.organs._4_vault import vault

        nested_data = {"level1": {"level2": {"level3": {"level4": ["deep", "data", "here"]}}}}

        result = await vault(action="commit", session_id="test-vault-nested", payload=nested_data)

        assert result["status"] == "committed"

    @pytest.mark.asyncio
    async def test_vault_binary_data(self):
        """Test vault handles binary-like data."""
        from core.organs._4_vault import vault

        binary_like = b"\x00\x01\x02\x03\xff\xfe".decode("latin-1", errors="replace")

        result = await vault(
            action="commit",
            session_id="test-vault-binary",
            payload={"binary": binary_like},
        )

        assert result["status"] == "committed"
