From 9ec12bf19ffd91f931692f14ac2db343383fbe09 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 15:47:29 -0400 Subject: [PATCH 1/5] Add mutation testing and contract test --- .github/workflows/python-ci.yml | 9 +++- src/requirements.txt | 1 + src/tests/test_nostr_contract.py | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/tests/test_nostr_contract.py diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml index 725a7f0..717b416 100644 --- a/.github/workflows/python-ci.yml +++ b/.github/workflows/python-ci.yml @@ -72,14 +72,19 @@ jobs: if [ "${{ github.event_name }}" = "schedule" ]; then echo "STRESS_ARGS=--stress" >> $GITHUB_ENV fi - - name: Enable Nostr network tests on main branch - if: github.ref == 'refs/heads/main' + - name: Enable Nostr network tests on main branch or nightly + if: github.ref == 'refs/heads/main' || github.event_name == 'schedule' run: echo "NOSTR_E2E=1" >> $GITHUB_ENV - name: Run tests with coverage shell: bash run: | pytest ${STRESS_ARGS} --cov=src --cov-report=xml --cov-report=term-missing \ --cov-fail-under=20 src/tests + - name: Run mutation tests + if: github.event_name == 'push' + run: | + mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" + mutmut results - name: Upload coverage report uses: actions/upload-artifact@v4 with: diff --git a/src/requirements.txt b/src/requirements.txt index ef0e389..97e0e95 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -18,3 +18,4 @@ websocket-client==1.7.0 websockets>=15.0.0 tomli hypothesis +mutmut>=2.4.4 diff --git a/src/tests/test_nostr_contract.py b/src/tests/test_nostr_contract.py new file mode 100644 index 0000000..2d3d106 --- /dev/null +++ b/src/tests/test_nostr_contract.py @@ -0,0 +1,76 @@ +import sys +from pathlib import Path +from unittest.mock import patch +from cryptography.fernet import Fernet + +sys.path.append(str(Path(__file__).resolve().parents[1])) + +from password_manager.encryption import EncryptionManager +from nostr.client import NostrClient + + +class MockNostrServer: + def __init__(self): + self.events = [] + + +class MockClient: + def __init__(self, server): + self.server = server + + async def add_relays(self, relays): + pass + + async def add_relay(self, relay): + pass + + async def connect(self): + pass + + async def disconnect(self): + pass + + async def send_event(self, event): + self.server.events.append(event) + + class FakeId: + def to_hex(self_inner): + return "abcd" + + class FakeOutput: + def __init__(self): + self.id = FakeId() + + return FakeOutput() + + async def fetch_events(self, filter_obj, timeout): + class FakeEvents: + def __init__(self, events): + self._events = events + + def to_vec(self): + return self._events + + return FakeEvents(self.server.events[-1:]) + + +def setup_client(tmp_path, server): + key = Fernet.generate_key() + enc_mgr = EncryptionManager(key, tmp_path) + + with patch("nostr.client.Client", lambda signer: MockClient(server)), patch( + "nostr.client.KeyManager" + ) as MockKM, patch.object(enc_mgr, "decrypt_parent_seed", return_value="seed"): + km_inst = MockKM.return_value + km_inst.keys.private_key_hex.return_value = "1" * 64 + km_inst.keys.public_key_hex.return_value = "2" * 64 + client = NostrClient(enc_mgr, "fp", relays=["ws://mock"]) + return client + + +def test_publish_and_retrieve(tmp_path): + server = MockNostrServer() + client = setup_client(tmp_path, server) + payload = b"contract-test" + assert client.publish_json_to_nostr(payload) is True + assert client.retrieve_json_from_nostr_sync() == payload From bfd83c818df5888bee835e3569eeafface44451f Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 15:55:04 -0400 Subject: [PATCH 2/5] Fix mutmut usage in CI --- .github/workflows/python-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml index 717b416..1231aa3 100644 --- a/.github/workflows/python-ci.yml +++ b/.github/workflows/python-ci.yml @@ -83,8 +83,8 @@ jobs: - name: Run mutation tests if: github.event_name == 'push' run: | - mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" - mutmut results + python -m mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" + python -m mutmut results - name: Upload coverage report uses: actions/upload-artifact@v4 with: From 1ecefbcdc2807975feb269ab22f0095f0089396c Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:01:10 -0400 Subject: [PATCH 3/5] Pin mutmut version --- src/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/requirements.txt b/src/requirements.txt index 97e0e95..4514dc2 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -18,4 +18,4 @@ websocket-client==1.7.0 websockets>=15.0.0 tomli hypothesis -mutmut>=2.4.4 +mutmut==2.4.4 From d3264d231c1865d2529cc40b23e520efd913eee6 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:09:08 -0400 Subject: [PATCH 4/5] speed up mutmut --- .github/workflows/python-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml index 1231aa3..118d32c 100644 --- a/.github/workflows/python-ci.yml +++ b/.github/workflows/python-ci.yml @@ -81,9 +81,9 @@ jobs: pytest ${STRESS_ARGS} --cov=src --cov-report=xml --cov-report=term-missing \ --cov-fail-under=20 src/tests - name: Run mutation tests - if: github.event_name == 'push' + if: github.event_name == 'push' && runner.os == 'Linux' run: | - python -m mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" + python -m mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" --use-coverage --no-progress python -m mutmut results - name: Upload coverage report uses: actions/upload-artifact@v4 From f7e05a3bc87f50b40abff77ff612f8b0e4d9f773 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:32:05 -0400 Subject: [PATCH 5/5] Remove mutation tests from CI --- .github/workflows/python-ci.yml | 5 ----- README.md | 10 ++++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml index 118d32c..59492bd 100644 --- a/.github/workflows/python-ci.yml +++ b/.github/workflows/python-ci.yml @@ -80,11 +80,6 @@ jobs: run: | pytest ${STRESS_ARGS} --cov=src --cov-report=xml --cov-report=term-missing \ --cov-fail-under=20 src/tests - - name: Run mutation tests - if: github.event_name == 'push' && runner.os == 'Linux' - run: | - python -m mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" --use-coverage --no-progress - python -m mutmut results - name: Upload coverage report uses: actions/upload-artifact@v4 with: diff --git a/README.md b/README.md index 0a2e1dd..341eaa0 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,16 @@ pip install -r src/requirements.txt pytest -vv ``` +To run mutation tests locally, generate coverage data first and then execute `mutmut`: + +```bash +pytest --cov=src src/tests +python -m mutmut run --paths-to-mutate src --tests-dir src/tests --runner "python -m pytest -q" --use-coverage --no-progress +python -m mutmut results +``` + +Mutation testing is disabled in the GitHub workflow due to reliability issues and should be run on a desktop environment instead. + ## Security Considerations **Important:** The password you use to encrypt your parent seed is also required to decrypt the seed index data retrieved from Nostr. **It is imperative to remember this password** and be sure to use it with the same seed, as losing it means you won't be able to access your stored index. Secure your 12-word seed **and** your master password.