Spaces:
Sleeping
Sleeping
File size: 3,986 Bytes
bd0c393 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | from unittest.mock import AsyncMock, patch
import pytest
from fastapi.testclient import TestClient
@pytest.fixture
def client():
"""Create test client after environment variables have been cleared by conftest."""
from api.main import app
return TestClient(app)
class TestNoteCreation:
"""Test suite for Note API endpoints."""
@patch("api.routers.notes.Note")
def test_create_note_returns_command_id(self, mock_note_cls, client):
"""Test that creating a note returns the embed command_id."""
mock_note = AsyncMock()
mock_note.id = "note:abc123"
mock_note.title = "Test Note"
mock_note.content = "Some content"
mock_note.note_type = "human"
mock_note.created = "2026-01-01T00:00:00Z"
mock_note.updated = "2026-01-01T00:00:00Z"
mock_note.save.return_value = "command:embed123"
mock_note.add_to_notebook = AsyncMock()
mock_note_cls.return_value = mock_note
response = client.post(
"/api/notes",
json={"content": "Some content", "note_type": "human"},
)
assert response.status_code == 200
data = response.json()
assert data["command_id"] == "command:embed123"
assert data["id"] == "note:abc123"
@patch("api.routers.notes.Note")
def test_create_note_command_id_none_when_no_content_embedding(
self, mock_note_cls, client
):
"""Test that command_id is None when save returns None (no embedding)."""
mock_note = AsyncMock()
mock_note.id = "note:abc456"
mock_note.title = "Empty Note"
mock_note.content = "Some content"
mock_note.note_type = "human"
mock_note.created = "2026-01-01T00:00:00Z"
mock_note.updated = "2026-01-01T00:00:00Z"
mock_note.save.return_value = None
mock_note.add_to_notebook = AsyncMock()
mock_note_cls.return_value = mock_note
response = client.post(
"/api/notes",
json={"content": "Some content", "note_type": "human"},
)
assert response.status_code == 200
data = response.json()
assert data["command_id"] is None
class TestNoteUpdate:
"""Test suite for Note update endpoint."""
@patch("api.routers.notes.Note")
def test_update_note_returns_command_id(self, mock_note_cls, client):
"""Test that updating a note returns the embed command_id."""
mock_note = AsyncMock()
mock_note.id = "note:abc123"
mock_note.title = "Test Note"
mock_note.content = "Original content"
mock_note.note_type = "human"
mock_note.created = "2026-01-01T00:00:00Z"
mock_note.updated = "2026-01-01T00:00:00Z"
mock_note.save.return_value = "command:embed789"
mock_note_cls.get = AsyncMock(return_value=mock_note)
response = client.put(
"/api/notes/note:abc123",
json={"content": "Updated content"},
)
assert response.status_code == 200
data = response.json()
assert data["command_id"] == "command:embed789"
@patch("api.routers.notes.Note")
def test_update_note_command_id_none_when_no_embedding(
self, mock_note_cls, client
):
"""Test that command_id is None on update when no embedding is triggered."""
mock_note = AsyncMock()
mock_note.id = "note:abc123"
mock_note.title = "Test Note"
mock_note.content = "Some content"
mock_note.note_type = "human"
mock_note.created = "2026-01-01T00:00:00Z"
mock_note.updated = "2026-01-01T00:00:00Z"
mock_note.save.return_value = None
mock_note_cls.get = AsyncMock(return_value=mock_note)
response = client.put(
"/api/notes/note:abc123",
json={"title": "Updated Title"},
)
assert response.status_code == 200
data = response.json()
assert data["command_id"] is None
|