server: - backup exporter, auth extractors, error shapes, CONTEXT (prior work) - spa assets served outside the rate limit via route_layer - requests_per_second went to per_second(), which takes an interval not a rate: 50 meant one request per 50s once burst was spent. now converted properly. 15/s, burst 60 spa fixes: - account delete cleared snake_case token keys that were never written - refresh interceptor could retry forever - date ranges used local day boundaries stamped +00:00 - "all" period trend plotted one page; calendar days fabricated mood 3 - chart grid invisible: hsl(var(--border)) against rgba tokens - blob url leak, orphaned media on failed save, devtools in prod bundle - pt-safe/safe-area-pb classes never existed spa features: - offline outbox: entries queue to IndexedDB, replay with backoff, only server refusals count against an entry - drafts persist, quick-log sheet, diary infinite scroll + filters - route error boundary, stale-chunk recovery, no service worker in dev a11y + perf: - mood picker is a radiogroup, activity picker keyboard-operable, text alternatives for colour/emoji, locale week start - dark glass over the bright photo: worst case 1.4:1 -> 4.9-9.6:1 - initial payload 1095->769kB raw, 306->230kB gzip; 38 unused components and 5 deps dropped; fonts 218->133kB 53 tests added (43 spa, 10 server)
143 lines
3.9 KiB
Rust
143 lines
3.9 KiB
Rust
use chrono::{DateTime, FixedOffset};
|
|
use sqlx::SqlitePool;
|
|
|
|
use domain::entry::{DateRange, Mood, MoodEntry};
|
|
use domain::ports::{MoodEntryCommandPort, MoodEntryQueryPort, UserCommandPort};
|
|
use domain::testing::test_user;
|
|
use domain::user::User;
|
|
|
|
use sqlite::repositories::{
|
|
SqliteEntryCommandRepository, SqliteEntryQueryRepository, SqliteUserCommandRepository,
|
|
};
|
|
|
|
fn a_file() -> String {
|
|
let name = format!("k-mood-instants-{}.sqlite", uuid::Uuid::new_v4());
|
|
|
|
std::env::temp_dir()
|
|
.join(name)
|
|
.to_string_lossy()
|
|
.to_string()
|
|
}
|
|
|
|
async fn a_pool_with_a_user() -> (SqlitePool, User) {
|
|
let path = a_file();
|
|
let pool = sqlite::create_pool(&format!("sqlite://{path}"))
|
|
.await
|
|
.unwrap();
|
|
sqlite::run_migrations(&pool).await.unwrap();
|
|
|
|
let user = test_user("alice");
|
|
SqliteUserCommandRepository::new(pool.clone())
|
|
.save(&user)
|
|
.await
|
|
.unwrap();
|
|
|
|
(pool, user)
|
|
}
|
|
|
|
fn instant(text: &str) -> DateTime<FixedOffset> {
|
|
DateTime::parse_from_rfc3339(text).unwrap()
|
|
}
|
|
|
|
async fn save(pool: &SqlitePool, user: &User, logged_at: &str) -> MoodEntry {
|
|
let entry = MoodEntry::new(user.id().clone(), Mood::Good, instant(logged_at));
|
|
SqliteEntryCommandRepository::new(pool.clone())
|
|
.save(&entry)
|
|
.await
|
|
.unwrap();
|
|
|
|
entry
|
|
}
|
|
|
|
async fn stored_text(pool: &SqlitePool, entry: &MoodEntry) -> String {
|
|
let (held,): (String,) = sqlx::query_as("SELECT logged_at FROM mood_entries WHERE id = ?")
|
|
.bind(entry.id().value().to_string())
|
|
.fetch_one(pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
held
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_instant_is_held_in_utc_whatever_offset_it_arrived_in() {
|
|
let (pool, user) = a_pool_with_a_user().await;
|
|
|
|
let entry = save(&pool, &user, "2026-08-25T20:00:00+02:00").await;
|
|
|
|
assert_eq!(
|
|
stored_text(&pool, &entry).await,
|
|
"2026-08-25T18:00:00+00:00",
|
|
"one canonical form makes the column sortable"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_instant_survives_the_round_trip_unchanged() {
|
|
let (pool, user) = a_pool_with_a_user().await;
|
|
|
|
let entry = save(&pool, &user, "2026-08-25T20:00:00+02:00").await;
|
|
|
|
let read = SqliteEntryQueryRepository::new(pool.clone())
|
|
.find_by_id(entry.id())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
read.logged_at(),
|
|
entry.logged_at(),
|
|
"the instant is the fact; the offset it was written in is not"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_range_finds_entries_written_in_other_offsets() {
|
|
let (pool, user) = a_pool_with_a_user().await;
|
|
|
|
let eastern = save(&pool, &user, "2026-08-25T01:30:00+03:00").await;
|
|
let western = save(&pool, &user, "2026-08-24T21:30:00-04:00").await;
|
|
|
|
let whole_of_the_24th_utc = DateRange::new(
|
|
instant("2026-08-24T00:00:00+00:00"),
|
|
instant("2026-08-24T23:59:59+00:00"),
|
|
)
|
|
.unwrap();
|
|
|
|
let found = SqliteEntryQueryRepository::new(pool.clone())
|
|
.find_by_date_range(user.id(), &whole_of_the_24th_utc)
|
|
.await
|
|
.unwrap();
|
|
|
|
let ids: Vec<_> = found.iter().map(|entry| entry.id().clone()).collect();
|
|
|
|
assert!(
|
|
ids.contains(eastern.id()),
|
|
"01:30+03:00 is 22:30Z on the 24th and belongs in the range"
|
|
);
|
|
assert!(
|
|
!ids.contains(western.id()),
|
|
"21:30-04:00 is 01:30Z on the 25th, so it falls outside the range"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn entries_come_back_newest_first_across_mixed_offsets() {
|
|
let (pool, user) = a_pool_with_a_user().await;
|
|
|
|
let earlier = save(&pool, &user, "2026-08-25T00:30:00+02:00").await;
|
|
let later = save(&pool, &user, "2026-08-25T00:00:00+00:00").await;
|
|
|
|
let found = SqliteEntryQueryRepository::new(pool.clone())
|
|
.find_all_by_user(user.id())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
found[0].id(),
|
|
later.id(),
|
|
"22:30Z precedes 00:00Z, however the offsets sort as text"
|
|
);
|
|
assert_eq!(found[1].id(), earlier.id());
|
|
}
|