|
6 | 6 |
|
7 | 7 | from dirplot.scanner import ( |
8 | 8 | Node, |
| 9 | + apply_breadcrumbs, |
9 | 10 | build_tree, |
10 | 11 | build_tree_multi, |
11 | 12 | collect_extensions, |
@@ -229,3 +230,80 @@ def test_build_tree_multi_single_delegates(tmp_path: Path) -> None: |
229 | 230 | root = build_tree_multi([tmp_path]) |
230 | 231 | assert root.path == tmp_path |
231 | 232 | assert len(root.children) == 1 |
| 233 | + |
| 234 | + |
| 235 | +# --------------------------------------------------------------------------- |
| 236 | +# apply_breadcrumbs |
| 237 | +# --------------------------------------------------------------------------- |
| 238 | + |
| 239 | + |
| 240 | +def _make_dir(name: str, children: list[Node] | None = None) -> Node: |
| 241 | + return Node(name=name, path=Path(name), size=1, is_dir=True, children=children or []) |
| 242 | + |
| 243 | + |
| 244 | +def _make_file(name: str) -> Node: |
| 245 | + return Node(name=name, path=Path(name), size=1, is_dir=False, extension=".txt") |
| 246 | + |
| 247 | + |
| 248 | +def test_breadcrumbs_collapses_chain() -> None: |
| 249 | + # root → a → b → c → [file.txt]; root is never collapsed, but a/b/c merge |
| 250 | + file_node = _make_file("file.txt") |
| 251 | + c = _make_dir("c", [file_node]) |
| 252 | + b = _make_dir("b", [c]) |
| 253 | + a = _make_dir("a", [b]) |
| 254 | + root = _make_dir("root", [a]) |
| 255 | + |
| 256 | + result = apply_breadcrumbs(root) |
| 257 | + |
| 258 | + assert result.name == "root" # root itself is never collapsed |
| 259 | + assert len(result.children) == 1 |
| 260 | + merged = result.children[0] |
| 261 | + assert merged.name == "a / b / c" |
| 262 | + assert len(merged.children) == 1 |
| 263 | + assert merged.children[0].name == "file.txt" |
| 264 | + |
| 265 | + |
| 266 | +def test_breadcrumbs_no_collapse_with_files() -> None: |
| 267 | + # root → a → [file.txt, subdir] — a has file child, must not collapse |
| 268 | + file_node = _make_file("file.txt") |
| 269 | + subdir = _make_dir("subdir", [_make_file("inner.txt")]) |
| 270 | + a = _make_dir("a", [file_node, subdir]) |
| 271 | + root = _make_dir("root", [a]) |
| 272 | + |
| 273 | + result = apply_breadcrumbs(root) |
| 274 | + |
| 275 | + assert result.name == "root" |
| 276 | + child = result.children[0] |
| 277 | + assert child.name == "a" |
| 278 | + assert {c.name for c in child.children} == {"file.txt", "subdir"} |
| 279 | + |
| 280 | + |
| 281 | +def test_breadcrumbs_no_collapse_multi_children() -> None: |
| 282 | + # root → a → [dir1, dir2] — a has two dir children, must not collapse |
| 283 | + dir1 = _make_dir("dir1", [_make_file("x.txt")]) |
| 284 | + dir2 = _make_dir("dir2", [_make_file("y.txt")]) |
| 285 | + a = _make_dir("a", [dir1, dir2]) |
| 286 | + root = _make_dir("root", [a]) |
| 287 | + |
| 288 | + result = apply_breadcrumbs(root) |
| 289 | + |
| 290 | + assert result.name == "root" |
| 291 | + child = result.children[0] |
| 292 | + assert child.name == "a" |
| 293 | + assert {c.name for c in child.children} == {"dir1", "dir2"} |
| 294 | + |
| 295 | + |
| 296 | +def test_breadcrumbs_partial_chain() -> None: |
| 297 | + # root → a → b → [dir1, dir2] — b has two children, so a/b merges but stops there |
| 298 | + dir1 = _make_dir("dir1", [_make_file("x.txt")]) |
| 299 | + dir2 = _make_dir("dir2", [_make_file("y.txt")]) |
| 300 | + b = _make_dir("b", [dir1, dir2]) |
| 301 | + a = _make_dir("a", [b]) |
| 302 | + root = _make_dir("root", [a]) |
| 303 | + |
| 304 | + result = apply_breadcrumbs(root) |
| 305 | + |
| 306 | + assert result.name == "root" |
| 307 | + child = result.children[0] |
| 308 | + assert child.name == "a / b" |
| 309 | + assert {c.name for c in child.children} == {"dir1", "dir2"} |
0 commit comments