#!/usr/bin/env python3 """AI 프롬프트 수정/초안 API 스모크 테스트 — 호출만 하고 저장은 하지 않는다.""" import json import subprocess import sys import urllib.error import urllib.request BASE = "http://127.0.0.1:8081" fails = [] def check(name, cond, extra=""): print(("OK " if cond else "FAIL ") + name + (f"\n {extra}" if extra else "")) if not cond: fails.append(name) def post(path, payload): request = urllib.request.Request( BASE + path, data=json.dumps(payload).encode(), headers={"Content-Type": "application/json"}, method="POST", ) try: with urllib.request.urlopen(request, timeout=300) as res: return res.status, json.loads(res.read().decode()) except urllib.error.HTTPError as error: body = error.read().decode() try: return error.code, json.loads(body) except json.JSONDecodeError: return error.code, {"raw": body} def git_status(): return subprocess.run(["git", "status", "--porcelain"], cwd="/Users/woozooni/Projects/teamat", capture_output=True, text=True).stdout.strip() before_status = git_status() before_head = subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd="/Users/woozooni/Projects/teamat", capture_output=True, text=True).stdout.strip() # 1) 작품 수정 제안 code, data = post("/api/ai", {"mode": "edit", "slug": "whitework-tea-mat", "prompt": "부제를 더 담백하게, 20자 이내로. 본문은 그대로 둬."}) check("edit 200", code == 200 and data.get("ok") is True, json.dumps(data, ensure_ascii=False)[:400]) if data.get("ok"): check("note 있음", bool(data.get("note")), str(data.get("note"))) check("변경 있음", bool(data.get("changes")), json.dumps({k: v for k, v in data.get("changes", {}).items() if k != "body"}, ensure_ascii=False)) check("부제만 바뀜", set(data.get("changes", {}).keys()) <= {"subtitle"}, str(list(data.get("changes", {}).keys()))) check("본문 유지", "body" not in data.get("changes", {})) print(" 제안:", data.get("note")) for k, v in data.get("changes", {}).items(): print(f" {k}: {str(v)[:120]}") # 2) 본문 다시 쓰기 code, data2 = post("/api/ai", {"mode": "edit", "slug": "whitework-tea-mat", "prompt": "본문을 3문단으로 다시 써. 재료와 걸린 시간을 숫자로 넣어."}) if data2.get("ok"): body = data2.get("changes", {}).get("body", "") check("본문 변경 제안", bool(body), str(list(data2.get("changes", {}).keys()))) check("본문 3문단 근사", body.count("\n\n") >= 1, str(body.count("\n\n"))) print(" 본문 앞부분:", body[:160].replace("\n", " / ")) # 3) 새 작품 초안 code, draft = post("/api/ai", {"mode": "draft", "prompt": "겨울 매화 티매트, 32×48cm, 린넨 100%, 62000원. 눈 내린 창가 분위기."}) check("draft 200", code == 200 and draft.get("ok") is True, json.dumps(draft, ensure_ascii=False)[:400]) if draft.get("ok"): fields = draft.get("fields", {}) check("draft 제목", bool(str(fields.get("title", "")).strip()), str(fields.get("title"))) check("draft slug 형식", bool(fields.get("slug")) and all(c.islower() or c.isdigit() or c == "-" for c in str(fields.get("slug"))), str(fields.get("slug"))) check("draft 본문", len(str(draft.get("body", ""))) > 60, str(len(str(draft.get("body", ""))))) print(" 초안:", fields.get("slug"), "|", fields.get("title"), "|", fields.get("price"), "|", fields.get("size")) # 4) 빈 요청 거부 code, bad = post("/api/ai", {"mode": "edit", "slug": "whitework-tea-mat", "prompt": ""}) check("빈 요청 거부", code == 400, str(code)) # 5) AI 호출이 저장소를 건드리지 않아야 한다 check("git 상태 불변", git_status() == before_status, git_status()) check("HEAD 불변", subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd="/Users/woozooni/Projects/teamat", capture_output=True, text=True).stdout.strip() == before_head) print() print("실패 " + str(len(fails)) + "건: " + ", ".join(fails) if fails else "전부 통과") sys.exit(1 if fails else 0)