from __future__ import annotations

import time

from txidgen.v2 import TxIdV2Generator, parse_txid_v2


def test_generate_and_parse_roundtrip():
    gen = TxIdV2Generator(node_id=3)
    txid = gen.generate(prefix="P", currency="BDT")
    parts = parse_txid_v2(txid)

    assert parts.prefix == "P"
    assert parts.node == 3
    assert parts.currency_id == 0
    assert parts.seq >= 0
    assert len(txid) == 12


def test_sortability_same_node():
    gen = TxIdV2Generator(node_id=1)

    a = gen.generate("P", "BDT")
    time.sleep(0.02)  # 20ms
    b = gen.generate("P", "BDT")

    assert a < b  # lexicographic order by time


def test_multiple_in_same_tick_increases_seq():
    gen = TxIdV2Generator(node_id=2)
    # Force same tick by passing same now_unix_ns
    now = time.time_ns()
    a = gen.generate("P", "BDT", now_unix_ns=now)
    b = gen.generate("P", "BDT", now_unix_ns=now)
    pa = parse_txid_v2(a)
    pb = parse_txid_v2(b)
    assert pb.seq == pa.seq + 1
