test(cache): cover save_semantic_cache merge_existing union + default overwrite (#1715)

The per-chunk checkpoint feature added merge_existing without test coverage.
Add tests asserting merge_existing=True unions a file's slices across chunks and
the default still overwrites (the authoritative final write in extract).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
safishamsi
2026-07-08 01:14:12 +01:00
parent 2ea37734c0
commit 27b523e769
+30
View File
@@ -510,3 +510,33 @@ def test_semantic_prune_ignores_ast_and_tmp(tmp_path):
assert not (semantic_dir / "deadbeef.json").exists()
assert tmp_entry.exists(), "*.tmp temporaries must not be swept"
assert len(list(ast_dir.glob("*.json"))) == 1, "AST entries must not be touched"
def test_save_semantic_cache_overwrites_by_default(tmp_path):
"""Default save_semantic_cache replaces a file's cached entry (the final,
authoritative write in the extract pipeline)."""
from graphify.cache import save_semantic_cache
f = tmp_path / "doc.md"; f.write_text("# Doc\n")
save_semantic_cache([{"id": "a", "source_file": "doc.md"}], [], root=tmp_path)
save_semantic_cache([{"id": "b", "source_file": "doc.md"}], [], root=tmp_path)
cached = load_cached(f, root=tmp_path, kind="semantic")
ids = {n["id"] for n in cached["nodes"]}
assert ids == {"b"}, "default must overwrite, not accumulate"
def test_save_semantic_cache_merge_existing_unions(tmp_path):
"""#1715: merge_existing=True unions with the prior entry so a file split
across chunks (checkpointed per chunk) keeps every slice."""
from graphify.cache import save_semantic_cache
f = tmp_path / "big.md"; f.write_text("# Big\n")
# chunk 1 slice
save_semantic_cache([{"id": "a", "source_file": "big.md"}],
[{"source": "a", "target": "x", "source_file": "big.md"}],
root=tmp_path, merge_existing=True)
# chunk 2 slice for the same file
save_semantic_cache([{"id": "b", "source_file": "big.md"}], [],
root=tmp_path, merge_existing=True)
cached = load_cached(f, root=tmp_path, kind="semantic")
ids = {n["id"] for n in cached["nodes"]}
assert ids == {"a", "b"}, "merge_existing must union both chunk slices"
assert len(cached["edges"]) == 1