File: test_ls.py

package info (click to toggle)
tmuxp 1.64.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: python: 17,788; sh: 22; makefile: 6
file content (744 lines) | stat: -rw-r--r-- 23,109 bytes parent folder | download | duplicates (2)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
"""CLI tests for tmuxp ls command."""

from __future__ import annotations

import contextlib
import json
import pathlib

import pytest

from tmuxp import cli
from tmuxp.cli.ls import (
    _get_workspace_info,
    create_ls_subparser,
)


def test_get_workspace_info_yaml(tmp_path: pathlib.Path) -> None:
    """Extract metadata from YAML workspace file."""
    workspace = tmp_path / "test.yaml"
    workspace.write_text("session_name: my-session\nwindows: []")

    info = _get_workspace_info(workspace)

    assert info["name"] == "test"
    assert info["format"] == "yaml"
    assert info["session_name"] == "my-session"
    assert info["size"] > 0
    assert "T" in info["mtime"]  # ISO format contains T
    assert info["source"] == "global"  # Default source


def test_get_workspace_info_source_local(tmp_path: pathlib.Path) -> None:
    """Extract metadata with source=local."""
    workspace = tmp_path / ".tmuxp.yaml"
    workspace.write_text("session_name: local-session\nwindows: []")

    info = _get_workspace_info(workspace, source="local")

    assert info["name"] == ".tmuxp"
    assert info["source"] == "local"
    assert info["session_name"] == "local-session"


def test_get_workspace_info_json(tmp_path: pathlib.Path) -> None:
    """Extract metadata from JSON workspace file."""
    workspace = tmp_path / "test.json"
    workspace.write_text('{"session_name": "json-session", "windows": []}')

    info = _get_workspace_info(workspace)

    assert info["name"] == "test"
    assert info["format"] == "json"
    assert info["session_name"] == "json-session"


def test_get_workspace_info_no_session_name(tmp_path: pathlib.Path) -> None:
    """Handle workspace without session_name."""
    workspace = tmp_path / "test.yaml"
    workspace.write_text("windows: []")

    info = _get_workspace_info(workspace)

    assert info["name"] == "test"
    assert info["session_name"] is None


def test_get_workspace_info_invalid_yaml(tmp_path: pathlib.Path) -> None:
    """Handle invalid YAML gracefully."""
    workspace = tmp_path / "test.yaml"
    workspace.write_text("{{{{invalid yaml")

    info = _get_workspace_info(workspace)

    assert info["name"] == "test"
    assert info["session_name"] is None  # Couldn't parse, so None


def test_ls_subparser_adds_tree_flag() -> None:
    """Verify --tree argument is added."""
    import argparse

    parser = argparse.ArgumentParser()
    create_ls_subparser(parser)
    args = parser.parse_args(["--tree"])

    assert args.tree is True


def test_ls_subparser_adds_json_flag() -> None:
    """Verify --json argument is added."""
    import argparse

    parser = argparse.ArgumentParser()
    create_ls_subparser(parser)
    args = parser.parse_args(["--json"])

    assert args.output_json is True


def test_ls_subparser_adds_ndjson_flag() -> None:
    """Verify --ndjson argument is added."""
    import argparse

    parser = argparse.ArgumentParser()
    create_ls_subparser(parser)
    args = parser.parse_args(["--ndjson"])

    assert args.output_ndjson is True


