"""Tests for SVG/HTML/MD/DOCX/ODT container cleaners.""" from __future__ import annotations import io import sys import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SCRIPTS = ROOT / "skills" / "remove-ai-marks" / "scripts" sys.path.insert(0, str(SCRIPTS)) from container_meta import ( # noqa: E402 clean_container, clean_docx, clean_html, clean_markdown, clean_odt, clean_svg, inspect_container, inspect_html, inspect_markdown, inspect_svg, ) def test_markdown_frontmatter(): text = """--- title: Hello generator: Claude ai_generated: true --- Body\u200b text. """ _c2, has_ai, findings, _d = inspect_markdown(text) assert has_ai assert any("generator" in f or "ai" in f.lower() for f in findings) cleaned, actions = clean_markdown(text) assert "generator:" not in cleaned assert "ai_generated:" not in cleaned assert "title: Hello" in cleaned assert any("drop" in a for a in actions) def test_markdown_frontmatter_with_blank_line_does_not_crash(): """Regression: a blank line inside frontmatter used to raise IndexError.""" text = "---\ntitle: Demo\n\nauthor: you\n---\nBody\n" cleaned, actions = clean_markdown(text) assert "title: Demo" in cleaned assert "author: you" in cleaned assert actions def test_markdown_drops_nested_children_of_dropped_key(): """Regression: nested values under a dropped AI key survived the clean.""" text = ( "---\n" "title: Demo\n" "model:\n" " name: claude-opus\n" " version: 4\n" "author: you\n" "---\nBody\n" ) cleaned, actions = clean_markdown(text) assert "claude-opus" not in cleaned # the leak assert "version: 4" not in cleaned assert "title: Demo" in cleaned # siblings untouched assert "author: you" in cleaned assert any("drop frontmatter key: model" in a for a in actions) def test_markdown_clean_output_is_no_longer_flagged(): """Round-trip: re-inspecting a cleaned document reports nothing AI-ish.""" text = "---\ntitle: Demo\nmodel:\n name: claude-opus\ngenerator: Claude\n---\nBody\n" cleaned, _ = clean_markdown(text) _c2, has_ai, findings, _d = inspect_markdown(cleaned) assert not has_ai, findings def test_markdown_preserves_comments_and_non_ai_keys(): text = "---\n# editorial notes\ntitle: Demo\ntags:\n - one\n - two\n---\nBody\n" cleaned, _ = clean_markdown(text) assert "# editorial notes" in cleaned assert "- one" in cleaned and "- two" in cleaned def test_html_meta_strip(): html = """ Hi""" _c2, has_ai, findings, _ = inspect_html(html) assert has_ai cleaned, actions = clean_html(html) assert "ChatGPT" not in cleaned assert "viewport" in cleaned assert "data-ai-model" not in cleaned assert any("drop" in a for a in actions) def test_html_cms_generator_not_ai(): html = '' has_c2pa, has_ai, findings, _ = inspect_html(html) assert not has_c2pa assert not has_ai assert any("cms" in f for f in findings) def test_html_cms_generator_preserved_by_clean(): html = '' cleaned, actions = clean_html(html) assert "WordPress" in cleaned assert "viewport" in cleaned def test_html_cms_generator_attribute_names_are_case_insensitive(): for html in ( '', '', ): has_c2pa, has_ai, findings, _ = inspect_html(html) assert not has_c2pa assert not has_ai assert any("cms" in finding for finding in findings) assert clean_html(html)[0] == html ai_html = '' assert inspect_html(ai_html)[1] assert clean_html(ai_html)[0] == "" def test_html_ai_generator_still_dropped(): html = '' cleaned, actions = clean_html(html) assert "Claude" not in cleaned assert any("drop" in a for a in actions) def test_pdf_stream_byte_collision_not_ai(tmp_path: Path): from container_meta import inspect_pdf pdf = b"%PDF-1.4\n1 0 obj<< /Length 4 >>stream\nAIGC\nendstream\nendobj\n%%EOF\n" src = tmp_path / "collision.pdf" src.write_bytes(pdf) has_c2pa, has_ai, findings, _ = inspect_pdf(src, pdf) assert not has_c2pa assert not has_ai def test_svg_metadata(): svg = b""" c2pa contentcredentials Anthropic """ has_c2pa, has_ai, findings, _ = inspect_svg(svg) assert has_c2pa or has_ai cleaned, actions = clean_svg(svg) assert b" bytes: buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: zf.writestr( "[Content_Types].xml", """ """, ) zf.writestr( "word/document.xml", 'Hello', ) zf.writestr( "docProps/app.xml", f'{app_name}', ) zf.writestr( "customXml/item1.xml", 'c2pa contentcredentials', ) return buf.getvalue() def test_docx_strips_app_and_customxml(tmp_path: Path): data = _make_docx_with_app() cleaned, actions = clean_docx(data) assert any("customXml" in a or "Application" in a or "drop" in a for a in actions) with zipfile.ZipFile(io.BytesIO(cleaned)) as zf: names = zf.namelist() assert "word/document.xml" in names assert not any(n.startswith("customXml/") for n in names) app = zf.read("docProps/app.xml").decode() assert "Claude" not in app def _make_docx_with_body_text(body_text: str = "Claude wrote this.") -> bytes: buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: zf.writestr( "[Content_Types].xml", """ """, ) zf.writestr( "word/document.xml", '' + body_text + "", ) zf.writestr( "docProps/core.xml", '', ) return buf.getvalue() def test_docx_body_vendor_word_is_not_ai_metadata(): from container_meta import inspect_docx data = _make_docx_with_body_text() has_c2pa, has_ai, findings, _ = inspect_docx(data) assert not has_c2pa assert not has_ai assert not any("Claude" in f for f in findings) def test_docx_metadata_vendor_word_is_still_flagged(): from container_meta import inspect_docx data = _make_docx_with_app("Claude AI Writer") has_c2pa, has_ai, findings, _ = inspect_docx(data) assert has_ai assert any("Claude" in f for f in findings) def _make_odt(generator: str = "Anthropic Claude") -> bytes: buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: zf.writestr("mimetype", "application/vnd.oasis.opendocument.text") zf.writestr( "meta.xml", f'{generator}', ) zf.writestr( "content.xml", '', ) zf.writestr( "META-INF/manifest.xml", '', ) return buf.getvalue() def test_odt_drops_generator(tmp_path: Path): data = _make_odt() cleaned, actions = clean_odt(data) assert any("generator" in a for a in actions) with zipfile.ZipFile(io.BytesIO(cleaned)) as zf: meta = zf.read("meta.xml").decode() assert "Claude" not in meta assert "meta:generator" not in meta or "Anthropic" not in meta def test_clean_container_markdown_file(tmp_path: Path): src = tmp_path / "x.md" src.write_text("---\ngenerator: OpenAI\n---\nHi\u200b\n", encoding="utf-8") dest = tmp_path / "x.cleaned.md" result = clean_container(src, dest) assert dest.is_file() body = dest.read_text(encoding="utf-8") assert "generator" not in body assert "\u200b" not in body assert result["format"] == "markdown" def test_inspect_container_svg(tmp_path: Path): src = tmp_path / "a.svg" src.write_bytes( b'c2pa' ) report = inspect_container(src) assert report.format == "svg" assert report.has_c2pa or report.has_ai_metadata def test_fixtures_md_html_svg_roundtrip(tmp_path: Path): root = Path(__file__).resolve().parents[1] / "tests" / "fixtures" for name in ("sample_ai.md", "sample_ai.html", "sample_meta.svg"): src = root / name dest = tmp_path / f"{name}.cleaned{src.suffix}" result = clean_container(src, dest) assert dest.is_file() assert result["format"] in ("markdown", "html", "svg") # AI-ish keys/tags should be reduced body = dest.read_bytes().lower() assert b"chatgpt" not in body assert b"generator: claude" not in body def test_pdf_degraded_clean_without_crash(tmp_path: Path): """Minimal PDF with an XMP packet; clean should not raise (may be degraded).""" from container_meta import clean_pdf, inspect_pdf xmp = ( b"" b"" b"" b"" b"trainedAlgorithmicMedia" b"" b"" ) # Minimal-ish PDF skeleton (not renderable; enough for byte-level tools) pdf = ( b"%PDF-1.4\n" b"1 0 obj<<>>endobj\n" b"trailer<<>>\n" + xmp + b"\n%%EOF\n" ) src = tmp_path / "t.pdf" dest = tmp_path / "t.cleaned.pdf" src.write_bytes(pdf) has_c2pa, has_ai, findings, _ = inspect_pdf(src, pdf) assert has_ai or has_c2pa or findings actions, meta = clean_pdf(src, dest) assert dest.is_file() assert actions assert meta.get("mode") in ("exiftool", "stdlib-xmp", "copy")