123 lines
4.5 KiB
Python
123 lines
4.5 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
RUNTIME = Path(__file__).resolve().parents[1] / "runtime"
|
|
sys.path.insert(0, str(RUNTIME))
|
|
import source_hygiene # noqa: E402
|
|
|
|
|
|
class SourceHygieneTest(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.tempdir = tempfile.TemporaryDirectory()
|
|
self.root = Path(self.tempdir.name)
|
|
source = self.root / "harness/source"
|
|
source.mkdir(parents=True)
|
|
workflow = source / "workflow.json"
|
|
body = source / "body.md"
|
|
target = self.root / ".claude/commands/demo.md"
|
|
target.parent.mkdir(parents=True)
|
|
workflow.write_text(
|
|
json.dumps({
|
|
"source_kind": "workflow",
|
|
"targets": [{"path": ".claude/commands/demo.md"}],
|
|
}),
|
|
encoding="utf-8",
|
|
)
|
|
body.write_text("# 정상 중립 본문\n", encoding="utf-8")
|
|
target.write_text("# 정상 생성 본문\n", encoding="utf-8")
|
|
(source / "generation-manifest.json").write_text(
|
|
json.dumps({
|
|
"schema_version": 1,
|
|
"sources": [{
|
|
"metadata": "harness/source/workflow.json",
|
|
"body": "harness/source/body.md",
|
|
}],
|
|
}),
|
|
encoding="utf-8",
|
|
)
|
|
(self.root / ".repomixignore").write_text(
|
|
".agents/plugins/wiki-superpowers/scratch/**\n"
|
|
"**/__pycache__/**\n"
|
|
"**/*.pyc\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def tearDown(self) -> None:
|
|
self.tempdir.cleanup()
|
|
|
|
def test_clean_neutral_and_generated_context_passes(self) -> None:
|
|
result = source_hygiene.check(self.root)
|
|
self.assertEqual(result["status"], "PASS")
|
|
self.assertEqual(result["findings"], [])
|
|
|
|
def test_transport_merge_and_scratch_residue_fail(self) -> None:
|
|
body = self.root / "harness/source/body.md"
|
|
body.write_text(
|
|
"<content>\n<<<<<<< HEAD\n"
|
|
"import .agents/plugins/wiki-superpowers/scratch/build_master_report.py\n",
|
|
encoding="utf-8",
|
|
)
|
|
codes = {finding["code"] for finding in source_hygiene.check(self.root)["findings"]}
|
|
self.assertEqual(codes, {"TRANSPORT_WRAPPER", "MERGE_MARKER", "SCRATCH_REFERENCE"})
|
|
|
|
def test_missing_repomix_defense_in_depth_rule_fails(self) -> None:
|
|
(self.root / ".repomixignore").write_text("**/*.pyc\n", encoding="utf-8")
|
|
result = source_hygiene.check(self.root)
|
|
self.assertEqual(result["status"], "FAIL")
|
|
self.assertEqual(
|
|
{finding.get("rule") for finding in result["findings"]},
|
|
{".agents/plugins/wiki-superpowers/scratch/**", "**/__pycache__/**"},
|
|
)
|
|
|
|
def test_generated_wrapper_and_retired_scratch_file_fail(self) -> None:
|
|
target = self.root / ".claude/commands/demo.md"
|
|
target.write_text('<file path="demo">\n', encoding="utf-8")
|
|
scratch = self.root / ".agents/plugins/wiki-superpowers/scratch"
|
|
scratch.mkdir(parents=True)
|
|
(scratch / "retired.py").write_text("# executable residue\n", encoding="utf-8")
|
|
|
|
result = source_hygiene.check(self.root)
|
|
|
|
self.assertEqual(result["status"], "FAIL")
|
|
self.assertEqual(
|
|
{finding["code"] for finding in result["findings"]},
|
|
{"TRANSPORT_WRAPPER", "ACTIVE_SCRATCH_FILE"},
|
|
)
|
|
|
|
def test_control_bytes_and_unresolved_generator_placeholder_fail(self) -> None:
|
|
body = self.root / "harness/source/body.md"
|
|
body.write_text("neutral\x00body\n", encoding="utf-8")
|
|
target = self.root / ".claude/commands/demo.md"
|
|
target.write_text("# generated\n{{arguments}}\n", encoding="utf-8")
|
|
|
|
result = source_hygiene.check(self.root)
|
|
|
|
codes = {finding["code"] for finding in result["findings"]}
|
|
self.assertEqual(
|
|
codes,
|
|
{"FORBIDDEN_CONTROL_CHARACTER", "UNRESOLVED_GENERATOR_PLACEHOLDER"},
|
|
)
|
|
|
|
def test_neutral_source_allows_only_arguments_placeholder(self) -> None:
|
|
body = self.root / "harness/source/body.md"
|
|
body.write_text("# neutral\n{{arguments}}\n{{ target_path }}\n", encoding="utf-8")
|
|
|
|
result = source_hygiene.check(self.root)
|
|
|
|
findings = [
|
|
item for item in result["findings"]
|
|
if item["code"] == "UNAPPROVED_NEUTRAL_PLACEHOLDER"
|
|
]
|
|
self.assertEqual(len(findings), 1)
|
|
self.assertEqual(findings[0]["placeholder"], "{{ target_path }}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|