ba3eb0c090
Phase 6: Eliminate Optional[T] returns - BATCH 2 of 7
Before: 7 more Optional[T] returns removed
After: 0 in command_palette.py, diff_viewer.py, fuzzy_anchor.py,
multi_agent_conductor.py, patch_modal.py, app_controller.py
Delta: -7 sites (cumulative: -15 of 30)
Specific changes:
- src/command_palette.py:50: CommandRegistry.get() returns Command (zero-init
sentinel: id="", title="", category="uncategorized", action=lambda: None)
- src/diff_viewer.py:117: get_line_color returns "" when no marker prefix
- src/fuzzy_anchor.py:40: FuzzyAnchor.resolve_slice returns (-1, -1) sentinel
(replaced 3x `return None` with `return (-1, -1)`)
- src/multi_agent_conductor.py:64: WorkerPool.spawn returns threading.Thread()
(empty sentinel, not started) when pool is full
- src/patch_modal.py:33: PatchModalManager.get_pending_patch returns
PendingPatch; class has EMPTY_PATCH sentinel; field type changed from
Optional[PendingPatch] to PendingPatch; 2x `= None` reset replaced with
`= EMPTY_PATCH`
- src/app_controller.py:4414: _confirm_and_run returns "" when not approved
(was Optional[str] returning None)
Test updates:
- tests/test_diff_viewer.py:95: get_line_color(" context") == ""
- tests/test_fuzzy_anchor.py:42,59: assert result == (-1, -1)
- tests/test_parallel_execution.py:31: t3 sentinel is now unstarted thread
(check via not t3.is_alive())
- tests/test_patch_modal.py:9,31,78: get_pending_patch() == "" sentinel check
Verification:
- audit_weak_types --strict: OK (107 <= 112 baseline)
- 22+ tests pass (test_diff_viewer, test_fuzzy_anchor,
test_parallel_execution, test_patch_modal, test_command_palette)
- py_check_syntax: OK on all changed files
REMAINING: ~15 Optional[T] returns in:
- src/external_editor.py (3)
- src/file_cache.py (7)
- src/diff_viewer.py: parse_hunk_header (1)
- src/models.py: ExternalEditorConfig.get_default (1)
- src/project_manager.py: load_track_state (1)
- src/session_logger.py: log_tool_call (1)
- src/app_controller.py: _pending_mma_spawn, _pending_mma_approval (2)
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
import pytest
|
|
from src.fuzzy_anchor import FuzzyAnchor
|
|
|
|
|
|
class TestFuzzyAnchor:
|
|
def test_create_slice_basic(self):
|
|
text = "line0\nline1\nline2\nline3\nline4\n"
|
|
result = FuzzyAnchor.create_slice(text, 2, 4)
|
|
assert "start_line" in result
|
|
assert "end_line" in result
|
|
assert "content_hash" in result
|
|
assert "start_context" in result
|
|
assert "end_context" in result
|
|
assert result["start_line"] == 2
|
|
assert result["end_line"] == 4
|
|
assert result["start_context"] == result["end_context"]
|
|
|
|
def test_resolve_slice_exact_match(self):
|
|
text = "line0\nline1\nline2\nline3\nline4\n"
|
|
slc = FuzzyAnchor.create_slice(text, 2, 4)
|
|
result = FuzzyAnchor.resolve_slice(text, slc)
|
|
assert result is not None
|
|
start, end = result
|
|
assert start == 2
|
|
assert end == 4
|
|
|
|
def test_resolve_slice_line_inserted_before(self):
|
|
original = "line0\nline1\nline2\nline3\nline4\n"
|
|
modified = "NEW\nline0\nline1\nline2\nline3\nline4\n"
|
|
slc = FuzzyAnchor.create_slice(original, 2, 4)
|
|
result = FuzzyAnchor.resolve_slice(modified, slc)
|
|
assert result is not None
|
|
start, end = result
|
|
assert start == 3
|
|
assert end == 5
|
|
|
|
def test_resolve_slice_line_deleted_before_returns_none(self):
|
|
original = "line0\nline1\nline2\nline3\nline4\n"
|
|
modified = "line0\nline2\nline3\nline4\n"
|
|
slc = FuzzyAnchor.create_slice(original, 2, 4)
|
|
result = FuzzyAnchor.resolve_slice(modified, slc)
|
|
assert result == (-1, -1)
|
|
|
|
def test_resolve_slice_multiple_lines_changed(self):
|
|
original = "line0\nline1\nline2\nline3\nline4\n"
|
|
modified = "a\nb\nc\nd\ne\nline0\nline1\nline2\nline3\nline4\n"
|
|
slc = FuzzyAnchor.create_slice(original, 1, 2)
|
|
result = FuzzyAnchor.resolve_slice(modified, slc)
|
|
assert result is not None
|
|
start, end = result
|
|
assert start == 6
|
|
assert end == 7
|
|
|
|
def test_resolve_slice_anchor_mismatch_returns_none(self):
|
|
original = "alpha\nbeta\ngamma\ndelta\nepsilon\n"
|
|
modified = "foo\nbar\nbaz\ndelta\nepsilon\n"
|
|
slc = FuzzyAnchor.create_slice(original, 2, 3)
|
|
result = FuzzyAnchor.resolve_slice(modified, slc)
|
|
assert result == (-1, -1)
|