Files
kvitt/server/crates/kvitt-server/tests/sync_api/audit.rs
T
jacobh2 15056dc7df M4: sync pull and push (E-31, E-32), audit writer and diff, FCM sender, push batching, scheduler, pruning, rotate-epoch
- src/sync/: pull with UNION ALL paging and cursor_ahead; push processing of SPEC §14.4.4 steps 1 to 12 in one
  BEGIN IMMEDIATE with a savepoint per mutation, LWW with delete-wins, tombstones, clock_skew, replay from
  sync_mutations, sync_batch for more than 3 notifiable changes; 90-day pruning.
- src/audit/: SqlAuditWriter and the §14.4.7 diff (now the default in AppState::new).
- src/push/: bounded PushHub (100, drop oldest), FCM HTTP v1 client with service-account JWT (RS256, jsonwebtoken
  on aws-lc-rs) and token cache, sender with 1/5/25 s retries and token clearing; disabled without credentials.
- src/tasks/: start installs push and spawns the 06:00 generation (and at start) and 04:00 pruning loops on the
  injectable clock.
- cli: rotate-epoch; serve uses into_make_service_with_connect_info; stale doc comments updated; /i/ empty code
  gets the HTML 404.
- tests: sync_api, push_fcm (local stub of Google's endpoints), scheduler_daily; skeleton routes test now asserts
  no route is 501.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TQd3pEP9UeN2uifbHknQ7B
2026-10-17 22:27:30 +02:00

91 lines
3.0 KiB
Rust

//! The real audit writer (SPEC §14.4.7, FR-12) through M3's recurring generation and through sync: source, actor,
//! times and append-only entries.
use serde_json::json;
use crate::common::*;
use crate::m4::*;
#[tokio::test]
async fn generation_writes_recurring_audit_with_server_now_and_no_device() {
let app = app().await;
app.set_now("2026-10-17T12:00:00.000Z");
let template = new_id();
sqlx::query(
"INSERT INTO recurring_templates (id, account_id, category_id, amount_ore, description, notes, payer_user_id, \
split_mode, day_of_month, start_period, created_by_user_id, updated_by_user_id, created_at, updated_at, \
server_version) VALUES (?, ?, ?, 1250000, 'Hyra', '', ?, 'default', 1, '2026-10', ?, ?, ?, ?, 900000)",
)
.bind(&template)
.bind(ACCOUNT_1)
.bind(HOME)
.bind(ADMIN_ID)
.bind(MEMBER_ID)
.bind(MEMBER_ID)
.bind(NOW)
.bind(NOW)
.execute(&app.state.db)
.await
.unwrap();
let generated =
kvitt_server::api::recurring::generate_due_periods(&app.state, template.parse().unwrap())
.await
.unwrap();
assert_eq!(generated.len(), 1);
let audit = audit_of(&app, &generated[0].expense_id.to_string()).await;
assert_eq!(audit.len(), 1);
let a = &audit[0];
assert_eq!(
(a.action.as_str(), a.source.as_str()),
("create", "recurring")
);
assert_eq!(a.actor_user_id, MEMBER_ID);
assert_eq!(a.actor_device_id, None);
assert_eq!((a.client_at.as_str(), a.server_at.as_str()), (NOW, NOW));
assert_eq!(
a.changes[3],
json!({"field": "amount_ore", "old": null, "new": 1250000})
);
// The entry reaches the phones through pull.
let page = pull(&app, &admin_token(), 0, None).await.json;
assert!(
page["audit_entries"]
.as_array()
.unwrap()
.iter()
.any(|e| e["source"] == "recurring")
);
}
#[tokio::test]
async fn entries_are_append_only_create_update_delete() {
let app = app().await;
let id = new_id();
push_one(
&app,
&admin_token(),
exp(expense(&id, "2026-10-17T10:00:00.000Z")),
)
.await;
let first = audit_of(&app, &id).await;
let mut e = expense(&id, "2026-10-17T11:00:00.000Z");
e["description"] = json!("Coop");
push_one(&app, &member_token(), exp(e)).await;
let mut d = expense(&id, "2026-10-17T11:30:00.000Z");
d["deleted_at"] = json!("2026-10-17T11:30:00.000Z");
push_one(&app, &admin_token(), exp(d)).await;
let all = audit_of(&app, &id).await;
let actions: Vec<_> = all.iter().map(|a| a.action.as_str()).collect();
assert_eq!(actions, ["create", "update", "delete"]);
assert_eq!(all[0], first[0], "earlier entries are never updated");
assert_eq!(
all[1].changes,
json!([{"field": "description", "old": "ICA Maxi", "new": "Coop"}])
);
assert_eq!(all[1].actor_user_id, MEMBER_ID);
assert!(
all.windows(2)
.all(|w| w[0].server_version < w[1].server_version)
);
}