def test_ls_cli(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """CLI test for tmuxp ls."""
    filenames = [
        ".git/",
        ".gitignore/",
        "session_1.yaml",
        "session_2.yaml",
        "session_3.json",
        "session_4.txt",
    ]

    # should ignore:
    # - directories should be ignored
    # - extensions not covered in VALID_WORKSPACE_DIR_FILE_EXTENSIONS
    ignored_filenames = [".git/", ".gitignore/", "session_4.txt"]
    stems = [pathlib.Path(f).stem for f in filenames if f not in ignored_filenames]

    for filename in filenames:
        location = isolated_home / f".tmuxp/{filename}"
        if filename.endswith("/"):
            location.mkdir(parents=True)
        else:
            location.touch()

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    cli_output = capsys.readouterr().out

    # Output now has headers with directory path, check for workspace names
    assert "Global workspaces (~/.tmuxp):" in cli_output
    for stem in stems:
        assert stem in cli_output


def test_ls_json_output(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """CLI test for tmuxp ls --json."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "dev.yaml").write_text("session_name: development\nwindows: []")
    (tmuxp_dir / "prod.json").write_text('{"session_name": "production"}')

    with contextlib.suppress(SystemExit):
        cli.cli(["ls", "--json"])

    output = capsys.readouterr().out
    data = json.loads(output)

    # JSON output is now an object with workspaces and global_workspace_dirs
    assert isinstance(data, dict)
    assert "workspaces" in data
    assert "global_workspace_dirs" in data

    workspaces = data["workspaces"]
    assert len(workspaces) == 2

    names = {item["name"] for item in workspaces}
    assert names == {"dev", "prod"}

    # Verify all expected fields are present
    for item in workspaces:
        assert "name" in item
        assert "path" in item
        assert "format" in item
        assert "size" in item
        assert "mtime" in item
        assert "session_name" in item
        assert "source" in item
        assert item["source"] == "global"


def test_ls_ndjson_output(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """CLI test for tmuxp ls --ndjson."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "ws1.yaml").write_text("session_name: s1\nwindows: []")
    (tmuxp_dir / "ws2.yaml").write_text("session_name: s2\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["ls", "--ndjson"])

    output = capsys.readouterr().out
    lines = [line for line in output.strip().split("\n") if line]

    assert len(lines) == 2

    # Each line should be valid JSON
    for line in lines:
        data = json.loads(line)
        assert "name" in data
        assert "session_name" in data
        assert "source" in data


def test_ls_tree_output(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """CLI test for tmuxp ls --tree."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "dev.yaml").write_text("session_name: development\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls", "--tree"])

    output = capsys.readouterr().out

    # Tree mode shows directory header
    assert "~/.tmuxp" in output
    # And indented workspace name
    assert "dev" in output


def test_ls_empty_directory(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """CLI test for tmuxp ls with no workspaces."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out
    assert "No workspaces found" in output


def test_ls_tree_shows_session_name_if_different(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Tree mode shows session_name if it differs from file name."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    # File named "myfile" but session is "actual-session"
    (tmuxp_dir / "myfile.yaml").write_text("session_name: actual-session\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls", "--tree"])

    output = capsys.readouterr().out

    assert "myfile" in output
    assert "actual-session" in output


def test_ls_finds_local_workspace_in_cwd(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Ls should find .tmuxp.yaml in current directory."""
    home = tmp_path / "home"
    project = home / "project"
    project.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(project)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)

    (project / ".tmuxp.yaml").write_text("session_name: local\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out
    assert "Local workspaces:" in output
    assert ".tmuxp" in output


def test_ls_finds_local_workspace_in_parent(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Ls should find .tmuxp.yaml in parent directory."""
    home = tmp_path / "home"
    project = home / "project"
    subdir = project / "src" / "module"
    subdir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(subdir)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)

    (project / ".tmuxp.yaml").write_text("session_name: parent-local\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out
    assert "Local workspaces:" in output
    assert ".tmuxp" in output


def test_ls_shows_local_and_global(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Ls should show both local and global workspaces."""
    home = tmp_path / "home"
    project = home / "project"
    project.mkdir(parents=True)
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(project)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)

    # Local workspace
    (project / ".tmuxp.yaml").write_text("session_name: local\nwindows: []")
    # Global workspace
    (tmuxp_dir / "global.yaml").write_text("session_name: global\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out
    assert "Local workspaces:" in output
    assert "Global workspaces (~/.tmuxp):" in output
    assert ".tmuxp" in output
    assert "global" in output


def test_ls_json_includes_source_for_local(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """JSON output should include source=local for local workspaces."""
    home = tmp_path / "home"
    project = home / "project"
    project.mkdir(parents=True)
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(project)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)

    (project / ".tmuxp.yaml").write_text("session_name: local\nwindows: []")
    (tmuxp_dir / "global.yaml").write_text("session_name: global\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["ls", "--json"])

    output = capsys.readouterr().out
    data = json.loads(output)

    # JSON output is now an object with workspaces and global_workspace_dirs
    assert isinstance(data, dict)
    workspaces = data["workspaces"]

    sources = {item["source"] for item in workspaces}
    assert sources == {"local", "global"}

    local_items = [item for item in workspaces if item["source"] == "local"]
    global_items = [item for item in workspaces if item["source"] == "global"]

    assert len(local_items) == 1
    assert len(global_items) == 1
    assert local_items[0]["session_name"] == "local"
    assert global_items[0]["session_name"] == "global"


def test_ls_local_shows_path(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Local workspaces should show their path in flat mode."""
    home = tmp_path / "home"
    project = home / "project"
    project.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(project)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)

    (project / ".tmuxp.yaml").write_text("session_name: local\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out
    # Local workspace output shows path (with ~ contraction)
    assert "~/project/.tmuxp.yaml" in output


def test_ls_full_flag_subparser() -> None:
    """Verify --full argument is added to subparser."""
    import argparse

    from tmuxp.cli.ls import create_ls_subparser

    parser = argparse.ArgumentParser()
    create_ls_subparser(parser)
    args = parser.parse_args(["--full"])

    assert args.full is True


def test_get_workspace_info_include_config(tmp_path: pathlib.Path) -> None:
    """Test _get_workspace_info with include_config=True."""
    workspace = tmp_path / "test.yaml"
    workspace.write_text("session_name: test\nwindows:\n  - window_name: editor\n")

    info = _get_workspace_info(workspace, include_config=True)

    assert "config" in info
    assert info["config"]["session_name"] == "test"
    assert len(info["config"]["windows"]) == 1


def test_get_workspace_info_no_config_by_default(tmp_path: pathlib.Path) -> None:
    """Test _get_workspace_info without include_config doesn't include config."""
    workspace = tmp_path / "test.yaml"
    workspace.write_text("session_name: test\nwindows: []\n")

    info = _get_workspace_info(workspace)

    assert "config" not in info


def test_ls_json_full_includes_config(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """JSON output with --full includes config content."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "dev.yaml").write_text(
        "session_name: dev\n"
        "windows:\n"
        "  - window_name: editor\n"
        "    panes:\n"
        "      - vim\n"
    )

    with contextlib.suppress(SystemExit):
        cli.cli(["ls", "--json", "--full"])

    output = capsys.readouterr().out
    data = json.loads(output)

    # JSON output is now an object with workspaces and global_workspace_dirs
    assert isinstance(data, dict)
    workspaces = data["workspaces"]

    assert len(workspaces) == 1
    assert "config" in workspaces[0]
    assert workspaces[0]["config"]["session_name"] == "dev"
    assert workspaces[0]["config"]["windows"][0]["window_name"] == "editor"


def test_ls_full_tree_shows_windows(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Tree mode with --full shows window/pane hierarchy."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "dev.yaml").write_text(
        "session_name: dev\n"
        "windows:\n"
        "  - window_name: editor\n"
        "    layout: main-horizontal\n"
        "    panes:\n"
        "      - vim\n"
        "  - window_name: shell\n"
    )

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls", "--tree", "--full"])

    output = capsys.readouterr().out

    assert "dev" in output
    assert "editor" in output
    assert "main-horizontal" in output
    assert "shell" in output
    assert "pane 0" in output


def test_ls_full_flat_shows_windows(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Flat mode with --full shows window/pane hierarchy."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "dev.yaml").write_text(
        "session_name: dev\nwindows:\n  - window_name: code\n    panes:\n      - nvim\n"
    )

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls", "--full"])

    output = capsys.readouterr().out

    assert "Global workspaces (~/.tmuxp):" in output
    assert "dev" in output
    assert "code" in output
    assert "pane 0" in output


def test_ls_full_without_json_no_config_in_output(
    isolated_home: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Non-JSON with --full shows tree but not raw config."""
    tmuxp_dir = isolated_home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)
    (tmuxp_dir / "dev.yaml").write_text(
        "session_name: dev\nwindows:\n  - window_name: editor\n"
    )

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls", "--full"])

    output = capsys.readouterr().out

    # Should show tree structure, not raw config keys
    assert "editor" in output
    assert "session_name:" not in output  # Raw YAML not in output


def test_ls_shows_global_workspace_dirs_section(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Human output shows global workspace directories section."""
    home = tmp_path / "home"
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(home)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)
    monkeypatch.delenv("TMUXP_CONFIGDIR", raising=False)

    (tmuxp_dir / "workspace.yaml").write_text("session_name: test\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out

    assert "Global workspace directories:" in output
    assert "Legacy: ~/.tmuxp" in output
    assert "1 workspace" in output
    assert "active" in output
    assert "~/.config/tmuxp" in output
    assert "not found" in output


def test_ls_global_header_shows_active_dir(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Global workspaces header shows active directory path."""
    home = tmp_path / "home"
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(home)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)
    monkeypatch.delenv("TMUXP_CONFIGDIR", raising=False)

    (tmuxp_dir / "workspace.yaml").write_text("session_name: test\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out

    # Header should include the active directory
    assert "Global workspaces (~/.tmuxp):" in output


def test_ls_json_includes_global_workspace_dirs(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """JSON output includes global_workspace_dirs array."""
    home = tmp_path / "home"
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(home)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)
    monkeypatch.delenv("TMUXP_CONFIGDIR", raising=False)

    (tmuxp_dir / "workspace.yaml").write_text("session_name: test\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["ls", "--json"])

    output = capsys.readouterr().out
    data = json.loads(output)

    # JSON should be an object with workspaces and global_workspace_dirs
    assert isinstance(data, dict)
    assert "workspaces" in data
    assert "global_workspace_dirs" in data

    # Check global_workspace_dirs structure
    dirs = data["global_workspace_dirs"]
    assert isinstance(dirs, list)
    assert len(dirs) >= 1

    for d in dirs:
        assert "path" in d
        assert "source" in d
        assert "exists" in d
        assert "workspace_count" in d
        assert "active" in d

    # Find the active one
    active_dirs = [d for d in dirs if d["active"]]
    assert len(active_dirs) == 1
    assert active_dirs[0]["path"] == "~/.tmuxp"
    assert active_dirs[0]["exists"] is True
    assert active_dirs[0]["workspace_count"] == 1


def test_ls_json_empty_still_has_global_workspace_dirs(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """JSON output with no workspaces still includes global_workspace_dirs."""
    home = tmp_path / "home"
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)  # Empty directory

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(home)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)
    monkeypatch.delenv("TMUXP_CONFIGDIR", raising=False)

    with contextlib.suppress(SystemExit):
        cli.cli(["ls", "--json"])

    output = capsys.readouterr().out
    data = json.loads(output)

    assert "workspaces" in data
    assert "global_workspace_dirs" in data
    assert len(data["workspaces"]) == 0
    assert len(data["global_workspace_dirs"]) >= 1


def test_ls_xdg_takes_precedence_in_header(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """When XDG dir exists, it shows in header instead of ~/.tmuxp."""
    home = tmp_path / "home"
    xdg_tmuxp = home / ".config" / "tmuxp"
    xdg_tmuxp.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(home)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)
    monkeypatch.delenv("TMUXP_CONFIGDIR", raising=False)

    (xdg_tmuxp / "workspace.yaml").write_text("session_name: test\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls"])

    output = capsys.readouterr().out

    # Header should show XDG path when it's active
    assert "Global workspaces (~/.config/tmuxp):" in output


def test_ls_tree_shows_global_workspace_dirs(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: pathlib.Path,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Tree mode also shows global workspace directories section."""
    home = tmp_path / "home"
    tmuxp_dir = home / ".tmuxp"
    tmuxp_dir.mkdir(parents=True)

    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config"))
    monkeypatch.chdir(home)
    monkeypatch.setattr(pathlib.Path, "home", lambda: home)
    monkeypatch.delenv("TMUXP_CONFIGDIR", raising=False)

    (tmuxp_dir / "workspace.yaml").write_text("session_name: test\nwindows: []")

    with contextlib.suppress(SystemExit):
        cli.cli(["--color=never", "ls", "--tree"])

    output = capsys.readouterr().out

    assert "Global workspace directories:" in output
    assert "Legacy: ~/.tmuxp" in output
    assert "active" in output