gabriel / musehub public
test_mists.py python
611 lines 30.0 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" # fixed in MuseHub 0.2.3rc32: every grammar ships in the image (grammar-requirements.txt)
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,)
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, _T_ANCHORS) # 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_every_documented_language(self, text: str) -> None:
211 """What the page says is produced is produced in the shipped image (every grammar is installed, musehub#323)."""
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 "a.js::f" in extract("a.js", b"function f() { return 1; }\n")
215 assert "a.ts::f" in extract("a.ts", b"export function f(): number { return 1; }\n")
216 assert extract("a.md", b"# Title\n\n## Sub\n") == ["a.md::Title", "a.md::Title.Sub"]
217 assert any(a.startswith("a.toml::tool") for a in extract("a.toml", b"[tool]\nx = 1\n"))
218 assert "a.yaml::server.port" in extract("a.yaml", b"server:\n port: 1\n")
219 assert "every language the code domain parses" in text and "TOML, JSON and YAML keys" in text
220
221
222 # ── Limits (VERIFIED against the models and constants) ──────────────────────
223
224
225 class TestLimits:
226 def _row(self, html: str, label: str) -> str:
227 return next(r[1] for r in _table_rows(_section(html, "limits")) if r[0] == label)
228
229 def test_field_lengths(self, html: str) -> None:
230 fields = MistCreateRequest.model_fields
231 for name, label, expected in [("filename", "Filename length", 255), ("title", "Title length", 500), ("description", "Description length", 10_000)]:
232 assert max(m.max_length for m in fields[name].metadata if hasattr(m, "max_length")) == expected
233 assert self._row(html, label).startswith(f"{expected:,}") or self._row(html, label).startswith(str(expected))
234
235 def test_tag_rules(self) -> None:
236 ok = _req(tags=[f"t{i}" for i in range(10)])
237 assert len(ok.tags) == 10
238 for bad in ([f"t{i}" for i in range(11)], ["x" * 65], ["<b>"], ["a\x00b"]):
239 with pytest.raises(ValidationError):
240 _req(tags=bad)
241 assert _req(tags=["x" * 64]).tags == ["x" * 64]
242
243 def test_filename_rules(self) -> None:
244 for bad in ("../x.py", "a/b.py", "a\\b.py", "a\x00b.py", "a\x1b[31mb.py", "a\x07b.py", ""):
245 with pytest.raises(ValidationError):
246 _req(filename=bad)
247
248 def test_visibility_values(self) -> None:
249 assert mist_plugin.MIST_VISIBILITIES == {"public", "secret"}
250 assert _req().visibility == "public"
251 for bad in ("private", "SECRET", "banana"):
252 with pytest.raises(ValidationError):
253 _req(visibility=bad)
254
255 def test_fork_depth_rate_limits_and_body_cap(self, html: str) -> None:
256 from musehub.models import mists as models
257 from musehub.rate_limits import MIST_CREATE_LIMIT, MIST_FORK_LIMIT
258
259 assert musehub_mists._FORK_DEPTH_LIMIT == 5
260 assert (MIST_CREATE_LIMIT, MIST_FORK_LIMIT) == ("20/minute", "30/minute")
261 assert models._MAX_CONTENT_BYTES == 10 * 1024 * 1024
262 rows = dict((r[0], r[1]) for r in _table_rows(_section(html, "limits"))[1:])
263 assert rows["Fork depth"] == "5"
264 assert rows["Create rate"].startswith("20 / minute") and rows["Fork rate"].startswith("30 / minute")
265 assert rows["Content size"].startswith("10 MiB")
266
267
268 # ── The security table names real symbols (VERIFIED) ────────────────────────
269
270
271 class TestSecurityTableSymbols:
272 def test_named_symbols_exist(self, html: str) -> None:
273 from musehub.middleware.content_size import ContentSizeLimitMiddleware # noqa: F401
274
275 assert callable(mist_plugin.validate_mist_filename)
276 assert "validate_tags" in MistCreateRequest.__pydantic_decorators__.field_validators
277 section = _section(html, "security")
278 assert "<code>validate_mist_filename()</code>" in section
279 assert "_validate_mist_filename" not in html # that name never existed
280 assert "_FORK_DEPTH_LIMIT = 5" in section and "ContentSizeLimitMiddleware" in section
281
282 def test_rate_limits_are_per_msign_handle(self) -> None:
283 src = (ROOT / "musehub/api/routes/musehub/mists.py").read_text()
284 assert src.count("key_func=get_msign_handle") >= 2 # create and fork
285
286
287 # ── Routes, CLI and MCP surface (VERIFIED against the registrations) ────────
288
289
290 class TestSurface:
291 def _documented(self, html: str) -> list[tuple[str, str, str]]:
292 """(method, path, comment) for each line of the URL-schema block."""
293 rows = []
294 for line in _pre(_section(html, "url-schema")).splitlines():
295 m = re.match(r"^(?:(GET|POST|PATCH|DELETE)\s+)?(/\S+)\s*(?:#\s*(.*))?$", line.strip())
296 if m:
297 rows.append((m.group(1) or "GET", m.group(2), m.group(3) or ""))
298 return rows
299
300 def test_every_documented_route_exists(self, html: str) -> None:
301 """Each documented path reaches a mist handler first, including the two list pages the repo wildcard used
302 to shadow (musehub#317)."""
303 rows = self._documented(html)
304 assert len(rows) == 15
305 for method, path, comment in rows:
306 assert "404" not in comment, (path, comment)
307 route = _first_route(method, _sample(path))
308 assert route is not None, (method, path)
309 assert route.endpoint.__module__.endswith((".mists", ".ui_mists")), (method, path, route.endpoint)
310
311 def test_every_muse_mist_subcommand_but_push_is_documented(self, html: str) -> None:
312 names = set(_subparsers(_subparsers(cc.capture_parser())["mist"]))
313 documented = set(re.findall(r"<code>muse mist (\w+)", html)) | {"create"}
314 # `push` needs a hub remote and a mist repo, so it was not executed by this QA: not documented yet.
315 assert names - documented == {"push"}, names - documented
316
317 def test_mcp_tools_equal_the_registry(self, html: str) -> None:
318 from musehub.mcp.tools import musehub as reg
319
320 read = {t["name"] for t in reg.MUSEHUB_READ_TOOLS if t["name"].startswith("muse_mist_")}
321 write = {t["name"] for t in reg.MUSEHUB_WRITE_TOOLS if t["name"].startswith("muse_mist_")}
322 rows = {r[0]: r[1] for r in _table_rows(_section(html, "mcp"))[1:]}
323 assert {n for n, a in rows.items() if a == "read"} == read
324 assert {n for n, a in rows.items() if a == "write"} == write
325 assert len(rows) == 9
326
327 def test_mcp_resources_are_registered(self, html: str) -> None:
328 src = (ROOT / "musehub/mcp/resources.py").read_text()
329 for uri in ("musehub://mists/{owner}/{mist_id}", "musehub://mists/{owner}"):
330 assert f'"uriTemplate": "{uri}"' in src
331 assert uri in _section(html, "mcp")
332
333
334 # ── The create flow figure (V1) agrees with the route ───────────────────────
335
336
337 class TestCreateFlow:
338 @staticmethod
339 def _route() -> ast.AsyncFunctionDef:
340 tree = ast.parse((ROOT / "musehub/api/routes/musehub/mists.py").read_text())
341 return next(n for n in ast.walk(tree) if isinstance(n, ast.AsyncFunctionDef) and n.name == "create_mist")
342
343 @staticmethod
344 def _calls(fn: ast.AsyncFunctionDef) -> list[tuple[int, str]]:
345 out = []
346 for n in ast.walk(fn):
347 if isinstance(n, ast.Call):
348 f = n.func
349 name = f.attr if isinstance(f, ast.Attribute) else f.id if isinstance(f, ast.Name) else ""
350 out.append((n.lineno, name))
351 return sorted(out)
352
353 def test_step_order_is_the_routes_order(self) -> None:
354 order = [name for _, name in self._calls(self._route())]
355 wanted = ["compute_mist_id", "detect_artifact_type", "extract_mist_symbol_anchors", "create_repo", "create_mist", "commit"]
356 positions = [order.index(w) for w in wanted]
357 assert positions == sorted(positions), order
358
359 def test_no_muse_commit_is_recorded(self) -> None:
360 names = {name for _, name in self._calls(self._route())}
361 assert {n for n in names if "commit" in n} == {"commit"} # only the database commit
362 assert data.CREATE_FLOW[-1].performed is False and all(s.performed for s in data.CREATE_FLOW[:-1])
363
364 def test_both_conflicts_roll_back_and_answer_409(self) -> None:
365 handlers = [n for n in ast.walk(self._route()) if isinstance(n, ast.ExceptHandler)]
366 integrity = [h for h in handlers if isinstance(h.type, ast.Name) and h.type.id == "IntegrityError"]
367 assert len(integrity) == 2
368 for h in integrity:
369 src = ast.unparse(h)
370 assert "rollback" in src and "HTTP_409_CONFLICT" in src, src
371 conflicts = [f for s in data.CREATE_FLOW for f in s.failures if f.status == "409"]
372 assert [s.key for s in data.CREATE_FLOW if any(f.status == "409" for f in s.failures)] == ["repo", "row"]
373 assert len(conflicts) == 2
374
375 def test_the_page_renders_every_step(self, html: str) -> None:
376 figure = _after_id(html, "create-flow-figure")
377 assert figure.count('class="mfig__step') == len(data.CREATE_FLOW) == 7
378 assert "not performed today" in figure
379 for step in data.CREATE_FLOW:
380 assert step.name in figure
381
382 def test_the_backing_repo_is_a_mist_domain_repo(self) -> None:
383 assert 'domain="mist"' in inspect.getsource(mists_routes.create_mist)
384
385
386 # ── The page links every known defect to its ticket ─────────────────────────
387
388
389 class TestKnownIssueNotes:
390 def test_every_ticket_is_linked(self, html: str) -> None:
391 section = _section(html, "known-issues")
392 for t in _OPEN_HUB:
393 assert f'href="{HUB_ISSUES}{t}"' in section, t
394 for t in _OPEN_CLI:
395 assert f'href="{MUSE_ISSUES}{t}"' in section, t
396
397 def test_fixed_tickets_are_not_listed(self, html: str) -> None:
398 """A fixed ticket's row leaves the table in the same change as the fix."""
399 section = _section(html, "known-issues")
400 for t in _FIXED_HUB:
401 assert f'href="{HUB_ISSUES}{t}"' not in section, t
402 for t in _FIXED_CLI:
403 assert f'href="{MUSE_ISSUES}{t}"' not in section, t
404
405 def test_the_table_has_one_row_per_ticket(self, html: str) -> None:
406 assert len(_table_rows(_section(html, "known-issues"))) - 1 == len(_OPEN_HUB) + len(_OPEN_CLI)
407
408 def test_the_page_says_which_release_it_was_checked_against(self, text: str) -> None:
409 assert "39de5806" in text and "0.2.1rc20" in text
410
411
412 # ── Known defects, strict xfails: each flips when its ticket is fixed ───────
413
414
415 def _xfail(ticket: str, what: str, repo: str = "musehub") -> Any:
416 # raises=: only the finding's own assert is expected; a setup error stays an error (musehub#273).
417 return pytest.mark.xfail(strict=True, raises=AssertionError, reason=f"gabriel/{repo}#{ticket}: {what}")
418
419
420 class _Session:
421 """Just enough of AsyncSession for the service functions under test. Records what is deleted or executed."""
422
423 def __init__(self, row: Any, repo: Any = None) -> None:
424 self.row, self.repo = row, repo
425 self.deleted: list[Any] = []
426 self.executed: list[str] = []
427
428 async def get(self, model: Any, key: Any) -> Any:
429 return self.row if model is MusehubMist else self.repo if model is MusehubRepo else None
430
431 async def delete(self, obj: Any) -> None:
432 self.deleted.append(obj)
433
434 async def execute(self, stmt: Any, *a: Any, **k: Any) -> Any:
435 self.executed.append(str(stmt))
436 return SimpleNamespace(rowcount=1, scalar_one_or_none=lambda: None, scalars=lambda: SimpleNamespace(all=list))
437
438 async def flush(self) -> None: ...
439 async def refresh(self, obj: Any) -> None: ...
440
441
442 async def test_delete_removes_backing_repo() -> None:
443 """musehub#316: the mist row and its backing musehub_repos row go in the same transaction."""
444 row = SimpleNamespace(owner="alice", repo_id="repo-1", mist_id="m1")
445 repo = SimpleNamespace(repo_id="repo-1", domain_id="mist", owner="alice")
446 session = _Session(row, repo)
447 assert await musehub_mists.delete_mist(session, "m1", "alice") is True # type: ignore[arg-type]
448 removed_repo = any(o is repo for o in session.deleted) or any("musehub_repos" in s for s in session.executed)
449 assert removed_repo, "the mist row was deleted but its backing musehub_repos row was not"
450
451
452 def test_ui_list_routes_are_not_shadowed() -> None:
453 """musehub#317: /mists/explore and /{owner}/mists reach the mist pages, not the repo wildcard."""
454 explore = _first_route("GET", "/mists/explore")
455 owner_list = _first_route("GET", "/gabriel/mists")
456 assert explore is not None and explore.endpoint is ui_mists.mist_explore_page, getattr(explore, "name", None)
457 assert owner_list is not None and owner_list.endpoint is ui_mists.mist_list_page, getattr(owner_list, "name", None)
458
459
460 def test_mist_ui_router_is_mounted_once() -> None:
461 """musehub#317: no mist HTML page is also served under /api."""
462 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)
463 contexts = _contexts_for(*pages)
464 assert sorted(c.path for c in contexts) == sorted(
465 ["/mists/explore", "/{owner}/mists", "/{owner}/mists/{mist_id}", "/{owner}/mists/{mist_id}/raw", "/{owner}/mists/{mist_id}/embed"]
466 ), [c.path for c in contexts]
467
468
469 def _public_mist() -> SimpleNamespace:
470 fields = {"artifact_type": "code", "language": "python", "filename": "a.py", "size_bytes": 3, "version": 1, "title": "", "content": "x=1", "mist_id": "m1"}
471 return SimpleNamespace(owner="alice", visibility="public", model_dump=lambda mode="json": dict(fields), **fields)
472
473
474 def _client_for_mist(mist: SimpleNamespace) -> tuple[TestClient, Any]:
475 class _Db:
476 async def commit(self) -> None: ...
477
478 async def _db() -> Any:
479 yield _Db()
480
481 app.dependency_overrides[get_db] = _db
482 stack = [
483 patch.object(musehub_mists, "get_mist", new=AsyncMock(return_value=mist)),
484 patch.object(musehub_mists, "increment_mist_embed", new=AsyncMock(return_value=None)),
485 ]
486 for p in stack:
487 p.start()
488 return TestClient(app), stack
489
490
491 def _stop(stack: list[Any]) -> None:
492 for p in stack:
493 p.stop()
494 app.dependency_overrides.pop(get_db, None)
495
496
497 def test_embed_response_can_be_framed() -> None:
498 """musehub#318: through the full middleware stack, the public embed card may be framed by any origin."""
499 client, stack = _client_for_mist(_public_mist())
500 try:
501 resp = client.get("/alice/mists/m1/embed")
502 finally:
503 _stop(stack)
504 assert resp.status_code == 200
505 csp = resp.headers.get("content-security-policy", "")
506 assert "frame-ancestors 'none'" not in csp, csp
507 assert resp.headers.get("x-frame-options", "").upper() not in {"DENY", "SAMEORIGIN", "ALLOWALL"}, resp.headers.get("x-frame-options")
508
509
510 def test_the_page_states_the_embed_policy(text: str) -> None:
511 """The Embedding section's policy sentence names what the middleware sends (musehub#318)."""
512 from musehub import main
513
514 assert "frame-ancestors *" in main._EMBED_CSP and "https://cdnjs.cloudflare.com" in main._EMBED_CSP # noqa: SLF001
515 assert "frame-ancestors 'none'" in main._DEFAULT_CSP and "cdnjs" not in main._DEFAULT_CSP # noqa: SLF001
516 assert "frame-ancestors *" in text and "X-Frame-Options: DENY" in text and "cdnjs.cloudflare.com" in text
517
518
519 def test_embed_offers_only_snippets_that_work() -> None:
520 """DOC_MST_04 (musehub#319): the embed API offers only the iframe, never a script tag or badge pointing at assets
521 the server does not serve."""
522 client, stack = _client_for_mist(_public_mist())
523 try:
524 snippets = client.get("/api/alice/mists/m1/embed").json()
525 finally:
526 _stop(stack)
527 assert "iframe" in snippets and "/alice/mists/m1/embed" in snippets["iframe"]
528 assert not ({"js", "badge"} & set(snippets)), snippets
529
530
531 def test_update_takes_no_content() -> None:
532 """musehub#320 (immutable model): PATCH carrying content is a 422; the service and MCP take no content."""
533 from musehub.models.mists import MistUpdateRequest
534
535 try:
536 MistUpdateRequest.model_validate({"content": "x"})
537 accepted = True
538 except ValidationError:
539 accepted = False
540 assert not accepted
541 assert "content" not in inspect.signature(musehub_mists.update_mist).parameters
542
543 def test_the_page_states_the_immutable_model(html: str) -> None:
544 """musehub#320: the PATCH section says content is immutable and describes the raw routes' caching."""
545 section = _after_id(html, "rest-api").split("DELETE /api/mists", 1)[0].split("PATCH /api/mists", 1)[1]
546 text = " ".join(re.sub(r"<[^>]+>", "", section).split())
547 assert "Content is immutable" in text and "422" in text
548 assert musehub_mists.RAW_PUBLIC_CACHE_CONTROL == "public, no-cache" and "Cache-Control: public, no-cache" in text
549 assert "ETag" in text and "304" in text
550
551 def test_nul_in_content_is_rejected_by_the_model() -> None:
552 """musehub#321 (NUL half): a 422 at the request model, never a database 500."""
553 try:
554 _req(filename="nul.bin", content="a\x00b")
555 accepted = True
556 except ValidationError:
557 accepted = False
558 assert not accepted, "a NUL in `content` must be a 422, not a database 500"
559
560
561 def _le(field: Any) -> int | None:
562 for meta in getattr(field, "field_info", field).metadata:
563 if hasattr(meta, "le"):
564 return int(meta.le)
565 return None
566
567
568 def test_list_limit_caps_agree(html: str) -> None:
569 """musehub#322: one `limit` maximum on the REST lists, the web lists and the MCP tool, and the page says it."""
570 from musehub.mcp.tools import musehub as reg
571 from musehub.models.mists import MIST_LIST_LIMIT_MAX
572
573 def _route_le(endpoint: Any) -> int | None:
574 route = _contexts_for(endpoint)[0].route
575 return next(_le(f) for f in route.dependant.query_params if f.name == "limit")
576
577 caps = {name: _route_le(ep) for name, ep in [
578 ("api explore", mists_routes.explore_mists), ("api owner", mists_routes.list_owner_mists),
579 ("web explore", ui_mists.mist_explore_page), ("web owner", ui_mists.mist_list_page),
580 ]}
581 caps["mcp"] = next(t for t in reg.MUSEHUB_READ_TOOLS if t["name"] == "muse_mist_list")["inputSchema"]["properties"]["limit"]["maximum"]
582 assert set(caps.values()) == {MIST_LIST_LIMIT_MAX} == {100}, caps
583 rows = dict((r[0], r[1]) for r in _table_rows(_section(html, "limits"))[1:])
584 assert rows["Explore page size"].startswith("1–100 (default 20)"), rows["Explore page size"]
585
586
587 def test_invalid_cursor_is_rejected() -> None:
588 """musehub#322: the cursor parse raises instead of restarting at page 1 (REST and web answer 422)."""
589 tree = ast.parse(inspect.getsource(musehub_mists))
590 handlers = [h for n in ast.walk(tree) if isinstance(n, ast.Try) for h in n.handlers
591 if isinstance(h.type, ast.Name) and h.type.id == "ValueError" and "fromisoformat" in "\n".join(ast.unparse(stmt) for stmt in n.body)]
592 assert handlers, "the cursor parse moved: update this test"
593 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)"
594
595
596 def test_shipped_dependencies_include_the_documented_anchor_grammars() -> None:
597 """Environment-independent on purpose: it reads the image's hash-pinned grammar list, not whatever the test interpreter has."""
598 pinned = {
599 line.split("==", 1)[0].strip().lower().replace("_", "-")
600 for line in (ROOT / "grammar-requirements.txt").read_text().splitlines()
601 if "==" in line and not line.lstrip().startswith("#")
602 }
603 missing = {"tree-sitter-javascript", "tree-sitter-typescript"} - pinned
604 assert not missing, f"the image installs no grammar for: {sorted(missing)}"
605
606
607 def test_no_second_compute_mist_id() -> None:
608 """musehub#322: the mist ID has one implementation, the engine's base-58 one."""
609 tree = ast.parse((ROOT / "musehub/core/genesis.py").read_text())
610 defs = [n.name for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == "compute_mist_id"]
611 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