"""Additional tests for ledger module."""

import time

import pytest

from ect import Payload, MemoryLedger, ErrTaskIDExists


def test_ledger_append_and_get():
    m = MemoryLedger()
    p = Payload(iss="i", aud=["a"], iat=1, exp=2, jti="j1", exec_act="act", pred=[])
    seq = m.append("jws1", p)
    assert seq == 1
    assert m.get_by_tid("j1").jti == "j1"


def test_ledger_err_task_id_exists():
    m = MemoryLedger()
    p = Payload(iss="i", aud=["a"], iat=1, exp=2, jti="j-dup", exec_act="e", pred=[])
    m.append("jws1", p)
    with pytest.raises(ErrTaskIDExists):
        m.append("jws2", p)


def test_ledger_contains_wid():
    m = MemoryLedger()
    p = Payload(iss="i", aud=["a"], iat=1, exp=2, jti="j1", exec_act="e", pred=[], wid="wf1")
    m.append("jws", p)
    assert m.contains("j1", "") is True
    assert m.contains("j1", "wf1") is True
    assert m.contains("j1", "wf2") is False


def test_ledger_append_none():
    m = MemoryLedger()
    seq = m.append("jws", None)
    assert seq == 0
