84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from django.test import Client
|
||
|
|
from django.urls import reverse
|
||
|
|
|
||
|
|
from control_plane.authoring.models import (
|
||
|
|
DocumentAuthority,
|
||
|
|
DocumentType,
|
||
|
|
Series,
|
||
|
|
StandaloneScene,
|
||
|
|
Work,
|
||
|
|
)
|
||
|
|
from control_plane.authoring.sources import register_source
|
||
|
|
|
||
|
|
|
||
|
|
def api_work() -> Work:
|
||
|
|
series = Series.objects.create(title="Labyrinth Hero", slug="labyrinth-hero")
|
||
|
|
return Work.objects.create(series=series, title="The Fortune Below", slug="the-fortune-below")
|
||
|
|
|
||
|
|
|
||
|
|
def test_scene_api_creates_lists_and_returns_optional_prose() -> None:
|
||
|
|
work = api_work()
|
||
|
|
response = Client().post(
|
||
|
|
reverse("standalone_scenes"),
|
||
|
|
data=json.dumps(
|
||
|
|
{
|
||
|
|
"series_slug": work.series.slug,
|
||
|
|
"work_slug": work.slug,
|
||
|
|
"title": "Office Visit",
|
||
|
|
"brief": "Sabine visits Corin during ordinary work.",
|
||
|
|
"target_words": 1200,
|
||
|
|
}
|
||
|
|
),
|
||
|
|
content_type="application/json",
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 201
|
||
|
|
scene = StandaloneScene.objects.get(id=response.json()["id"])
|
||
|
|
scene.prose = "Draft prose."
|
||
|
|
scene.save()
|
||
|
|
listing = Client().get(reverse("standalone_scenes"))
|
||
|
|
detail = Client().get(reverse("standalone_scene_detail", args=[scene.id]))
|
||
|
|
detail_with_prose = Client().get(
|
||
|
|
reverse("standalone_scene_detail", args=[scene.id]), {"include_prose": "1"}
|
||
|
|
)
|
||
|
|
|
||
|
|
assert listing.status_code == 200
|
||
|
|
assert listing.json()["scenes"][0]["id"] == str(scene.id)
|
||
|
|
assert "prose" not in detail.json()
|
||
|
|
assert detail_with_prose.json()["prose"] == "Draft prose."
|
||
|
|
|
||
|
|
|
||
|
|
def test_scene_api_previews_cited_context_without_model_call(tmp_path: Path) -> None:
|
||
|
|
work = api_work()
|
||
|
|
root = tmp_path / "sources"
|
||
|
|
root.mkdir()
|
||
|
|
source = root / "canon.md"
|
||
|
|
source.write_text("Sabine visits Corin's office during ordinary work.", encoding="utf-8")
|
||
|
|
register_source(
|
||
|
|
work=work,
|
||
|
|
path=source,
|
||
|
|
root=root,
|
||
|
|
authority=DocumentAuthority.CANON,
|
||
|
|
document_type=DocumentType.CANON,
|
||
|
|
)
|
||
|
|
scene = StandaloneScene.objects.create(
|
||
|
|
work=work,
|
||
|
|
scene_key="office-visit",
|
||
|
|
title="Office Visit",
|
||
|
|
brief="Sabine visits Corin's office.",
|
||
|
|
)
|
||
|
|
|
||
|
|
response = Client().post(
|
||
|
|
reverse("standalone_scene_action", args=[scene.id]),
|
||
|
|
data=json.dumps({"action": "context", "authorities": ["canon"]}),
|
||
|
|
content_type="application/json",
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
assert response.json()["citations"][0]["document_key"] == "canon.md"
|