|
| 1 | +from ingestion import indexed_file_state |
| 2 | + |
| 3 | + |
| 4 | +# --- compute_file_sha1: stable whole-file digest -------------------------- |
| 5 | + |
| 6 | +def test_compute_file_sha1_is_the_hex_sha1_of_the_utf8_bytes(): |
| 7 | + # Pinned digest: it is stored in Qdrant and compared against the next |
| 8 | + # run, so changing the algorithm or the encoding would mark every |
| 9 | + # indexed file as changed and re-embed the whole corpus. The accented |
| 10 | + # character makes the utf-8 choice observable. |
| 11 | + assert (indexed_file_state.compute_file_sha1("héllo") |
| 12 | + == "35b5ea45c5e41f78b46a937cc74d41dfea920890") |
| 13 | + |
| 14 | + |
| 15 | +def test_compute_file_sha1_is_deterministic(): |
| 16 | + assert (indexed_file_state.compute_file_sha1("const a = 1") |
| 17 | + == indexed_file_state.compute_file_sha1("const a = 1")) |
| 18 | + |
| 19 | + |
| 20 | +def test_compute_file_sha1_changes_with_content(): |
| 21 | + assert (indexed_file_state.compute_file_sha1("const a = 1") |
| 22 | + != indexed_file_state.compute_file_sha1("const a = 2")) |
| 23 | + |
| 24 | + |
| 25 | +# --- load_indexed_file_hashes: rebuild the state from stored payloads ----- |
| 26 | + |
| 27 | +def test_load_indexed_file_hashes_keys_on_repository_and_source_path( |
| 28 | + monkeypatch): |
| 29 | + monkeypatch.setattr(indexed_file_state.vectordb, "iter_payloads", lambda: [ |
| 30 | + {"repository": "kdk", "source_path": "map/x.js", "file_sha1": "aaa"}, |
| 31 | + {"repository": "kano", "source_path": "map/x.js", "file_sha1": "bbb"}, |
| 32 | + ]) |
| 33 | + assert indexed_file_state.load_indexed_file_hashes() == { |
| 34 | + ("kdk", "map/x.js"): "aaa", |
| 35 | + ("kano", "map/x.js"): "bbb", |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +def test_load_indexed_file_hashes_shares_one_entry_per_file(monkeypatch): |
| 40 | + # Every chunk of a file carries the same file_sha1, so many payloads |
| 41 | + # collapse into a single entry. |
| 42 | + monkeypatch.setattr(indexed_file_state.vectordb, "iter_payloads", lambda: [ |
| 43 | + {"repository": "kdk", "source_path": "map/x.js", "file_sha1": "aaa"}, |
| 44 | + {"repository": "kdk", "source_path": "map/x.js", "file_sha1": "aaa"}, |
| 45 | + ]) |
| 46 | + assert indexed_file_state.load_indexed_file_hashes() == { |
| 47 | + ("kdk", "map/x.js"): "aaa"} |
| 48 | + |
| 49 | + |
| 50 | +def test_load_indexed_file_hashes_skips_payloads_without_a_digest(monkeypatch): |
| 51 | + monkeypatch.setattr(indexed_file_state.vectordb, "iter_payloads", lambda: [ |
| 52 | + {"repository": "kdk", "source_path": "map/x.js", "file_sha1": ""}, |
| 53 | + {"repository": "kdk", "source_path": "map/y.js"}, |
| 54 | + ]) |
| 55 | + assert indexed_file_state.load_indexed_file_hashes() == {} |
| 56 | + |
| 57 | + |
| 58 | +def test_load_indexed_file_hashes_is_empty_on_the_first_run(monkeypatch): |
| 59 | + monkeypatch.setattr( |
| 60 | + indexed_file_state.vectordb, "iter_payloads", lambda: []) |
| 61 | + assert indexed_file_state.load_indexed_file_hashes() == {} |
| 62 | + |
| 63 | + |
| 64 | +# --- select_changed_chunks: drop chunks whose file is unchanged ----------- |
| 65 | + |
| 66 | +def test_select_changed_chunks_keeps_a_file_that_is_not_indexed_yet(): |
| 67 | + chunk = {"metadata": { |
| 68 | + "repository": "kdk", "source_path": "map/x.js", "file_sha1": "aaa"}} |
| 69 | + assert indexed_file_state.select_changed_chunks([chunk], {}) == [chunk] |
| 70 | + |
| 71 | + |
| 72 | +def test_select_changed_chunks_drops_a_file_indexed_at_the_same_digest(): |
| 73 | + chunk = {"metadata": { |
| 74 | + "repository": "kdk", "source_path": "map/x.js", "file_sha1": "aaa"}} |
| 75 | + indexed = {("kdk", "map/x.js"): "aaa"} |
| 76 | + assert indexed_file_state.select_changed_chunks([chunk], indexed) == [] |
| 77 | + |
| 78 | + |
| 79 | +def test_select_changed_chunks_keeps_a_file_whose_content_changed(): |
| 80 | + chunk = {"metadata": { |
| 81 | + "repository": "kdk", "source_path": "map/x.js", "file_sha1": "bbb"}} |
| 82 | + indexed = {("kdk", "map/x.js"): "aaa"} |
| 83 | + assert indexed_file_state.select_changed_chunks([chunk], indexed) == [chunk] |
| 84 | + |
| 85 | + |
| 86 | +def test_select_changed_chunks_distinguishes_repositories(): |
| 87 | + # source_path is repo-relative, so the same path in another repo must not |
| 88 | + # be mistaken for an already indexed file. |
| 89 | + chunk = {"metadata": { |
| 90 | + "repository": "kano", "source_path": "map/x.js", "file_sha1": "aaa"}} |
| 91 | + indexed = {("kdk", "map/x.js"): "aaa"} |
| 92 | + assert indexed_file_state.select_changed_chunks([chunk], indexed) == [chunk] |
| 93 | + |
| 94 | + |
| 95 | +def test_select_changed_chunks_keeps_every_chunk_of_a_changed_file(): |
| 96 | + chunks = [ |
| 97 | + {"metadata": {"repository": "kdk", "source_path": "map/x.js", |
| 98 | + "file_sha1": "bbb", "chunk_index": 0}}, |
| 99 | + {"metadata": {"repository": "kdk", "source_path": "map/x.js", |
| 100 | + "file_sha1": "bbb", "chunk_index": 1}}, |
| 101 | + ] |
| 102 | + indexed = {("kdk", "map/x.js"): "aaa"} |
| 103 | + assert indexed_file_state.select_changed_chunks(chunks, indexed) == chunks |
0 commit comments