gabriel / musehub public
test_mists.py python
609 lines 29.8 KB Hotspot
Raw
sha256:b2889a0a4d93327d59b0be36fa41f9b6b516f92ad50e737426d9827a70fb66b5 docs(mists): JS/TS (and every parsed language) get symbol a… Opus 5.5 patch 14 hours ago
1 """DOC_MST — /muse/mists contract (musehub#257, QA contract v2).
2
3 Every claim below was checked against musehub ``dev`` 39de5806 and muse 0.2.1rc20, partly on a loopback hub with a
4 private disposable Postgres (the probes are not part of this suite). The page's example ID, type table and
5 create-flow figure are computed from the engine code (``docs_mists_data``); these tests re-derive each one from the
6 code the page documents, so a change to either side fails here. Known product defects stay documented as contract
7 plus warning (#241 defect-preservation rule): each is pinned by a strict xfail that flips when its ticket is fixed,
8 so the warning on the page must be removed in the same change.
9
10 DOC_MST_01 (commands parse), _02 (anchors) and _03 (URL scheme) are the #242 checkers (test_doc_inf_cli.py,
11 test_doc_inf_registry.py, test_doc_inf_scheme.py); DOC_MST_04 (embed snippets resolve) is
12 ``test_embed_snippet_assets_exist`` below. No database is used.
13 """
14
15 from __future__ import annotations
16
17 import argparse
18 import ast
19 import hashlib
20 import html as html_lib
21 import inspect
22 import re
23 from pathlib import Path
24 from types import SimpleNamespace
25 from typing import Any
26 from unittest.mock import AsyncMock, patch
27
28 import pytest
29 from fastapi.routing import iter_route_contexts
30 from fastapi.testclient import TestClient
31 from muse.plugins.mist import plugin as mist_plugin
32 from pydantic import ValidationError
33 from starlette.routing import compile_path
34
35 from musehub.api.routes.musehub import docs_mists_data as data
36 from musehub.api.routes.musehub import mists as mists_routes
37 from musehub.api.routes.musehub import ui_mists
38 from musehub.db import get_db
39 from musehub.db.musehub_repo_models import MusehubMist, MusehubRepo
40 from musehub.main import app
41 from musehub.models.mists import MistCreateRequest
42 from musehub.services import musehub_mists
43 from tests.docs_contract import cli_check as cc
44 from tests.docs_contract import registry_check as rc
45
46 pytestmark = pytest.mark.no_db
47
48 PAGE = rc.phase_path("mists")
49 ROOT = Path(__file__).resolve().parents[2]
50 HUB_ISSUES = "https://staging.musehub.ai/gabriel/musehub/issues/"
51 MUSE_ISSUES = "https://staging.musehub.ai/gabriel/muse/issues/"
52
53 _T_DELETE_REPO = "316" # DELETE leaves the backing repo; the content can never be re-published
54 _T_EMBED_FRAME = "318" # global CSP overwrites the embed route's frame-ancestors
55 _T_PATCH_MODEL = "320" # fixed: content is immutable (owner decision 2026-11-15)
56 _T_BINARY_NUL = "321" # binary hashed as base64 text (the NUL 500 is fixed)
57 _T_API_ODDS = "322" # limit cap, invalid cursor, empty url, duplicate compute_mist_id
58 _T_ANCHORS = "323" # JS/TS symbol anchors promised, grammars not installed
59 _C_DELETE_EXIT = "210" # fixed in Muse 0.2.2rc6: delete exits 0 on the hub's 204
60 _C_SNAKE_KEYS = "211" # fixed in Muse 0.2.2rc6: the CLI reads the hub's camelCase keys
61 _OPEN_HUB = (_T_BINARY_NUL, _T_ANCHORS)
62 _OPEN_CLI: tuple[str, ...] = ()
63 _FIXED_CLI = (_C_DELETE_EXIT, _C_SNAKE_KEYS)
64 _FIXED_HUB = (_T_DELETE_REPO, "317", _T_EMBED_FRAME, _T_API_ODDS, _T_PATCH_MODEL) # 317: the web list pages are no longer shadowed by the repo wildcard
65
66
67 @pytest.fixture(scope="module")
68 def html(rendered: dict[str, str]) -> str:
69 return html_lib.unescape(rendered[PAGE])
70
71
72 @pytest.fixture(scope="module")
73 def text(html: str) -> str:
74 return " ".join(re.sub(r"<[^>]+>", "", html).split())
75
76
77 def _after_id(html: str, anchor: str) -> str:
78 """The page source from the element carrying ``id="<anchor>"`` onwards."""
79 return html.split(f'id="{anchor}"', 1)[1]
80
81
82 def _section(html: str, anchor: str) -> str:
83 """The ``<section id="<anchor>">`` element, up to its closing tag."""
84 return _after_id(html, anchor).split("</section>", 1)[0]
85
86
87 def _table_rows(fragment: str) -> list[list[str]]:
88 """Cell texts of the first table in ``fragment``."""
89 table = fragment.split("<table", 1)[1].split("</table>", 1)[0]
90 rows = []
91 for tr in re.findall(r"<tr[^>]*>(.*?)</tr>", table, flags=re.S):
92 cells = re.findall(r"<t[dh][^>]*>(.*?)</t[dh]>", tr, flags=re.S)
93 rows.append([" ".join(re.sub(r"<[^>]+>", "", c).split()) for c in cells])
94 return rows
95
96
97 def _pre(fragment: str, index: int = 0) -> str:
98 """Text of the ``index``-th ``<pre>`` block in ``fragment``."""
99 block = fragment.split("<pre", index + 1)[index + 1].split("</pre>", 1)[0]
100 return re.sub(r"<[^>]+>", "", block.split(">", 1)[1])
101
102
103 def _first_route(method: str, path: str) -> Any:
104 """The first route, in declaration order, whose path pattern matches ``path`` and allows ``method``: the one a
105 request reaches. FastAPI 0.162 nests included routers, so ``app.routes`` is not flat; the flattened, fully
106 prefixed view is ``iter_route_contexts`` (QA ran on the shipped 0.162.0, not the 0.136.1 of older QA venvs)."""
107 for ctx in iter_route_contexts(app.routes):
108 if ctx.methods is not None and method not in ctx.methods:
109 continue
110 if ctx.path is None:
111 continue
112 regex, _, _ = compile_path(ctx.path)
113 if regex.match(path):
114 return ctx
115 return None
116
117
118 def _contexts_for(*endpoints: Any) -> list[Any]:
119 return [c for c in iter_route_contexts(app.routes) if c.endpoint in endpoints]
120
121
122 def _req(**fields: Any) -> MistCreateRequest:
123 """A create request built through the same validators POST runs (``model_validate``), with a valid default body."""
124 return MistCreateRequest.model_validate({"filename": "a.py", "content": "x", **fields})
125
126
127 def _subparsers(parser: argparse.ArgumentParser) -> dict[str, argparse.ArgumentParser]:
128 """The sub-commands of ``parser``."""
129 for action in parser._actions: # noqa: SLF001
130 if isinstance(action, argparse._SubParsersAction): # noqa: SLF001
131 return dict(action.choices)
132 raise AssertionError("parser has no sub-commands")
133
134
135 def _sample(path: str) -> str:
136 return path.replace("{owner}", "gabriel").replace("{mist_id}", "aB3xKq9dPwNm")
137
138
139 # ── Identity and content addressing (VERIFIED, executed) ────────────────────
140
141
142 class TestContentAddressing:
143 def test_the_pages_snippet_is_the_servers_algorithm(self, html: str) -> None:
144 """The page's code block is executed; it must agree with the engine for every input, including a digest
145 that starts with a zero byte (the case that needs the leading-'1' rule)."""
146 snippet = _pre(_section(html, "content-addressing"))
147 body = snippet.split("compute_mist_id(b", 1)[0] + "\n" # definitions only, not the example call
148 ns: dict[str, Any] = {}
149 exec(compile(body, "<docs_muse_mists#content-addressing>", "exec"), ns) # noqa: S102 - the page's own example
150 zero_lead = next(i for i in range(5000) if hashlib.sha256(str(i).encode()).digest()[0] == 0)
151 for sample in [b"", b"a", b"hello", data.EXAMPLE_CONTENT.encode(), str(zero_lead).encode(), bytes(range(256)) * 40]:
152 assert ns["compute_mist_id"](sample) == mist_plugin.compute_mist_id(sample), sample[:12]
153
154 def test_the_example_id_is_the_id_of_the_example_content(self, html: str) -> None:
155 assert data.EXAMPLE_ID == mist_plugin.compute_mist_id(b"def _validate_handle(h): ...")
156 assert re.search(rf'compute_mist_id\(b"{re.escape(data.EXAMPLE_CONTENT)}"\)\s*# → "{data.EXAMPLE_ID}"', html)
157 assert f'"mist_id": "{data.EXAMPLE_ID}"' in html
158
159 def test_the_id_is_twelve_base58_characters(self) -> None:
160 assert data.ID_LENGTH == 12 and data.ID_ALPHABET_SIZE == 58
161 assert set(data.EXAMPLE_ID) <= set("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
162
163 def test_the_id_space_is_stated_correctly(self, html: str) -> None:
164 assert data.ID_SPACE == 58**12
165 assert f"{data.ID_SPACE_MANTISSA}×10<sup>{data.ID_SPACE_EXPONENT}</sup>" in html
166 assert (data.ID_SPACE_MANTISSA, data.ID_SPACE_EXPONENT) == ("1.4", "21")
167 assert "7×10" not in html # the earlier figure was 7×10^16, about 2^56
168
169 def test_fork_ids_are_not_content_addresses(self) -> None:
170 """The page says so; the code must agree: the fork route derives the ID from a seed that includes a random
171 token (and a nanosecond timestamp), not from the content."""
172 src = inspect.getsource(mists_routes.fork_mist)
173 assert "secrets.token_hex" in src and "time.time_ns()" in src and "compute_mist_id(fork_seed)" in src, src[:400]
174
175
176 # ── Artifact types (VERIFIED against detect_artifact_type) ──────────────────
177
178
179 class TestArtifactTypes:
180 def test_the_extension_table_equals_the_detector(self) -> None:
181 for row in data.EXTENSION_TYPES:
182 for ext in row.extensions:
183 got = mist_plugin.detect_artifact_type(f"x{ext}", b"not midi, not json")
184 assert got["artifact_type"] == row.artifact_type and got["language"] in row.languages, (ext, got)
185
186 def test_detection_order(self) -> None:
187 detect = mist_plugin.detect_artifact_type
188 assert detect("anything.py", b"MThd\x00\x00\x00\x06")["artifact_type"] == "midi" # magic bytes beat the extension
189 assert detect("c.json", b'[{"type": "function", "name": "f"}]')["artifact_type"] == "abi"
190 assert detect("c.abi", b'[{"type": "function", "name": "f"}]')["artifact_type"] == "abi"
191 assert detect("s.json", b'{"$schema": "http://json-schema.org/draft-07/schema#"}')["artifact_type"] == "json_schema"
192 assert detect("a.json", b'{"type": "object", "properties": {}}')["artifact_type"] == "unknown" # not a $schema key
193 assert detect("a.tsv", b"a\tb")["artifact_type"] == "unknown"
194 assert detect("a.png", b"\x89PNG") == {"artifact_type": "unknown", "language": "binary"}
195
196 def test_the_page_lists_exactly_the_real_types(self, html: str) -> None:
197 rows = _table_rows(_section(html, "artifact-types"))[1:]
198 types = [r[0] for r in rows]
199 assert types == ["code", "data", "midi", "abi", "json_schema", "unknown"]
200 for retired in ("schema", "dataset", "config", "text"):
201 assert retired not in types
202
203 def test_the_page_states_the_detection_rules(self, text: str) -> None:
204 assert "4D 54 68 64" in text and "$schema" in text
205 assert "not an input to POST /api/mists" in text
206
207 def test_the_create_model_has_no_artifact_type_input(self) -> None:
208 assert "artifact_type" not in MistCreateRequest.model_fields
209
210 def test_symbol_anchors_are_produced_for_python_markdown_and_toml(self, text: str) -> None:
211 """What the page says is produced is produced without any optional grammar package."""
212 extract = mist_plugin.extract_mist_symbol_anchors
213 assert extract("a.py", b"def f():\n return 1\n") == ["a.py::f"]
214 assert extract("a.md", b"# Title\n\n## Sub\n") == ["a.md::Title", "a.md::Title.Sub"]
215 assert any(a.startswith("a.toml::tool") for a in extract("a.toml", b"[tool]\nx = 1\n"))
216 assert "Python code, Markdown headings and TOML sections" in text
217
218
219 # ── Limits (VERIFIED against the models and constants) ──────────────────────
220
221
222 class TestLimits:
223 def _row(self, html: str, label: str) -> str:
224 return next(r[1] for r in _table_rows(_section(html, "limits")) if r[0] == label)
225
226 def test_field_lengths(self, html: str) -> None:
227 fields = MistCreateRequest.model_fields
228 for name, label, expected in [("filename", "Filename length", 255), ("title", "Title length", 500), ("description", "Description length", 10_000)]:
229 assert max(m.max_length for m in fields[name].metadata if hasattr(m, "max_length")) == expected
230 assert self._row(html, label).startswith(f"{expected:,}") or self._row(html, label).startswith(str(expected))
231
232 def test_tag_rules(self) -> None:
233 ok = _req(tags=[f"t{i}" for i in range(10)])
234 assert len(ok.tags) == 10
235 for bad in ([f"t{i}" for i in range(11)], ["x" * 65], ["<b>"], ["a\x00b"]):
236 with pytest.raises(ValidationError):
237 _req(tags=bad)
238 assert _req(tags=["x" * 64]).tags == ["x" * 64]
239
240 def test_filename_rules(self) -> None:
241 for bad in ("../x.py", "a/b.py", "a\\b.py", "a\x00b.py", "a\x1b[31mb.py", "a\x07b.py", ""):
242 with pytest.raises(ValidationError):
243 _req(filename=bad)
244
245 def test_visibility_values(self) -> None:
246 assert mist_plugin.MIST_VISIBILITIES == {"public", "secret"}
247 assert _req().visibility == "public"
248 for bad in ("private", "SECRET", "banana"):
249 with pytest.raises(ValidationError):
250 _req(visibility=bad)
251
252 def test_fork_depth_rate_limits_and_body_cap(self, html: str) -> None:
253 from musehub.models import mists as models
254 from musehub.rate_limits import MIST_CREATE_LIMIT, MIST_FORK_LIMIT
255
256 assert musehub_mists._FORK_DEPTH_LIMIT == 5
257 assert (MIST_CREATE_LIMIT, MIST_FORK_LIMIT) == ("20/minute", "30/minute")
258 assert models._MAX_CONTENT_BYTES == 10 * 1024 * 1024
259 rows = dict((r[0], r[1]) for r in _table_rows(_section(html, "limits"))[1:])
260 assert rows["Fork depth"] == "5"
261 assert rows["Create rate"].startswith("20 / minute") and rows["Fork rate"].startswith("30 / minute")
262 assert rows["Content size"].startswith("10 MiB")
263
264
265 # ── The security table names real symbols (VERIFIED) ────────────────────────
266
267
268 class TestSecurityTableSymbols:
269 def test_named_symbols_exist(self, html: str) -> None:
270 from musehub.middleware.content_size import ContentSizeLimitMiddleware # noqa: F401
271
272 assert callable(mist_plugin.validate_mist_filename)
273 assert "validate_tags" in MistCreateRequest.__pydantic_decorators__.field_validators
274 section = _section(html, "security")
275 assert "<code>validate_mist_filename()</code>" in section
276 assert "_validate_mist_filename" not in html # that name never existed
277 assert "_FORK_DEPTH_LIMIT = 5" in section and "ContentSizeLimitMiddleware" in section
278
279 def test_rate_limits_are_per_msign_handle(self) -> None:
280 src = (ROOT / "musehub/api/routes/musehub/mists.py").read_text()
281 assert src.count("key_func=get_msign_handle") >= 2 # create and fork
282
283
284 # ── Routes, CLI and MCP surface (VERIFIED against the registrations) ────────
285
286
287 class TestSurface:
288 def _documented(self, html: str) -> list[tuple[str, str, str]]:
289 """(method, path, comment) for each line of the URL-schema block."""
290 rows = []
291 for line in _pre(_section(html, "url-schema")).splitlines():
292 m = re.match(r"^(?:(GET|POST|PATCH|DELETE)\s+)?(/\S+)\s*(?:#\s*(.*))?$", line.strip())
293 if m:
294 rows.append((m.group(1) or "GET", m.group(2), m.group(3) or ""))
295 return rows
296
297 def test_every_documented_route_exists(self, html: str) -> None:
298 """Each documented path reaches a mist handler first, including the two list pages the repo wildcard used
299 to shadow (musehub#317)."""
300 rows = self._documented(html)
301 assert len(rows) == 15
302 for method, path, comment in rows:
303 assert "404" not in comment, (path, comment)
304 route = _first_route(method, _sample(path))
305 assert route is not None, (method, path)
306 assert route.endpoint.__module__.endswith((".mists", ".ui_mists")), (method, path, route.endpoint)
307
308 def test_every_muse_mist_subcommand_but_push_is_documented(self, html: str) -> None:
309 names = set(_subparsers(_subparsers(cc.capture_parser())["mist"]))
310 documented = set(re.findall(r"<code>muse mist (\w+)", html)) | {"create"}
311 # `push` needs a hub remote and a mist repo, so it was not executed by this QA: not documented yet.
312 assert names - documented == {"push"}, names - documented
313
314 def test_mcp_tools_equal_the_registry(self, html: str) -> None:
315 from musehub.mcp.tools import musehub as reg
316
317 read = {t["name"] for t in reg.MUSEHUB_READ_TOOLS if t["name"].startswith("muse_mist_")}
318 write = {t["name"] for t in reg.MUSEHUB_WRITE_TOOLS if t["name"].startswith("muse_mist_")}
319 rows = {r[0]: r[1] for r in _table_rows(_section(html, "mcp"))[1:]}
320 assert {n for n, a in rows.items() if a == "read"} == read
321 assert {n for n, a in rows.items() if a == "write"} == write
322 assert len(rows) == 9
323
324 def test_mcp_resources_are_registered(self, html: str) -> None:
325 src = (ROOT / "musehub/mcp/resources.py").read_text()
326 for uri in ("musehub://mists/{owner}/{mist_id}", "musehub://mists/{owner}"):
327 assert f'"uriTemplate": "{uri}"' in src
328 assert uri in _section(html, "mcp")
329
330
331 # ── The create flow figure (V1) agrees with the route ───────────────────────
332
333
334 class TestCreateFlow:
335 @staticmethod
336 def _route() -> ast.AsyncFunctionDef:
337 tree = ast.parse((ROOT / "musehub/api/routes/musehub/mists.py").read_text())
338 return next(n for n in ast.walk(tree) if isinstance(n, ast.AsyncFunctionDef) and n.name == "create_mist")
339
340 @staticmethod
341 def _calls(fn: ast.AsyncFunctionDef) -> list[tuple[int, str]]:
342 out = []
343 for n in ast.walk(fn):
344 if isinstance(n, ast.Call):
345 f = n.func
346 name = f.attr if isinstance(f, ast.Attribute) else f.id if isinstance(f, ast.Name) else ""
347 out.append((n.lineno, name))
348 return sorted(out)
349
350 def test_step_order_is_the_routes_order(self) -> None:
351 order = [name for _, name in self._calls(self._route())]
352 wanted = ["compute_mist_id", "detect_artifact_type", "extract_mist_symbol_anchors", "create_repo", "create_mist", "commit"]
353 positions = [order.index(w) for w in wanted]
354 assert positions == sorted(positions), order
355
356 def test_no_muse_commit_is_recorded(self) -> None:
357 names = {name for _, name in self._calls(self._route())}
358 assert {n for n in names if "commit" in n} == {"commit"} # only the database commit
359 assert data.CREATE_FLOW[-1].performed is False and all(s.performed for s in data.CREATE_FLOW[:-1])
360
361 def test_both_conflicts_roll_back_and_answer_409(self) -> None:
362 handlers = [n for n in ast.walk(self._route()) if isinstance(n, ast.ExceptHandler)]
363 integrity = [h for h in handlers if isinstance(h.type, ast.Name) and h.type.id == "IntegrityError"]
364 assert len(integrity) == 2
365 for h in integrity:
366 src = ast.unparse(h)
367 assert "rollback" in src and "HTTP_409_CONFLICT" in src, src
368 conflicts = [f for s in data.CREATE_FLOW for f in s.failures if f.status == "409"]
369 assert [s.key for s in data.CREATE_FLOW if any(f.status == "409" for f in s.failures)] == ["repo", "row"]
370 assert len(conflicts) == 2
371
372 def test_the_page_renders_every_step(self, html: str) -> None:
373 figure = _after_id(html, "create-flow-figure")
374 assert figure.count('class="mfig__step') == len(data.CREATE_FLOW) == 7
375 assert "not performed today" in figure
376 for step in data.CREATE_FLOW:
377 assert step.name in figure
378
379 def test_the_backing_repo_is_a_mist_domain_repo(self) -> None:
380 assert 'domain="mist"' in inspect.getsource(mists_routes.create_mist)
381
382
383 # ── The page links every known defect to its ticket ─────────────────────────
384
385
386 class TestKnownIssueNotes:
387 def test_every_ticket_is_linked(self, html: str) -> None:
388 section = _section(html, "known-issues")
389 for t in _OPEN_HUB:
390 assert f'href="{HUB_ISSUES}{t}"' in section, t
391 for t in _OPEN_CLI:
392 assert f'href="{MUSE_ISSUES}{t}"' in section, t
393
394 def test_fixed_tickets_are_not_listed(self, html: str) -> None:
395 """A fixed ticket's row leaves the table in the same change as the fix."""
396 section = _section(html, "known-issues")
397 for t in _FIXED_HUB:
398 assert f'href="{HUB_ISSUES}{t}"' not in section, t
399 for t in _FIXED_CLI:
400 assert f'href="{MUSE_ISSUES}{t}"' not in section, t
401
402 def test_the_table_has_one_row_per_ticket(self, html: str) -> None:
403 assert len(_table_rows(_section(html, "known-issues"))) - 1 == len(_OPEN_HUB) + len(_OPEN_CLI)
404
405 def test_the_page_says_which_release_it_was_checked_against(self, text: str) -> None:
406 assert "39de5806" in text and "0.2.1rc20" in text
407
408
409 # ── Known defects, strict xfails: each flips when its ticket is fixed ───────
410
411
412 def _xfail(ticket: str, what: str, repo: str = "musehub") -> Any:
413 # raises=: only the finding's own assert is expected; a setup error stays an error (musehub#273).
414 return pytest.mark.xfail(strict=True, raises=AssertionError, reason=f"gabriel/{repo}#{ticket}: {what}")
415
416
417 class _Session:
418 """Just enough of AsyncSession for the service functions under test. Records what is deleted or executed."""
419
420 def __init__(self, row: Any, repo: Any = None) -> None:
421 self.row, self.repo = row, repo
422 self.deleted: list[Any] = []
423 self.executed: list[str] = []
424
425 async def get(self, model: Any, key: Any) -> Any:
426 return self.row if model is MusehubMist else self.repo if model is MusehubRepo else None
427
428 async def delete(self, obj: Any) -> None:
429 self.deleted.append(obj)
430
431 async def execute(self, stmt: Any, *a: Any, **k: Any) -> Any:
432 self.executed.append(str(stmt))
433 return SimpleNamespace(rowcount=1, scalar_one_or_none=lambda: None, scalars=lambda: SimpleNamespace(all=list))
434
435 async def flush(self) -> None: ...
436 async def refresh(self, obj: Any) -> None: ...
437
438
439 async def test_delete_removes_backing_repo() -> None:
440 """musehub#316: the mist row and its backing musehub_repos row go in the same transaction."""
441 row = SimpleNamespace(owner="alice", repo_id="repo-1", mist_id="m1")
442 repo = SimpleNamespace(repo_id="repo-1", domain_id="mist", owner="alice")
443 session = _Session(row, repo)
444 assert await musehub_mists.delete_mist(session, "m1", "alice") is True # type: ignore[arg-type]
445 removed_repo = any(o is repo for o in session.deleted) or any("musehub_repos" in s for s in session.executed)
446 assert removed_repo, "the mist row was deleted but its backing musehub_repos row was not"
447
448
449 def test_ui_list_routes_are_not_shadowed() -> None:
450 """musehub#317: /mists/explore and /{owner}/mists reach the mist pages, not the repo wildcard."""
451 explore = _first_route("GET", "/mists/explore")
452 owner_list = _first_route("GET", "/gabriel/mists")
453 assert explore is not None and explore.endpoint is ui_mists.mist_explore_page, getattr(explore, "name", None)
454 assert owner_list is not None and owner_list.endpoint is ui_mists.mist_list_page, getattr(owner_list, "name", None)
455
456
457 def test_mist_ui_router_is_mounted_once() -> None:
458 """musehub#317: no mist HTML page is also served under /api."""
459 pages = (ui_mists.mist_explore_page, ui_mists.mist_list_page, ui_mists.mist_detail_page, ui_mists.mist_raw, ui_mists.mist_embed_page)
460 contexts = _contexts_for(*pages)
461 assert sorted(c.path for c in contexts) == sorted(
462 ["/mists/explore", "/{owner}/mists", "/{owner}/mists/{mist_id}", "/{owner}/mists/{mist_id}/raw", "/{owner}/mists/{mist_id}/embed"]
463 ), [c.path for c in contexts]
464
465
466 def _public_mist() -> SimpleNamespace:
467 fields = {"artifact_type": "code", "language": "python", "filename": "a.py", "size_bytes": 3, "version": 1, "title": "", "content": "x=1", "mist_id": "m1"}
468 return SimpleNamespace(owner="alice", visibility="public", model_dump=lambda mode="json": dict(fields), **fields)
469
470
471 def _client_for_mist(mist: SimpleNamespace) -> tuple[TestClient, Any]:
472 class _Db:
473 async def commit(self) -> None: ...
474
475 async def _db() -> Any:
476 yield _Db()
477
478 app.dependency_overrides[get_db] = _db
479 stack = [
480 patch.object(musehub_mists, "get_mist", new=AsyncMock(return_value=mist)),
481 patch.object(musehub_mists, "increment_mist_embed", new=AsyncMock(return_value=None)),
482 ]
483 for p in stack:
484 p.start()
485 return TestClient(app), stack
486
487
488 def _stop(stack: list[Any]) -> None:
489 for p in stack:
490 p.stop()
491 app.dependency_overrides.pop(get_db, None)
492
493
494 def test_embed_response_can_be_framed() -> None:
495 """musehub#318: through the full middleware stack, the public embed card may be framed by any origin."""
496 client, stack = _client_for_mist(_public_mist())
497 try:
498 resp = client.get("/alice/mists/m1/embed")
499 finally:
500 _stop(stack)
501 assert resp.status_code == 200
502 csp = resp.headers.get("content-security-policy", "")
503 assert "frame-ancestors 'none'" not in csp, csp
504 assert resp.headers.get("x-frame-options", "").upper() not in {"DENY", "SAMEORIGIN", "ALLOWALL"}, resp.headers.get("x-frame-options")
505
506
507 def test_the_page_states_the_embed_policy(text: str) -> None:
508 """The Embedding section's policy sentence names what the middleware sends (musehub#318)."""
509 from musehub import main
510
511 assert "frame-ancestors *" in main._EMBED_CSP and "https://cdnjs.cloudflare.com" in main._EMBED_CSP # noqa: SLF001
512 assert "frame-ancestors 'none'" in main._DEFAULT_CSP and "cdnjs" not in main._DEFAULT_CSP # noqa: SLF001
513 assert "frame-ancestors *" in text and "X-Frame-Options: DENY" in text and "cdnjs.cloudflare.com" in text
514
515
516 def test_embed_offers_only_snippets_that_work() -> None:
517 """DOC_MST_04 (musehub#319): the embed API offers only the iframe, never a script tag or badge pointing at assets
518 the server does not serve."""
519 client, stack = _client_for_mist(_public_mist())
520 try:
521 snippets = client.get("/api/alice/mists/m1/embed").json()
522 finally:
523 _stop(stack)
524 assert "iframe" in snippets and "/alice/mists/m1/embed" in snippets["iframe"]
525 assert not ({"js", "badge"} & set(snippets)), snippets
526
527
528 def test_update_takes_no_content() -> None:
529 """musehub#320 (immutable model): PATCH carrying content is a 422; the service and MCP take no content."""
530 from musehub.models.mists import MistUpdateRequest
531
532 try:
533 MistUpdateRequest.model_validate({"content": "x"})
534 accepted = True
535 except ValidationError:
536 accepted = False
537 assert not accepted
538 assert "content" not in inspect.signature(musehub_mists.update_mist).parameters
539
540 def test_the_page_states_the_immutable_model(html: str) -> None:
541 """musehub#320: the PATCH section says content is immutable and describes the raw routes' caching."""
542 section = _after_id(html, "rest-api").split("DELETE /api/mists", 1)[0].split("PATCH /api/mists", 1)[1]
543 text = " ".join(re.sub(r"<[^>]+>", "", section).split())
544 assert "Content is immutable" in text and "422" in text
545 assert musehub_mists.RAW_PUBLIC_CACHE_CONTROL == "public, no-cache" and "Cache-Control: public, no-cache" in text
546 assert "ETag" in text and "304" in text
547
548 def test_nul_in_content_is_rejected_by_the_model() -> None:
549 """musehub#321 (NUL half): a 422 at the request model, never a database 500."""
550 try:
551 _req(filename="nul.bin", content="a\x00b")
552 accepted = True
553 except ValidationError:
554 accepted = False
555 assert not accepted, "a NUL in `content` must be a 422, not a database 500"
556
557
558 def _le(field: Any) -> int | None:
559 for meta in getattr(field, "field_info", field).metadata:
560 if hasattr(meta, "le"):
561 return int(meta.le)
562 return None
563
564
565 def test_list_limit_caps_agree(html: str) -> None:
566 """musehub#322: one `limit` maximum on the REST lists, the web lists and the MCP tool, and the page says it."""
567 from musehub.mcp.tools import musehub as reg
568 from musehub.models.mists import MIST_LIST_LIMIT_MAX
569
570 def _route_le(endpoint: Any) -> int | None:
571 route = _contexts_for(endpoint)[0].route
572 return next(_le(f) for f in route.dependant.query_params if f.name == "limit")
573
574 caps = {name: _route_le(ep) for name, ep in [
575 ("api explore", mists_routes.explore_mists), ("api owner", mists_routes.list_owner_mists),
576 ("web explore", ui_mists.mist_explore_page), ("web owner", ui_mists.mist_list_page),
577 ]}
578 caps["mcp"] = next(t for t in reg.MUSEHUB_READ_TOOLS if t["name"] == "muse_mist_list")["inputSchema"]["properties"]["limit"]["maximum"]
579 assert set(caps.values()) == {MIST_LIST_LIMIT_MAX} == {100}, caps
580 rows = dict((r[0], r[1]) for r in _table_rows(_section(html, "limits"))[1:])
581 assert rows["Explore page size"].startswith("1–100 (default 20)"), rows["Explore page size"]
582
583
584 def test_invalid_cursor_is_rejected() -> None:
585 """musehub#322: the cursor parse raises instead of restarting at page 1 (REST and web answer 422)."""
586 tree = ast.parse(inspect.getsource(musehub_mists))
587 handlers = [h for n in ast.walk(tree) if isinstance(n, ast.Try) for h in n.handlers
588 if isinstance(h.type, ast.Name) and h.type.id == "ValueError" and "fromisoformat" in "\n".join(ast.unparse(stmt) for stmt in n.body)]
589 assert handlers, "the cursor parse moved: update this test"
590 assert all(any(isinstance(s, ast.Raise) for s in ast.walk(h)) for h in handlers), "a bad cursor is swallowed (cursor_dt = None)"
591
592
593 @_xfail(_T_ANCHORS, "the documented JS/TS symbol anchors need grammar packages that constraints.txt does not pin")
594 def test_shipped_dependencies_include_the_documented_anchor_grammars() -> None:
595 """Environment-independent on purpose: it reads the shipped constraints, not whatever the test interpreter has."""
596 pinned = {
597 line.split("==", 1)[0].strip().lower().replace("_", "-")
598 for line in (ROOT / "constraints.txt").read_text().splitlines()
599 if "==" in line and not line.lstrip().startswith("#")
600 }
601 missing = {"tree-sitter-javascript", "tree-sitter-typescript"} - pinned
602 assert not missing, f"the image installs no grammar for: {sorted(missing)}"
603
604
605 def test_no_second_compute_mist_id() -> None:
606 """musehub#322: the mist ID has one implementation, the engine's base-58 one."""
607 tree = ast.parse((ROOT / "musehub/core/genesis.py").read_text())
608 defs = [n.name for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == "compute_mist_id"]
609 assert defs == [], "delete core/genesis.py::compute_mist_id (nothing on the mist paths uses it)"
File History 12 commits
sha256:b2889a0a4d93327d59b0be36fa41f9b6b516f92ad50e737426d9827a70fb66b5 docs(mists): JS/TS (and every parsed language) get symbol a… Opus 5.5 patch 14 hours ago
sha256:3cdbbfe4d7fbc1558b9d36cfbc1902be7d9f749dd76374719511a42dd35e9f1a fix(mists): a Mist's content is immutable — PATCH and MCP r… Opus 5.5 minor ⚠ 1 day ago
sha256:9511d1c6e932d9f45df27b37c9d9d2efa4aedb53c0061b2e80de2f2574ce0636 docs(mists,api): Muse 0.2.2rc6 fixes muse#210/#211 — drop t… Opus 5.5 patch 1 day ago
sha256:d2aba013f6e84cec4f9d3cfcbc7c90bfde5f12a0fa8cb75b74745741f1807b6c fix(mists): offer only the iframe embed — drop the script-t… Opus 5.5 minor ⚠ 1 day ago
sha256:41ed2ded57d17544c111c81960ee6a7a7febc9a0d81a778616319265ac93a654 merge: rc25 — Mists wave (#316 #317 #318 #322; #319 #320 #3… Opus 5.5 minor ⚠ 2 days ago
sha256:217dd826590bc15be90c3f505334270128a9985f7ac6507c90c6dc881229090e fix(mists): delete only the caller's own backing repo; esca… Opus 5.5 patch 2 days ago
sha256:bcd996529ff074e75f0a165197b1696b5448ca199e2f882d17b0b12bcce36076 fix(mists): PATCH re-derives type fields and raw revalidate… Opus 5.5 minor ⚠ 2 days ago
sha256:24713a1bc91e3f21bcb423f1e3849ab2d0c39679f5a91ab409337ff95133c302 fix(mists): one list limit, 422 on an invalid cursor, url e… Opus 5.5 minor ⚠ 2 days ago
sha256:cc22d4c91c9707a0700270d9a68eb87014436a99729a3327a57e76c9bfc6aafc fix(mists): make the public embed card frameable on purpose… Opus 5.5 minor ⚠ 2 days ago
sha256:e1f7557d1669de53158574bd676f594a6d82eb2977939dcaae13a89b5a565f92 fix(mists): DELETE removes the backing repo so the owner ca… Opus 5.5 patch 2 days ago
sha256:c4341b8be331c768cd3938ce86919e91b0bbe640ba8a9a22a3dd616f29f7b683 fix(mists): serve /mists/explore and /{owner}/mists before … Opus 5.5 minor ⚠ 2 days ago
sha256:27d91ce6191e753cb9f0dbd6b0b5d59dde18bc69fe10acc63bda269a0a0b238e docs(mists): #257 Mist Domain QA contract v2 (replay of 510… Opus 5.5 minor ⚠ 8 days ago