Task 010 implement research manifest flow

This commit is contained in:
2026-05-21 21:56:44 +03:00
parent 5b27d1a3ca
commit 7d4a79acb5
23 changed files with 1063 additions and 26 deletions
@@ -112,8 +112,8 @@ class PlanGenerationReviewPublicApiTest(unittest.TestCase):
f"/api/articles/{article_id}/research/start",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
)
self.assertEqual(202, allowed_research_response.status_code, allowed_research_response.text)
self.assertEqual("RESEARCH", allowed_research_response.json()["jobs"][0]["job_type"])
self.assertEqual(201, allowed_research_response.status_code, allowed_research_response.text)
self.assertEqual("RESEARCH", allowed_research_response.json()["job"]["job_type"])
detail_response = self.client.get(
f"/api/articles/{article_id}",
@@ -0,0 +1,150 @@
from __future__ import annotations
import os
import sys
import tempfile
import unittest
from pathlib import Path
from fastapi.testclient import TestClient
BACKEND_ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(BACKEND_ROOT))
from src.application.seed_data import seed_reference_data # noqa: E402
from src.infrastructure.repositories import open_backend_repository # noqa: E402
from src.presentation.dependencies import get_repository # noqa: E402
from src.presentation.main import app # noqa: E402
DEMO_EDITOR_EMAIL = "editor@example.com"
DEMO_USER_EMAIL_HEADER = "X-Demo-User-Email"
class ResearchManifestPublicApiTest(unittest.TestCase):
def setUp(self) -> None:
self.tmp_dir = tempfile.TemporaryDirectory()
self.storage_root = Path(self.tmp_dir.name) / "object-storage"
os.environ["OBJECT_STORAGE_LOCAL_ROOT"] = str(self.storage_root)
dsn = f"sqlite:///{Path(self.tmp_dir.name) / 'research.db'}"
self.repository = open_backend_repository(dsn)
self.repository.setup()
seed_reference_data(self.repository)
app.dependency_overrides[get_repository] = lambda: self.repository
self.client = TestClient(app)
def tearDown(self) -> None:
app.dependency_overrides.clear()
os.environ.pop("OBJECT_STORAGE_LOCAL_ROOT", None)
self.tmp_dir.cleanup()
def test_approved_plan_research_creates_manifest_with_uploaded_artifacts(self) -> None:
article_id = self._create_article_with_approved_plan()
start_response = self.client.post(
f"/api/articles/{article_id}/research/start",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
)
self.assertEqual(201, start_response.status_code, start_response.text)
body = start_response.json()
self.assertEqual("RESEARCH_RUNNING", body["article"]["status"])
self.assertEqual("RESEARCH", body["job"]["job_type"])
manifest = body["manifest"]
self.assertTrue(manifest["s3_prefix"])
self.assertEqual(article_id, manifest["article_id"])
self.assertEqual(body["job"]["id"], manifest["agent_job_id"])
self.assertGreaterEqual(len(manifest["artifacts"]), 2)
object_keys = [artifact["object_key"] for artifact in manifest["artifacts"]]
content_hashes = [artifact["content_hash"] for artifact in manifest["artifacts"]]
artifact_types = [artifact["artifact_type"] for artifact in manifest["artifacts"]]
source_urls = [artifact["source_url"] for artifact in manifest["artifacts"]]
self.assertEqual(len(object_keys), len(set(object_keys)))
self.assertTrue(all(key.startswith(manifest["s3_prefix"]) for key in object_keys))
self.assertTrue(all(content_hashes))
self.assertIn("source_section_snapshot", artifact_types)
self.assertTrue(all(url.startswith("https://") for url in source_urls))
for artifact in manifest["artifacts"]:
metadata = artifact["metadata"]
self.assertTrue(metadata["source_title"])
self.assertTrue(metadata["domain"])
self.assertTrue(metadata["summary"])
self.assertTrue(metadata["retrieved_at"])
self.assertTrue(metadata["artifact_link"])
self.assertIn("search_path", metadata)
self.assertIn("selection_criteria", metadata)
self.assertIn("head_metadata", metadata)
self.assertIn("server_ip_address", metadata)
self.assertIn("whois_owner", metadata)
self.assertTrue((self.storage_root / artifact["object_key"]).is_file())
list_response = self.client.get(
f"/api/articles/{article_id}/research",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
)
self.assertEqual(200, list_response.status_code, list_response.text)
self.assertEqual([manifest["id"]], [item["id"] for item in list_response.json()["manifests"]])
rerun_response = self.client.post(
f"/api/articles/{article_id}/research/start",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
)
self.assertEqual(201, rerun_response.status_code, rerun_response.text)
self.assertNotEqual(manifest["id"], rerun_response.json()["manifest"]["id"])
self.assertNotEqual(
manifest["s3_prefix"],
rerun_response.json()["manifest"]["s3_prefix"],
)
def _create_article_with_approved_plan(self) -> str:
site = self.client.get(
"/api/sites",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
).json()[0]["site"]
article = self.client.post(
"/api/articles",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
json={
"target_site_id": site["id"],
"brief_description": "Research auditable AI content operations.",
"working_title": "Auditable AI Content Operations",
"content_type": "longform_guide",
"primary_keyword": "auditable AI content",
},
).json()["article"]
article_id = article["id"]
questions = self.client.post(
f"/api/articles/{article_id}/boundary-questions/generate",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
).json()["questions"]
for question in questions:
if question["is_required"]:
response = self.client.patch(
f"/api/articles/{article_id}/boundary-questions/{question['id']}",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
json={"answer": f"Answer for {question['category']}"},
)
self.assertEqual(200, response.status_code, response.text)
self.client.post(
f"/api/articles/{article_id}/boundary-questions/submit",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
)
plan = self.client.post(
f"/api/articles/{article_id}/plan/generate",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
).json()["plan"]
approve_response = self.client.post(
f"/api/articles/{article_id}/plans/{plan['id']}/approve",
headers={DEMO_USER_EMAIL_HEADER: DEMO_EDITOR_EMAIL},
)
self.assertEqual(200, approve_response.status_code, approve_response.text)
return article_id
if __name__ == "__main__":
unittest.main()