File size: 20,003 Bytes
79a3ba9
 
 
 
 
 
 
 
 
 
 
 
f4952b6
 
8947d88
79a3ba9
 
f4952b6
79a3ba9
 
 
2a9dc4c
79a3ba9
2a9dc4c
79a3ba9
 
 
 
 
 
 
 
2a9dc4c
79a3ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62eac4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79a3ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1de134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79a3ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8947d88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48f2b75
 
 
 
 
 
 
 
 
 
c35ac38
 
48f2b75
8947d88
 
 
 
 
 
 
 
 
 
 
f4952b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
"""The command line is a published interface, so it is run as a process here.

Nothing in this file reaches into the CLI module. Each test starts the same process a
reader of the card would start, and asserts on the exit code and on the text that comes
back, because a documented command that crashes is a broken promise even when every
library call beside it is correct.
"""

from __future__ import annotations

import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import time
from pathlib import Path
from typing import Any, Dict

from lingologos_python.patterns import PATTERNS

from lingologos_python.rules import RULE_IDS

ROOT = Path(__file__).resolve().parents[1]
LAB = ROOT / "fixtures" / "pattern_lab"
SAMPLE = ROOT / "fixtures" / "sample_pkg"


def run_cli(*args: str) -> subprocess.CompletedProcess:
    return subprocess.run(
        [sys.executable, "-B", "-m", "lingologos_python", *args],
        cwd=str(ROOT),
        capture_output=True,
        text=True,
    )


def _tree_state(root: Path) -> Dict[str, str]:
    state = {}
    for path in sorted(root.rglob("*")):
        if path.is_file():
            digest = hashlib.sha256(path.read_bytes()).hexdigest()
            state[str(path.relative_to(root))] = digest
    return state


def test_patterns_list_prints_every_card_once_in_a_stable_order() -> None:
    proc = run_cli("patterns", "list")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    lines = [line for line in proc.stdout.splitlines() if line.strip()]
    assert len(lines) == len(PATTERNS)
    assert [line.split()[0] for line in lines] == sorted(PATTERNS)
    for line in lines:
        assert line.split()[1] in {"contracts", "mutation", "resources", "shape", "typing"}


def test_patterns_list_json_carries_the_fields_a_reader_needs() -> None:
    proc = run_cli("patterns", "list", "--json")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    rows = json.loads(proc.stdout)["patterns"]
    assert [row["pattern_id"] for row in rows] == sorted(PATTERNS)
    for row in rows:
        assert set(row) == {
            "pattern_id",
            "family",
            "intent_tags",
            "min_assurance",
            "rule",
            "companions",
        }
        assert row["rule"] in RULE_IDS
        assert row["companions"]
        assert row["min_assurance"] >= 2


def test_patterns_show_answers_for_one_card_in_text_and_in_json() -> None:
    proc = run_cli("patterns", "show", "--pattern", "chain-to-registry")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    for part in ("shape", "why", "needs", "lock", "refuses"):
        assert part in proc.stdout
    assert "BRANCH_SHAPE_MISMATCH" in proc.stdout

    proc = run_cli("patterns", "show", "--pattern", "chain-to-registry", "--json")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    payload: Dict[str, Any] = json.loads(proc.stdout)
    assert payload["pattern_id"] == "chain-to-registry"
    assert payload["lock_claims"]


def test_an_unknown_card_and_a_missing_flag_answer_before_any_work_starts() -> None:
    proc = run_cli("patterns", "show", "--pattern", "no-such-card")
    assert proc.returncode == 2
    assert "unknown pattern" in proc.stderr

    proc = run_cli("patterns", "scan", "--workspace", str(LAB))
    assert proc.returncode == 2
    assert "--pattern" in proc.stderr

    proc = run_cli("patterns", "apply", "--pattern", "chain-to-registry")
    assert proc.returncode == 2
    assert "--workspace" in proc.stderr


def test_patterns_scan_names_the_functions_in_the_shape_of_the_card() -> None:
    proc = run_cli("patterns", "scan", "--workspace", str(LAB), "--pattern", "chain-to-registry")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "src/registry.py::render" in proc.stdout
    assert "render_pair" not in proc.stdout


