test: move application tests to tests/

test_support/test_helpers -> tests/support; watched_at unified w/ domain test_watched_at (2024-03-15).
Kept inline: users::diary_filter tests (feed_sort_to_direction_all_variants, paged_user_filter_builds_correctly) - pub(super) helpers in private module.
Dropped stale never-compiled users/tests/get_users.rs (old execute signature).
This commit is contained in:
2026-10-25 22:36:52 +01:00
parent a2c713c1fa
commit 8526b77f18
175 changed files with 541 additions and 695 deletions

View File

@@ -0,0 +1,110 @@
use crate::support::{add_to_watchlist_command, movie_input_manual};
use domain::testing::test_movie_with_year;
use std::sync::Arc;
use domain::{
ports::MovieCommand,
testing::{InMemoryStore, NoopEventPublisher},
};
use application::watchlist::{add, commands::AddToWatchlistCommand, deps::WatchlistAddDeps};
fn make_deps(movies: Arc<InMemoryStore>, watchlist: Arc<InMemoryStore>) -> WatchlistAddDeps {
WatchlistAddDeps {
movie_command: Arc::clone(&movies) as _,
movie_query: movies,
metadata: Arc::new(domain::testing::FakeMetadataClient),
watchlist,
event_publisher: NoopEventPublisher::new(),
}
}
#[tokio::test]
async fn adding_to_the_watchlist_resolves_the_movie_and_saves_the_entry() {
let movies = InMemoryStore::new();
let watchlist = Arc::clone(&movies);
let movie = test_movie_with_year("The Thing", 1982);
let movie_uuid = movie.id().value();
movies.upsert_movie(&movie).await.unwrap();
let deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
let cmd = add_to_watchlist_command(uuid::Uuid::new_v4(), movie_uuid);
add::execute(&deps, cmd).await.unwrap();
assert_eq!(watchlist.watchlist_count(), 1);
}
#[tokio::test]
async fn adding_a_movie_already_on_the_watchlist_is_idempotent() {
let movies = InMemoryStore::new();
let watchlist = Arc::clone(&movies);
let movie = test_movie_with_year("RoboCop", 1987);
let movie_uuid = movie.id().value();
let user_id = uuid::Uuid::new_v4();
movies.upsert_movie(&movie).await.unwrap();
let deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
let make_cmd = || add_to_watchlist_command(user_id, movie_uuid);
add::execute(&deps, make_cmd()).await.unwrap();
add::execute(&deps, make_cmd()).await.unwrap();
assert_eq!(
watchlist.watchlist_count(),
1,
"idempotent add should not duplicate"
);
}
#[tokio::test]
async fn adding_a_manual_movie_to_the_watchlist_creates_and_saves_it() {
let movies = InMemoryStore::new();
let watchlist = Arc::clone(&movies);
let deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
let cmd = AddToWatchlistCommand {
user_id: uuid::Uuid::new_v4(),
input: movie_input_manual("New Manual Movie", 2024),
};
add::execute(&deps, cmd).await.unwrap();
assert_eq!(watchlist.watchlist_count(), 1);
assert_eq!(movies.movie_count(), 1);
}
#[tokio::test]
async fn adding_an_unknown_movie_id_to_the_watchlist_fails() {
let movies = InMemoryStore::new();
let watchlist = Arc::clone(&movies);
let deps = make_deps(Arc::clone(&movies), Arc::clone(&watchlist));
let cmd = add_to_watchlist_command(uuid::Uuid::new_v4(), uuid::Uuid::new_v4());
assert!(add::execute(&deps, cmd).await.is_err());
}
#[tokio::test]
async fn adding_the_same_movie_to_a_watchlist_twice_keeps_a_single_entry_and_succeeds() {
let movies = InMemoryStore::new();
let movie = test_movie_with_year("Heat", 1995);
let movie_uuid = movie.id().value();
let user_id = uuid::Uuid::new_v4();
movies.upsert_movie(&movie).await.unwrap();
let deps = make_deps(Arc::clone(&movies), Arc::clone(&movies));
for _ in 0..2 {
add::execute(&deps, add_to_watchlist_command(user_id, movie_uuid))
.await
.expect("second add must not error");
}
assert_eq!(movies.watchlist_count(), 1);
}

