fix(insta-lab): 프롬프트 템플릿 GET이 미저장 시 코드 기본값 반환
slate_writer/category_seeds가 DB에 없으면 404 대신 생성 파이프라인이 실제 폴백하는 코드 기본값(card_writer.DEFAULT_PROMPT, DEFAULT_CATEGORY_SEEDS)을 is_default=true로 반환. 편집 UI가 마스터 프롬프트를 표시·수정 가능. 미지정 이름은 여전히 404. 테스트 4건. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -271,12 +271,40 @@ class TemplateBody(BaseModel):
|
||||
description: str = ""
|
||||
|
||||
|
||||
def _default_prompt_templates() -> dict:
|
||||
"""DB에 저장된 override가 없을 때 노출할 코드 기본값.
|
||||
생성 파이프라인이 실제로 폴백하는 값과 동일한 단일 소스를 사용."""
|
||||
return {
|
||||
"slate_writer": {
|
||||
"template": card_writer.DEFAULT_PROMPT,
|
||||
"description": "카드 10페이지 카피 생성 마스터 프롬프트 (Claude Sonnet). "
|
||||
"{category}/{keyword}/{articles} 치환자 필수.",
|
||||
},
|
||||
"category_seeds": {
|
||||
"template": json.dumps(DEFAULT_CATEGORY_SEEDS, ensure_ascii=False, indent=2),
|
||||
"description": "트렌드 수집·분류용 카테고리별 시드 키워드 (JSON). "
|
||||
"최상위 키가 분류 라벨로도 쓰임.",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/insta/templates/prompts/{name}")
|
||||
def get_prompt(name: str):
|
||||
pt = db.get_prompt_template(name)
|
||||
if not pt:
|
||||
raise HTTPException(404)
|
||||
return pt
|
||||
if pt:
|
||||
return pt
|
||||
# DB override 없음 → 코드 기본값 노출 (편집 UI가 마스터 프롬프트를 보고 수정 가능)
|
||||
defaults = _default_prompt_templates()
|
||||
if name in defaults:
|
||||
d = defaults[name]
|
||||
return {
|
||||
"name": name,
|
||||
"template": d["template"],
|
||||
"description": d["description"],
|
||||
"updated_at": None,
|
||||
"is_default": True,
|
||||
}
|
||||
raise HTTPException(404)
|
||||
|
||||
|
||||
@app.put("/api/insta/templates/prompts/{name}")
|
||||
|
||||
63
insta-lab/tests/test_main_prompt_defaults.py
Normal file
63
insta-lab/tests/test_main_prompt_defaults.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import os
|
||||
import gc
|
||||
import json
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import db as db_module
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(monkeypatch):
|
||||
fd, path = tempfile.mkstemp(suffix=".db")
|
||||
os.close(fd)
|
||||
monkeypatch.setattr(db_module, "DB_PATH", path)
|
||||
db_module.init_db()
|
||||
from app import main
|
||||
monkeypatch.setattr(main, "DB_PATH", path)
|
||||
with TestClient(main.app) as c:
|
||||
yield c
|
||||
gc.collect()
|
||||
for ext in ("", "-wal", "-shm"):
|
||||
try:
|
||||
os.remove(path + ext)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def test_get_slate_writer_returns_default_when_unset(client):
|
||||
"""DB에 없으면 코드 기본 마스터 프롬프트를 200으로 반환 (404 아님)."""
|
||||
resp = client.get("/api/insta/templates/prompts/slate_writer")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["is_default"] is True
|
||||
assert "{keyword}" in body["template"]
|
||||
assert "{category}" in body["template"]
|
||||
|
||||
|
||||
def test_get_category_seeds_returns_default_when_unset(client):
|
||||
"""category_seeds 기본값은 유효한 JSON (카테고리→시드 배열)."""
|
||||
resp = client.get("/api/insta/templates/prompts/category_seeds")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["is_default"] is True
|
||||
seeds = json.loads(body["template"])
|
||||
assert "economy" in seeds and isinstance(seeds["economy"], list)
|
||||
|
||||
|
||||
def test_get_unknown_prompt_still_404(client):
|
||||
resp = client.get("/api/insta/templates/prompts/does_not_exist")
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
def test_saved_template_overrides_default(client):
|
||||
"""PUT로 저장하면 이후 GET은 저장본(is_default 없음)을 반환."""
|
||||
client.put("/api/insta/templates/prompts/slate_writer",
|
||||
json={"template": "내 커스텀 프롬프트", "description": "custom"})
|
||||
resp = client.get("/api/insta/templates/prompts/slate_writer")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["template"] == "내 커스텀 프롬프트"
|
||||
assert not body.get("is_default")
|
||||
Reference in New Issue
Block a user