def test_patterns_scan_shows_the_edits_a_hit_would_apply_and_dumps_nothing() -> None:
    proc = run_cli("patterns", "scan", "--workspace", str(LAB), "--pattern", "chain-to-registry")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    lines = [line.strip() for line in proc.stdout.splitlines() if line.strip()]
    assert lines[0] == "chain-to-registry: 1 hit(s) in this workspace"
    assert lines[1] == "src/registry.py::render"
    assert lines[2].startswith("src/registry.py")
    assert lines[3].startswith("tests/test_registry.py")
    assert any(line.startswith("why:") for line in lines)
    for noise in ("RuleHit", "Edit(", "evidence=", "Traceback"):
        assert noise not in proc.stdout


def test_patterns_scan_json_carries_the_edits_a_reader_can_act_on() -> None:
    proc = run_cli(
        "patterns",
        "scan",
        "--workspace",
        str(LAB),
        "--pattern",
        "chain-to-registry",
        "--json",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    payload: Dict[str, Any] = json.loads(proc.stdout)
    assert payload["pattern"] == "chain-to-registry"
    assert len(payload["hits"]) == 1
    hit = payload["hits"][0]
    assert hit["target"] == "src/registry.py::render"
    assert hit["rule"] == "chain-to-registry"
    assert [edit["file"] for edit in hit["edits"]] == ["src/registry.py", "tests/test_registry.py"]
    for edit in hit["edits"]:
        assert edit["search"] and edit["replace"] and edit["label"]
    assert hit["evidence"] and all(hit["evidence"])


def test_patterns_scan_says_so_when_nothing_in_the_workspace_qualifies() -> None:
    proc = run_cli("patterns", "scan", "--workspace", str(SAMPLE), "--pattern", "chain-to-registry")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "no function in this workspace has the shape of this card" in proc.stdout


def test_apply_dry_run_verifies_the_pair_without_writing_it(pattern_lab: Path) -> None:
    before = _tree_state(pattern_lab)
    proc = run_cli(
        "patterns",
        "apply",
        "--workspace",
        str(pattern_lab),
        "--pattern",
        "chain-to-registry",
        "--target",
        "src/registry.py::render",
        "--dry-run",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "VERIFIED" in proc.stdout
    assert "committed=False" in proc.stdout
    assert _tree_state(pattern_lab) == before


def test_a_shape_the_card_does_not_claim_exits_non_zero_and_writes_nothing(pattern_lab: Path) -> None:
    before = _tree_state(pattern_lab)
    proc = run_cli(
        "patterns",
        "apply",
        "--workspace",
        str(pattern_lab),
        "--pattern",
        "chain-to-registry",
        "--target",
        "src/registry.py::render_pair",
        "--dry-run",
    )
    assert proc.returncode == 1
    assert "REFUSED" in proc.stdout
    assert "BRANCH_SHAPE_MISMATCH" in proc.stdout
    assert "nothing was written" in proc.stdout
    assert _tree_state(pattern_lab) == before


def test_apply_exits_zero_only_once_the_pair_has_landed(pattern_lab: Path) -> None:
    before = _tree_state(pattern_lab)
    proc = run_cli(
        "patterns",
        "apply",
        "--workspace",
        str(pattern_lab),
        "--pattern",
        "chain-to-registry",
        "--target",
        "src/registry.py::render",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "VERIFIED" in proc.stdout
    assert "committed=True" in proc.stdout
    assert _tree_state(pattern_lab) != before


def test_symbols_names_the_functions_of_a_workspace_and_ranks_them() -> None:
    proc = run_cli("symbols", "--workspace", str(LAB), "--limit", "200")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "files=" in proc.stdout
    assert "src/registry.py::render" in proc.stdout


def test_select_tests_answers_with_the_tests_that_import_the_changed_file() -> None:
    proc = run_cli("select-tests", "--workspace", str(LAB), "--files", "src/registry.py")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "tests/test_registry.py" in proc.stdout.split()


def test_the_adapter_answers_the_contract_of_the_kernel() -> None:
    proc = run_cli("check-adapter", "--workspace", str(SAMPLE))
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "ADAPTER OK" in proc.stdout


def _delete_workspace(tmp_path: Path) -> Path:
    ws = tmp_path / "delete_pkg"
    shutil.copytree(SAMPLE, ws)
    (ws / "src" / "helper.py").write_text(
        '"""Extra module."""\n\nVALUE = 1\n', encoding="utf-8", newline=""
    )
    return ws


def test_delete_dry_run_verifies_the_removal_without_touching_the_disk(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    proc = run_cli("delete", "--workspace", str(ws), "--file", "src/helper.py", "--dry-run")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "VERIFIED" in proc.stdout
    assert "committed=False" in proc.stdout
    assert (ws / "src" / "helper.py").is_file()


def test_delete_exits_zero_only_once_the_file_is_really_gone(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    proc = run_cli(
        "delete",
        "--workspace",
        str(ws),
        "--file",
        "src/helper.py",
        "--node-timeout",
        "60",
        "--total-timeout",
        "300",
        "--gate-timeout",
        "60",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "committed=True" in proc.stdout
    assert not (ws / "src" / "helper.py").exists()


def test_delete_answers_a_refusal_for_a_file_that_is_not_there(tmp_path: Path) -> None:
    ws = tmp_path / "delete_empty"
    shutil.copytree(SAMPLE, ws)
    proc = run_cli("delete", "--workspace", str(ws), "--file", "src/absent.py")
    assert proc.returncode == 1
    assert "REFUSED" in proc.stderr


def _hold_the_lock(ws: Path) -> None:
    """A lock record naming this live test process, the shape a running session writes."""
    (ws / ".lingologos-session.lock").write_text(
        "%d - %s" % (os.getpid(), "0" * 32), encoding="utf-8", newline=""
    )


def test_graph_shows_both_directions_and_the_reverse_index_agrees() -> None:
    proc = run_cli("graph", "--workspace", str(LAB))
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "import_edges=" in proc.stdout
    back = [line for line in proc.stdout.splitlines() if line.startswith("  src/registry.py <- ")]
    assert back and "tests/test_registry.py" in back[0]


def test_graph_json_lists_the_importers_of_every_file() -> None:
    proc = run_cli("graph", "--workspace", str(LAB), "--json")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    payload = json.loads(proc.stdout)
    assert set(payload) == {"files", "import_edges", "importers"}
    assert "tests/test_registry.py" in payload["importers"]["src/registry.py"]
    assert any(edge[1] == "src/registry.py" for edge in payload["import_edges"])
    for source, target in payload["import_edges"]:
        assert source in payload["importers"][target]


def test_graph_refuses_a_workspace_that_is_not_a_directory(tmp_path: Path) -> None:
    proc = run_cli("graph", "--workspace", str(tmp_path / "nowhere"))
    assert proc.returncode == 1
    assert "does not exist" in proc.stderr


def test_lock_status_reads_a_quiet_workspace_as_free(tmp_path: Path) -> None:
    proc = run_cli("lock-status", "--workspace", str(tmp_path), "--json")
    assert proc.returncode == 0, proc.stdout + proc.stderr
    payload = json.loads(proc.stdout)
    assert payload["present"] is False
    assert payload["held"] is False
    text = run_cli("lock-status", "--workspace", str(tmp_path))
    assert text.returncode == 0
    assert "free: no lock file" in text.stdout


def test_lock_status_names_a_live_holder_and_says_held(tmp_path: Path) -> None:
    _hold_the_lock(tmp_path)
    proc = run_cli("lock-status", "--workspace", str(tmp_path))
    assert proc.returncode == 1
    assert "held: process %d" % os.getpid() in proc.stdout


def test_lock_status_reads_a_leftover_lock_as_free(tmp_path: Path) -> None:
    (tmp_path / ".lingologos-session.lock").write_text(
        "999999 - %s" % ("0" * 32), encoding="utf-8", newline=""
    )
    proc = run_cli("lock-status", "--workspace", str(tmp_path))
    assert proc.returncode == 0
    assert "no longer runs" in proc.stdout


def test_a_held_workspace_refuses_at_once_and_a_wait_budget_is_honoured(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    _hold_the_lock(ws)
    started = time.monotonic()
    proc = run_cli(
        "delete", "--workspace", str(ws), "--file", "src/helper.py",
        "--dry-run", "--lock-wait", "0",
    )
    at_once = time.monotonic() - started
    assert proc.returncode == 1
    assert "SESSION_LOCKED" in proc.stdout
    started = time.monotonic()
    proc = run_cli(
        "delete", "--workspace", str(ws), "--file", "src/helper.py",
        "--dry-run", "--lock-wait", "0.6",
    )
    waited = time.monotonic() - started
    assert proc.returncode == 1
    assert "SESSION_LOCKED" in proc.stdout
    assert waited >= at_once + 0.3


def test_a_config_file_sets_the_same_knobs_and_a_flag_still_wins(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    _hold_the_lock(ws)
    config = tmp_path / "codex.python.json"
    config.write_text(json.dumps({"lock_wait": -5}), encoding="utf-8")
    refused = run_cli(
        "delete", "--workspace", str(ws), "--file", "src/helper.py", "--config", str(config)
    )
    assert refused.returncode == 2
    assert "REFUSED" in refused.stderr
    config.write_text(json.dumps({"lock_wait": 1.5}), encoding="utf-8")
    started = time.monotonic()
    overridden = run_cli(
        "delete", "--workspace", str(ws), "--file", "src/helper.py",
        "--config", str(config), "--lock-wait", "0",
    )
    at_once = time.monotonic() - started
    assert overridden.returncode == 1
    assert "SESSION_LOCKED" in overridden.stdout
    started = time.monotonic()
    followed = run_cli(
        "delete", "--workspace", str(ws), "--file", "src/helper.py", "--config", str(config)
    )
    waited = time.monotonic() - started
    assert followed.returncode == 1
    assert "SESSION_LOCKED" in followed.stdout
    assert waited >= at_once + 1.2


def test_a_config_file_that_makes_no_sense_refuses_with_exit_two(tmp_path: Path) -> None:
    ws = tmp_path / "quiet"
    ws.mkdir()
    config = tmp_path / "bad.json"
    config.write_text(json.dumps({"invented": 1}), encoding="utf-8")
    unknown = run_cli("verify", "--workspace", str(ws), "--config", str(config))
    assert unknown.returncode == 2
    assert "unknown key" in unknown.stderr
    config.write_text("{ this is not json", encoding="utf-8")
    broken = run_cli("verify", "--workspace", str(ws), "--config", str(config))
    assert broken.returncode == 2
    assert "not JSON" in broken.stderr
    config.write_text(json.dumps({"strict_tools": "yes"}), encoding="utf-8")
    mistyped = run_cli("verify", "--workspace", str(ws), "--config", str(config))
    assert mistyped.returncode == 2
    assert "true or false" in mistyped.stderr


def test_an_audit_file_collects_one_json_line_per_session_run(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    _hold_the_lock(ws)
    audit = tmp_path / "ledger.jsonl"
    for _ in range(2):
        proc = run_cli(
            "delete", "--workspace", str(ws), "--file", "src/helper.py",
            "--dry-run", "--lock-wait", "0", "--audit", str(audit),
        )
        assert proc.returncode == 1
        assert "SESSION_LOCKED" in proc.stdout
    lines = audit.read_text(encoding="utf-8").splitlines()
    assert len(lines) == 2
    for line in lines:
        row = json.loads(line)
        assert row["command"] == "delete"
        assert row["verdict"] == "REFUSED"
        assert row["reason"] == "SESSION_LOCKED"
        assert row["committed"] is False


def test_the_exit_code_table_in_the_card_holds_for_the_real_commands() -> None:
    readme = (ROOT / "README.md").read_text(encoding="utf-8", newline="")
    table: Dict[str, Dict[int, str]] = {}
    for line in readme.splitlines():
        if not line.startswith("| `"):
            continue
        cells = [cell.strip() for cell in line.strip().strip("|").split("|")]
        if len(cells) != 4:
            continue
        codes = {0: cells[1], 1: cells[2], 2: cells[3]}
        for verb in cells[0].strip("`").split("`, `"):
            table[verb] = codes
    assert table, "the card lost its exit code table"

    surface = re.search(r"\{([a-z][a-z, |-]+)\}", run_cli("--help").stdout)
    assert surface is not None, "the parser surface no longer prints in the usage line"
    for verb in (part.strip() for part in surface.group(1).split(",")):
        covered = any(name == verb or name.startswith(verb + " ") for name in table)
        assert covered, "the table forgot the verb " + verb

    probes = [
        (("patterns", "show", "--pattern", "no-such-card"), "patterns show", 2),
        (("graph", "--workspace", str(ROOT / "no-such-workspace")), "graph", 1),
        (("lock-status", "--workspace", str(LAB)), "lock-status", 0),
        (("symbols", "--workspace", str(SAMPLE)), "symbols", 0),
        (("verify", "--workspace", str(SAMPLE), "--file", "src/mathops.py"), "verify", 0),
    ]
    for argv, verb, expected in probes:
        proc = run_cli(*argv)
        assert proc.returncode == expected, "%s answered %d, not %d" % (
            " ".join(argv),
            proc.returncode,
            expected,
        )
        cell = table[verb][expected]
        assert cell and cell != "--", "%s answers %d but the table leaves that cell empty" % (
            verb,
            expected,
        )


def test_protect_adds_one_more_path_the_session_will_not_write(tmp_path: Path) -> None:
    ws = tmp_path / "guarded"
    shutil.copytree(SAMPLE, ws)
    before = (ws / "src" / "mathops.py").read_text(encoding="utf-8")
    proc = run_cli(
        "edit",
        "--workspace",
        str(ws),
        "--file",
        "src/mathops.py",
        "--search",
        "return left + right",
        "--replace",
        "return left - right",
        "--protect",
        "src/mathops.py",
    )
    assert proc.returncode == 1, proc.stdout + proc.stderr
    assert "PROTECTED_PATH" in proc.stdout
    assert (ws / "src" / "mathops.py").read_text(encoding="utf-8") == before


def test_print_diff_shows_the_pair_before_the_verdict(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    proc = run_cli(
        "edit",
        "--workspace",
        str(ws),
        "--file",
        "src/mathops.py",
        "--search",
        "return left + right",
        "--replace",
        "return left + right  # the sum keeps its name",
        "--dry-run",
        "--print-diff",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "--- a/src/mathops.py" in proc.stdout
    assert "+++ b/src/mathops.py" in proc.stdout
    assert "-return left + right" in proc.stdout
    assert "+return left + right  # the sum keeps its name" in proc.stdout


def test_delete_prints_what_the_removal_takes_away(tmp_path: Path) -> None:
    ws = _delete_workspace(tmp_path)
    proc = run_cli(
        "delete",
        "--workspace",
        str(ws),
        "--file",
        "src/helper.py",
        "--dry-run",
        "--print-diff",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "--- a/src/helper.py" in proc.stdout
    assert "+++ /dev/null" in proc.stdout
    assert "-VALUE = 1" in proc.stdout


def test_a_card_shows_its_pair_before_the_verdict(pattern_lab: Path) -> None:
    proc = run_cli(
        "patterns",
        "apply",
        "--workspace",
        str(pattern_lab),
        "--pattern",
        "chain-to-registry",
        "--target",
        "src/registry.py::render",
        "--dry-run",
        "--print-diff",
    )
    assert proc.returncode == 0, proc.stdout + proc.stderr
    assert "--- a/src/registry.py" in proc.stdout
    assert "--- a/tests/test_registry.py" in proc.stdout