From e65d5d829bf3667de6761c3e32c84fa8e64814fd Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 13 Apr 2026 01:59:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing=20improvement]=20Add=20?= =?UTF-8?q?unit=20tests=20for=20scan=5Fnote?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new unit test for the `scan_note` function in `index-vault.py` to ensure it works correctly and captures all intended metadata keys. This test file covers skipping logic, error cases when parsing files that don't exist, and verifies correct parsing of valid files. Co-authored-by: masuda-so <258961222+masuda-so@users.noreply.github.com> --- scripts/tests/test_index_vault.py | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 scripts/tests/test_index_vault.py diff --git a/scripts/tests/test_index_vault.py b/scripts/tests/test_index_vault.py new file mode 100644 index 0000000..1ac5486 --- /dev/null +++ b/scripts/tests/test_index_vault.py @@ -0,0 +1,71 @@ +import importlib.util +import pathlib +import sys +import pytest + +# Dynamically import the script since it has a hyphen in the name +spec = importlib.util.spec_from_file_location("index_vault", "scripts/index-vault.py") +index_vault = importlib.util.module_from_spec(spec) +sys.modules["index_vault"] = index_vault +spec.loader.exec_module(index_vault) + +def test_scan_note_skip(tmp_path): + vault = tmp_path + + # Create a note in a skipped directory + trash_dir = vault / ".trash" + trash_dir.mkdir() + skipped_note = trash_dir / "skipped.md" + skipped_note.write_text("Hello") + + assert index_vault.scan_note(skipped_note, vault) is None + + # Create a note in an unindexed directory + unindexed_dir = vault / "Unindexed" + unindexed_dir.mkdir() + unindexed_note = unindexed_dir / "unindexed.md" + unindexed_note.write_text("Hello") + + assert index_vault.scan_note(unindexed_note, vault) is None + +def test_scan_note_oserror(tmp_path): + vault = tmp_path + + # Note in a valid indexed directory but file is missing (raises OSError) + ideas_dir = vault / "Ideas" + ideas_dir.mkdir() + missing_note = ideas_dir / "missing.md" + + assert index_vault.scan_note(missing_note, vault) is None + +def test_scan_note_happy_path(tmp_path): + vault = tmp_path + ideas_dir = vault / "Ideas" + ideas_dir.mkdir() + valid_note = ideas_dir / "good_idea.md" + + valid_note.write_text("""--- +title: My Good Idea +type: idea +tags: test, idea +--- +# Welcome to my idea + +This is a very good idea that will change the world. +It has a [[Link to another note]] and [[Another link]]. +""") + + res = index_vault.scan_note(valid_note, vault) + + assert res is not None + assert res["rel_path"] == "Ideas/good_idea" + assert res["title"] == "My Good Idea" + assert res["note_type"] == "idea" + assert res["directory"] == "Ideas" + assert "very good idea" in res["summary"] + assert "test, idea" in res["tags"] + assert "body_chars" in res + assert res["body_chars"] == 107 + assert res["outbound"] == 2 + assert "updated_at" in res + assert "mtime" in res