feat: always-on Insights — correlation analysis over viewing habits
Some checks failed
CI / Check / Test (push) Failing after 9m16s

Conservative, live insights (ADR-0010) derived from each user's diary:
rating lift by genre (first-time watches only), genre seasonality vs a
uniform 1/12 month baseline, medium weekend-lift, and watch-time rhythm.

- domain: WatchRow model, InsightsQuery port, pure insights_analyzer with
  all honesty thresholds (sample minimums, strength cutoffs, suppressions
  for import noise), unit-tested
- adapters: thin Sqlite/Postgres fetch twins, reusing WrapUp's genre fetch
- application: get_insights use case, InsightsGroup in Deps
- composition/server: wiring through Repositories/DatabaseOutput
- presentation: GET /api/v1/insights handler, typed serde-tagged DTOs,
  openapi doc
- spa: /insights route (shadcn cards), insights feature hook, profile
  entry link, en locale strings
- server test: end-to-end seed sqlite → HTTP → rating lift + rhythm

Also records ADR-0010, defines "Insight" in the glossary, and includes
pre-existing worktree comment-stripping cleanups.
This commit is contained in:
2026-10-12 19:29:54 +02:00
parent 4d221e60de
commit 020fbb891e
126 changed files with 1922 additions and 639 deletions

View File

@@ -0,0 +1,93 @@
use std::sync::Arc;
use chrono::NaiveDate;
use uuid::Uuid;
use domain::{models::insights::WatchRow, testing::InMemoryInsightsQuery, value_objects::UserId};
use super::*;
fn watch(genre: &str, rating: u8, day: u32, hour: u32) -> WatchRow {
WatchRow {
movie_id: Uuid::new_v4(),
rating,
watched_at: NaiveDate::from_ymd_opt(2024, 1, day)
.unwrap()
.and_hms_opt(hour, 0, 0)
.unwrap(),
watch_medium: Some("streaming".to_string()),
genres: vec![genre.to_string()],
}
}
#[tokio::test]
async fn returns_analyzer_output_for_the_user() {
let user = Uuid::new_v4();
let rows: Vec<WatchRow> = (0..12)
.map(|i| watch("Thriller", 5, 1 + i, 22))
.chain((0..12).map(|i| watch("Comedy", 2, 13 + i, 22)))
.collect();
let deps = GetInsightsDeps {
insights: InMemoryInsightsQuery::with_rows(user, rows),
};
let insights = execute(&deps, UserId::from_uuid(user)).await.unwrap();
assert!(insights.iter().any(
|i| matches!(i, domain::models::Insight::RatingLift { genre, .. } if genre == "Thriller")
));
}
#[tokio::test]
async fn other_users_rows_are_not_seen() {
let user = Uuid::new_v4();
let other = Uuid::new_v4();
let rows: Vec<WatchRow> = (0..12)
.map(|i| watch("Thriller", 5, 1 + i, 22))
.chain((0..12).map(|i| watch("Comedy", 2, 13 + i, 22)))
.collect();
let deps = GetInsightsDeps {
insights: InMemoryInsightsQuery::with_rows(other, rows),
};
let insights = execute(&deps, UserId::from_uuid(user)).await.unwrap();
assert!(insights.is_empty());
}
#[tokio::test]
async fn empty_diary_returns_empty_list() {
let user = Uuid::new_v4();
let deps = GetInsightsDeps {
insights: InMemoryInsightsQuery::new(),
};
let insights = execute(&deps, UserId::from_uuid(user)).await.unwrap();
assert!(insights.is_empty());
}
#[tokio::test]
async fn port_errors_propagate() {
struct Failing;
#[async_trait::async_trait]
impl domain::ports::InsightsQuery for Failing {
async fn get_watch_rows(
&self,
_: &UserId,
) -> Result<Vec<WatchRow>, domain::errors::DomainError> {
Err(domain::errors::DomainError::InfrastructureError(
"db down".into(),
))
}
}
let deps = GetInsightsDeps {
insights: Arc::new(Failing),
};
let err = execute(&deps, UserId::from_uuid(Uuid::new_v4()))
.await
.unwrap_err();
assert!(matches!(
err,
domain::errors::DomainError::InfrastructureError(_)
));
}