diff --git a/tests/test_tmux.py b/tests/test_tmux.py index 2b48878..69515bd 100644 --- a/tests/test_tmux.py +++ b/tests/test_tmux.py @@ -139,6 +139,151 @@ async def test_list_windows(runner): assert result == ["hqt", "hqt-1", "hqt-2"] +@pytest.mark.asyncio +async def test_reset_window_indexes_missing_home_returns_false(runner): + runner._exec.return_value = ( + 0, + "@1\t1\thqt-foo\n@2\t4\thqt-bar\n", + "", + ) + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is False + runner._exec.assert_called_once_with( + "list-windows", + "-t", + "hqt-main", + "-F", + "#{window_id}\t#{window_index}\t#{window_name}", + ) + + +@pytest.mark.asyncio +async def test_reset_window_indexes_already_compact_no_moves(runner): + runner._exec.return_value = ( + 0, + "@0\t0\t⌂ HQT\n@1\t1\thqt-foo\n@2\t2\thqt-bar\n", + "", + ) + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is True + assert runner._exec.call_count == 1 + + +@pytest.mark.asyncio +async def test_reset_window_indexes_sparse_indexes_compact_in_current_order(runner): + runner._exec.side_effect = [ + ( + 0, + "@0\t0\t⌂ HQT\n@1\t1\thqt-foo\n@4\t4\thqt-bar\n@9\t9\thqt-baz\n", + "", + ), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + ] + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is True + assert runner._exec.call_count == 9 + calls = [call.args for call in runner._exec.call_args_list] + assert calls[1] == ("move-window", "-s", "@0", "-t", "hqt-main:14") + assert calls[2] == ("move-window", "-s", "@1", "-t", "hqt-main:15") + assert calls[3] == ("move-window", "-s", "@4", "-t", "hqt-main:16") + assert calls[4] == ("move-window", "-s", "@9", "-t", "hqt-main:17") + assert calls[5] == ("move-window", "-s", "@0", "-t", "hqt-main:0") + assert calls[6] == ("move-window", "-s", "@1", "-t", "hqt-main:1") + assert calls[7] == ("move-window", "-s", "@4", "-t", "hqt-main:2") + assert calls[8] == ("move-window", "-s", "@9", "-t", "hqt-main:3") + + +@pytest.mark.asyncio +async def test_reset_window_indexes_home_not_zero_moves_home_first_in_final_layout( + runner, +): + runner._exec.side_effect = [ + ( + 0, + "@2\t2\thqt-foo\n@5\t5\t⌂ HQT\n@7\t7\thqt-bar\n", + "", + ), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + (0, "", ""), + ] + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is True + calls = [call.args for call in runner._exec.call_args_list] + assert calls[1] == ("move-window", "-s", "@2", "-t", "hqt-main:11") + assert calls[2] == ("move-window", "-s", "@5", "-t", "hqt-main:12") + assert calls[3] == ("move-window", "-s", "@7", "-t", "hqt-main:13") + assert calls[4] == ("move-window", "-s", "@5", "-t", "hqt-main:0") + assert calls[5] == ("move-window", "-s", "@2", "-t", "hqt-main:1") + assert calls[6] == ("move-window", "-s", "@7", "-t", "hqt-main:2") + + +@pytest.mark.asyncio +async def test_reset_window_indexes_move_failure_returns_false(runner): + runner._exec.side_effect = [ + ( + 0, + "@0\t0\t⌂ HQT\n@1\t1\thqt-foo\n@4\t4\thqt-bar\n", + "", + ), + (0, "", ""), + (1, "", "index in use"), + ] + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is False + assert runner._exec.call_count == 3 + + +@pytest.mark.asyncio +async def test_reset_window_indexes_list_failure_returns_false(runner): + runner._exec.return_value = (1, "", "no server running") + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is False + runner._exec.assert_called_once_with( + "list-windows", + "-t", + "hqt-main", + "-F", + "#{window_id}\t#{window_index}\t#{window_name}", + ) + + +@pytest.mark.asyncio +async def test_reset_window_indexes_malformed_list_line_returns_false(runner): + runner._exec.return_value = ( + 0, + "@0\t0\t⌂ HQT\nnot-enough-columns\n", + "", + ) + + result = await runner.reset_window_indexes("⌂ HQT") + + assert result is False + assert runner._exec.call_count == 1 + + @pytest.mark.asyncio async def test_manager_spawn(runner): runner._exec.side_effect = [