여러 줄 주석이 설명보다 경위(예전·실측·지적)를 적고 있어 읽는 사람이 결론을 찾기 어려웠다. - ts·tsx·js·mjs·css·py 478개: 여러 줄 주석은 첫 문장 한 줄로, 과거형·날짜 문장은 삭제 - 주석 위치는 TypeScript 파서·파이썬 tokenize/ast 로 찾는다 — 문자열 안의 # · /* 는 건드리지 않는다 - eslint·ts·noqa·type: ignore 같은 지시 주석은 그대로 둔다 파이썬 275개 정리 전후 AST 동일, TS 298개 주석 뺀 토큰 동일(빈 JSX 주석 10곳만 차이). site·frontend·admin tsc, site vitest 105 passed Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
159 lines
7.7 KiB
Python
159 lines
7.7 KiB
Python
"""FAQ 조회·승인 e2e."""
|
|
import uuid
|
|
|
|
from sqlalchemy import text
|
|
|
|
from common.enums import ErrorType, FactStatus, SourceType
|
|
|
|
|
|
async def _place(client, headers, name="FAQ펜션"):
|
|
r = await client.post("/v1/place", headers=headers, json={"name": name, "category": 1})
|
|
return r.json()["place"]["place_id"]
|
|
|
|
|
|
async def _seed_generated_faq(db_engine, pid, question="체크인은 몇 시인가요?", answer="15시입니다.", order=0):
|
|
"""LLM 출처 · UNVERIFIED 인 FAQ 한 건."""
|
|
fid = uuid.uuid4()
|
|
async with db_engine.begin() as conn:
|
|
await conn.execute(
|
|
text("INSERT INTO place_faqs (faq_id, place_id, question, answer, source_fact_ids, generated_by, status, sort_order) "
|
|
"VALUES (:f, :p, :q, :a, CAST(:k AS jsonb), :g, :st, :o)"),
|
|
{"f": fid, "p": uuid.UUID(pid), "q": question, "a": answer, "k": '["check_in_time"]',
|
|
"g": SourceType.LLM.value, "st": FactStatus.UNVERIFIED.value, "o": order},
|
|
)
|
|
return str(fid)
|
|
|
|
|
|
async def _list(client, headers, pid, **params):
|
|
return (await client.get(f"/v1/place/{pid}/faq/list", headers=headers, params=params)).json()
|
|
|
|
|
|
async def _transition(client, headers, pid, fid, body):
|
|
return (await client.post(f"/v1/place/{pid}/faq/{fid}/transition", headers=headers, json=body)).json()
|
|
|
|
|
|
# ── 조회 ─────────────────────────────────────────────────────────────────
|
|
async def test_generated_faq_is_pending_and_not_publishable(auth_headers, client, db_engine):
|
|
"""검증: LLM 이 만든 UNVERIFIED FAQ 를 조회한다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
await _seed_generated_faq(db_engine, pid)
|
|
|
|
body = await _list(client, h, pid)
|
|
assert len(body["faqs"]) == 1
|
|
assert body["faqs"][0]["status"] == FactStatus.UNVERIFIED.value
|
|
assert body["publishable"] == 0
|
|
assert body["pending_review"] == 1
|
|
# 사이트 빌드가 보는 것과 같은 집합 — 승인 전에는 비어 있어야 한다.
|
|
assert (await _list(client, h, pid, publishable_only=True)).get("faqs", []) == []
|
|
|
|
|
|
async def test_other_owner_cannot_read_or_touch_faq(auth_headers, client, db_engine):
|
|
"""검증: 남의 사용자가 place_id 를 알아내 FAQ 를 조회·전이한다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
fid = await _seed_generated_faq(db_engine, pid)
|
|
|
|
other = await auth_headers("u2")
|
|
assert (await _list(client, other, pid))["result"]["code"] == ErrorType.PLACE_NOT_FOUND.value
|
|
body = await _transition(client, other, pid, fid, {"status": FactStatus.VERIFIED.value})
|
|
assert body["result"]["code"] == ErrorType.PLACE_NOT_FOUND.value
|
|
|
|
|
|
# ── 승인 ─────────────────────────────────────────────────────────────────
|
|
async def test_approving_faq_makes_it_publishable(auth_headers, client, db_engine):
|
|
"""검증: UNVERIFIED FAQ 를 VERIFIED 로 승인한다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
fid = await _seed_generated_faq(db_engine, pid)
|
|
|
|
body = await _transition(client, h, pid, fid, {"status": FactStatus.VERIFIED.value})
|
|
assert body["result"]["success"] is True
|
|
assert body["faq"]["status"] == FactStatus.VERIFIED.value
|
|
assert (await _list(client, h, pid))["publishable"] == 1
|
|
place = (await client.get(f"/v1/place/{pid}", headers=h)).json()["place"]
|
|
assert place["content_updated_at"] is not None
|
|
|
|
|
|
async def test_illegal_transition_is_rejected(auth_headers, client, db_engine):
|
|
"""검증: UNVERIFIED → CORRECTED 처럼 전이표에 없는 이동."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
fid = await _seed_generated_faq(db_engine, pid)
|
|
|
|
body = await _transition(client, h, pid, fid, {"status": FactStatus.CORRECTED.value, "answer": "15:00 입니다."})
|
|
assert body["result"]["code"] == ErrorType.FACT_INVALID_TRANSITION.value
|
|
|
|
|
|
async def test_correction_requires_new_text(auth_headers, client, db_engine):
|
|
"""검증: 고친 문구 없이 CORRECTED 로 보낸다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
fid = await _seed_generated_faq(db_engine, pid)
|
|
await _transition(client, h, pid, fid, {"status": FactStatus.VERIFIED.value})
|
|
|
|
body = await _transition(client, h, pid, fid, {"status": FactStatus.CORRECTED.value})
|
|
assert body["result"]["code"] == ErrorType.INVALID_REQUEST_DATA.value
|
|
|
|
|
|
async def test_corrected_faq_is_locked_against_regeneration(auth_headers, client, db_engine):
|
|
"""검증: 사장님이 문구를 고쳐 승인(CORRECTED)한 뒤 COPY 잡이 재생성을 돌린다."""
|
|
from common.database.db_session_manager import DB_SESSION_MNG
|
|
from common.database.model.models import place_faqs
|
|
from common.utils.gtime import GTime
|
|
from crud.faq_crud import FaqCRUD
|
|
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
fid = await _seed_generated_faq(db_engine, pid)
|
|
await _transition(client, h, pid, fid, {"status": FactStatus.VERIFIED.value})
|
|
|
|
body = await _transition(client, h, pid, fid, {"status": FactStatus.CORRECTED.value, "answer": "15시 이후 입실입니다."})
|
|
assert body["faq"]["status"] == FactStatus.CORRECTED.value
|
|
assert body["faq"]["answer"] == "15시 이후 입실입니다."
|
|
assert body["faq"]["generated_by"] == SourceType.OWNER.value
|
|
|
|
# 재생성이 미확인 FAQ 를 내리는 단계(COPY 잡이 실제로 부르는 그 함수).
|
|
await DB_SESSION_MNG.execute_lambda_claim(
|
|
place_faqs.DBType(),
|
|
lambda s: FaqCRUD().expire_generated(s, uuid.UUID(pid), GTime.UTC()),
|
|
)
|
|
after = await _list(client, h, pid, publishable_only=True)
|
|
assert [f["answer"] for f in after["faqs"]] == ["15시 이후 입실입니다."]
|
|
|
|
|
|
# ── 직접 추가 ────────────────────────────────────────────────────────────
|
|
async def test_owner_written_faq_is_published_immediately(auth_headers, client):
|
|
"""검증: 사장님이 FAQ 를 직접 쓴다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
|
|
body = (await client.post(f"/v1/place/{pid}/faq", headers=h,
|
|
json={"question": "주차 되나요?", "answer": "무료 주차 가능합니다."})).json()
|
|
assert body["faq"]["status"] == FactStatus.VERIFIED.value
|
|
assert body["faq"]["generated_by"] == SourceType.OWNER.value
|
|
assert body["faq"]["sort_order"] == 0
|
|
assert (await _list(client, h, pid))["publishable"] == 1
|
|
|
|
|
|
async def test_owner_faq_appends_after_existing_ones(auth_headers, client, db_engine):
|
|
"""검증: 이미 FAQ 가 있는 사업장에 순서 없이 추가한다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
await _seed_generated_faq(db_engine, pid, order=0)
|
|
await _seed_generated_faq(db_engine, pid, question="와이파이 되나요?", answer="됩니다.", order=1)
|
|
|
|
body = (await client.post(f"/v1/place/{pid}/faq", headers=h,
|
|
json={"question": "주차 되나요?", "answer": "무료 주차 가능합니다."})).json()
|
|
assert body["faq"]["sort_order"] == 2
|
|
|
|
|
|
async def test_blank_faq_is_rejected(auth_headers, client):
|
|
"""검증: 공백만 있는 질문으로 FAQ 를 추가한다."""
|
|
h = await auth_headers("u1")
|
|
pid = await _place(client, h)
|
|
|
|
body = (await client.post(f"/v1/place/{pid}/faq", headers=h,
|
|
json={"question": " ", "answer": "무료 주차 가능합니다."})).json()
|
|
assert body["result"]["code"] == ErrorType.INVALID_REQUEST_DATA.value
|