from __future__ import annotations

import unittest

from govoplan_core.core.pagination import (
    KeysetCursorError,
    decode_keyset_cursor,
    encode_keyset_cursor,
    keyset_query_fingerprint,
)


class PaginationCursorTests(unittest.TestCase):
    def test_keyset_cursor_round_trips_and_rejects_query_mismatch(self) -> None:
        fingerprint = keyset_query_fingerprint("audit.admin", {"scope": "system", "page_size": 25})
        cursor = encode_keyset_cursor(
            "audit.admin",
            fingerprint=fingerprint,
            values={"id": "row-1", "sort_value": "2026-01-01T00:00:00+00:00"},
        )

        self.assertEqual(
            decode_keyset_cursor("audit.admin", cursor, fingerprint=fingerprint),
            {"id": "row-1", "sort_value": "2026-01-01T00:00:00+00:00"},
        )
        with self.assertRaises(KeysetCursorError):
            decode_keyset_cursor("audit.admin", cursor, fingerprint=keyset_query_fingerprint("audit.admin", {"scope": "tenant"}))
        with self.assertRaises(KeysetCursorError):
            decode_keyset_cursor("files.list", cursor, fingerprint=fingerprint)


if __name__ == "__main__":
    unittest.main()
