diff --git a/.github/workflows/test-only.yml b/.github/workflows/test-only.yml index 3064e97d..f7aec92e 100644 --- a/.github/workflows/test-only.yml +++ b/.github/workflows/test-only.yml @@ -51,6 +51,7 @@ jobs: run: | # Unit tests docker run test-changedetectionio bash -c 'python3 -m unittest changedetectionio.tests.unit.test_notification_diff' + docker run test-changedetectionio bash -c 'python3 -m unittest changedetectionio.tests.unit.test_watch_model_diff' # All tests docker run --network changedet-network test-changedetectionio bash -c 'cd changedetectionio && ./run_basic_tests.sh' diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index 8d58ca46..802a7919 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -271,15 +271,18 @@ class model(dict): """Unfortunately for now timestamp is stored as string key""" keys = list(self.history.keys()) + last_viewed = self.get('last_viewed') - prev_k=keys[0] - for k in sorted(keys, key=lambda x: int(x)): - if int(k) >= int(last_viewed): + prev_k = keys[0] + sorted_keys = sorted(keys, key=lambda x: int(x)) + sorted_keys.reverse() + + for k in sorted_keys: + if int(k) < int(last_viewed): return prev_k prev_k = k - #@check this, return last if not found? - return keys[-1] + return keys[0] def get_history_snapshot(self, timestamp): import brotli diff --git a/changedetectionio/tests/unit/test_watch_model.py b/changedetectionio/tests/unit/test_watch_model.py new file mode 100644 index 00000000..d7d26d71 --- /dev/null +++ b/changedetectionio/tests/unit/test_watch_model.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +# run from dir above changedetectionio/ dir +# python3 -m unittest changedetectionio.tests.unit.test_notification_diff + +import unittest +import os + +from changedetectionio.model import Watch + +# mostly +class TestDiffBuilder(unittest.TestCase): + + def test_watch_module(self): + import uuid as uuid_builder + watch = Watch.model(datastore_path='/tmp', default={}) + watch.ensure_data_dir_exists() + + watch['last_viewed'] = 110 + + watch.save_history_text(contents=b"hello world", timestamp=100, snapshot_id=str(uuid_builder.uuid4())) + watch.save_history_text(contents=b"hello world", timestamp=105, snapshot_id=str(uuid_builder.uuid4())) + watch.save_history_text(contents=b"hello world", timestamp=109, snapshot_id=str(uuid_builder.uuid4())) + watch.save_history_text(contents=b"hello world", timestamp=112, snapshot_id=str(uuid_builder.uuid4())) + watch.save_history_text(contents=b"hello world", timestamp=115, snapshot_id=str(uuid_builder.uuid4())) + + p = watch.get_next_snapshot_key_to_last_viewed + assert p == "112", "Correct last-viewed timestamp was detected" + +if __name__ == '__main__': + unittest.main()