add 400+ unit tests for domain and application layers
Some checks failed
CI / Check / Test (push) Has been cancelled
Some checks failed
CI / Check / Test (push) Has been cancelled
Extract ReviewLogger trait to decouple import/integrations from diary::log_review (cross-module coupling smell). Add in-memory fakes for all repository ports, enabling isolated testing of every use case module without a database. Coverage: domain+application 22% → 80%, 427 tests.
This commit is contained in:
372
crates/application/src/integrations/tests/confirm.rs
Normal file
372
crates/application/src/integrations/tests/confirm.rs
Normal file
@@ -0,0 +1,372 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::models::{WatchEvent, WatchEventSource};
|
||||
use domain::ports::{MovieRepository, WatchEventRepository};
|
||||
use domain::testing::{InMemoryWatchEventRepository, NoopEventPublisher};
|
||||
use domain::value_objects::UserId;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::integrations::commands::{ConfirmWatchEventsCommand, WatchEventConfirmation};
|
||||
use crate::integrations::confirm;
|
||||
use crate::test_helpers::TestContextBuilder;
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirms_watch_event_via_review_logger() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let uid = Uuid::new_v4();
|
||||
|
||||
let event = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"Test Movie".into(),
|
||||
Some(2024),
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let event_id = event.id().value();
|
||||
watch_events.save(&event).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(Arc::clone(&watch_events) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: uid,
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: event_id,
|
||||
rating: 4,
|
||||
comment: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn empty_confirmations_returns_zero() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: Uuid::new_v4(),
|
||||
confirmations: vec![],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirms_event_with_external_metadata_id_and_no_movie_id() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let uid = Uuid::new_v4();
|
||||
|
||||
let event = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"External Movie".into(),
|
||||
Some(2023),
|
||||
Some("tt1234567".into()),
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let event_id = event.id().value();
|
||||
watch_events.save(&event).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(Arc::clone(&watch_events) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: uid,
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: event_id,
|
||||
rating: 3,
|
||||
comment: Some("Great film".into()),
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_other_users_event() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let owner = Uuid::new_v4();
|
||||
let intruder = Uuid::new_v4();
|
||||
|
||||
let event = WatchEvent::new(
|
||||
UserId::from_uuid(owner),
|
||||
"Movie".into(),
|
||||
None,
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let event_id = event.id().value();
|
||||
watch_events.save(&event).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(Arc::clone(&watch_events) as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: intruder,
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: event_id,
|
||||
rating: 3,
|
||||
comment: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_event_not_found() {
|
||||
let ctx = TestContextBuilder::new().build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: Uuid::new_v4(),
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: Uuid::new_v4(),
|
||||
rating: 4,
|
||||
comment: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirms_event_with_movie_id() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let uid = Uuid::new_v4();
|
||||
let movie_uuid = Uuid::new_v4();
|
||||
|
||||
let event = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"Movie With Id".into(),
|
||||
Some(2024),
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
Some(domain::value_objects::MovieId::from_uuid(movie_uuid)),
|
||||
);
|
||||
let event_id = event.id().value();
|
||||
watch_events.save(&event).await.unwrap();
|
||||
|
||||
// Also seed movie repo so review_logger can find it
|
||||
let movies = domain::testing::InMemoryMovieRepository::new();
|
||||
let movie = domain::models::Movie::from_persistence(
|
||||
domain::value_objects::MovieId::from_uuid(movie_uuid),
|
||||
None,
|
||||
domain::value_objects::MovieTitle::new("Movie With Id".into()).unwrap(),
|
||||
domain::value_objects::ReleaseYear::new(2024).unwrap(),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
movies.upsert_movie(&movie).await.unwrap();
|
||||
|
||||
// Build a real review logger
|
||||
let reviews = domain::testing::InMemoryReviewRepository::new();
|
||||
let watchlist = domain::testing::InMemoryWatchlistRepository::new();
|
||||
let review_logger = std::sync::Arc::new(crate::diary::review_logger::DefaultReviewLogger::new(
|
||||
std::sync::Arc::clone(&movies) as _,
|
||||
std::sync::Arc::clone(&reviews) as _,
|
||||
std::sync::Arc::clone(&watchlist) as _,
|
||||
std::sync::Arc::new(domain::testing::FakeMetadataClient) as _,
|
||||
std::sync::Arc::clone(&events) as _,
|
||||
));
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(std::sync::Arc::clone(&watch_events) as _)
|
||||
.with_event_publisher(std::sync::Arc::clone(&events) as _)
|
||||
.with_movies(std::sync::Arc::clone(&movies) as _)
|
||||
.with_review_logger(review_logger as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: uid,
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: event_id,
|
||||
rating: 4,
|
||||
comment: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirms_event_without_movie_id_and_without_external_metadata_id() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let uid = Uuid::new_v4();
|
||||
|
||||
let event = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"Title Only Movie".into(),
|
||||
Some(2022),
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let event_id = event.id().value();
|
||||
watch_events.save(&event).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(Arc::clone(&watch_events) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: uid,
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: event_id,
|
||||
rating: 5,
|
||||
comment: Some("Amazing".into()),
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirms_multiple_events() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let uid = Uuid::new_v4();
|
||||
|
||||
let event1 = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"Movie One".into(),
|
||||
Some(2020),
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let id1 = event1.id().value();
|
||||
|
||||
let event2 = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"Movie Two".into(),
|
||||
Some(2021),
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let id2 = event2.id().value();
|
||||
|
||||
watch_events.save(&event1).await.unwrap();
|
||||
watch_events.save(&event2).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(Arc::clone(&watch_events) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: uid,
|
||||
confirmations: vec![
|
||||
WatchEventConfirmation {
|
||||
watch_event_id: id1,
|
||||
rating: 3,
|
||||
comment: None,
|
||||
},
|
||||
WatchEventConfirmation {
|
||||
watch_event_id: id2,
|
||||
rating: 4,
|
||||
comment: None,
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirms_event_without_year() {
|
||||
let watch_events = InMemoryWatchEventRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let uid = Uuid::new_v4();
|
||||
|
||||
let event = WatchEvent::new(
|
||||
UserId::from_uuid(uid),
|
||||
"No Year Movie".into(),
|
||||
None, // no year
|
||||
None,
|
||||
WatchEventSource::Jellyfin,
|
||||
chrono::Utc::now().naive_utc(),
|
||||
None,
|
||||
);
|
||||
let event_id = event.id().value();
|
||||
watch_events.save(&event).await.unwrap();
|
||||
|
||||
let ctx = TestContextBuilder::new()
|
||||
.with_watch_events(Arc::clone(&watch_events) as _)
|
||||
.with_event_publisher(Arc::clone(&events) as _)
|
||||
.build();
|
||||
|
||||
let result = confirm::execute(
|
||||
&ctx,
|
||||
ConfirmWatchEventsCommand {
|
||||
user_id: uid,
|
||||
confirmations: vec![WatchEventConfirmation {
|
||||
watch_event_id: event_id,
|
||||
rating: 3,
|
||||
comment: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, 1);
|
||||
}
|
||||
Reference in New Issue
Block a user