refactor
This commit is contained in:
parent
7cef56de15
commit
ecb5f68e32
17 changed files with 760 additions and 1626 deletions
|
|
@ -6,79 +6,68 @@ from gibby.models import AccountRecord, StateFile, UsageSnapshot, UsageWindow
|
|||
from gibby.store import JsonStateStore
|
||||
|
||||
|
||||
def test_store_writes_canonical_usage_snapshot_shape(tmp_path) -> None:
|
||||
def test_store_writes_minimal_accounts_schema(tmp_path) -> None:
|
||||
store = JsonStateStore(tmp_path / "accounts.json")
|
||||
snapshot = UsageSnapshot(
|
||||
checked_at=1000,
|
||||
used_percent=75,
|
||||
remaining_percent=25,
|
||||
exhausted=False,
|
||||
primary_window=UsageWindow(75, 18000, 300, 1300),
|
||||
secondary_window=UsageWindow(10, 604800, 3600, 4600),
|
||||
limit_reached=False,
|
||||
allowed=True,
|
||||
)
|
||||
store.save(
|
||||
StateFile(
|
||||
active_account="acc@example.com",
|
||||
accounts=[
|
||||
AccountRecord(
|
||||
id="acc@example.com",
|
||||
email="acc@example.com",
|
||||
access_token="tok",
|
||||
refresh_token="ref",
|
||||
expires_at=2000,
|
||||
last_known_usage=snapshot,
|
||||
token_refresh_at=2000,
|
||||
usage=UsageSnapshot(
|
||||
checked_at=1000,
|
||||
primary_window=UsageWindow(used_percent=70, reset_at=1300),
|
||||
secondary_window=UsageWindow(used_percent=20, reset_at=4600),
|
||||
),
|
||||
usage_checked_at=1000,
|
||||
disabled=False,
|
||||
)
|
||||
]
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
payload = json.loads((tmp_path / "accounts.json").read_text())
|
||||
saved_snapshot = payload["accounts"][0]["last_known_usage"]
|
||||
|
||||
assert set(saved_snapshot) == {
|
||||
"checked_at",
|
||||
"primary_window",
|
||||
"secondary_window",
|
||||
"limit_reached",
|
||||
"allowed",
|
||||
assert payload == {
|
||||
"active_account": "acc@example.com",
|
||||
"accounts": [
|
||||
{
|
||||
"email": "acc@example.com",
|
||||
"access_token": "tok",
|
||||
"refresh_token": "ref",
|
||||
"token_refresh_at": 2000,
|
||||
"usage": {
|
||||
"primary": {"used_percent": 70, "reset_at": 1300},
|
||||
"secondary": {"used_percent": 20, "reset_at": 4600},
|
||||
},
|
||||
"usage_checked_at": 1000,
|
||||
"disabled": False,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def test_store_load_reconstructs_derived_usage_fields(tmp_path) -> None:
|
||||
def test_store_load_reconstructs_account_state(tmp_path) -> None:
|
||||
path = tmp_path / "accounts.json"
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"version": 1,
|
||||
"active_account_id": "acc@example.com",
|
||||
"active_account": "acc@example.com",
|
||||
"accounts": [
|
||||
{
|
||||
"id": "acc@example.com",
|
||||
"email": "acc@example.com",
|
||||
"account_id": "acc-1",
|
||||
"access_token": "tok",
|
||||
"refresh_token": "ref",
|
||||
"expires_at": 2000,
|
||||
"cooldown_until": None,
|
||||
"last_known_usage": {
|
||||
"checked_at": 1000,
|
||||
"primary_window": {
|
||||
"used_percent": 80,
|
||||
"limit_window_seconds": 18000,
|
||||
"reset_after_seconds": 300,
|
||||
"reset_at": 1300,
|
||||
},
|
||||
"secondary_window": {
|
||||
"used_percent": 100,
|
||||
"limit_window_seconds": 604800,
|
||||
"reset_after_seconds": 3600,
|
||||
"reset_at": 4600,
|
||||
},
|
||||
"limit_reached": False,
|
||||
"allowed": True,
|
||||
"token_refresh_at": 2000,
|
||||
"usage": {
|
||||
"primary": {"used_percent": 80, "reset_at": 1300},
|
||||
"secondary": {"used_percent": 15, "reset_at": 4600},
|
||||
},
|
||||
"last_error": None,
|
||||
"usage_checked_at": 1000,
|
||||
"disabled": True,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
|
@ -86,11 +75,40 @@ def test_store_load_reconstructs_derived_usage_fields(tmp_path) -> None:
|
|||
)
|
||||
|
||||
state = JsonStateStore(path).load()
|
||||
snapshot = state.accounts[0].last_known_usage
|
||||
|
||||
assert snapshot is not None
|
||||
assert snapshot.used_percent == 100
|
||||
assert snapshot.remaining_percent == 0
|
||||
assert snapshot.exhausted is True
|
||||
assert snapshot.limit_reached is False
|
||||
assert snapshot.allowed is True
|
||||
assert state.active_account == "acc@example.com"
|
||||
assert state.accounts[0].email == "acc@example.com"
|
||||
assert state.accounts[0].token_refresh_at == 2000
|
||||
assert state.accounts[0].usage is not None
|
||||
assert state.accounts[0].usage.primary_window is not None
|
||||
assert state.accounts[0].usage.primary_window.used_percent == 80
|
||||
assert state.accounts[0].disabled is True
|
||||
|
||||
|
||||
def test_append_failed_account_writes_failed_json_shape(tmp_path) -> None:
|
||||
store = JsonStateStore(tmp_path / "accounts.json")
|
||||
store.append_failed_account(
|
||||
AccountRecord(
|
||||
email="failed@example.com",
|
||||
access_token="tok",
|
||||
refresh_token="ref",
|
||||
token_refresh_at=2000,
|
||||
disabled=False,
|
||||
)
|
||||
)
|
||||
|
||||
payload = json.loads((tmp_path / "failed.json").read_text())
|
||||
|
||||
assert payload == {
|
||||
"accounts": [
|
||||
{
|
||||
"email": "failed@example.com",
|
||||
"access_token": "tok",
|
||||
"refresh_token": "ref",
|
||||
"token_refresh_at": 2000,
|
||||
"usage": None,
|
||||
"usage_checked_at": None,
|
||||
"disabled": False,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue