285 lines
9.4 KiB
Python
285 lines
9.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
import jwt
|
|
from fastapi.testclient import TestClient
|
|
|
|
from graphrag_platform.api import create_app
|
|
from graphrag_platform.auth import Permission
|
|
from graphrag_platform.config import Settings
|
|
from graphrag_platform.runtime import StageContext
|
|
from graphrag_platform.worker import work_once
|
|
|
|
from conftest import FakeLLM
|
|
|
|
|
|
def _token(settings: Settings, mask: int) -> str:
|
|
now = datetime.now(timezone.utc)
|
|
return jwt.encode(
|
|
{
|
|
"iss": settings.internal_jwt_issuer,
|
|
"aud": settings.internal_jwt_audience,
|
|
"sub": "dev-test-client",
|
|
"iat": now,
|
|
"exp": now + timedelta(minutes=10),
|
|
"jti": str(uuid4()),
|
|
"features": {settings.internal_feature_name: mask},
|
|
},
|
|
settings.internal_jwt_secret,
|
|
algorithm="HS256",
|
|
)
|
|
|
|
|
|
def test_bff_contract_and_worker_execute_durable_job(tmp_path: Path) -> None:
|
|
source = tmp_path / "source.jsonl"
|
|
source.write_text(
|
|
json.dumps(
|
|
{
|
|
"schema_version": "1.0",
|
|
"external_id": "atlas",
|
|
"corpus": "api-corpus",
|
|
"url": "https://docs.example/atlas",
|
|
"retrieved_at": "2026-10-13T18:00:00Z",
|
|
"body": (
|
|
"<html><head><title>Atlas</title></head><body><main>"
|
|
"Acme created Atlas for graph retrieval.</main></body></html>"
|
|
),
|
|
}
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
settings = Settings(
|
|
_env_file=None,
|
|
database_url=f"sqlite:///{tmp_path / 'api.db'}",
|
|
auto_create_schema=True,
|
|
artifact_dir=tmp_path / "artifacts",
|
|
llm_embedding_dimensions=3,
|
|
internal_jwt_secret="dev-test-secret-at-least-32-bytes-long",
|
|
)
|
|
app = create_app(settings, llm=FakeLLM())
|
|
full_token = _token(
|
|
settings,
|
|
int(Permission.READ | Permission.ADD | Permission.EXECUTE),
|
|
)
|
|
|
|
with TestClient(app) as client:
|
|
assert client.get("/internal/knowledge-graph/health").status_code == 200
|
|
assert (
|
|
client.post(
|
|
"/internal/knowledge-graph/jobs",
|
|
json={
|
|
"corpus": "api-corpus",
|
|
"from_stage": 1,
|
|
"through_stage": 8,
|
|
"input_jsonl": str(source),
|
|
},
|
|
).status_code
|
|
== 401
|
|
)
|
|
execute_only = _token(settings, int(Permission.EXECUTE))
|
|
assert (
|
|
client.post(
|
|
"/internal/knowledge-graph/jobs",
|
|
headers={"Authorization": f"Bearer {execute_only}"},
|
|
json={
|
|
"corpus": "api-corpus",
|
|
"from_stage": 1,
|
|
"through_stage": 8,
|
|
"input_jsonl": str(source),
|
|
},
|
|
).status_code
|
|
== 403
|
|
)
|
|
queued = client.post(
|
|
"/internal/knowledge-graph/jobs",
|
|
headers={"Authorization": f"Bearer {full_token}"},
|
|
json={
|
|
"corpus": "api-corpus",
|
|
"from_stage": 1,
|
|
"through_stage": 8,
|
|
"input_jsonl": str(source),
|
|
},
|
|
)
|
|
assert queued.status_code == 202
|
|
job_id = queued.json()["job_id"]
|
|
|
|
assert work_once(StageContext.create(settings, llm=FakeLLM()))
|
|
|
|
status_response = client.get(
|
|
f"/internal/knowledge-graph/jobs/{job_id}",
|
|
headers={"Authorization": f"Bearer {full_token}"},
|
|
)
|
|
assert status_response.status_code == 200
|
|
assert status_response.json()["status"] == "succeeded"
|
|
assert len(status_response.json()["stages"]) == 8
|
|
|
|
answer = client.post(
|
|
"/internal/knowledge-graph/answer",
|
|
headers={"Authorization": f"Bearer {full_token}"},
|
|
json={"corpus": "api-corpus", "question": "Who created Atlas?"},
|
|
)
|
|
assert answer.status_code == 200
|
|
assert answer.json()["answer"] == "Acme created Atlas. [S1]"
|
|
|
|
|
|
def test_failed_job_can_be_repaired_and_retried(tmp_path: Path) -> None:
|
|
source = tmp_path / "retry-source.jsonl"
|
|
source.write_text(
|
|
json.dumps(
|
|
{
|
|
"schema_version": "2.0",
|
|
"external_id": "retry-record",
|
|
"corpus": "retry-corpus",
|
|
"url": "https://docs.example/retry",
|
|
"retrieved_at": "2026-10-13T18:00:00Z",
|
|
"body": "invalid schema",
|
|
}
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
settings = Settings(
|
|
_env_file=None,
|
|
database_url=f"sqlite:///{tmp_path / 'retry.db'}",
|
|
auto_create_schema=True,
|
|
artifact_dir=tmp_path / "artifacts",
|
|
llm_embedding_dimensions=3,
|
|
internal_jwt_secret="dev-test-secret-at-least-32-bytes-long",
|
|
)
|
|
app = create_app(settings, llm=FakeLLM())
|
|
token = _token(
|
|
settings,
|
|
int(Permission.READ | Permission.ADD | Permission.EXECUTE),
|
|
)
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
with TestClient(app) as client:
|
|
queued = client.post(
|
|
"/internal/knowledge-graph/jobs",
|
|
headers=headers,
|
|
json={
|
|
"corpus": "retry-corpus",
|
|
"from_stage": 1,
|
|
"through_stage": 1,
|
|
"input_jsonl": str(source),
|
|
},
|
|
)
|
|
job_id = queued.json()["job_id"]
|
|
assert work_once(StageContext.create(settings, llm=FakeLLM()))
|
|
assert (
|
|
client.get(
|
|
f"/internal/knowledge-graph/jobs/{job_id}",
|
|
headers=headers,
|
|
).json()["status"]
|
|
== "failed"
|
|
)
|
|
|
|
source.write_text(
|
|
json.dumps(
|
|
{
|
|
"schema_version": "1.0",
|
|
"external_id": "retry-record",
|
|
"corpus": "retry-corpus",
|
|
"url": "https://docs.example/retry",
|
|
"retrieved_at": "2026-10-13T18:00:00Z",
|
|
"content_type": "text/plain",
|
|
"body": "Corrected source content.",
|
|
}
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
retried = client.post(
|
|
f"/internal/knowledge-graph/jobs/{job_id}/retry",
|
|
headers=headers,
|
|
)
|
|
assert retried.status_code == 202
|
|
assert work_once(StageContext.create(settings, llm=FakeLLM()))
|
|
status_response = client.get(
|
|
f"/internal/knowledge-graph/jobs/{job_id}",
|
|
headers=headers,
|
|
)
|
|
assert status_response.json()["status"] == "succeeded"
|
|
assert status_response.json()["attempt"] == 2
|
|
|
|
|
|
def test_retry_resumes_after_last_successful_stage(tmp_path: Path) -> None:
|
|
source = tmp_path / "resume-source.jsonl"
|
|
source.write_text(
|
|
json.dumps(
|
|
{
|
|
"schema_version": "1.0",
|
|
"external_id": "resume-record",
|
|
"corpus": "resume-corpus",
|
|
"url": "https://docs.example/resume",
|
|
"retrieved_at": "2026-10-13T18:00:00Z",
|
|
"content_type": "text/plain",
|
|
"body": "Acme created Atlas for graph retrieval.",
|
|
}
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
model_file = tmp_path / "model.json"
|
|
model_file.write_text("[]", encoding="utf-8")
|
|
settings = Settings(
|
|
_env_file=None,
|
|
database_url=f"sqlite:///{tmp_path / 'resume.db'}",
|
|
auto_create_schema=True,
|
|
artifact_dir=tmp_path / "artifacts",
|
|
llm_embedding_dimensions=3,
|
|
internal_jwt_secret="dev-test-secret-at-least-32-bytes-long",
|
|
)
|
|
app = create_app(settings, llm=FakeLLM())
|
|
token = _token(
|
|
settings,
|
|
int(Permission.READ | Permission.ADD | Permission.EXECUTE),
|
|
)
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
with TestClient(app) as client:
|
|
queued = client.post(
|
|
"/internal/knowledge-graph/jobs",
|
|
headers=headers,
|
|
json={
|
|
"corpus": "resume-corpus",
|
|
"from_stage": 1,
|
|
"through_stage": 3,
|
|
"input_jsonl": str(source),
|
|
"model_file": str(model_file),
|
|
},
|
|
)
|
|
job_id = queued.json()["job_id"]
|
|
assert work_once(StageContext.create(settings, llm=FakeLLM()))
|
|
failed = client.get(
|
|
f"/internal/knowledge-graph/jobs/{job_id}", headers=headers
|
|
).json()
|
|
assert failed["status"] == "failed"
|
|
|
|
model_file.write_text(
|
|
json.dumps(settings.model_definition()),
|
|
encoding="utf-8",
|
|
)
|
|
assert (
|
|
client.post(
|
|
f"/internal/knowledge-graph/jobs/{job_id}/retry",
|
|
headers=headers,
|
|
).status_code
|
|
== 202
|
|
)
|
|
assert work_once(StageContext.create(settings, llm=FakeLLM()))
|
|
succeeded = client.get(
|
|
f"/internal/knowledge-graph/jobs/{job_id}", headers=headers
|
|
).json()
|
|
|
|
assert succeeded["status"] == "succeeded"
|
|
stage_numbers = [row["stage"] for row in succeeded["stages"]]
|
|
assert stage_numbers.count(1) == 1
|
|
assert stage_numbers.count(2) == 1
|
|
assert stage_numbers.count(3) == 2
|