From 1e4c1b42b780ffecf6d1c617eda58aa7a920ea9f Mon Sep 17 00:00:00 2001 From: gahusb Date: Sat, 23 May 2026 02:50:33 +0900 Subject: [PATCH] =?UTF-8?q?fix(insta-lab):=20=ED=94=84=EB=A1=AC=ED=94=84?= =?UTF-8?q?=ED=8A=B8=20=ED=85=9C=ED=94=8C=EB=A6=BF=20GET=EC=9D=B4=20?= =?UTF-8?q?=EB=AF=B8=EC=A0=80=EC=9E=A5=20=EC=8B=9C=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=B3=B8=EA=B0=92=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- insta-lab/app/main.py | 34 ++++++++++- insta-lab/tests/test_main_prompt_defaults.py | 63 ++++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 insta-lab/tests/test_main_prompt_defaults.py diff --git a/insta-lab/app/main.py b/insta-lab/app/main.py index 49fb1dd..e8998e9 100644 --- a/insta-lab/app/main.py +++ b/insta-lab/app/main.py @@ -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}") diff --git a/insta-lab/tests/test_main_prompt_defaults.py b/insta-lab/tests/test_main_prompt_defaults.py new file mode 100644 index 0000000..e4376de --- /dev/null +++ b/insta-lab/tests/test_main_prompt_defaults.py @@ -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")