refactor(goals): scoped Arc deps instead of AppContext

This commit is contained in:
2026-06-11 21:36:11 +02:00
parent 2b295e10ba
commit f006ba00a8
12 changed files with 143 additions and 110 deletions

View File

@@ -10,15 +10,11 @@ use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn creates_goal_and_returns_progress() {
let goals = InMemoryGoalRepository::new();
goals.set_review_count(Uuid::nil(), 2025, 5);
let events = NoopEventPublisher::new();
let ctx = TestContextBuilder::new()
.with_goal(Arc::clone(&goals) as _)
.with_event_publisher(Arc::clone(&events) as _)
.build();
let result = create::execute(
&ctx,
Arc::clone(&goals) as _,
Arc::clone(&events) as _,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -30,19 +26,40 @@ async fn creates_goal_and_returns_progress() {
assert_eq!(result.goal.year(), 2025);
assert_eq!(result.goal.target_count(), 50);
assert_eq!(result.current_count, 0);
assert_eq!(goals.count(), 1);
}
#[tokio::test]
async fn creates_goal_with_review_count() {
let goals = InMemoryGoalRepository::new();
goals.set_review_count(Uuid::nil(), 2025, 5);
let events = NoopEventPublisher::new();
let result = create::execute(
Arc::clone(&goals) as _,
Arc::clone(&events) as _,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
target_count: 50,
},
)
.await
.unwrap();
assert_eq!(result.current_count, 5);
assert_eq!(goals.count(), 1);
}
#[tokio::test]
async fn emits_goal_created_event() {
let b = TestContextBuilder::new();
let events = NoopEventPublisher::new();
let ctx = TestContextBuilder::new()
.with_event_publisher(Arc::clone(&events) as _)
.build();
create::execute(
&ctx,
b.goal_repo.clone(),
Arc::clone(&events) as _,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -62,17 +79,20 @@ async fn emits_goal_created_event() {
#[tokio::test]
async fn rejects_duplicate_year() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let cmd = CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
target_count: 10,
};
create::execute(&ctx, cmd).await.unwrap();
create::execute(b.goal_repo.clone(), b.event_publisher.clone(), cmd)
.await
.unwrap();
let result = create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -86,9 +106,10 @@ async fn rejects_duplicate_year() {
#[tokio::test]
async fn rejects_year_before_2020() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let result = create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2019,
@@ -102,9 +123,10 @@ async fn rejects_year_before_2020() {
#[tokio::test]
async fn rejects_zero_target() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let result = create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -13,13 +13,10 @@ use crate::test_helpers::TestContextBuilder;
async fn deletes_existing_goal() {
let goals = InMemoryGoalRepository::new();
let events = NoopEventPublisher::new();
let ctx = TestContextBuilder::new()
.with_goal(Arc::clone(&goals) as _)
.with_event_publisher(Arc::clone(&events) as _)
.build();
create::execute(
&ctx,
Arc::clone(&goals) as _,
Arc::clone(&events) as _,
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -31,7 +28,8 @@ async fn deletes_existing_goal() {
assert_eq!(goals.count(), 1);
delete::execute(
&ctx,
Arc::clone(&goals) as _,
Arc::clone(&events) as _,
DeleteGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -45,9 +43,10 @@ async fn deletes_existing_goal() {
#[tokio::test]
async fn fails_when_not_found() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let result = delete::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
DeleteGoalCommand {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -5,9 +5,10 @@ use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn returns_goal_when_exists() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -18,7 +19,7 @@ async fn returns_goal_when_exists() {
.unwrap();
let result = get::execute(
&ctx,
b.goal_repo.clone(),
GetGoalQuery {
user_id: Uuid::nil(),
year: 2025,
@@ -33,9 +34,9 @@ async fn returns_goal_when_exists() {
#[tokio::test]
async fn returns_none_when_missing() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let result = get::execute(
&ctx,
b.goal_repo.clone(),
GetGoalQuery {
user_id: Uuid::nil(),
year: 2025,

View File

@@ -5,9 +5,9 @@ use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn returns_empty_when_no_goals() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let result = list::execute(
&ctx,
b.goal_repo.clone(),
ListGoalsQuery {
user_id: Uuid::nil(),
},
@@ -20,10 +20,11 @@ async fn returns_empty_when_no_goals() {
#[tokio::test]
async fn returns_all_goals_for_user() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
for year in [2023, 2024, 2025] {
create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year,
@@ -35,7 +36,7 @@ async fn returns_all_goals_for_user() {
}
let result = list::execute(
&ctx,
b.goal_repo.clone(),
ListGoalsQuery {
user_id: Uuid::nil(),
},

View File

@@ -8,9 +8,10 @@ use crate::test_helpers::TestContextBuilder;
#[tokio::test]
async fn updates_target_count() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -21,7 +22,8 @@ async fn updates_target_count() {
.unwrap();
let result = update::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
UpdateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -36,9 +38,10 @@ async fn updates_target_count() {
#[tokio::test]
async fn fails_when_goal_not_found() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
let result = update::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
UpdateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -52,9 +55,10 @@ async fn fails_when_goal_not_found() {
#[tokio::test]
async fn rejects_zero_target() {
let ctx = TestContextBuilder::new().build();
let b = TestContextBuilder::new();
create::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
CreateGoalCommand {
user_id: Uuid::nil(),
year: 2025,
@@ -65,7 +69,8 @@ async fn rejects_zero_target() {
.unwrap();
let result = update::execute(
&ctx,
b.goal_repo.clone(),
b.event_publisher.clone(),
UpdateGoalCommand {
user_id: Uuid::nil(),
year: 2025,