fix: accept a leading UTF-8 BOM in instinct files

RED: BOM parsing and disk loading failed in 3 cases. GREEN: 18 targeted tests pass, including LF/CRLF and preserving interior BOM characters. parse_instinct_file line coverage is 91.4%.
This commit is contained in:
Eneko Ruiz committed 2026-10-11 11:33:47 +02:00
1 parent 2d515e4fc2
commit 60ce177d74
2 files changed
+18 -1

No files matched your search

@@ -525,7 +525,7 @@ def parse_instinct_file(content: str) -> list[dict]:
in_frontmatter = False
content_lines = []
for line in content.split('\n'):
for line in content.removeprefix('\ufeff').split('\n'):
if line.strip() == '---':
if in_frontmatter:
# End of frontmatter - content comes next
@@ -208,6 +208,23 @@ Prevents vulnerabilities.
assert "Prevents vulnerabilities." in result[0]["content"]
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
def test_parse_initial_utf8_bom(newline):
content = SAMPLE_INSTINCT_YAML.replace("\n", newline)
assert parse_instinct_file("\ufeff" + content) == parse_instinct_file(content)
def test_parse_preserves_bom_inside_content():
content = SAMPLE_INSTINCT_YAML.replace("Always write", "Always \ufeffwrite")
assert "Always \ufeffwrite" in parse_instinct_file(content)[0]["content"]
def test_load_instinct_file_with_utf8_bom(tmp_path):
(tmp_path / "bom.yaml").write_text(SAMPLE_INSTINCT_YAML, encoding="utf-8-sig")
instincts = _load_instincts_from_dir(tmp_path, "personal", "project")
assert [instinct["id"] for instinct in instincts] == ["test-instinct"]
def test_empty_content_no_error():
content = """\
---