#!/usr/bin/env python3 # -*- coding: utf-8 -*- """آزمون «امکانسنجی مسیرهای داده»: آیا سنجش، راست میگوید یا فقط عدد نشان میدهد؟""" from __future__ import annotations import json import os import socket import subprocess import sys import tempfile import threading import time import unittest from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path GUI = Path(__file__).resolve().parent.parent if str(GUI) not in sys.path: sys.path.insert(0, str(GUI)) import data_sources as ds # noqa: E402 class FixtureProbeTests(unittest.TestCase): """سنجش روی سرور نمونهٔ محلی با همان ساختار نشانیهای واقعی.""" @classmethod def setUpClass(cls): cls.httpd, cls.host = ds.start_fixture() @classmethod def tearDownClass(cls): cls.httpd.shutdown() cls.httpd.server_close() def test_all_endpoints_detected_and_code_resolved(self): res = ds.probe_all(host=self.host, with_stability=False, timeout=5) self.assertEqual(len(ds.ENDPOINTS), res["summary"]["total"]) self.assertEqual(res["summary"]["total"], res["summary"]["ok"], [i["reason"] for i in res["items"]]) self.assertEqual("46348559193224090", res["code"]) # کد نماد از جستوجو درآمد self.assertTrue(res["summary"]["usable_for_engine"]) def test_declared_paths_are_the_source_of_truth(self): ep = next(e for e in ds.ENDPOINTS if e["name"] == "price_info") item = ds.probe_endpoint(ep, code="46348559193224090", host=self.host, timeout=5) paths = [f["path"] for f in item["fields"]] self.assertIn("closingPriceInfo.pDrCotVal", paths) self.assertEqual(4, item["count"]) def test_wrong_paths_make_endpoint_unusable(self): """اگر ساختار سرویس عوض شود، باید «سرخ» شود، نه اینکه بیصدا قبول شود.""" ep = {"name": "broken", "label": "نشانی با ساختار عوضشده", "url": self.host + "/api/ClosingPrice/GetClosingPriceInfo/{code}", "needs": "code", "gives": "", "paths": ["nope.0.notHere"]} item = ds.probe_endpoint(ep, code="1", host=self.host, timeout=5) self.assertFalse(item["ok"]) self.assertIn("ساختار", item["reason"]) def test_stability_detects_a_moving_price(self): ep = next(e for e in ds.ENDPOINTS if e["name"] == "price_info") s = ds.stability_check(ep, code="46348559193224090", host=self.host, wait=0.3, timeout=5) self.assertTrue(s["checked"]) self.assertEqual("تازهشونده", s["verdict"]) def test_stability_skips_non_price_endpoints(self): ep = next(e for e in ds.ENDPOINTS if e["name"] == "symbol_search") s = ds.stability_check(ep, symbol="فولاد", host=self.host, wait=0.2, timeout=5) self.assertFalse(s["checked"]) self.assertIn("قیمت نیست", s["reason"]) def test_missing_code_is_reported_in_persian(self): ep = next(e for e in ds.ENDPOINTS if e["name"] == "closing_daily") item = ds.probe_endpoint(ep, code="", host=self.host, timeout=5) self.assertFalse(item["ok"]) self.assertIn("insCode", item["reason"]) def test_offline_flag_does_not_touch_network(self): ep = next(e for e in ds.ENDPOINTS if e["name"] == "market_watch") item = ds.probe_endpoint(ep, host="http://127.0.0.1:9", allow_network=False, timeout=1) self.assertFalse(item["ok"]) self.assertIn("شبکه خاموش", item["reason"]) def test_report_and_sheet_are_written(self): res = ds.probe_all(host=self.host, with_stability=False, timeout=5) with tempfile.TemporaryDirectory() as tmp: report = ds.build_report(res, ds.build_matrix(), path=Path(tmp) / "f.html") html = report.read_text(encoding="utf-8") self.assertIn('dir="rtl"', html) self.assertIn("Masoud Mojarabian", html) self.assertIn("وجه تضمین", html) self.assertNotIn("http://cdn", html) sheet = ds.requirement_sheet() self.assertIn("برگهٔ نیاز داده", sheet) self.assertIn("وجه تضمین", sheet) for q in ("قالب دسترسی", "احراز هویت", "پایداری", "پرداخت", "اجازهٔ استفاده"): self.assertIn(q, sheet) # پنج پرسش فنی از فروشنده def test_matrix_covers_three_routes_with_verdicts(self): m = ds.build_matrix() self.assertEqual(3, len(m)) for row in m: for key in ("route", "gives", "misses", "cost", "verdict", "test"): self.assertTrue(row[key], key) self.assertIn("نیاز نداریم", m[2]["verdict"]) # پوش لحظهای def test_ws_block_appears_only_when_probed(self): res = ds.probe_all(host=self.host, with_stability=False, timeout=5) with tempfile.TemporaryDirectory() as tmp: plain = ds.build_report(res, ds.build_matrix(), path=Path(tmp) / "a.html").read_text(encoding="utf-8") with_ws = ds.build_report(res, ds.build_matrix(), ws={"url": "wss://x/y", "tcp": True, "tls": True, "status": 401, "verdict": "سرویس هست ولی احراز هویت میخواهد", "note": "پاسخ 401"}, path=Path(tmp) / "b.html").read_text(encoding="utf-8") self.assertNotIn("