From 6a921b8f846b00eaafafe46761b77807cd24e9ed Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Feb 2026 19:15:19 +0000 Subject: [PATCH] test: add comprehensive unit tests for generate_fingerprint Add `src/tests/test_fingerprint.py` to cover: - Happy path fingerprint generation. - Custom fingerprint length. - Input normalization (whitespace/case). - Error handling via mocked exception in `hashlib.sha256`. - Invalid input handling (NoneType). This addresses the gap where the exception path in `src/utils/fingerprint.py` was unreachable with standard inputs. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/tests/test_fingerprint.py | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/tests/test_fingerprint.py diff --git a/src/tests/test_fingerprint.py b/src/tests/test_fingerprint.py new file mode 100644 index 00000000..072b42f6 --- /dev/null +++ b/src/tests/test_fingerprint.py @@ -0,0 +1,64 @@ +import sys +import logging +from unittest.mock import patch +from pathlib import Path +import pytest + +sys.path.append(str(Path(__file__).resolve().parents[1])) + +from utils.fingerprint import generate_fingerprint + +# Sample seed for testing +TEST_SEED = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" +EXPECTED_FINGERPRINT = "C557EEC878DFD852" + + +def test_generate_fingerprint_valid(): + """Test standard successful fingerprint generation.""" + fp = generate_fingerprint(TEST_SEED) + assert fp is not None + assert len(fp) == 16 + assert fp.isupper() + assert fp == EXPECTED_FINGERPRINT + + +def test_generate_fingerprint_custom_length(): + """Test fingerprint generation with custom length.""" + length = 8 + fp = generate_fingerprint(TEST_SEED, length=length) + assert fp is not None + assert len(fp) == length + assert fp == EXPECTED_FINGERPRINT[:length] + + +def test_generate_fingerprint_normalization(): + """Test that input is normalized (stripped and lowercased).""" + messy_seed = " " + TEST_SEED.upper() + " " + fp = generate_fingerprint(messy_seed) + assert fp == EXPECTED_FINGERPRINT + + +def test_generate_fingerprint_error_handling(caplog): + """Test error handling when an exception occurs during generation.""" + # Patch hashlib.sha256 to raise an exception + with patch("hashlib.sha256", side_effect=Exception("Mocked hashing error")): + # Ensure we capture logs at ERROR level + with caplog.at_level(logging.ERROR): + fp = generate_fingerprint(TEST_SEED) + + # Function should return None on error + assert fp is None + + # Check that the error was logged + assert "Failed to generate fingerprint: Mocked hashing error" in caplog.text + + +def test_generate_fingerprint_invalid_input(caplog): + """Test handling of invalid input type which raises AttributeError on .strip().""" + with caplog.at_level(logging.ERROR): + # Passing None will cause AttributeError: 'NoneType' object has no attribute 'strip' + fp = generate_fingerprint(None) + + assert fp is None + assert "Failed to generate fingerprint" in caplog.text + assert "'NoneType' object has no attribute 'strip'" in caplog.text