Skip to content

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

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

"""Tests for error scenarios: invalid JSON, missing fields, error mapping."""

import httpx


def _assert_invalid_request_response(resp: httpx.Response):
    assert resp.status_code == 400
    body = resp.json()
    assert body["status"] == "error"
    assert body["error"]["code"] == "INVALID_ARGUMENT"
    assert body["error"]["message"]
    assert body["error"]["details"]["validation_errors"]
    return body


async def test_invalid_json_body(client: httpx.AsyncClient):
    """Sending invalid JSON should return a structured invalid argument error."""
    resp = await client.post(
        "/api/v1/resources",
        content=b"not-valid-json",
        headers={"Content-Type": "application/json"},
    )
    _assert_invalid_request_response(resp)


async def test_missing_required_field(client: httpx.AsyncClient):
    """Missing required 'path' field should return a structured invalid argument error."""
    resp = await client.post(
        "/api/v1/resources",
        json={"reason": "test"},  # missing 'path'
    )
    body = _assert_invalid_request_response(resp)
    assert "path" in body["error"]["message"]


async def test_not_found_resource_returns_structured_error(
    client: httpx.AsyncClient,
):
    """Accessing non-existent resource should return structured error."""
    resp = await client.get(
        "/api/v1/fs/stat",
        params={"uri": "viking://does_not_exist"},
    )
    assert resp.status_code == 404
    body = resp.json()
    assert body["status"] == "error"
    assert "code" in body["error"]
    assert "message" in body["error"]


async def test_missing_task_returns_structured_error(client: httpx.AsyncClient):
    """Missing task should use the standard error envelope."""
    resp = await client.get("/api/v1/tasks/missing-task-id")

    assert resp.status_code == 404
    body = resp.json()
    assert body["status"] == "error"
    assert body["error"]["code"] == "NOT_FOUND"
    assert body["error"]["message"] == "Task not found or expired"


async def test_add_resource_file_not_found(client: httpx.AsyncClient):
    """Adding a resource with non-existent file path.

    HTTP server no longer accepts direct host filesystem paths.
    """
    resp = await client.post(
        "/api/v1/resources",
        json={"path": "/tmp/nonexistent_file_xyz_12345.md", "reason": "test"},
    )
    body = resp.json()
    assert resp.status_code == 403
    assert body["status"] == "error"
    assert body["error"]["code"] == "PERMISSION_DENIED"


async def test_empty_body_on_post(client: httpx.AsyncClient):
    """POST with empty body should return a structured invalid argument error."""
    resp = await client.post(
        "/api/v1/resources",
        content=b"",
        headers={"Content-Type": "application/json"},
    )
    _assert_invalid_request_response(resp)


async def test_wrong_content_type(client: httpx.AsyncClient):
    """POST with wrong content type should return a structured invalid argument error."""
    resp = await client.post(
        "/api/v1/resources",
        content=b"path=/tmp/test",
        headers={"Content-Type": "text/plain"},
    )
    _assert_invalid_request_response(resp)


async def test_invalid_uri_format(client: httpx.AsyncClient):
    """Public URI fields reject filesystem-like paths without a Viking scheme."""
    for uri in ("resources", "/resources"):
        resp = await client.get(
            "/api/v1/fs/ls",
            params={"uri": uri},
        )
        assert resp.status_code == 400
        body = resp.json()
        assert body["error"]["code"] == "INVALID_URI"
        assert "viking://" in body["error"]["message"]


async def test_export_nonexistent_uri(client: httpx.AsyncClient):
    """Exporting a non-existent URI triggers unhandled AGFSClientError.

    BUG: The server should catch AGFSClientError and return a structured error,
    but currently it propagates as an unhandled 500.
    """
    # Just verify the export endpoint is reachable with valid params
    # (actual export of nonexistent URI is a known unhandled error)
    resp = await client.post(
        "/api/v1/pack/export",
        json={"uri": "viking://", "to": "/tmp/test_export.ovpack"},
    )
    # Root URI export may succeed or fail, but should not crash
    assert resp.status_code in (200, 400, 404, 500)