File size: 13,305 Bytes
891fe52 | 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | """End-to-end workflow integration tests."""
import json
import shutil
import time
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from polyhedra.server import call_tool, get_services
from polyhedra.services.project_initializer import ProjectInitializer
@pytest.fixture
def test_project():
"""Create a temporary test project."""
with TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
# Initialize project structure
initializer = ProjectInitializer(root)
initializer.initialize("test-project")
yield root
# Force cleanup on Windows
try:
shutil.rmtree(root, ignore_errors=True)
except Exception:
pass
class TestEndToEndWorkflows:
"""Test complete end-to-end workflows."""
@pytest.mark.integration
@pytest.mark.asyncio
async def test_workflow_search_save_index_query(self, test_project, monkeypatch):
"""Test: Search β Save β Index β Query workflow."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
# Step 1: Search for papers
search_result = await call_tool(
"search_papers",
{"query": "machine learning", "limit": 3}
)
papers = json.loads(search_result[0].text)
assert len(papers) > 0
print(f"β Found {len(papers)} papers")
# Step 2: Save papers to file
save_result = await call_tool(
"save_file",
{
"path": "literature/papers.json",
"content": json.dumps(papers, indent=2)
}
)
save_data = json.loads(save_result[0].text)
assert save_data["success"] is True
print(f"β Saved {save_data['bytes_written']} bytes")
# Step 3: Index papers for semantic search
index_result = await call_tool(
"index_papers",
{"papers_path": "literature/papers.json"}
)
index_data = json.loads(index_result[0].text)
assert index_data.get("success") is True
assert index_data["indexed_count"] == len(papers)
print(f"β Indexed {index_data['indexed_count']} papers")
# Step 4: Query similar papers
query_result = await call_tool(
"query_similar_papers",
{"query": "deep learning", "k": 2}
)
similar = json.loads(query_result[0].text)
assert isinstance(similar, list)
assert len(similar) > 0
assert "relevance_score" in similar[0]
print(f"β Found {len(similar)} similar papers")
@pytest.mark.integration
@pytest.mark.asyncio
async def test_workflow_search_add_citations(self, test_project, monkeypatch):
"""Test: Search β Add Citations workflow."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
# Step 1: Search for papers
search_result = await call_tool(
"search_papers",
{"query": "transformer architecture", "limit": 2}
)
papers = json.loads(search_result[0].text)
assert len(papers) > 0
print(f"β Found {len(papers)} papers")
# Step 2: Add citations from papers
citations_added = 0
for paper in papers:
if paper.get("bibtex_entry"):
cite_result = await call_tool(
"add_citation",
{"bibtex": paper["bibtex_entry"]}
)
cite_data = json.loads(cite_result[0].text)
if cite_data.get("added"):
citations_added += 1
print(f"β Added {citations_added} citations")
# Step 3: Verify citations were added
get_citations_result = await call_tool("get_citations", {})
citations = json.loads(get_citations_result[0].text)
assert len(citations) >= citations_added
print(f"β Total citations: {len(citations)}")
@pytest.mark.asyncio
async def test_workflow_init_get_status(self, monkeypatch):
"""Test: Init Project β Get Status workflow."""
tmpdir = TemporaryDirectory(delete=False)
project_root = Path(tmpdir.name)
try:
monkeypatch.chdir(project_root)
# Clear service cache
services = get_services()
services.clear()
# Step 1: Initialize project
init_result = await call_tool(
"init_project",
{"project_name": "test-research"}
)
init_data = json.loads(init_result[0].text)
assert len(init_data["created_dirs"]) > 0
assert len(init_data["created_files"]) > 0
print(f"β Created {len(init_data['created_dirs'])} dirs, {len(init_data['created_files'])} files")
# Step 2: Get project status
status_result = await call_tool("get_project_status", {})
status = json.loads(status_result[0].text)
assert status["papers_count"] == 0
assert status["citations_count"] == 0
assert status["rag_indexed"] is False
assert len(status["standard_files"]) > 0
print(f"β Project status retrieved: {len(status['standard_files'])} standard files tracked")
finally:
# Manual cleanup to avoid Windows recursion issues
import gc
gc.collect() # Force garbage collection first
try:
shutil.rmtree(project_root, ignore_errors=True)
except Exception:
pass
@pytest.mark.asyncio
async def test_workflow_write_read_file(self, test_project, monkeypatch):
"""Test: Write File β Read File workflow."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
# Step 1: Write file
content = "# Research Notes\n\nThese are my notes on ML papers."
write_result = await call_tool(
"save_file",
{"path": "ideas/notes.md", "content": content}
)
write_data = json.loads(write_result[0].text)
assert write_data["success"] is True
print(f"β Wrote {write_data['bytes_written']} bytes")
# Step 2: Read file back
read_result = await call_tool(
"get_context",
{"paths": ["ideas/notes.md"]}
)
read_data = json.loads(read_result[0].text)
assert "ideas/notes.md" in read_data["contents"]
assert read_data["contents"]["ideas/notes.md"] == content
print(f"β Read file successfully, content matches")
# Step 3: Append to file
append_content = "\n\n## More Notes\n\nAdditional thoughts."
append_result = await call_tool(
"save_file",
{
"path": "ideas/notes.md",
"content": append_content,
"append": True
}
)
append_data = json.loads(append_result[0].text)
assert append_data["success"] is True
print(f"β Appended {append_data['bytes_written']} bytes")
# Step 4: Verify appended content
verify_result = await call_tool(
"get_context",
{"paths": ["ideas/notes.md"]}
)
verify_data = json.loads(verify_result[0].text)
full_content = verify_data["contents"]["ideas/notes.md"]
assert content in full_content
assert append_content.strip() in full_content
print(f"β Verified appended content")
class TestPerformance:
"""Test performance targets."""
@pytest.mark.integration
@pytest.mark.skip(reason="External API timing is unreliable, depends on network and rate limits")
@pytest.mark.asyncio
async def test_search_performance(self):
"""Search should complete in < 2s."""
from polyhedra.services.semantic_scholar import SemanticScholarService
service = SemanticScholarService()
try:
start = time.time()
results = await service.search("machine learning", limit=5)
duration = time.time() - start
assert duration < 2.0, f"Search took {duration:.2f}s, expected < 2s"
assert len(results) > 0
print(f"β Search completed in {duration:.3f}s (< 2s target)")
finally:
await service.close()
@pytest.mark.asyncio
async def test_local_operations_performance(self, test_project, monkeypatch):
"""Local operations should complete in < 100ms."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
# Test write file
start = time.time()
await call_tool(
"save_file",
{"path": "test.txt", "content": "test content"}
)
write_duration = time.time() - start
assert write_duration < 0.1, f"Write took {write_duration*1000:.1f}ms, expected < 100ms"
print(f"β Write file: {write_duration*1000:.1f}ms")
# Test read file
start = time.time()
await call_tool("get_context", {"paths": ["test.txt"]})
read_duration = time.time() - start
assert read_duration < 0.1, f"Read took {read_duration*1000:.1f}ms, expected < 100ms"
print(f"β Read file: {read_duration*1000:.1f}ms")
# Test get status
start = time.time()
await call_tool("get_project_status", {})
status_duration = time.time() - start
assert status_duration < 0.1, f"Status took {status_duration*1000:.1f}ms, expected < 100ms"
print(f"β Get status: {status_duration*1000:.1f}ms")
@pytest.mark.integration
@pytest.mark.asyncio
async def test_rag_query_performance(self, test_project, monkeypatch):
"""RAG query should complete in < 500ms."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
# Create sample papers
papers = [
{
"paperId": "1",
"title": "Deep Learning for Computer Vision",
"abstract": "This paper explores deep learning techniques for image recognition.",
"authors": ["Smith, J."],
"year": 2020
},
{
"paperId": "2",
"title": "Transformer Models in NLP",
"abstract": "We present transformer architectures for natural language processing.",
"authors": ["Jones, A."],
"year": 2021
}
]
# Save and index
await call_tool(
"save_file",
{"path": "literature/papers.json", "content": json.dumps(papers)}
)
await call_tool("index_papers", {})
# Test query performance
start = time.time()
result = await call_tool(
"query_similar_papers",
{"query": "deep learning", "k": 2}
)
duration = time.time() - start
assert duration < 0.5, f"RAG query took {duration*1000:.1f}ms, expected < 500ms"
similar = json.loads(result[0].text)
assert len(similar) > 0
print(f"β RAG query: {duration*1000:.1f}ms")
class TestErrorHandling:
"""Test error handling in workflows."""
@pytest.mark.asyncio
async def test_query_before_indexing(self, test_project, monkeypatch):
"""Should handle query before indexing gracefully."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
result = await call_tool(
"query_similar_papers",
{"query": "test"}
)
data = json.loads(result[0].text)
assert "error" in data
assert "not indexed" in data["error"].lower()
print("β Handles query before indexing")
@pytest.mark.asyncio
async def test_missing_file_read(self, test_project, monkeypatch):
"""Should handle missing files gracefully."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
result = await call_tool(
"get_context",
{"paths": ["nonexistent.md"]}
)
data = json.loads(result[0].text)
assert "missing" in data
assert "nonexistent.md" in data["missing"]
print("β Handles missing files")
@pytest.mark.asyncio
async def test_invalid_citation(self, test_project, monkeypatch):
"""Should handle invalid BibTeX gracefully."""
monkeypatch.chdir(test_project)
# Clear service cache
services = get_services()
services.clear()
result = await call_tool(
"add_citation",
{"bibtex": "invalid bibtex"}
)
data = json.loads(result[0].text)
assert "error" in data
print("β Handles invalid BibTeX")
|