View File

@@ -0,0 +1,25 @@
use uuid::Uuid;
use crate::support::TestContextBuilder;
use application::watchlist::{deps::GetWatchlistDeps, get, queries::GetWatchlistQuery};
#[tokio::test]
async fn returns_empty_page_for_new_user() {
let b = TestContextBuilder::new();
let deps = GetWatchlistDeps {
watchlist: b.watchlist_repo.clone(),
};
let result = get::execute(
&deps,
GetWatchlistQuery {
user_id: Uuid::new_v4(),
limit: None,
offset: None,
},
)
.await
.unwrap();
assert!(result.items.is_empty());
assert_eq!(result.total_count, 0);
}

View File

@@ -0,0 +1,121 @@
use crate::support::register_command;
use std::sync::Arc;
use uuid::Uuid;
use domain::testing::{InMemoryStore, PanicRemoteWatchlistRepository, PanicWatchlistRepository};
use domain::value_objects::Email;
use crate::support::TestContextBuilder;
use application::auth::deps::RegisterDeps;
use application::auth::register;
use application::watchlist::deps::GetWatchlistForOwnerDeps;
use application::watchlist::get_watchlist_for_owner::{self, WatchlistView};
async fn setup_user(b: &TestContextBuilder, email: &str, username: &str) {
let deps = RegisterDeps {
user: b.user_repo.clone(),
password_hasher: b.password_hasher.clone(),
config: b.config.clone(),
};
register::execute(&deps, register_command(email, username))
.await
.unwrap();
}
#[tokio::test]
async fn watchlist_for_owner_returns_local_when_user_row_exists() {
let b = TestContextBuilder::new();
setup_user(&b, "owner@test.com", "owneruser").await;
let email = Email::new("owner@test.com".into()).unwrap();
let user = b.user_repo.find_by_email(&email).await.unwrap().unwrap();
let uid = user.id().value();
let deps = GetWatchlistForOwnerDeps {
user: b.user_repo.clone(),
watchlist: InMemoryStore::new(),
remote_watchlist: Arc::new(PanicRemoteWatchlistRepository),
};
let view = get_watchlist_for_owner::execute(&deps, uid, Some(20), Some(0))
.await
.unwrap();
assert!(matches!(view, WatchlistView::Local(_)), "expected Local");
}
#[tokio::test]
async fn watchlist_for_owner_returns_remote_when_no_local_row() {
let owner_id = Uuid::new_v4();
let remote = InMemoryStore::with_remote_watchlist(vec![domain::models::RemoteWatchlistEntry {
ap_id: "https://remote.example/ap/1".into(),
actor_url: "https://remote.example/users/owner".into(),
movie_title: "Remote Movie".into(),
release_year: 2021,
external_metadata_id: None,
poster_url: None,
added_at: chrono::Utc::now(),
}]);
let deps = GetWatchlistForOwnerDeps {
user: InMemoryStore::new(),
watchlist: Arc::new(PanicWatchlistRepository),
remote_watchlist: remote,
};
let view = get_watchlist_for_owner::execute(&deps, owner_id, Some(20), Some(0))
.await
.unwrap();
match view {
WatchlistView::Remote(entries) => {
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].movie_title, "Remote Movie");
}
WatchlistView::Local(_) => panic!("expected Remote"),
}
}
#[tokio::test]
async fn watchlist_for_owner_local_arm_validates_limit() {
let b = TestContextBuilder::new();
setup_user(&b, "badlimit@test.com", "badlimituser").await;
let email = Email::new("badlimit@test.com".into()).unwrap();
let user = b.user_repo.find_by_email(&email).await.unwrap().unwrap();
let uid = user.id().value();
let deps = GetWatchlistForOwnerDeps {
user: b.user_repo.clone(),
watchlist: InMemoryStore::new(),
remote_watchlist: Arc::new(PanicRemoteWatchlistRepository),
};
let err = match get_watchlist_for_owner::execute(&deps, uid, Some(0), Some(0)).await {
Err(e) => e,
Ok(_) => panic!("expected Err(ValidationError) for limit=0 on the local arm, got Ok"),
};
assert!(matches!(
err,
domain::errors::DomainError::ValidationError(_)
));
}
#[tokio::test]
async fn watchlist_for_owner_remote_arm_ignores_invalid_limit() {
let owner_id = Uuid::new_v4();
let remote = InMemoryStore::new();
let deps = GetWatchlistForOwnerDeps {
user: InMemoryStore::new(),
watchlist: Arc::new(PanicWatchlistRepository),
remote_watchlist: remote,
};
let view = get_watchlist_for_owner::execute(&deps, owner_id, Some(0), Some(0))
.await
.unwrap();
assert!(matches!(view, WatchlistView::Remote(_)), "expected Remote");
}

