Files
manual_slop/tests/test_history_persistence.py

45 lines
1.6 KiB
Python

import pytest
import tomli_w
import tomllib
from pathlib import Path
from project_manager import load_project, save_project, default_project, entry_to_str
def test_history_persistence_across_turns(tmp_path):
proj_path = tmp_path / "manual_slop.toml"
hist_path = tmp_path / "manual_slop_history.toml"
# 1. Start project
proj = default_project("test-persistence")
save_project(proj, proj_path)
# 2. Add a turn
proj = load_project(proj_path)
entry1 = {"role": "User", "content": "Hello", "ts": "2026-02-24T13:00:00"}
proj["discussion"]["discussions"]["main"]["history"].append(entry_to_str(entry1))
save_project(proj, proj_path)
# Verify separation
with open(proj_path, "rb") as f:
p_disk = tomllib.load(f)
assert "discussion" not in p_disk
with open(hist_path, "rb") as f:
h_disk = tomllib.load(f)
assert h_disk["discussions"]["main"]["history"] == ["@2026-02-24T13:00:00\nUser:\nHello"]
# 3. Add another turn
proj = load_project(proj_path)
entry2 = {"role": "AI", "content": "Hi there!", "ts": "2026-02-24T13:01:00"}
proj["discussion"]["discussions"]["main"]["history"].append(entry_to_str(entry2))
save_project(proj, proj_path)
# Verify persistence
with open(hist_path, "rb") as f:
h_disk = tomllib.load(f)
assert len(h_disk["discussions"]["main"]["history"]) == 2
assert h_disk["discussions"]["main"]["history"][1] == "@2026-02-24T13:01:00\nAI:\nHi there!"
# 4. Reload and check
proj_final = load_project(proj_path)
assert len(proj_final["discussion"]["discussions"]["main"]["history"]) == 2