|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
| 6 | +import asyncio |
| 7 | +import importlib |
6 | 8 | from pathlib import Path |
7 | 9 |
|
8 | 10 | import pytest |
|
18 | 20 | from codewiki.src.be.prompt_template import format_cluster_prompt |
19 | 21 | from codewiki.src.be.dependency_analyzer.models.core import Node |
20 | 22 | from codewiki.src.config import Config |
| 23 | +from codewiki.src.be.documentation_generator import DocumentationGenerator |
21 | 24 | from codewiki.src.utils import file_manager |
22 | 25 |
|
23 | 26 |
|
@@ -450,3 +453,86 @@ def test_request_fingerprint_and_component_set_must_match(tmp_path): |
450 | 453 | assert cache.lookup("__root__", 1, ["b", "a"], request_fingerprint="request-one") == _tree() |
451 | 454 | assert cache.lookup("__root__", 1, ["a", "b"], request_fingerprint="request-two") is None |
452 | 455 | assert cache.lookup("__root__", 1, ["a", "c"], request_fingerprint="request-one") is None |
| 456 | + |
| 457 | + |
| 458 | +def test_scoped_cleanup_preserves_failed_and_unrelated_scopes(tmp_path): |
| 459 | + cache = ClusterBatchCache(str(tmp_path)) |
| 460 | + scopes = ["core", "core/child", "core_extra", "failed", "failed/child", "__root__"] |
| 461 | + for scope in scopes: |
| 462 | + cache.store(scope, 1, ["a"], _tree(), request_fingerprint="test-request") |
| 463 | + delete_clustering_batch_cache(str(tmp_path), scopes=["core"]) |
| 464 | + resumed = ClusterBatchCache(str(tmp_path)) |
| 465 | + assert resumed.entry_count() == 4 |
| 466 | + for scope in scopes: |
| 467 | + result = resumed.lookup(scope, 1, ["a"], request_fingerprint="test-request") |
| 468 | + assert (result is None) == (scope in {"core", "core/child"}) |
| 469 | + delete_clustering_batch_cache(str(tmp_path), scopes=scopes[2:]) |
| 470 | + assert not Path(clustering_batch_cache_path(str(tmp_path))).exists() |
| 471 | + |
| 472 | + |
| 473 | +def test_empty_scope_cleanup_keeps_checkpoints(tmp_path): |
| 474 | + cache = ClusterBatchCache(str(tmp_path)) |
| 475 | + cache.store("failed", 1, ["a"], _tree(), request_fingerprint="test-request") |
| 476 | + delete_clustering_batch_cache(str(tmp_path), scopes=[]) |
| 477 | + assert ClusterBatchCache(str(tmp_path)).entry_count() == 1 |
| 478 | + |
| 479 | + |
| 480 | +def test_failed_scoped_cleanup_preserves_disk_checkpoints(tmp_path, monkeypatch): |
| 481 | + cache = ClusterBatchCache(str(tmp_path)) |
| 482 | + for scope in ["core", "failed"]: |
| 483 | + cache.store(scope, 1, ["a"], _tree(), request_fingerprint="test-request") |
| 484 | + |
| 485 | + def denied(*args): |
| 486 | + raise PermissionError("replace denied") |
| 487 | + |
| 488 | + monkeypatch.setattr("codewiki.src.be.cluster_batch_cache.os.replace", denied) |
| 489 | + delete_clustering_batch_cache(str(tmp_path), scopes=["core"]) |
| 490 | + assert ClusterBatchCache(str(tmp_path)).entry_count() == 2 |
| 491 | + |
| 492 | + |
| 493 | +@pytest.mark.parametrize("failed_save", [None, "first_module_tree.json", "module_tree.json"]) |
| 494 | +def test_full_build_cleanup_follows_both_tree_writes(tmp_path, monkeypatch, failed_save): |
| 495 | + ids, components, config = _inputs(tmp_path) |
| 496 | + cache = ClusterBatchCache(config.docs_dir) |
| 497 | + cache.store("__root__", 1, ["a"], _tree(), request_fingerprint="test-request") |
| 498 | + cache_path = Path(clustering_batch_cache_path(config.docs_dir)) |
| 499 | + tree = { |
| 500 | + name: {"components": ids[i : i + 2], "children": {}} |
| 501 | + for name, i in [("Alpha", 0), ("Beta", 2), ("Gamma", 4)] |
| 502 | + } |
| 503 | + generator = DocumentationGenerator(config, backend=object()) |
| 504 | + monkeypatch.setattr( |
| 505 | + generator.graph_builder, "build_dependency_graph", lambda: (components, ids) |
| 506 | + ) |
| 507 | + module = importlib.import_module("codewiki.src.be.documentation_generator") |
| 508 | + monkeypatch.setattr(module, "cluster_modules", lambda *args, **kwargs: tree) |
| 509 | + |
| 510 | + async def generate_pages(*args): |
| 511 | + for name in ["overview", *tree]: |
| 512 | + (Path(config.docs_dir) / (name + ".md")).write_text( |
| 513 | + f"# {name}\n\nSmoke documentation.\n" |
| 514 | + ) |
| 515 | + return config.docs_dir |
| 516 | + |
| 517 | + monkeypatch.setattr(generator, "generate_module_documentation", generate_pages) |
| 518 | + original_save = file_manager.save_json |
| 519 | + writes = [] |
| 520 | + |
| 521 | + def save(data, path): |
| 522 | + filename = Path(path).name |
| 523 | + if filename in {"first_module_tree.json", "module_tree.json"}: |
| 524 | + assert cache_path.exists() |
| 525 | + writes.append(filename) |
| 526 | + if filename == failed_save: |
| 527 | + raise PermissionError("tree save failed") |
| 528 | + original_save(data, path) |
| 529 | + |
| 530 | + monkeypatch.setattr(file_manager, "save_json", save) |
| 531 | + if failed_save: |
| 532 | + with pytest.raises(PermissionError, match="tree save failed"): |
| 533 | + asyncio.run(generator.run()) |
| 534 | + assert cache_path.exists() |
| 535 | + else: |
| 536 | + asyncio.run(generator.run()) |
| 537 | + assert writes == ["first_module_tree.json", "module_tree.json"] |
| 538 | + assert not cache_path.exists() |
0 commit comments