from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def test_frontend_v2_shell_assets_exist_and_are_indexed(): assert (ROOT / "web/static/css/ewoooc-tokens.css").exists() assert (ROOT / "web/static/css/ewoooc-shell.css").exists() assert (ROOT / "templates/ewoooc_base.html").exists() assert (ROOT / "templates/components/_ewoooc_shell.html").exists() agents = (ROOT / "AGENTS.md").read_text(encoding="utf-8") constitution = (ROOT / "CONSTITUTION.md").read_text(encoding="utf-8") roadmap = (ROOT / "docs/guides/frontend_upgrade_roadmap.md").read_text(encoding="utf-8") assert "docs/guides/frontend_upgrade_roadmap.md" in agents assert "前端 V2 視覺基準" in constitution assert "禁止用 mock data" in roadmap def test_frontend_v2_shell_uses_real_runtime_context(): shell = (ROOT / "templates/components/_ewoooc_shell.html").read_text(encoding="utf-8") base = (ROOT / "templates/ewoooc_base.html").read_text(encoding="utf-8") assert "scheduler_stats" in shell assert "session.get('username')" in shell assert "next_run|default(None)" in shell assert "components/_ewoooc_shell.html" in base assert 'name="ui" value="v2"' not in base forbidden_markers = [ "mockProducts", "mockData", "fake", "假商品", "假 KPI", ] combined = shell + "\n" + base assert all(marker not in combined for marker in forbidden_markers) def test_dashboard_v2_is_production_default_and_uses_real_dashboard_data(): route_source = (ROOT / "routes/dashboard_routes.py").read_text(encoding="utf-8") dashboard = (ROOT / "templates/dashboard_v2.html").read_text(encoding="utf-8") assert "request.args.get('ui') == 'legacy'" in route_source assert "template_name = 'dashboard.html' if request.args.get('ui') == 'legacy' else 'dashboard_v2.html'" in route_source assert "get_full_dashboard_data()" in route_source assert "MockRecord" not in route_source assert "{% for item in items %}" in dashboard assert "ui='v2'" not in dashboard assert 'name="ui" value="v2"' not in dashboard assert "mockProducts" not in dashboard assert "假商品" not in dashboard def test_dashboard_v2_restores_real_price_history_chart(): route_source = (ROOT / "routes/api_routes.py").read_text(encoding="utf-8") dashboard = (ROOT / "templates/dashboard_v2.html").read_text(encoding="utf-8") assert "@api_bp.route('/api/history/')" in route_source assert "PRICE_HISTORY_RANGES" in route_source assert "'week': {'days': 7" in route_source assert "'month': {'days': 30" in route_source assert "'quarter': {'days': 90" in route_source assert "'year': {'days': 365" in route_source assert "session.query(PriceRecord)" in route_source assert "PriceRecord.product_id == product.id" in route_source assert "https://cdn.jsdelivr.net/npm/chart.js" in dashboard assert 'id="historyModal"' in dashboard assert 'id="priceChart"' in dashboard assert "data-product-id=\"{{ product.id }}\"" in dashboard assert "data-history-trigger" in dashboard assert "onclick=\"event.stopPropagation(); showHistory(this.dataset.productId, this.dataset.productName);\"" in dashboard assert "data-history-range=\"week\"" in dashboard assert "data-history-range=\"month\"" in dashboard assert "data-history-range=\"quarter\"" in dashboard assert "data-history-range=\"year\"" in dashboard assert "fetch(`/api/history/${productId}?range=${activeHistoryRange}&format=v2`)" in dashboard assert "priceChartInstance = new Chart" in dashboard assert "目前沒有可顯示的歷史價格紀錄" in dashboard def test_dashboard_v2_shows_pchome_competitor_pricing_and_links(): route_source = (ROOT / "routes/dashboard_routes.py").read_text(encoding="utf-8") api_source = (ROOT / "routes/api_routes.py").read_text(encoding="utf-8") feeder_source = (ROOT / "services/competitor_price_feeder.py").read_text(encoding="utf-8") scheduler_source = (ROOT / "scheduler.py").read_text(encoding="utf-8") dashboard = (ROOT / "templates/dashboard_v2.html").read_text(encoding="utf-8") migration = (ROOT / "migrations/022_competitor_price_history.sql").read_text(encoding="utf-8") assert "_load_pchome_competitor_map(" in route_source assert "FROM competitor_prices" in route_source assert "competitor_product_id" in route_source assert "https://24h.pchome.com.tw/prod/" in route_source assert "_build_competitor_decision(" in route_source assert "PChome 優勢" in route_source assert "MOMO 威脅" in route_source assert "item['pchome_competitor']" in route_source assert "item['competitor_decision']" in route_source assert "MOMO 價格" in dashboard assert "PChome 價格" in dashboard assert "競價判讀" in dashboard assert "MOMO {{ product.i_code }}" in dashboard assert "PChome {{ competitor.product_id }}" in dashboard assert "competitor.product_url" in dashboard assert "dashboard-competition-badge" in dashboard assert "decision.summary" in dashboard assert "PChome 待比對" in dashboard assert "series.pchome" in dashboard assert "label: 'PChome'" in dashboard assert "含 PChome 歷史快照" in dashboard assert "competitor_price_history" in migration assert "momo_price" in migration assert "competitor_product_id" in migration assert "CREATE INDEX IF NOT EXISTS idx_comp_price_history_sku_source_time" in migration assert "_ensure_competitor_price_history_table" in feeder_source assert "INSERT INTO competitor_price_history" in feeder_source assert "history_written" in feeder_source assert "momo_price" in feeder_source assert "competitor_price_history" in api_source assert "'series': {" in api_source assert "'pchome': competitor_data" in api_source assert "history_written" in scheduler_source def test_edm_dashboard_v2_is_production_default_and_uses_real_campaign_data(): route_source = (ROOT / "routes/edm_routes.py").read_text(encoding="utf-8") template = (ROOT / "templates/edm_dashboard_v2.html").read_text(encoding="utf-8") assert route_source.count("request.args.get('ui') == 'legacy'") == 5 assert "template_name = 'edm_dashboard.html' if request.args.get('ui') == 'legacy' else 'edm_dashboard_v2.html'" in route_source assert "{% for slot, stats in slot_stats.items() %}" in template assert "{% for item in items %}" in template assert "scheduler_stats.get(task_key, [])" in template assert "@api_bp.route('/api/history/i-code/')" in (ROOT / "routes/api_routes.py").read_text(encoding="utf-8") assert "data-campaign-filter=\"new\"" in template assert "data-campaign-filter=\"up\"" in template assert "data-campaign-filter=\"down\"" in template assert "data-campaign-filter=\"delisted\"" in template assert "data-campaign-history-trigger" in template assert "showCampaignHistory(this.dataset.iCode, this.dataset.productName)" in template assert "fetch(`/api/history/i-code/${encodeURIComponent(iCode)}?range=${activeCampaignHistoryRange}`)" in template assert "data-campaign-history-range=\"week\"" in template assert "data-campaign-history-range=\"month\"" in template assert "data-campaign-history-range=\"quarter\"" in template assert "data-campaign-history-range=\"year\"" in template assert "applyCampaignFilter(card, button.dataset.campaignFilter)" in template assert "?ui=v2" not in template assert "ui='v2'" not in template assert "mock" not in template.lower() assert "假商品" not in template def test_vendor_stockout_v2_is_production_default_and_uses_real_vendor_data(): route_source = (ROOT / "routes/vendor_routes.py").read_text(encoding="utf-8") template = (ROOT / "templates/vendor_stockout_index_v2.html").read_text(encoding="utf-8") assert "request.args.get('ui') == 'legacy'" in route_source assert not (ROOT / "web/templates/vendor_stockout_index_v2.html").exists() assert "vendor_stockout_index_v2.html" in route_source assert "_get_vendor_dashboard_stats()" in route_source assert "session.query(VendorStockout).count()" in route_source assert "EmailSendLog" in route_source assert "stats.pending_stockouts" in template assert "stats.email_success_rate" in template assert "ui='v2'" not in template assert "mock" not in template.lower() assert "假" not in template def test_vendor_stockout_list_v2_is_feature_flagged_and_uses_real_stockout_rows(): route_source = (ROOT / "routes/vendor_routes.py").read_text(encoding="utf-8") template = (ROOT / "templates/vendor_stockout_list_v2.html").read_text(encoding="utf-8") assert "vendor_stockout_list_v2.html" in route_source assert "_get_vendor_stockout_list_context()" in route_source assert "_apply_stockout_filters(" in route_source assert "session.query(VendorStockout)" in route_source assert "VendorStockout.status == 'pending'" in route_source assert "VendorStockout.batch_id == batch_id" in route_source assert "{% for record in records %}" in template assert "{% for batch in batches %}" in template assert "stats.pending" in template assert "record.product_code" in template assert "record.product_name" in template assert "record.vendor_name" in template assert "ui='v2'" not in template assert "mock" not in template.lower() assert "假" not in template def test_vendor_stockout_import_v2_is_feature_flagged_and_does_not_ship_sample_rows(): route_source = (ROOT / "routes/vendor_routes.py").read_text(encoding="utf-8") template = (ROOT / "templates/vendor_stockout_import_v2.html").read_text(encoding="utf-8") template_function = route_source.split("def api_import_template():", 1)[1].split("@vendor_bp.route('/api/stockout/list'", 1)[0] assert "vendor_stockout_import_v2.html" in route_source assert "template_columns = [" in template_function assert "pd.DataFrame(columns=template_columns)" in template_function assert "fetchWithCSRF('/vendor-stockout/api/import/excel'" in template assert "vendor_stockout" in template assert "範例" not in template_function assert "ui='v2'" not in template assert "mock" not in template.lower() assert "假" not in template