File size: 6,503 Bytes
c970469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Behavior tests for the ctx-agent-mirror user story."""

from __future__ import annotations

import json
import sys
from pathlib import Path

import pytest

import agent_mirror


def _agent_text(name: str = "reviewer") -> str:
    return (
        "---\n"
        f"name: {name}\n"
        "description: Reviews changes and reports concrete risks.\n"
        "---\n"
        f"# {name}\n\n"
        "Inspect the change and report evidence-backed findings.\n"
    )


@pytest.fixture()
def mirror_dirs(tmp_path: Path) -> tuple[Path, Path]:
    agents_dir = tmp_path / "agents"
    wiki_dir = tmp_path / "wiki"
    agents_dir.mkdir()
    wiki_dir.mkdir()
    return agents_dir, wiki_dir


def test_mirror_one_writes_verbatim_and_then_reports_unchanged(mirror_dirs) -> None:
    agents_dir, wiki_dir = mirror_dirs
    source = agents_dir / "reviewer.md"
    body = _agent_text()
    source.write_text(body, encoding="utf-8")

    first = agent_mirror.mirror_one(
        "reviewer",
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
    )
    destination = wiki_dir / "converted-agents" / "reviewer.md"

    assert first.status == "mirrored"
    assert first.bytes_copied == len(body.encode("utf-8"))
    assert destination.read_text(encoding="utf-8") == body

    second = agent_mirror.mirror_one(
        "reviewer",
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
    )
    assert second.status == "unchanged"
    assert second.bytes_copied == 0


def test_mirror_one_dry_run_and_force_are_explicit(mirror_dirs) -> None:
    agents_dir, wiki_dir = mirror_dirs
    source = agents_dir / "reviewer.md"
    source.write_text(_agent_text(), encoding="utf-8")
    destination = wiki_dir / "converted-agents" / "reviewer.md"

    dry_run = agent_mirror.mirror_one(
        "reviewer",
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
        dry_run=True,
    )
    assert dry_run.status == "mirrored"
    assert dry_run.message == "dry-run: no files written"
    assert not destination.exists()

    destination.parent.mkdir(parents=True)
    destination.write_text("stale\n", encoding="utf-8")
    forced = agent_mirror.mirror_one(
        "reviewer",
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
        force=True,
    )
    assert forced.status == "mirrored"
    assert destination.read_text(encoding="utf-8") == _agent_text()


def test_mirror_all_filters_pipeline_fragments_non_agents_and_nested_files(
    mirror_dirs,
) -> None:
    agents_dir, wiki_dir = mirror_dirs
    (agents_dir / "reviewer.md").write_text(_agent_text(), encoding="utf-8")
    (agents_dir / "BUILDER.md").write_text(_agent_text("BUILDER"), encoding="utf-8")
    (agents_dir / "notes.md").write_text("# not an agent\n", encoding="utf-8")
    nested = agents_dir / "pipeline"
    nested.mkdir()
    (nested / "nested.md").write_text(_agent_text("nested"), encoding="utf-8")

    results = agent_mirror.mirror_all(agents_dir=agents_dir, wiki_dir=wiki_dir)
    statuses = {result.slug: result.status for result in results}

    assert statuses == {
        "BUILDER": "skipped-pipeline-fragment",
        "notes": "skipped-no-frontmatter",
        "reviewer": "mirrored",
    }
    assert not (wiki_dir / "converted-agents" / "nested.md").exists()


def test_prune_orphans_dry_run_then_apply(mirror_dirs) -> None:
    agents_dir, wiki_dir = mirror_dirs
    (agents_dir / "kept.md").write_text(_agent_text("kept"), encoding="utf-8")
    mirror_dir = wiki_dir / "converted-agents"
    mirror_dir.mkdir()
    kept = mirror_dir / "kept.md"
    orphan = mirror_dir / "orphan.md"
    kept.write_text(_agent_text("kept"), encoding="utf-8")
    orphan.write_text(_agent_text("orphan"), encoding="utf-8")

    preview = agent_mirror.prune_orphans(
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
        dry_run=True,
    )
    assert [(result.slug, result.status) for result in preview] == [("orphan", "pruned")]
    assert orphan.exists()

    applied = agent_mirror.prune_orphans(agents_dir=agents_dir, wiki_dir=wiki_dir)
    assert [(result.slug, result.status) for result in applied] == [("orphan", "pruned")]
    assert kept.exists()
    assert not orphan.exists()


def test_mirror_one_reports_invalid_and_missing_sources(mirror_dirs) -> None:
    agents_dir, wiki_dir = mirror_dirs

    invalid = agent_mirror.mirror_one(
        "../escape",
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
    )
    missing = agent_mirror.mirror_one(
        "missing",
        agents_dir=agents_dir,
        wiki_dir=wiki_dir,
    )

    assert invalid.status == "skipped-no-frontmatter"
    assert invalid.message.startswith("invalid slug:")
    assert missing.status == "not-found"


def test_cli_json_reports_single_slug_result(mirror_dirs, monkeypatch, capsys) -> None:
    agents_dir, wiki_dir = mirror_dirs
    (agents_dir / "reviewer.md").write_text(_agent_text(), encoding="utf-8")
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "ctx-agent-mirror",
            "--slug",
            "reviewer",
            "--agents-dir",
            str(agents_dir),
            "--wiki-dir",
            str(wiki_dir),
            "--json",
        ],
    )

    with pytest.raises(SystemExit) as exit_info:
        agent_mirror.main()

    assert exit_info.value.code == 0
    payload = json.loads(capsys.readouterr().out)
    assert payload[0]["slug"] == "reviewer"
    assert payload[0]["status"] == "mirrored"


def test_cli_read_failure_exits_nonzero(mirror_dirs, monkeypatch, capsys) -> None:
    agents_dir, wiki_dir = mirror_dirs
    source = agents_dir / "broken.md"
    source.write_text(_agent_text("broken"), encoding="utf-8")
    original_read_text = Path.read_text

    def fail_source_read(path: Path, *args, **kwargs):
        if path == source:
            raise OSError("simulated read failure")
        return original_read_text(path, *args, **kwargs)

    monkeypatch.setattr(Path, "read_text", fail_source_read)
    monkeypatch.setattr(
        sys,
        "argv",
        [
            "ctx-agent-mirror",
            "--slug",
            "broken",
            "--agents-dir",
            str(agents_dir),
            "--wiki-dir",
            str(wiki_dir),
            "--json",
        ],
    )

    with pytest.raises(SystemExit) as exit_info:
        agent_mirror.main()

    assert exit_info.value.code == 1
    payload = json.loads(capsys.readouterr().out)
    assert payload[0]["message"] == "read failed: simulated read failure"