File size: 904 Bytes
b65f9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations

import sqlite3
import time
from pathlib import Path

from zai2api.db import Database, LOG_RETENTION_DAYS_KEY


def test_list_logs_prunes_entries_older_than_retention_window(tmp_path: Path) -> None:
    db = Database(str(tmp_path / "state.db"))
    db.initialize()
    db.set_setting(LOG_RETENTION_DAYS_KEY, "7")

    now = int(time.time())
    with sqlite3.connect(db.path) as conn:
        conn.execute(
            "INSERT INTO logs(created_at, level, category, message, details) VALUES (?, ?, ?, ?, ?)",
            (now - 10 * 86400, "info", "tests", "old-log", None),
        )
        conn.execute(
            "INSERT INTO logs(created_at, level, category, message, details) VALUES (?, ?, ?, ?, ?)",
            (now, "info", "tests", "fresh-log", None),
        )

    logs = db.list_logs(limit=20)

    assert [item.message for item in logs] == ["fresh-log"]