1- from moss_cli .commands .playground import PLAYGROUND_HTML
1+ import asyncio
2+ import inspect
3+ from types import SimpleNamespace
4+
5+ from moss_cli .commands .playground import PLAYGROUND_HTML , PlaygroundHandler
6+
7+
8+ class FakeClient :
9+ def __init__ (self ):
10+ self .query_calls = []
11+
12+ def query (self , name , query , options ):
13+ self .query_calls .append ((name , query , options ))
14+ doc = SimpleNamespace (id = "d1" , text = "hello" , score = 0.9 , metadata = {"k" : "v" })
15+ return SimpleNamespace (docs = [doc ], time_taken_ms = 12 , query = query )
16+
17+
18+ class FakeWorker :
19+ def __init__ (self , client ):
20+ self .client = client
21+
22+ def submit (self , coro_fn ):
23+ result = coro_fn ()
24+ if inspect .isawaitable (result ):
25+ return asyncio .run (result )
26+ return result
27+
28+
29+ class SupersedingWorker :
30+ """Simulates a newer query arriving while an older one is queued."""
31+
32+ def __init__ (self , client ):
33+ self .client = client
34+
35+ def submit (self , coro_fn ):
36+ PlaygroundHandler ._latest_request_id = 99
37+ result = coro_fn ()
38+ if inspect .isawaitable (result ):
39+ return asyncio .run (result )
40+ return result
41+
42+
43+ def _make_handler (monkeypatch , client = None , worker = None ):
44+ handler = PlaygroundHandler .__new__ (PlaygroundHandler )
45+ captured = {}
46+
47+ def fake_send_json (status , data ):
48+ captured ["status" ] = status
49+ captured ["data" ] = data
50+
51+ monkeypatch .setattr (handler , "_send_json" , fake_send_json )
52+ monkeypatch .setattr (PlaygroundHandler , "client" , client )
53+ monkeypatch .setattr (PlaygroundHandler , "_worker" , worker )
54+ monkeypatch .setattr (PlaygroundHandler , "_latest_request_id" , 0 )
55+ return handler , captured
56+
57+
58+ def _valid_body ():
59+ return {"name" : "idx" , "query" : "hello" , "requestId" : 1 , "topK" : 5 , "alpha" : 0.5 }
260
361
462def test_playground_html_asset_exists ():
563 assert PLAYGROUND_HTML .exists (), (
664 f"Playground HTML not found at { PLAYGROUND_HTML } . "
765 "Check that the asset is included in the package data."
8- )
66+ )
67+
68+
69+ def test_html_renders_metadata_safely ():
70+ html = PLAYGROUND_HTML .read_text (encoding = "utf-8" )
71+ assert "result-metadata" in html
72+ assert "JSON.stringify(doc.metadata, null, 2)" in html
73+ assert "textContent" in html
74+
75+
76+ def test_html_aborts_stale_queries ():
77+ html = PLAYGROUND_HTML .read_text (encoding = "utf-8" )
78+ assert "AbortController" in html
79+ assert "signal: thisAbort.signal" in html
80+ assert "requestId: thisRequestId" in html
81+
82+
83+ def test_query_accepts_valid_params (monkeypatch ):
84+ client = FakeClient ()
85+ worker = FakeWorker (client )
86+ handler , captured = _make_handler (monkeypatch , client = client , worker = worker )
87+ handler ._handle_post_query (dict (_valid_body ()))
88+ assert captured ["status" ] == 200
89+ assert len (client .query_calls ) == 1
90+ name , query , options = client .query_calls [0 ]
91+ assert (name , query ) == ("idx" , "hello" )
92+ assert options .top_k == 5
93+ assert options .alpha == 0.5
94+ assert captured ["data" ]["docs" ][0 ]["metadata" ] == {"k" : "v" }
95+
96+
97+ def test_query_rejects_bool_topk (monkeypatch ):
98+ client = FakeClient ()
99+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
100+ body = _valid_body ()
101+ body ["topK" ] = True
102+ handler ._handle_post_query (body )
103+ assert captured ["status" ] == 400
104+ assert client .query_calls == []
105+
106+
107+ def test_query_rejects_fractional_topk (monkeypatch ):
108+ client = FakeClient ()
109+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
110+ body = _valid_body ()
111+ body ["topK" ] = 1.9
112+ handler ._handle_post_query (body )
113+ assert captured ["status" ] == 400
114+ assert client .query_calls == []
115+
116+
117+ def test_query_rejects_string_topk (monkeypatch ):
118+ client = FakeClient ()
119+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
120+ body = _valid_body ()
121+ body ["topK" ] = "5"
122+ handler ._handle_post_query (body )
123+ assert captured ["status" ] == 400
124+ assert client .query_calls == []
125+
126+
127+ def test_query_rejects_out_of_range_topk (monkeypatch ):
128+ client = FakeClient ()
129+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
130+ for bad in (0 , 51 ):
131+ body = _valid_body ()
132+ body ["topK" ] = bad
133+ handler ._handle_post_query (body )
134+ assert captured ["status" ] == 400 , f"expected 400 for topK={ bad } "
135+ assert client .query_calls == []
136+
137+
138+ def test_query_rejects_bool_alpha (monkeypatch ):
139+ client = FakeClient ()
140+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
141+ body = _valid_body ()
142+ body ["alpha" ] = True
143+ handler ._handle_post_query (body )
144+ assert captured ["status" ] == 400
145+ assert client .query_calls == []
146+
147+
148+ def test_query_rejects_non_finite_alpha (monkeypatch ):
149+ client = FakeClient ()
150+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
151+ for bad in (float ("nan" ), float ("inf" ), float ("-inf" )):
152+ body = _valid_body ()
153+ body ["alpha" ] = bad
154+ handler ._handle_post_query (body )
155+ assert captured ["status" ] == 400 , f"expected 400 for alpha={ bad } "
156+ assert client .query_calls == []
157+
158+
159+ def test_query_rejects_out_of_range_alpha (monkeypatch ):
160+ client = FakeClient ()
161+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
162+ for bad in (- 0.1 , 1.1 ):
163+ body = _valid_body ()
164+ body ["alpha" ] = bad
165+ handler ._handle_post_query (body )
166+ assert captured ["status" ] == 400 , f"expected 400 for alpha={ bad } "
167+ assert client .query_calls == []
168+
169+
170+ def test_query_requires_request_id (monkeypatch ):
171+ client = FakeClient ()
172+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
173+ body = _valid_body ()
174+ for bad in (None , True , "1" ):
175+ body ["requestId" ] = bad
176+ handler ._handle_post_query (body )
177+ assert captured ["status" ] == 400 , f"expected 400 for requestId={ bad } "
178+ assert client .query_calls == []
179+
180+
181+ def test_query_drops_already_superseded_request (monkeypatch ):
182+ client = FakeClient ()
183+ handler , captured = _make_handler (monkeypatch , client = client , worker = FakeWorker (client ))
184+ monkeypatch .setattr (PlaygroundHandler , "_latest_request_id" , 5 )
185+ body = _valid_body ()
186+ body ["requestId" ] = 3
187+ handler ._handle_post_query (body )
188+ assert captured ["status" ] == 200
189+ assert captured ["data" ].get ("superseded" ) is True
190+ assert client .query_calls == []
191+
192+
193+ def test_query_skips_stale_queued_job (monkeypatch ):
194+ client = FakeClient ()
195+ handler , captured = _make_handler (
196+ monkeypatch , client = client , worker = SupersedingWorker (client )
197+ )
198+ handler ._handle_post_query (dict (_valid_body ()))
199+ assert captured ["status" ] == 200
200+ assert captured ["data" ].get ("superseded" ) is True
201+ assert client .query_calls == []
0 commit comments