"""사진 분석(VISION) — 잡 배선과 ★ 신뢰도 게이트. ★ 이 잡이 절대 하면 안 되는 것: - 신뢰도 낮은 라벨을 자동 반영하는 것 (사람 확인 큐를 건너뛰는 것) - 한 장 실패로 잡 전체를 실패시키는 것 - 같은 사진을 재분석해 요금을 두 번 내는 것 """ import uuid import pytest from sqlalchemy import text from common.enums import ErrorType, JobStatus, JobType, MediaStatus, PlaceCategory, SourceType from crud.job_crud import JobQueue from services.external import gemini from worker.handlers import build_handler from worker.runner import Worker async def _place_with_media(client, h, db_engine, n=3, kakao="v1"): pid = (await client.post("/v1/place", headers=h, json={"name": "비전펜션", "category": 1})).json()["place"]["place_id"] await client.post(f"/v1/place/{pid}/verify", headers=h, json={"external_place_id": kakao, "source": 1}) async with db_engine.begin() as c: for i in range(n): await c.execute( text("INSERT INTO place_photos (media_id, place_id, url, origin_url, source_type, status, sort_order) " "VALUES (:m, :p, :u, :u, :s, :st, :o)"), {"m": uuid.uuid4(), "p": uuid.UUID(pid), "u": f"https://cdn.test/{pid}/{i}.jpg", "s": SourceType.CRAWL.value, "st": MediaStatus.PENDING_REVIEW.value, "o": i}, ) return pid async def _media_rows(db_engine, pid): async with db_engine.begin() as c: return (await c.execute( text("SELECT origin_url, label, alt_text, vision_confidence, status FROM place_photos " "WHERE place_id = :p ORDER BY sort_order"), {"p": uuid.UUID(pid)}, )).all() def _fake_analyze(results): """gemini.analyze_images 를 대체 — 실제 API 를 때리지 않는다.""" async def _fn(images, **kw): out = [] for img in images: r = results.get(img.origin_url) out.append(r if r else gemini.VisionResult( origin_url=img.origin_url, label=None, alt_text=None, confidence=0.0, needs_review=True, ok=False, error="no result")) return out return _fn async def test_high_confidence_is_auto_applied(auth_headers, client, db_engine, monkeypatch): """검증: 신뢰도 높은 분석 결과. 기대결과: 라벨·alt 가 반영되고 status 가 APPROVED 로 올라간다.""" h = await auth_headers("u1") pid = await _place_with_media(client, h, db_engine, n=2) urls = [r[0] for r in await _media_rows(db_engine, pid)] monkeypatch.setattr(gemini, "is_configured", lambda: True) monkeypatch.setattr(gemini, "analyze_images", _fake_analyze({ urls[0]: gemini.VisionResult(urls[0], "A동 침실", "침대와 창문이 있는 객실", 0.93, False, True, None), urls[1]: gemini.VisionResult(urls[1], "외관", "2층 건물 외관", 0.88, False, True, None), })) job_id = (await client.post(f"/v1/place/{pid}/vision", headers=h, json={})).json()["job_id"] await Worker("w", JobQueue(), build_handler(), job_deadline_sec=30).process_one() job = (await client.get(f"/v1/job/{job_id}", headers=h)).json()["job"] assert job["status"] == JobStatus.DONE.value, job.get("last_error") assert job["result"]["approved"] == 2 rows = await _media_rows(db_engine, pid) assert rows[0][1] == "A동 침실" and rows[0][2] == "침대와 창문이 있는 객실" assert all(r[4] == MediaStatus.APPROVED.value for r in rows) async def test_low_confidence_goes_to_review_queue(auth_headers, client, db_engine, monkeypatch): """검증: 신뢰도가 임계값 미만인 결과. 기대결과: ★ 라벨은 저장되지만 status 는 PENDING_REVIEW 로 남는다 — 자동 반영하지 않는다.""" h = await auth_headers("u1") pid = await _place_with_media(client, h, db_engine, n=1, kakao="v2") url = (await _media_rows(db_engine, pid))[0][0] monkeypatch.setattr(gemini, "is_configured", lambda: True) monkeypatch.setattr(gemini, "analyze_images", _fake_analyze({ url: gemini.VisionResult(url, "수영장", "물이 있는 공간", 0.31, True, True, None), })) job_id = (await client.post(f"/v1/place/{pid}/vision", headers=h, json={})).json()["job_id"] await Worker("w", JobQueue(), build_handler(), job_deadline_sec=30).process_one() job = (await client.get(f"/v1/job/{job_id}", headers=h)).json()["job"] assert job["result"]["needs_review"] == 1 assert job["result"]["approved"] == 0 row = (await _media_rows(db_engine, pid))[0] assert row[1] == "수영장", "사람이 고칠 재료는 저장돼야 한다" assert row[4] == MediaStatus.PENDING_REVIEW.value, "★ 신뢰도 낮은 결과가 자동 승인됐다" async def test_partial_failure_does_not_fail_the_job(auth_headers, client, db_engine, monkeypatch): """검증: 3장 중 1장만 분석에 실패한다. 기대결과: 잡은 DONE, 실패한 장만 확인 큐에 남는다 — 한 장이 나머지를 죽이지 않는다.""" h = await auth_headers("u1") pid = await _place_with_media(client, h, db_engine, n=3, kakao="v3") urls = [r[0] for r in await _media_rows(db_engine, pid)] monkeypatch.setattr(gemini, "is_configured", lambda: True) monkeypatch.setattr(gemini, "analyze_images", _fake_analyze({ urls[0]: gemini.VisionResult(urls[0], "외관", "건물 외관", 0.9, False, True, None), urls[1]: gemini.VisionResult(urls[1], None, None, 0.0, True, False, "download failed"), urls[2]: gemini.VisionResult(urls[2], "거실", "소파가 있는 거실", 0.85, False, True, None), })) job_id = (await client.post(f"/v1/place/{pid}/vision", headers=h, json={})).json()["job_id"] await Worker("w", JobQueue(), build_handler(), job_deadline_sec=30).process_one() job = (await client.get(f"/v1/job/{job_id}", headers=h)).json()["job"] assert job["status"] == JobStatus.DONE.value assert job["result"]["approved"] == 2 assert job["result"]["failed"] == 1 async def test_second_run_skips_already_analyzed(auth_headers, client, db_engine, monkeypatch): """검증: 분석이 끝난 사업장에 다시 분석을 건다. 기대결과: MEDIA_NOT_FOUND — 같은 사진 재분석은 요금만 나간다. force 로만 다시 돈다.""" h = await auth_headers("u1") pid = await _place_with_media(client, h, db_engine, n=1, kakao="v4") url = (await _media_rows(db_engine, pid))[0][0] monkeypatch.setattr(gemini, "is_configured", lambda: True) monkeypatch.setattr(gemini, "analyze_images", _fake_analyze({ url: gemini.VisionResult(url, "외관", "건물 외관", 0.9, False, True, None), })) await client.post(f"/v1/place/{pid}/vision", headers=h, json={}) await Worker("w", JobQueue(), build_handler(), job_deadline_sec=30).process_one() r = await client.post(f"/v1/place/{pid}/vision", headers=h, json={}) assert r.json()["result"]["code"] == ErrorType.MEDIA_NOT_FOUND.value forced = await client.post(f"/v1/place/{pid}/vision", headers=h, json={"force": True}) assert forced.json()["result"]["success"] is True async def test_vision_requires_api_key(auth_headers, client, db_engine, monkeypatch): """검증: GEMINI_API_KEY 없이 분석을 건다. 기대결과: GENERATOR_NOT_CONFIGURED — 잡을 만들지 않는다(만들어봐야 DEAD 로 간다).""" h = await auth_headers("u1") pid = await _place_with_media(client, h, db_engine, n=1, kakao="v5") monkeypatch.setattr(gemini, "is_configured", lambda: False) r = await client.post(f"/v1/place/{pid}/vision", headers=h, json={}) assert r.json()["result"]["code"] == ErrorType.GENERATOR_NOT_CONFIGURED.value async def test_collect_chains_vision_job(auth_headers, client, monkeypatch): """검증: 수집이 사진을 저장하면 사진 분석 잡이 이어서 걸리는가. 기대결과: 수집 잡 result 에 vision_job_id 가 담기고 그 잡이 큐에 있다 — 파이프라인이 안 끊긴다.""" from services.collector import MockAdapter monkeypatch.setattr(gemini, "is_configured", lambda: True) h = await auth_headers("u1") pid = (await client.post("/v1/place", headers=h, json={"name": "체인펜션", "category": 1})).json()["place"]["place_id"] await client.post(f"/v1/place/{pid}/verify", headers=h, json={"external_place_id": "v6", "source": 1}) url = MockAdapter.url_for(PlaceCategory.LODGING, pid, channel="naver_place") lid = (await client.post(f"/v1/place/{pid}/link", headers=h, json={"channel": 3, "url": url})).json()["link"]["link_id"] await client.post(f"/v1/place/{pid}/link/{lid}/confirm", headers=h) job_id = (await client.post(f"/v1/place/{pid}/collect", headers=h, json={})).json()["job_id"] await Worker("w", JobQueue(), build_handler(), job_deadline_sec=120).process_one() job = (await client.get(f"/v1/job/{job_id}", headers=h)).json()["job"] vision_id = job["result"].get("vision_job_id") assert vision_id, "사진을 저장했는데 분석 잡이 안 걸렸다" chained = await JobQueue().get(vision_id) assert chained["job_type"] == JobType.VISION.value assert chained["status"] == JobStatus.PENDING.value