import pytest
from backplane_runtime.store import StateStore

def test_receipt_pages_are_stable_and_never_cross_tenants(tmp_path):
    store=StateStore(tmp_path/'state.sqlite3')
    for tenant in ['one','two']: store.ensure_demo_tenant(tenant,tenant)
    for i in range(6):store.save_receipt(f'r{i}','one',{'receipt_id':f'r{i}','tenant_id':'one'})
    store.save_receipt('other','two',{'receipt_id':'other','tenant_id':'two'})
    first=store.list_receipts('one',limit=2)
    assert [r['receipt_id'] for r in first['receipts']]==['r5','r4']
    store.save_receipt('new','one',{'receipt_id':'new','tenant_id':'one'})
    second=store.list_receipts('one',limit=2,before=first['next_cursor'])
    assert [r['receipt_id'] for r in second['receipts']]==['r3','r2']
    assert store.list_receipts('one',limit=2,before=second['next_cursor'])['next_cursor'] is None
    with pytest.raises(ValueError,match='this tenant'):store.list_receipts('two',before='r4')
    for limit in [0,201,True,1.5]:
        with pytest.raises(ValueError):store.list_receipts('one',limit=limit)