View File

@@ -0,0 +1,58 @@
use std::sync::Arc;
use domain::models::WatchlistEntry;
use domain::ports::WatchlistRepository;
use domain::testing::InMemoryStore;
use domain::value_objects::{MovieId, UserId};
use uuid::Uuid;
use crate::support::TestContextBuilder;
use application::watchlist::{deps::IsOnWatchlistDeps, is_on, queries::IsOnWatchlistQuery};
#[tokio::test]
async fn returns_true_when_present() {
let watchlist = InMemoryStore::new();
let uid = Uuid::new_v4();
let mid = Uuid::new_v4();
watchlist
.add(&WatchlistEntry::new(
UserId::from_uuid(uid),
MovieId::from_uuid(mid),
))
.await
.unwrap();
let deps = IsOnWatchlistDeps {
watchlist: Arc::clone(&watchlist) as _,
};
let result = is_on::execute(
&deps,
IsOnWatchlistQuery {
user_id: uid,
movie_id: mid,
},
)
.await
.unwrap();
assert!(result);
}
#[tokio::test]
async fn returns_false_when_absent() {
let b = TestContextBuilder::new();
let deps = IsOnWatchlistDeps {
watchlist: b.watchlist_repo.clone(),
};
let result = is_on::execute(
&deps,
IsOnWatchlistQuery {
user_id: Uuid::new_v4(),
movie_id: Uuid::new_v4(),
},
)
.await
.unwrap();
assert!(!result);
}

View File

@@ -0,0 +1,69 @@
use std::sync::Arc;
use domain::events::DomainEvent;
use domain::models::WatchlistEntry;
use domain::ports::WatchlistRepository;
use domain::testing::{InMemoryStore, NoopEventPublisher};
use domain::value_objects::{MovieId, UserId};
use uuid::Uuid;
use crate::support::TestContextBuilder;
use application::watchlist::{
commands::RemoveFromWatchlistCommand, deps::RemoveFromWatchlistDeps, remove,
};
#[tokio::test]
async fn removes_entry_and_emits_event() {
let watchlist = InMemoryStore::new();
let events = NoopEventPublisher::new();
let uid = Uuid::new_v4();
let mid = Uuid::new_v4();
watchlist
.add(&WatchlistEntry::new(
UserId::from_uuid(uid),
MovieId::from_uuid(mid),
))
.await
.unwrap();
let deps = RemoveFromWatchlistDeps {
watchlist: Arc::clone(&watchlist) as _,
event_publisher: Arc::clone(&events) as _,
};
remove::execute(
&deps,
RemoveFromWatchlistCommand {
user_id: uid,
movie_id: mid,
},
)
.await
.unwrap();
assert_eq!(watchlist.watchlist_count(), 0);
let published = events.published();
assert!(
published
.iter()
.any(|e| matches!(e, DomainEvent::WatchlistEntryRemoved { .. }))
);
}
#[tokio::test]
async fn fails_when_not_on_watchlist() {
let b = TestContextBuilder::new();
let deps = RemoveFromWatchlistDeps {
watchlist: b.watchlist_repo.clone(),
event_publisher: b.event_publisher.clone(),
};
let result = remove::execute(
&deps,
RemoveFromWatchlistCommand {
user_id: Uuid::new_v4(),
movie_id: Uuid::new_v4(),
},
)
.await;
assert!(result.is_err());
}