"""실적 대시보드(index.html)·광고비(ads.html)·설정(settings.html)을 테스트용 HTML 로 렌더한다(erp_base.html 은 최소 스텁, 가상의 데이터).
python tests/js/render_performance.py # → tests/js/.out/perf_dash.html · perf_ads.html · perf_settings.html
화면 데이터는 실제 build_dashboard·build_settings 로 만든다(저장소만 가짜). DB·쇼핑몰 API 는 쓰지 않는다.
쿠팡은 수동 입력 켬(10/5 수동 값 87,000, 10/6 은 API 값만), 카카오도 켬(값 없음), 나머지는 끔.
"""
from __future__ import annotations
import os
import sys
from datetime import date
from decimal import Decimal
import jinja2
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, ROOT)
OUT_DIR = os.path.join(ROOT, "tests", "js", ".out")
from app.modules.performance.ad_spend import build_ad_dashboard # noqa: E402
from app.modules.performance.dashboard import amount_text, build_dashboard, build_settings # noqa: E402
from app.modules.performance.router import _auto_sync_settings_ctx # noqa: E402
BASE_STUB = ("{% block head_extra %}{% endblock %}
{% block content %}{% endblock %}"
"{% block scripts %}{% endblock %}")
ORDER = ["cafe24", "naver", "coupang", "toss", "gsshop", "elevenst", "ohouse", "gmarket", "kakao", "lotteon"]
NAMES = {"cafe24": "카페24", "naver": "네이버 스마트스토어", "coupang": "쿠팡", "toss": "토스쇼핑", "gsshop": "GS샵",
"elevenst": "11번가", "ohouse": "오늘의집", "gmarket": "G마켓", "kakao": "카카오", "lotteon": "롯데ON"}
class FakeStore:
def list_marketplaces(self):
return [{"code": c, "name": NAMES[c], "enabled": True, "include_in_sales": True, "sort_order": i * 10,
"manual_input": c in ("coupang", "kakao")} for i, c in enumerate(ORDER, 1)]
def daily_amounts(self, start, end, codes):
api = {(date(2026, 10, 5), "coupang"): 50000, (date(2026, 10, 6), "coupang"): 7000,
(date(2026, 10, 5), "cafe24"): 100000}
out = {k: v for k, v in api.items() if start <= k[0] <= end and k[1] in codes}
out.update(self.manual_amounts(start, end, codes))
return out
def manual_amounts(self, start, end, codes, *, active_only=True):
manual = {(date(2026, 10, 5), "coupang"): 87000}
return {k: v for k, v in manual.items() if start <= k[0] <= end and k[1] in codes}
def rate_table(self):
return {"coupang": [(date(2026, 1, 1), Decimal("12"))]}
def statuses(self):
return {}
def last_update_at(self):
return None
def running_codes(self):
return []
def ad_amounts(self, start, end):
rows = {(date(2026, 10, 1), "coupang_rocket"): (220000, "upload"), (date(2026, 10, 2), "google"): (50000, "manual"),
(date(2026, 10, 5), "coupang_wing"): (29000, "manual"), (date(2026, 10, 3), "naver"): (300000, "upload"),
(date(2026, 10, 4), "google"): (123457, "api")} # 구글 광고 API 값(초록 숫자)
rows = {k: v for k, v in rows.items() if start <= k[0] <= end}
return {k: v[0] for k, v in rows.items()}, {k: v[1] for k, v in rows.items()}
def ad_roas(self, start, end):
rows = {(date(2026, 10, 3), "naver"): Decimal("250.5"), (date(2026, 10, 4), "google"): Decimal("500")}
return {k: v for k, v in rows.items() if start <= k[0] <= end}
def cell_memos(self, kind, start, end):
"""날짜 칸 메모(2026-10-22) — 매출: 쿠팡 10/5, 광고비: 쿠팡(윙) 10/5."""
rows = {"sales": {(date(2026, 10, 5), "coupang"): "행사 매출 포함\n2차 정산 예정"},
"ad": {(date(2026, 10, 5), "coupang_wing"): "쿠폰 광고"}}.get(kind, {})
return {k: v for k, v in rows.items() if start <= k[0] <= end}
def env() -> jinja2.Environment:
e = jinja2.Environment(loader=jinja2.ChoiceLoader([
jinja2.DictLoader({"erp_base.html": BASE_STUB}),
jinja2.FileSystemLoader(os.path.join(ROOT, "app", "modules", "performance", "templates")),
]), autoescape=True)
return e
def main() -> None:
os.makedirs(OUT_DIR, exist_ok=True)
e = env()
base = {"user": {"email": "me@test.local"}, "is_admin": True, "nav_items": [], "page_title": "실적", "page_subtitle": "",
"unit": "won", "unit_label": "원", "amt": lambda v: amount_text(v)}
ctx = build_dashboard(FakeStore(), kind="week", anchor=date(2026, 10, 7), today=date(2026, 10, 7))
# 다음 자동 업데이트(router._auto_sync_ctx 와 같은 모양) — 화면이 초 단위로 센다
# 일정 = 실적 설정 "자동 업데이트"(2026-10-26) — 화면이 schedule 로 다음 회차를 계산
ctx["auto_sync"] = {"next_at": "2026-10-07T10:30:00+09:00", "now": "2026-10-07T10:17:25.000+09:00",
"schedule": {"enabled": True, "interval": 30, "days": [0, 1, 2, 3, 4, 5, 6], "start": "09:00", "end": "19:00"},
"label": "매일 09:00~19:00 · 30분마다"}
html = e.get_template("performance/index.html").render(**base, active_tab="dashboard", metric="net", **ctx)
with open(os.path.join(OUT_DIR, "perf_dash.html"), "w", encoding="utf-8") as f:
f.write(html)
# 이번달 보기(기본) + 진짜 ERP 셸(erp_base.html — .erp-page 가 스크롤 상자) — 제목·기간 합계 행 스크롤 고정 확인용(2026-10-27)
shell = jinja2.Environment(loader=jinja2.FileSystemLoader([
os.path.join(ROOT, "app", "templates"), os.path.join(ROOT, "app", "modules", "performance", "templates")]), autoescape=True)
sbase = {**base, "user": {"email": "me@test.local", "name": "Me", "picture": ""}}
mctx = build_dashboard(FakeStore(), kind="month", anchor=date(2026, 10, 7), today=date(2026, 10, 7))
mctx["auto_sync"] = ctx["auto_sync"]
html = shell.get_template("performance/index.html").render(**sbase, active_tab="dashboard", metric="net", **mctx)
with open(os.path.join(OUT_DIR, "perf_dash_shell.html"), "w", encoding="utf-8") as f:
f.write(html)
sactx = build_ad_dashboard(FakeStore(), kind="month", anchor=date(2026, 10, 7), today=date(2026, 10, 7),
sales_codes=["cafe24", "coupang"])
sactx["auto_sync"] = ctx["auto_sync"]
html = shell.get_template("performance/ads.html").render(**sbase, active_tab="ads", **sactx)
with open(os.path.join(OUT_DIR, "perf_ads_shell.html"), "w", encoding="utf-8") as f:
f.write(html)
actx =build_ad_dashboard(FakeStore(), kind="month", anchor=date(2026, 10, 7), today=date(2026, 10, 7),
sales_codes=["cafe24", "coupang"])
actx["ad_sync_sources"] = [{"code": "google", "name": "구글 광고"}] # [구글 광고 불러오기]
actx["auto_sync"] = ctx["auto_sync"] # 광고비 탭도 다음 업데이트 카운트다운(router._auto_sync_ctx)
html = e.get_template("performance/ads.html").render(**base, active_tab="ads", **actx)
with open(os.path.join(OUT_DIR, "perf_ads.html"), "w", encoding="utf-8") as f:
f.write(html)
sctx = build_settings(FakeStore(), configured={}, key_info={}, key_versions_in_use={}, sample_count=0)
sctx["ad_api_ready"] = True # 광고 API 연동 표(마이그레이션 007)
sctx["ad_sources"] = [{"code": "google", "name": "구글 광고", "configured": True, "status": "ok", "status_label": "정상 연결",
"last_test_at": "2026-10-15T10:00:00+09:00", "last_error": "", "last_error_at": None,
"last_sync_at": "2026-10-15T10:30:02+09:00", "last_success_sync_at": "2026-10-15T10:30:02+09:00"}]
# 자동 업데이트 일정 카드(2026-10-26) — 실제 router._auto_sync_settings_ctx(저장소에 get_setting 없음 → 기본 일정)
sctx["auto_sync_settings"] = _auto_sync_settings_ctx(FakeStore())
html = e.get_template("performance/settings.html").render(**base, active_tab="settings", **sctx)
with open(os.path.join(OUT_DIR, "perf_settings.html"), "w", encoding="utf-8") as f:
f.write(html)
html = shell.get_template("performance/settings.html").render(**sbase, active_tab="settings", **sctx)
with open(os.path.join(OUT_DIR, "perf_settings_shell.html"), "w", encoding="utf-8") as f:
f.write(html)
# 직접 추가 쇼핑몰·그룹(2026-10-22) — perf_dash_groups.html · perf_settings_groups.html (perf_groups.browser.test.js)
gctx = build_dashboard(GroupStore(), kind="week", anchor=date(2026, 10, 7), today=date(2026, 10, 7))
gctx["auto_sync"] = None
html = e.get_template("performance/index.html").render(**base, active_tab="dashboard", metric="gross", **gctx)
with open(os.path.join(OUT_DIR, "perf_dash_groups.html"), "w", encoding="utf-8") as f:
f.write(html)
gsctx = build_settings(GroupStore(), configured={}, key_info={"ok": True}, key_versions_in_use={}, sample_count=0)
gsctx.update(ad_api_ready=True, ad_sources=[])
html = e.get_template("performance/settings.html").render(**base, active_tab="settings", **gsctx)
with open(os.path.join(OUT_DIR, "perf_settings_groups.html"), "w", encoding="utf-8") as f:
f.write(html)
print("ok")
class GroupStore(FakeStore):
"""카페24·네이버 + 그룹 '오프라인'(매장A·매장B) + 그룹 없는 직접 추가 '행사'."""
def list_marketplaces(self):
rows = [{"code": c, "name": NAMES[c], "enabled": True, "include_in_sales": True, "sort_order": i * 10,
"manual_input": False, "is_custom": False, "is_group": False, "parent_code": None}
for i, c in enumerate(("cafe24", "naver"), 1)]
custom = {"enabled": True, "include_in_sales": True, "manual_input": True, "is_custom": True, "is_group": False}
return rows + [
{"code": "group_off", "name": "오프라인", "sort_order": 30, "enabled": True, "include_in_sales": False,
"manual_input": False, "is_custom": True, "is_group": True, "parent_code": None},
{**custom, "code": "custom_a", "name": "매장A", "sort_order": 40, "parent_code": "group_off"},
{**custom, "code": "custom_b", "name": "매장B", "sort_order": 50, "parent_code": "group_off"},
{**custom, "code": "custom_c", "name": "행사", "sort_order": 60, "parent_code": None},
]
def manual_amounts(self, start, end, codes, *, active_only=True):
manual = {(date(2026, 10, 5), "custom_a"): 30000, (date(2026, 10, 5), "custom_b"): 12000}
return {k: v for k, v in manual.items() if start <= k[0] <= end and k[1] in codes}
def ad_amounts(self, start, end):
return {}, {}
if __name__ == "__main__":
main()