B7: a closed entry with no closedAt is stamped at the first commit that sees it and pruned seven days later
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
433243c8b9
commit
f28c4d8fa3
2 changed files with 23 additions and 1 deletions
|
|
@ -212,12 +212,18 @@ pub(crate) struct Applied {
|
|||
|
||||
/// The one applier: her state with one turn's proposals applied, as deltas to `state` as it stands at commit, and what
|
||||
/// was applied and refused. `key` names the turn (`<run>-t<turn>`); `now` is the commit's time. A failed turn applies
|
||||
/// nothing. Closed entries older than `CLOSED_KEPT_DAYS` are removed first.
|
||||
/// nothing. Closed entries older than `CLOSED_KEPT_DAYS` are removed first; a closed entry with no `closedAt` (an outside
|
||||
/// put that closed it without one) is stamped `now` at the first commit that sees it, and removed `CLOSED_KEPT_DAYS` after
|
||||
/// that (B7), so none is kept forever.
|
||||
pub(crate) fn apply(state: &Value, record: &Value, prose: &str, key: &str, now: DateTime<Utc>) -> (Value, Applied) {
|
||||
let mut state = state.clone();
|
||||
let mut applied = Applied::default();
|
||||
let kept_until = now - Duration::days(CLOSED_KEPT_DAYS);
|
||||
if let Some(entries) = state.get_mut("commitments").and_then(Value::as_array_mut) {
|
||||
let closed_at = |entry: &Value| entry["closedAt"].as_str().and_then(|at| DateTime::parse_from_rfc3339(at).ok());
|
||||
for entry in entries.iter_mut().filter(|entry| closed(entry["status"].as_str().unwrap_or_default()) && closed_at(entry).is_none()) {
|
||||
entry["closedAt"] = json!(now.to_rfc3339());
|
||||
}
|
||||
let before = entries.len();
|
||||
entries.retain(|entry| !closed(entry["status"].as_str().unwrap_or_default()) || entry["closedAt"].as_str().and_then(|at| DateTime::parse_from_rfc3339(at).ok()).is_none_or(|at| at.with_timezone(&Utc) > kept_until));
|
||||
applied.pruned = before - entries.len();
|
||||
|
|
|
|||
|
|
@ -216,3 +216,19 @@ fn b6_an_outside_put_wins_on_any_field_it_changed() {
|
|||
let (state, _) = apply(&holding("open"), &record(json!([close(How::Withdrawn, Evidence::Draft(1))]), shown("open"), true), PROSE, "r1-t2", now());
|
||||
assert_eq!(state["commitments"][0]["status"], "withdrawn");
|
||||
}
|
||||
|
||||
/// B7: a closed entry with no `closedAt` is stamped at the first commit that sees it and removed seven days after that.
|
||||
#[test]
|
||||
fn b7_a_closed_entry_without_a_close_time_is_stamped_then_pruned_a_week_later() {
|
||||
let mut no_at = holding("kept");
|
||||
no_at["commitments"][0]["closedAt"] = Value::Null;
|
||||
let (stamped, applied) = apply(&no_at, &record(json!([]), json!([]), false), PROSE, "r1-t9", now());
|
||||
assert_eq!((stamped["commitments"][0]["closedAt"].as_str(), applied.pruned), (Some(now().to_rfc3339().as_str()), 0), "{stamped}");
|
||||
let (kept, applied) = apply(&stamped, &record(json!([]), json!([]), false), PROSE, "r1-t10", now() + Duration::days(6));
|
||||
assert_eq!((kept["commitments"].as_array().map(Vec::len), applied.pruned), (Some(1), 0));
|
||||
let (gone, applied) = apply(&stamped, &record(json!([]), json!([]), false), PROSE, "r1-t10", now() + Duration::days(8));
|
||||
assert_eq!((gone["commitments"].as_array().map(Vec::len), applied.pruned), (Some(0), 1));
|
||||
// A failed turn stamps too: the stamp is the commit's, not the turn's.
|
||||
let (failed, _) = apply(&no_at, &json!({ "failed": "x" }), PROSE, "r1-t9", now());
|
||||
assert!(failed["commitments"][0]["closedAt"].is_string());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue