fix(task-1): refuse block_insert of an undefined block before writing

`add_blockref` never checks the name and `add_auto_attribs` skips autofill
when `Insert.block()` resolves to None, so a typo'd block name returned a
handle whose attribute values were silently dropped and whose dangling
INSERT `doc.audit()` later deletes. Check `name in self._doc.blocks` and
raise `ValueError` on both the attribute and no-attribute paths, matching
the COM engine's `InsertBlock` refusal for an unknown name.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
This commit is contained in:
Umut CAN
2026-09-15 13:21:16 +03:00
co-authored by Claude Opus 5
parent be7dfa86d3
commit 6f4bda5028
2 changed files with 19 additions and 0 deletions
+12
View File
@@ -41,3 +41,15 @@ async def test_insert_without_attributes_is_unchanged(backend):
info = await backend.entity_get(ref.handle)
assert info.properties["block_name"] == "TB"
assert await backend.block_get_attributes(ref.handle) == {}
@pytest.mark.parametrize("attributes", [{"TAG": "P-101"}, None], ids=["with-attrs", "no-attrs"])
async def test_insert_of_undefined_block_is_refused_before_writing(backend, attributes):
# `add_blockref` never checks the name and `add_auto_attribs` skips autofill
# when the definition is missing, so a typo'd name used to return a handle
# whose attribute values were dropped and which `doc.audit()` later deletes.
before = len(list(backend._msp()))
with pytest.raises(ValueError, match="'NO_SUCH_BLOCK' is not defined"):
await backend.block_insert("NO_SUCH_BLOCK", 5, 5, attributes=attributes)
assert len(list(backend._msp())) == before, "a refused insert must not write"
assert [b.name for b in backend._doc.blocks if b.name == "NO_SUCH_BLOCK"] == []