Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

37.6 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""Unit tests for WatchManager."""

import asyncio
import json
from datetime import datetime, timedelta
from pathlib import Path
from typing import AsyncGenerator
from unittest.mock import AsyncMock

import pytest
import pytest_asyncio

from openviking.resource.watch_manager import WatchManager, WatchTask
from openviking_cli.exceptions import ConflictError
from tests.utils.mock_agfs import MockLocalAGFS

TEST_ACCOUNT_ID = "default"
TEST_USER_ID = "default"
TEST_ROLE = "ROOT"


class MockVikingFS:
    """Mock VikingFS for testing."""

    def __init__(self, root_path: str):
        self.agfs = MockLocalAGFS(root_path=root_path)
        self._storage_data = {}

    async def read_file(self, uri: str, ctx=None) -> str:
        """Read file from storage."""
        path = self._uri_to_path(uri)
        content = self.agfs.read(path)
        if isinstance(content, bytes):
            return content.decode("utf-8")
        return content

    async def write_file(self, uri: str, content: str, ctx=None) -> None:
        """Write file to storage."""
        path = self._uri_to_path(uri)
        self.agfs.write(path, content.encode("utf-8"))

    def _uri_to_path(self, uri: str) -> str:
        """Convert URI to path."""
        if uri.startswith("viking://"):
            return uri.replace("viking://", "/local/default/")
        return uri


@pytest_asyncio.fixture
async def temp_storage(tmp_path: Path) -> AsyncGenerator[Path, None]:
    """Create temporary storage directory."""
    storage_dir = tmp_path / "watch_storage"
    storage_dir.mkdir(parents=True, exist_ok=True)
    yield storage_dir


@pytest_asyncio.fixture
async def mock_viking_fs(temp_storage: Path) -> MockVikingFS:
    """Create mock VikingFS instance."""
    return MockVikingFS(root_path=str(temp_storage))


@pytest_asyncio.fixture
async def watch_manager(mock_viking_fs: MockVikingFS) -> WatchManager:
    """Create an initialized WatchManager with isolated storage."""
    manager = WatchManager(viking_fs=mock_viking_fs)
    await manager.initialize()
    return manager


@pytest_asyncio.fixture
async def watch_manager_no_fs() -> WatchManager:
    """Create an initialized in-memory WatchManager."""
    manager = WatchManager(viking_fs=None)
    await manager.initialize()
    return manager


class TestWatchTask:
    """Tests for WatchTask data model."""

    def test_create_task_with_defaults(self):
        """Test creating a task with default values."""
        task = WatchTask(path="/test/path")

        assert task.path == "/test/path"
        assert task.source_type is None
        assert task.task_id is not None
        assert task.to_uri is None
        assert task.parent_uri is None
        assert task.reason == ""
        assert task.instruction == ""
        assert task.watch_interval == 60.0
        assert task.is_active is True
        assert task.created_at is not None
        assert task.last_execution_time is None
        assert task.next_execution_time is None
        assert task.processing_mode == "semantic_and_vectors"

    def test_create_task_with_all_fields(self):
        """Test creating a task with all fields specified."""
        now = datetime.now()
        task = WatchTask(
            task_id="test-task-id",
            path="/test/path",
            source_type="feishu_project",
            to_uri="viking://resources/test",
            parent_uri="viking://resources",
            reason="Test reason",
            instruction="Test instruction",
            watch_interval=30.0,
            processing_mode="vectors_only",
            created_at=now,
            last_execution_time=now,
            next_execution_time=now + timedelta(minutes=30),
            is_active=False,
        )

        assert task.task_id == "test-task-id"
        assert task.path == "/test/path"
        assert task.source_type == "feishu_project"
        assert task.to_uri == "viking://resources/test"
        assert task.parent_uri == "viking://resources"
        assert task.reason == "Test reason"
        assert task.instruction == "Test instruction"
        assert task.watch_interval == 30.0
        assert task.processing_mode == "vectors_only"
        assert task.is_active is False
        assert task.created_at == now
        assert task.last_execution_time == now

    def test_to_dict(self):
        """Test converting task to dictionary."""
        now = datetime.now()
        task = WatchTask(
            task_id="test-id",
            path="/test/path",
            source_type="url",
            to_uri="viking://test",
            auth_state={
                "provider": "feishu",
                "access_token": "u-test",
                "refresh_token": "r-test",
                "expires_at": None,
            },
            created_at=now,
        )

        data = task.to_dict()

        assert data["task_id"] == "test-id"
        assert data["path"] == "/test/path"
        assert data["source_type"] == "url"
        assert data["to_uri"] == "viking://test"
        assert data["created_at"] == now.isoformat()
        assert data["is_active"] is True
        assert data["processing_mode"] == "semantic_and_vectors"
        assert "auth_state" not in data

    def test_from_dict(self):
        """Test creating task from dictionary."""
        now = datetime.now()
        data = {
            "task_id": "test-id",
            "path": "/test/path",
            "source_type": "git",
            "to_uri": "viking://test",
            "parent_uri": "viking://parent",
            "reason": "Test",
            "instruction": "Instruction",
            "watch_interval": 45.0,
            "processing_mode": "vectors_only",
            "created_at": now.isoformat(),
            "last_execution_time": now.isoformat(),
            "next_execution_time": (now + timedelta(minutes=45)).isoformat(),
            "is_active": False,
        }

        task = WatchTask.from_dict(data)

        assert task.task_id == "test-id"
        assert task.path == "/test/path"
        assert task.source_type == "git"
        assert task.to_uri == "viking://test"
        assert task.watch_interval == 45.0
        assert task.processing_mode == "vectors_only"
        assert task.is_active is False
        assert task.created_at == now
        assert task.last_execution_time == now

    def test_from_dict_defaults_legacy_processing_mode(self):
        task = WatchTask.from_dict({"path": "/test/path"})

        assert task.processing_mode == "semantic_and_vectors"
        assert task.to_is_directory is None
        assert task.source_type is None

    def test_calculate_next_execution_time(self):
        """Test calculating next execution time."""
        now = datetime.now()
        task = WatchTask(
            path="/test",
            watch_interval=30.0,
            created_at=now,
        )

        next_time = task.calculate_next_execution_time()

        expected = now + timedelta(minutes=30.0)
        assert abs((next_time - expected).total_seconds()) < 1

    def test_calculate_next_execution_time_with_last_execution(self):
        """Test calculating next execution time based on last execution."""
        now = datetime.now()
        last_exec = now - timedelta(minutes=10)
        task = WatchTask(
            path="/test",
            watch_interval=30.0,
            created_at=now - timedelta(hours=1),
            last_execution_time=last_exec,
        )

        next_time = task.calculate_next_execution_time()

        expected = last_exec + timedelta(minutes=30.0)
        assert abs((next_time - expected).total_seconds()) < 1


class TestWatchManager:
    """Tests for WatchManager."""

    @pytest.mark.asyncio
    async def test_create_task(self, watch_manager: WatchManager):
        """Test creating a task."""
        task = await watch_manager.create_task(
            path="/test/path",
            to_uri="viking://resources/test",
            reason="Test task",
            watch_interval=30.0,
        )

        assert task.path == "/test/path"
        assert task.to_uri == "viking://resources/test"
        assert task.reason == "Test task"
        assert task.watch_interval == 30.0
        assert task.is_active is True
        assert task.next_execution_time is not None

    @pytest.mark.asyncio
    async def test_create_inactive_task_has_no_next_execution(self, watch_manager: WatchManager):
        task = await watch_manager.create_task(
            path="/test/path",
            to_uri="viking://resources/paused",
            watch_interval=30.0,
            is_active=False,
        )

        assert task.is_active is False
        assert task.next_execution_time is None
        assert task in await watch_manager.get_all_tasks(
            TEST_ACCOUNT_ID,
            TEST_USER_ID,
            TEST_ROLE,
        )
        assert task not in await watch_manager.get_all_tasks(
            TEST_ACCOUNT_ID,
            TEST_USER_ID,
            TEST_ROLE,
            active_only=True,
        )

    @pytest.mark.asyncio
    async def test_deactivate_tasks_under_uri_internal_matches_subtree(
        self, watch_manager_no_fs: WatchManager
    ):
        root = await watch_manager_no_fs.create_task(
            path="/test/root",
            to_uri="viking://resources/codeask/wiki",
            watch_interval=30.0,
        )
        child = await watch_manager_no_fs.create_task(
            path="/test/child",
            to_uri="viking://resources/codeask/wiki/page",
            watch_interval=30.0,
        )
        sibling = await watch_manager_no_fs.create_task(
            path="/test/sibling",
            to_uri="viking://resources/codeask/wiki-renamed",
            watch_interval=30.0,
        )

        deactivated = await watch_manager_no_fs.deactivate_tasks_under_uri_internal(
            "viking://resources/codeask/wiki", TEST_ACCOUNT_ID
        )

        assert {task.task_id for task in deactivated} == {root.task_id, child.task_id}
        assert (await watch_manager_no_fs.get_task(root.task_id)).is_active is False
        assert (await watch_manager_no_fs.get_task(child.task_id)).is_active is False
        assert (await watch_manager_no_fs.get_task(sibling.task_id)).is_active is True

    @pytest.mark.asyncio
    async def test_target_prefix_rewrite_rolls_back_task_state_on_save_failure(self):
        manager = WatchManager()
        root = await manager.create_task(
            path="/test/root",
            to_uri="viking://resources/codeask/wiki",
            parent_uri=None,
            watch_interval=30.0,
        )
        manager._save_tasks = AsyncMock(side_effect=RuntimeError("save failed"))

        with pytest.raises(RuntimeError, match="save failed"):
            await manager.rewrite_target_prefix_internal(
                "viking://resources/codeask/wiki",
                "viking://resources/codeask/wiki-renamed",
                account_id=TEST_ACCOUNT_ID,
            )

        restored = await manager.get_task(root.task_id)
        assert restored is not None
        assert restored.to_uri == "viking://resources/codeask/wiki"
        assert restored.parent_uri is None

    @pytest.mark.asyncio
    async def test_uri_index_move_and_deactivate_are_account_scoped(self):
        manager = WatchManager()
        uri = "viking://resources/shared"
        task_a = await manager.create_task(path="/a", account_id="account-a", to_uri=uri)
        task_b = await manager.create_task(path="/b", account_id="account-b", to_uri=uri)
        with pytest.raises(ConflictError):
            await manager.create_task(path="/duplicate", account_id="account-a", to_uri=uri)

        await manager.validate_target_prefix_rewrite_internal(
            uri,
            f"{uri}-moved",
            account_id="account-a",
        )
        await manager.rewrite_target_prefix_internal(
            uri,
            f"{uri}-moved",
            account_id="account-a",
        )
        deactivated = await manager.deactivate_tasks_under_uri_internal(uri, "account-b")

        assert task_a.to_uri == f"{uri}-moved"
        assert task_a.is_active is True
        assert task_b.to_uri == uri
        assert task_b.is_active is False
        assert deactivated == [task_b]
        moved = await manager.get_task_by_uri(f"{uri}-moved", "account-a", "default", "root")
        assert moved is task_a
        assert await manager.get_task_by_uri(uri, "account-b", "default", "root") is task_b

    @pytest.mark.asyncio
    async def test_auth_state_persisted_and_hidden_from_public_dict(
        self, mock_viking_fs: MockVikingFS
    ):
        manager1 = WatchManager(viking_fs=mock_viking_fs)
        await manager1.initialize()
        task = await manager1.create_task(
            path="https://example.feishu.cn/docx/doc_token",
            to_uri="viking://resources/feishu",
            watch_interval=30.0,
            auth_state={
                "provider": "feishu",
                "access_token": "u-test",
                "refresh_token": "r-test",
                "expires_at": None,
            },
        )

        manager2 = WatchManager(viking_fs=mock_viking_fs)
        await manager2.initialize()
        loaded = await manager2.get_task(task.task_id)

        assert loaded is not None
        assert loaded.auth_state == task.auth_state
        assert "auth_state" not in loaded.to_dict()

    @pytest.mark.asyncio
    async def test_connector_states_persisted_and_hidden_from_public_dict(
        self, mock_viking_fs: MockVikingFS
    ):
        manager1 = WatchManager(viking_fs=mock_viking_fs)
        await manager1.initialize()
        states = {"documents": {"cursor": {"sync_checkpoint": "2026-09-01T00:00:00+00:00"}}}
        task = await manager1.create_task(
            path="tos://bucket/docs/",
            source_type="tos",
            to_uri="viking://resources/imports",
            watch_interval=30.0,
            auth_state={"provider": "connector_encrypted", "ciphertext": "encrypted"},
            connector_states=states,
        )

        manager2 = WatchManager(viking_fs=mock_viking_fs)
        await manager2.initialize()
        loaded = await manager2.get_task(task.task_id)

        assert loaded is not None
        assert loaded.connector_states == states
        assert "connector_states" not in loaded.to_dict()

    @pytest.mark.asyncio
    async def test_execution_result_is_persisted_and_public(self, mock_viking_fs: MockVikingFS):
        manager1 = WatchManager(viking_fs=mock_viking_fs)
        await manager1.initialize()
        task = await manager1.create_task(
            path="tos://bucket/docs/",
            to_uri="viking://resources/imports",
            watch_interval=30.0,
        )

        await manager1.record_execution(
            task.task_id,
            status="failed",
            execution_task_id="ingest-task-1",
            error="pull failed with Bearer secret-token",
        )

        manager2 = WatchManager(viking_fs=mock_viking_fs)
        await manager2.initialize()
        loaded = await manager2.get_task(task.task_id)

        assert loaded is not None
        assert loaded.last_task_id == "ingest-task-1"
        assert loaded.last_status == "failed"
        assert loaded.last_error == "pull failed with Bearer [REDACTED]"
        assert loaded.last_execution_time is not None
        assert loaded.to_dict()["last_error"] == loaded.last_error

    @pytest.mark.asyncio
    async def test_create_task_without_path_raises(self, watch_manager: WatchManager):
        """Test that creating a task without path raises error."""
        with pytest.raises(ValueError, match="Path is required"):
            await watch_manager.create_task(path="")

    @pytest.mark.asyncio
    async def test_create_task_with_conflicting_uri(self, watch_manager: WatchManager):
        """Test that creating a task with conflicting URI raises error."""
        await watch_manager.create_task(
            path="/test/path1",
            to_uri="viking://resources/test",
        )

        with pytest.raises(ConflictError, match="already being monitored"):
            await watch_manager.create_task(
                path="/test/path2",
                to_uri="viking://resources/test",
            )

    @pytest.mark.asyncio
    async def test_update_task(self, watch_manager: WatchManager):
        """Test updating a task."""
        task = await watch_manager.create_task(
            path="/test/path",
            reason="Original reason",
        )

        updated = await watch_manager.update_task(
            task_id=task.task_id,
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
            reason="Updated reason",
            watch_interval=45.0,
            is_active=False,
        )

        assert updated.reason == "Updated reason"
        assert updated.watch_interval == 45.0
        assert updated.is_active is False
        assert updated.next_execution_time is None

    @pytest.mark.asyncio
    async def test_update_task_not_found(self, watch_manager: WatchManager):
        """Test updating a non-existent task."""
        with pytest.raises(ValueError, match="not found"):
            await watch_manager.update_task(
                task_id="non-existent-id",
                account_id=TEST_ACCOUNT_ID,
                user_id=TEST_USER_ID,
                role=TEST_ROLE,
                reason="Updated",
            )

    @pytest.mark.asyncio
    async def test_update_task_with_conflicting_uri(self, watch_manager: WatchManager):
        """Test updating a task with conflicting URI."""
        await watch_manager.create_task(
            path="/test/path1",
            to_uri="viking://resources/test1",
        )
        task2 = await watch_manager.create_task(
            path="/test/path2",
            to_uri="viking://resources/test2",
        )

        with pytest.raises(ConflictError, match="already being monitored"):
            await watch_manager.update_task(
                task_id=task2.task_id,
                account_id=TEST_ACCOUNT_ID,
                user_id=TEST_USER_ID,
                role=TEST_ROLE,
                to_uri="viking://resources/test1",
            )

    @pytest.mark.asyncio
    async def test_delete_task(self, watch_manager: WatchManager):
        """Test deleting a task."""
        task = await watch_manager.create_task(
            path="/test/path",
            to_uri="viking://resources/test",
        )

        result = await watch_manager.delete_task(
            task_id=task.task_id,
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )

        assert result is True

        retrieved = await watch_manager.get_task(task.task_id)
        assert retrieved is None

        uri_task = await watch_manager.get_task_by_uri(
            to_uri="viking://resources/test",
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )
        assert uri_task is None

    @pytest.mark.asyncio
    async def test_delete_task_not_found(self, watch_manager: WatchManager):
        """Test deleting a non-existent task."""
        result = await watch_manager.delete_task(
            task_id="non-existent-id",
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )
        assert result is False

    @pytest.mark.asyncio
    async def test_get_task(self, watch_manager: WatchManager):
        """Test getting a task by ID."""
        task = await watch_manager.create_task(path="/test/path")

        retrieved = await watch_manager.get_task(task.task_id)

        assert retrieved is not None
        assert retrieved.task_id == task.task_id
        assert retrieved.path == "/test/path"

    @pytest.mark.asyncio
    async def test_get_task_not_found(self, watch_manager: WatchManager):
        """Test getting a non-existent task."""
        retrieved = await watch_manager.get_task("non-existent-id")
        assert retrieved is None

    @pytest.mark.asyncio
    async def test_get_all_tasks(self, watch_manager: WatchManager):
        """Test getting all tasks."""
        await watch_manager.create_task(path="/test/path1")
        await watch_manager.create_task(path="/test/path2")
        await watch_manager.create_task(path="/test/path3")

        tasks = await watch_manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )

        assert len(tasks) == 3
        assert [task.path for task in tasks] == ["/test/path3", "/test/path2", "/test/path1"]

    @pytest.mark.asyncio
    async def test_get_all_tasks_active_only(self, watch_manager: WatchManager):
        """Test getting only active tasks."""
        task1 = await watch_manager.create_task(path="/test/path1")
        await watch_manager.create_task(path="/test/path2")

        await watch_manager.update_task(
            task_id=task1.task_id,
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
            is_active=False,
        )

        tasks = await watch_manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
            active_only=True,
        )

        assert len(tasks) == 1
        assert tasks[0].is_active is True

    @pytest.mark.asyncio
    async def test_get_all_tasks_admin_scoped_to_own_tasks(
        self, watch_manager: WatchManager
    ):
        """ADMIN is scoped to its own tasks; ROOT still sees the whole account."""
        await watch_manager.create_task(path="/test/alice1", user_id="alice")
        await watch_manager.create_task(path="/test/bob1", user_id="bob")
        await watch_manager.create_task(path="/test/alice2", user_id="alice")

        # ROOT keeps the system-wide view.
        all_tasks = await watch_manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID, user_id="root", role="root"
        )
        assert len(all_tasks) == 3

        # ADMIN no longer sees the whole account — only its own tasks.
        alice_tasks = await watch_manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID, user_id="alice", role="admin"
        )
        assert {task.user_id for task in alice_tasks} == {"alice"}
        assert len(alice_tasks) == 2

    @pytest.mark.asyncio
    async def test_get_all_tasks_isolates_same_account_users(
        self, watch_manager: WatchManager
    ):
        """Same account, different user_id → each caller sees only its own tasks."""
        await watch_manager.create_task(path="/test/alice1", user_id="alice")
        await watch_manager.create_task(path="/test/bob1", user_id="bob")

        bob_tasks = await watch_manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID, user_id="bob", role="user"
        )
        assert {task.user_id for task in bob_tasks} == {"bob"}

    @pytest.mark.asyncio
    async def test_get_task_by_uri(self, watch_manager: WatchManager):
        """Test getting a task by URI."""
        task = await watch_manager.create_task(
            path="/test/path",
            to_uri="viking://resources/test",
        )

        retrieved = await watch_manager.get_task_by_uri(
            to_uri="viking://resources/test",
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )

        assert retrieved is not None
        assert retrieved.task_id == task.task_id

    @pytest.mark.asyncio
    async def test_get_task_by_uri_not_found(self, watch_manager: WatchManager):
        """Test getting a task by non-existent URI."""
        retrieved = await watch_manager.get_task_by_uri(
            to_uri="viking://nonexistent",
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )
        assert retrieved is None

    @pytest.mark.asyncio
    async def test_update_execution_time(self, watch_manager: WatchManager):
        """Test updating execution time."""
        task = await watch_manager.create_task(
            path="/test/path",
            watch_interval=30.0,
        )

        original_next_time = task.next_execution_time

        await asyncio.sleep(0.1)
        await watch_manager.update_execution_time(task.task_id)

        updated = await watch_manager.get_task(task.task_id)
        assert updated is not None
        assert updated.last_execution_time is not None
        assert updated.next_execution_time > original_next_time

    @pytest.mark.asyncio
    async def test_get_due_tasks(self, watch_manager: WatchManager):
        """Test getting due tasks."""
        task1 = await watch_manager.create_task(
            path="/test/path1",
            watch_interval=0.001,
        )
        await watch_manager.create_task(
            path="/test/path2",
            watch_interval=60.0,
        )

        await asyncio.sleep(0.1)

        due_tasks = await watch_manager.get_due_tasks()

        assert len(due_tasks) == 1
        assert due_tasks[0].task_id == task1.task_id

    @pytest.mark.asyncio
    async def test_create_task_with_non_positive_interval_raises(self, watch_manager: WatchManager):
        with pytest.raises(ValueError, match="watch_interval must be > 0"):
            await watch_manager.create_task(path="/test/path", watch_interval=0)

    @pytest.mark.asyncio
    async def test_get_next_execution_time(self, watch_manager: WatchManager):
        await watch_manager.create_task(path="/test/path1", watch_interval=60.0)
        await watch_manager.create_task(path="/test/path2", watch_interval=0.001)
        await asyncio.sleep(0.05)
        next_time = await watch_manager.get_next_execution_time()
        assert next_time is not None


class TestWatchManagerPersistence:
    """Tests for WatchManager persistence."""

    @pytest.mark.asyncio
    async def test_persistence_save_and_load(
        self, mock_viking_fs: MockVikingFS, temp_storage: Path
    ):
        """Test that tasks are saved and loaded correctly."""
        manager1 = WatchManager(viking_fs=mock_viking_fs)
        await manager1.initialize()

        task = await manager1.create_task(
            path="/test/path",
            source_type="local",
            to_uri="viking://resources/test",
            reason="Test task",
            watch_interval=45.0,
        )
        task_id = task.task_id

        manager2 = WatchManager(viking_fs=mock_viking_fs)
        await manager2.initialize()

        loaded_task = await manager2.get_task(task_id)

        assert loaded_task is not None
        assert loaded_task.path == "/test/path"
        assert loaded_task.source_type == "local"
        assert loaded_task.to_uri == "viking://resources/test"
        assert loaded_task.reason == "Test task"
        assert loaded_task.watch_interval == 45.0

    @pytest.mark.asyncio
    async def test_persistence_without_vikingfs(self, watch_manager_no_fs: WatchManager):
        """Test that manager works without VikingFS (no persistence)."""
        task = await watch_manager_no_fs.create_task(
            path="/test/path",
            reason="Test task",
        )

        retrieved = await watch_manager_no_fs.get_task(task.task_id)
        assert retrieved is not None
        assert retrieved.path == "/test/path"

    @pytest.mark.asyncio
    async def test_persistence_after_delete(self, mock_viking_fs: MockVikingFS):
        """Test that deleted tasks are removed from persistence."""
        manager = WatchManager(viking_fs=mock_viking_fs)
        await manager.initialize()

        task = await manager.create_task(
            path="/test/path",
            to_uri="viking://resources/test",
        )

        await manager.delete_task(
            task_id=task.task_id,
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )

        manager2 = WatchManager(viking_fs=mock_viking_fs)
        await manager2.initialize()

        loaded_task = await manager2.get_task(task.task_id)
        assert loaded_task is None

        uri_task = await manager2.get_task_by_uri(
            to_uri="viking://resources/test",
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )
        assert uri_task is None

    @pytest.mark.asyncio
    async def test_persistence_backfill_next_execution_time(
        self, mock_viking_fs: MockVikingFS, temp_storage: Path
    ):
        manager1 = WatchManager(viking_fs=mock_viking_fs)
        await manager1.initialize()
        task = await manager1.create_task(
            path="/test/path",
            to_uri="viking://resources/test_backfill",
            watch_interval=30.0,
        )

        content = await mock_viking_fs.read_file(WatchManager.STORAGE_URI)
        data = json.loads(content)
        for t in data.get("tasks", []):
            if t.get("task_id") == task.task_id:
                t["next_execution_time"] = None
                break
        await mock_viking_fs.write_file(WatchManager.STORAGE_URI, json.dumps(data))

        manager2 = WatchManager(viking_fs=mock_viking_fs)
        await manager2.initialize()
        loaded = await manager2.get_task(task.task_id)
        assert loaded is not None
        assert loaded.is_active is True
        assert loaded.next_execution_time is not None

    @pytest.mark.asyncio
    async def test_persistence_empty_storage_file_is_ignored(self, mock_viking_fs: MockVikingFS):
        path = mock_viking_fs._uri_to_path(WatchManager.STORAGE_URI)
        mock_viking_fs.agfs.write(path, b"")

        manager = WatchManager(viking_fs=mock_viking_fs)
        await manager.initialize()

        tasks = await manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )
        assert tasks == []

    @pytest.mark.asyncio
    async def test_persistence_recovers_from_backup_on_corrupt_storage(
        self, mock_viking_fs: MockVikingFS
    ):
        storage_path = mock_viking_fs._uri_to_path(WatchManager.STORAGE_URI)
        bak_path = mock_viking_fs._uri_to_path(WatchManager.STORAGE_BAK_URI)

        mock_viking_fs.agfs.write(storage_path, b"")

        task_data = {
            "task_id": "bak-task-id",
            "path": "/test/bak",
            "to_uri": "viking://resources/bak",
            "reason": "Backup task",
            "instruction": "",
            "watch_interval": 60.0,
            "created_at": datetime.now().isoformat(),
            "last_execution_time": None,
            "next_execution_time": None,
            "is_active": True,
        }
        data = {"tasks": [task_data], "updated_at": datetime.now().isoformat()}
        mock_viking_fs.agfs.write(bak_path, json.dumps(data).encode("utf-8"))

        manager = WatchManager(viking_fs=mock_viking_fs)
        await manager.initialize()

        loaded = await manager.get_task("bak-task-id")
        assert loaded is not None
        assert loaded.to_uri == "viking://resources/bak"


class TestWatchManagerConcurrency:
    """Tests for WatchManager concurrent access."""

    @pytest.mark.asyncio
    async def test_concurrent_task_creation(self, watch_manager: WatchManager):
        """Test concurrent task creation."""

        async def create_task(index: int):
            return await watch_manager.create_task(
                path=f"/test/path{index}",
                to_uri=f"viking://resources/test{index}",
            )

        tasks = await asyncio.gather(
            *[asyncio.to_thread(lambda i=i: asyncio.run(create_task(i))) for i in range(10)]
        )

        assert len(tasks) == 10
        assert len({task.task_id for task in tasks}) == 10

        all_tasks = await watch_manager.get_all_tasks(
            account_id=TEST_ACCOUNT_ID,
            user_id=TEST_USER_ID,
            role=TEST_ROLE,
        )
        assert len(all_tasks) == 10

    @pytest.mark.asyncio
    async def test_concurrent_read_write(self, watch_manager: WatchManager):
        """A background result must preserve a concurrent HTTP pause, including on disk."""
        task = await watch_manager.create_task(path="/test/path")

        async def pause_task():
            await watch_manager.update_task(
                task_id=task.task_id,
                account_id=TEST_ACCOUNT_ID,
                user_id=TEST_USER_ID,
                role=TEST_ROLE,
                is_active=False,
            )

        await asyncio.gather(
            pause_task(),
            asyncio.to_thread(
                lambda: asyncio.run(
                    watch_manager.record_execution(task.task_id, status="completed")
                )
            ),
        )
        restored = WatchManager(viking_fs=watch_manager._viking_fs)
        await restored.initialize()
        final_task = await restored.get_task(task.task_id)
        assert final_task is not None
        assert final_task.is_active is False
        assert final_task.last_status == "completed"
        assert final_task.next_execution_time is None


_CONNECTOR_AUTH = {"provider": "connector_encrypted", "ciphertext": "unused"}


class TestSharedConnectorTargets:
    """Connector watches may share a target; native watches are exclusive."""

    @pytest.mark.asyncio
    @pytest.mark.parametrize("with_connector", [False, True])
    @pytest.mark.parametrize("operation", ["create", "update", "rewrite"])
    async def test_missing_indexed_task_blocks_target_sharing(
        self, watch_manager_no_fs: WatchManager, with_connector: bool, operation: str
    ):
        manager = watch_manager_no_fs
        target = "viking://resources/shared"
        source = "viking://resources/source"
        if with_connector:
            await manager.create_task(
                path="tos://bucket/existing/", to_uri=target, auth_state=dict(_CONNECTOR_AUTH)
            )
        moving = await manager.create_task(
            path="tos://bucket/moving/", to_uri=source, auth_state=dict(_CONNECTOR_AUTH)
        )
        manager._index_add(TEST_ACCOUNT_ID, target, "missing-task")
        original_index = {key: set(ids) for key, ids in manager._uri_to_task.items()}
        original_tasks = set(manager._tasks)

        with pytest.raises(ConflictError, match="already being monitored"):
            if operation == "create":
                await manager.create_task(
                    path="tos://bucket/new/", to_uri=target, auth_state=dict(_CONNECTOR_AUTH)
                )
            elif operation == "update":
                await manager.update_task(
                    moving.task_id, TEST_ACCOUNT_ID, TEST_USER_ID, TEST_ROLE, to_uri=target
                )
            else:
                await manager.rewrite_target_prefix_internal(source, target, TEST_ACCOUNT_ID)

        assert manager._uri_to_task == original_index
        assert set(manager._tasks) == original_tasks
        assert moving.to_uri == source

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ("user_id", "role", "visible_indexes"),
        [
            ("carol", "user", ()),
            ("bob", "user", (2,)),
            ("alice", "user", (0, 1)),
            ("bob", "admin", (2,)),
            ("bob", "root", (0, 1, 2)),
        ],
    )
    async def test_uri_lookup_only_considers_visible_tasks(
        self, watch_manager_no_fs: WatchManager, user_id, role, visible_indexes
    ):
        manager = watch_manager_no_fs
        to_uri = "viking://resources/shared"
        tasks = []
        for index, owner in enumerate(("alice", "alice", "bob")):
            tasks.append(
                await manager.create_task(
                    path=f"tos://bucket/{index}/",
                    user_id=owner,
                    to_uri=to_uri,
                    auth_state=dict(_CONNECTOR_AUTH),
                )
            )

        if len(visible_indexes) > 1:
            with pytest.raises(
                ConflictError, match=f"has {len(visible_indexes)} watch tasks"
            ) as exc_info:
                await manager.get_task_by_uri(to_uri, TEST_ACCOUNT_ID, user_id, role)
            for index, task in enumerate(tasks):
                assert (task.task_id in str(exc_info.value)) == (index in visible_indexes)
        else:
            task = await manager.get_task_by_uri(to_uri, TEST_ACCOUNT_ID, user_id, role)
            assert task is (tasks[visible_indexes[0]] if visible_indexes else None)

    @pytest.mark.asyncio
    async def test_connector_watches_coexist_and_native_is_refused(
        self, watch_manager: WatchManager
    ):
        to_uri = "viking://resources/shared"
        first = await watch_manager.create_task(
            path="tos://bucket/a/",
            to_uri=to_uri,
            watch_interval=5,
            auth_state=dict(_CONNECTOR_AUTH),
        )
        second = await watch_manager.create_task(
            path="tos://bucket/b/",
            to_uri=to_uri,
            watch_interval=5,
            auth_state=dict(_CONNECTOR_AUTH),
        )
        assert first.task_id != second.task_id

        with pytest.raises(ConflictError, match="already being monitored"):
            await watch_manager.create_task(path="/local/doc", to_uri=to_uri, watch_interval=5)

        # Removing one Connector watch keeps the other addressable by URI again.
        await watch_manager.delete_task(
            first.task_id, account_id="default", user_id="default", role="user"
        )
        remaining = await watch_manager.get_task_by_uri(
            to_uri, account_id="default", user_id="default", role="user"
        )
        assert remaining is not None
        assert remaining.task_id == second.task_id

    @pytest.mark.asyncio
    async def test_native_watch_blocks_connector_watch(self, watch_manager: WatchManager):
        to_uri = "viking://resources/native"
        await watch_manager.create_task(path="/local/doc", to_uri=to_uri, watch_interval=5)
        with pytest.raises(ConflictError, match="already being monitored"):
            await watch_manager.create_task(
                path="tos://bucket/a/",
                to_uri=to_uri,
                watch_interval=5,
                auth_state=dict(_CONNECTOR_AUTH),
            )