dd49d83933
The repo edit form already renders a <select id="model"> with one <option value="…"> per cost_model.PRICES key (sorted) plus a "— keep current —" placeholder. This commit adds the test that pins that behaviour: every key must appear as an <option>, no others. 1 test under tests/pilot/test_dashboard_select.py.
74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
"""Tests for the model <select> in the repo edit form (Task D)."""
|
|
import http.client
|
|
import os
|
|
import socket
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
ROOT = os.path.abspath(os.path.join(HERE, "..", ".."))
|
|
sys.path.insert(0, os.path.join(ROOT, "pilot"))
|
|
|
|
import dashboard as dash # noqa: E402
|
|
from pilot import cost_model, feedback # noqa: E402
|
|
|
|
|
|
def _free_port() -> int:
|
|
s = socket.socket()
|
|
s.bind(("127.0.0.1", 0))
|
|
port = s.getsockname()[1]
|
|
s.close()
|
|
return port
|
|
|
|
|
|
def _get(port: int, path: str, headers: dict | None = None) -> tuple[int, dict, bytes]:
|
|
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=5)
|
|
conn.request("GET", path, headers=headers or {})
|
|
r = conn.getresponse()
|
|
body = r.read()
|
|
h = dict(r.getheaders())
|
|
conn.close()
|
|
return r.status, h, body
|
|
|
|
|
|
class TestRepoEditSelect(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.db = f"{self.tmp.name}/f.db"
|
|
os.environ["PRAGENT_FEEDBACK_DB"] = self.db
|
|
os.environ["PRAGENT_DASHBOARD_TOKEN"] = ""
|
|
os.environ["PRAGENT_BOT_TOKEN"] = ""
|
|
conn = feedback.init(self.db)
|
|
feedback.record_review(conn, repo="o/r", pr=1, head_sha="x")
|
|
conn.close()
|
|
|
|
self.port = _free_port()
|
|
from http.server import ThreadingHTTPServer
|
|
self.srv = ThreadingHTTPServer(("127.0.0.1", self.port), dash.Handler)
|
|
self.thread = threading.Thread(target=self.srv.serve_forever, daemon=True)
|
|
self.thread.start()
|
|
|
|
def tearDown(self):
|
|
self.srv.shutdown()
|
|
self.srv.server_close()
|
|
self.thread.join(timeout=2)
|
|
for k in ("PRAGENT_FEEDBACK_DB", "PRAGENT_DASHBOARD_TOKEN", "PRAGENT_BOT_TOKEN"):
|
|
os.environ.pop(k, None)
|
|
self.tmp.cleanup()
|
|
|
|
def test_repo_page_renders_select_with_one_option_per_price(self):
|
|
status, _h, body = _get(self.port, "/r/o/r")
|
|
self.assertEqual(status, 200)
|
|
text = body.decode()
|
|
self.assertIn('<select id="model" name="model">', text)
|
|
# Every PRICES key should appear as an <option value="…">.
|
|
for k in sorted(cost_model.PRICES):
|
|
self.assertIn(f'<option value="{k}"', text, f"missing {k} in select")
|
|
# Plus the "keep current" placeholder.
|
|
self.assertIn("— keep current", text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |