Skip to content

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

4.9 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0

"""Session test fixtures"""

import asyncio
from functools import partial
from typing import AsyncGenerator

import pytest_asyncio

from openviking.message import TextPart, ToolPart
from openviking.server.identity import RequestContext
from openviking.service.core import OpenVikingService
from openviking.service.task_tracker import get_task_tracker
from openviking.session import Session
from openviking.storage.queuefs import QueueManager, SessionCommitMsg, get_queue_manager


@pytest_asyncio.fixture(scope="function")
async def client(
    service: OpenVikingService,
    request_context: RequestContext,
    monkeypatch,
) -> partial:
    """Bind the shared service's session factory to the test request context."""

    queue_manager = get_queue_manager()
    original_enqueue = queue_manager.enqueue
    commit_tasks = []
    queued_commit_tasks: set[str] = set()
    tracker = get_task_tracker()
    original_has_work = tracker.has_work

    async def has_work(task_id, **kwargs):
        return task_id in queued_commit_tasks or await original_has_work(task_id, **kwargs)

    monkeypatch.setattr(
        tracker,
        "has_work",
        has_work,
    )

    async def enqueue_with_session_commit_fallback(queue_name, data):
        if queue_name != QueueManager.SESSION_COMMIT:
            return await original_enqueue(queue_name, data)

        queued_commit_tasks.add(data["task_id"])

        async def process_commit():
            message = SessionCommitMsg(**data)
            queued_session = service.sessions.session(
                request_context,
                session_id=message.session_id,
                session_uri=message.session_uri,
            )
            while True:
                phase1 = await queued_session._read_phase1_meta(message.archive_uri)
                if phase1.get("status") == "ready" or await queued_session._archive_file_exists(
                    message.archive_uri,
                    ".failed.json",
                ):
                    break
                await asyncio.sleep(0)
            try:
                while not await queued_session.resume_queued_commit(message):
                    await asyncio.sleep(0)
            finally:
                queued_commit_tasks.discard(message.task_id)

        commit_tasks.append(asyncio.create_task(process_commit()))
        return data["task_id"]

    monkeypatch.setattr(queue_manager, "enqueue", enqueue_with_session_commit_fallback)
    yield partial(service.sessions.session, request_context)
    if commit_tasks:
        await asyncio.gather(*commit_tasks, return_exceptions=True)


@pytest_asyncio.fixture(scope="function")
async def session(
    client,
    service: OpenVikingService,
    request_context: RequestContext,
) -> AsyncGenerator[Session, None]:
    """Create new Session"""
    session = await service.sessions.create(request_context)
    yield session


@pytest_asyncio.fixture(scope="function")
async def session_with_messages(
    client,
    service: OpenVikingService,
    request_context: RequestContext,
) -> AsyncGenerator[Session, None]:
    """Create Session with existing messages"""
    session = await service.sessions.create(
        request_context,
        session_id="test_session_with_messages",
    )

    session.add_message("user", [TextPart("Hello, this is a test message.")])
    session.add_message("assistant", [TextPart("Hello! How can I help you today?")])
    session.add_message("user", [TextPart("I need help with testing.")])
    session.add_message("assistant", [TextPart("I can help you with testing.")])

    yield session


@pytest_asyncio.fixture(scope="function")
async def session_with_tool_call(
    client,
    service: OpenVikingService,
    request_context: RequestContext,
) -> AsyncGenerator[tuple[Session, str, str], None]:
    """Create Session with tool call"""
    session = await service.sessions.create(
        request_context,
        session_id="test_session_with_tool",
    )

    tool_id = "test_tool_001"
    tool_part = ToolPart(
        tool_id=tool_id,
        tool_name="test_tool",
        tool_uri=f"{session.uri}/tools/{tool_id}",
        skill_uri="viking://user/skills/test_skill",
        tool_input={"param": "value"},
        tool_status="running",
    )

    msg = session.add_message("assistant", [TextPart("Executing tool..."), tool_part])

    yield session, msg.id, tool_id


@pytest_asyncio.fixture
async def wm_session_with_messages(client, service, request_context, working_memory_policy):
    session = await service.sessions.create(request_context, memory_policy=working_memory_policy)
    for role, text in [
        ("user", "Hello, this is a test message."),
        ("assistant", "Hello! How can I help you today?"),
        ("user", "I need help with testing."),
        ("assistant", "I can help you with testing."),
    ]:
        await session.add_message_async(role, [TextPart(text)])
    yield session