"""Tests for the ext.struct built-in extension (SHARD-WP-0014 T6).""" import pytest from shard_wiki import InformationSpace from shard_wiki.adapters import assert_conformant from shard_wiki.engine import ExtensionRuntime, build_engine_shard from shard_wiki.engine.extensions import StructExt, parse_frontmatter from shard_wiki.model import PageShape, Verb _STRUCT_PAGE = "---\ntitle: Spec\nstatus: draft\n---\nbody text" def test_parse_frontmatter(): fields, has = parse_frontmatter(_STRUCT_PAGE) assert has and fields == {"title": "Spec", "status": "draft"} assert parse_frontmatter("just prose") == ({}, False) assert parse_frontmatter("---\nunterminated") == ({}, False) def _runtime(allowed=None): rt = ExtensionRuntime() rt.register(StructExt(allowed_fields=allowed)) return rt def test_feature_absent_when_extension_off(): shard = build_engine_shard("off", ExtensionRuntime(), activate=set()) shard.write("Spec", _STRUCT_PAGE) assert shard.read("Spec").shape is PageShape.PROSE # kernel: opaque prose assert not shard.profile().supports(Verb.STRUCTURED_PAYLOAD) # honest absence def test_feature_present_when_extension_on(): shard = build_engine_shard("on", _runtime(), activate={"ext.struct"}) shard.write("Spec", _STRUCT_PAGE) assert shard.read("Spec").shape is PageShape.TYPED_RECORD # tagged by ext.struct assert shard.read("Spec").body.endswith("body text") # content preserved (in-text) assert shard.profile().supports(Verb.STRUCTURED_PAYLOAD) # profile reflects activation (E-5) assert_conformant(shard) # still conformant def test_plain_page_is_not_tagged_even_when_on(): shard = build_engine_shard("on", _runtime(), activate={"ext.struct"}) shard.write("Plain", "no frontmatter here") assert shard.read("Plain").shape is PageShape.PROSE def test_allowed_fields_validation_rejects_disallowed(): shard = build_engine_shard("v", _runtime(allowed={"title"}), activate={"ext.struct"}) with pytest.raises(ValueError, match="disallowed fields"): shard.write("Bad", "---\ntitle: ok\nsecret: no\n---\nx") shard.write("Good", "---\ntitle: ok\n---\nx") # allowed field passes assert shard.read("Good").shape is PageShape.TYPED_RECORD def test_through_information_space_edit(): space = InformationSpace("team") space.attach(build_engine_shard("wikiE", _runtime(), activate={"ext.struct"})) space.union.shard("wikiE").write("Doc", "---\ntitle: T\n---\nv1") res = space.edit("Doc", "---\ntitle: T2\n---\nv2") # overlay→apply→write-through assert res.status.value == "applied" page = space.read("Doc") assert page.shape is PageShape.TYPED_RECORD and "v2" in page.body