krz/crumb

A local alternative to your browser's history.

clone: git clone https://gitbay.org/krz/crumb.git

5032dd4aff82f1deceae11269f8973e89945b893

verified · cmc

author: Christian Cleberg <hello@cleberg.net> · 2025-08-03T00:49:41Z

feat: add tests
 .github/workflows/tests.yml | 26 ++++++++++++++++++++++++++
 tests/test_search.py        | 34 ++++++++++++++++++++++++++++++++++
 tests/test_server.py        | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)

diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
new file mode 100644
index 0000000..b933244
--- /dev/null
+++ b/.github/workflows/tests.yml
@@ -0,0 +1,26 @@
+name: Run Tests
+
+on:
+  push:
+    branches:
+      - main
+  pull_request:
+    branches:
+      - main
+
+jobs:
+  test:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v3
+      - name: Set up Python
+        uses: actions/setup-python@v4
+        with:
+          python-version: "3.11"
+      - name: Install Dependencies
+        run: |
+          python -m pip install --upgrade pip
+          python -m pip install --upgrade pytest
+          python -m pip install -r requirements.txt
+      - name: Run Tests
+        run: pytest
diff --git a/tests/test_search.py b/tests/test_search.py
new file mode 100644
index 0000000..f2c003c
--- /dev/null
+++ b/tests/test_search.py
@@ -0,0 +1,34 @@
+import unittest
+from unittest.mock import patch
+from search import search_log
+
+class TestSearchLog(unittest.TestCase):
+
+    @patch('search.LOG_PATH', 'test_log_path')
+    def test_search_log_with_match(self, mock_log_path):
+        query = 'crumb'
+        # Create a dummy log file (for testing)
+        with open('test_log_path', 'w') as f:
+            f.write("* Example Entry\n")
+            f.write(":PROPERTIES:\n")
+            f.write(":URL:       http://example.com\n")
+            f.write(":TIMESTAMP: 2023-10-27 10:00:00\n")
+
+        search_log(query)
+        # Assert that the function prints the expected output
+        with open('test_log_path', 'r') as f:
+            output = f.read()
+        self.assertIn("* Example Entry\n", output)
+
+    @patch('search.LOG_PATH', 'test_log_path')
+    def test_search_log_no_match(self, mock_log_path):
+        query = 'nonexistent'
+        with open('test_log_path', 'w') as f:
+            f.write("* Example Entry\n")
+            f.write(":PROPERTIES:\n")
+            f.write(":URL:       http://example.com\n")
+            f.write(":TIMESTAMP: 2023-10-27 10:00:00\n")
+        search_log(query)
+        with open('test_log_path', 'r') as f:
+            output = f.read()
+        self.assertNotIn("* Example Entry\n", output)
diff --git a/tests/test_server.py b/tests/test_server.py
new file mode 100644
index 0000000..43584d8
--- /dev/null
+++ b/tests/test_server.py
@@ -0,0 +1,35 @@
+import unittest
+from flask import Flask
+from server import app
+
+class TestServer(unittest.TestCase):
+
+    def test_log_visit_post(self):
+        # Create a test client
+        client = app.test_client()
+
+        # Sample JSON data
+        data = {
+            'title': 'Test Visit',
+            'url': 'https://test.com',
+            'hostname': 'test.com',
+            'path': '/',
+            'query': 'test',
+            'tabId': 123,
+            'windowId': 456,
+            'favIconUrl': 'https://example.com/favicon.ico'
+        }
+
+        # Send a POST request
+        response = client.post('/', data=data, content_type='application/json')
+
+        # Assertions
+        self.assertEqual(response.status_code, 204)
+        # Optionally check the log file contents (for verification)
+        # This part would require additional logging setup.
+        # self.assertTrue('Test Visit' in self.get_log_content())
+
+    def test_log_visit_options(self):
+        # Send an OPTIONS request
+        response = client.options( '/', content_type='application/json')
+        self.assertEqual(response.status_code, 204) #Verify 204 No Content for OPTIONS