@@ -93,7 +93,7 @@ impl RelevantTweetRepository {
9393 }
9494
9595 pub async fn get_newest_tweet_id ( & self ) -> Result < Option < String > , DbError > {
96- let row = sqlx:: query ( "SELECT id FROM relevant_tweets ORDER BY created_at DESC LIMIT 1" )
96+ let row = sqlx:: query ( "SELECT id FROM relevant_tweets WHERE created_at >= NOW() - INTERVAL '7 days' ORDER BY created_at DESC LIMIT 1" )
9797 . fetch_optional ( & self . pool )
9898 . await ?;
9999
@@ -244,7 +244,7 @@ mod tests {
244244 utils:: test_db:: reset_database,
245245 Config ,
246246 } ;
247- use chrono:: Utc ;
247+ use chrono:: { Duration , Utc } ;
248248 use sqlx:: PgPool ;
249249
250250 // --- Helpers to create dummy data ---
@@ -291,7 +291,6 @@ mod tests {
291291 }
292292
293293 // --- Tests ---
294-
295294 #[ tokio:: test]
296295 async fn test_upsert_and_find_by_id ( ) {
297296 let ( repo, author_repo) = setup_test_repository ( ) . await ;
@@ -347,4 +346,39 @@ mod tests {
347346 assert ! ( existing. contains( "t2" ) ) ;
348347 assert ! ( !existing. contains( "t3" ) ) ;
349348 }
349+
350+ #[ tokio:: test]
351+ async fn test_get_newest_tweet_id_returns_none_when_no_recent_tweets ( ) {
352+ let ( repo, author_repo) = setup_test_repository ( ) . await ;
353+ let author_id = "author_old" ;
354+ seed_author ( & author_repo, author_id, "stale_user" ) . await ;
355+
356+ let mut old_tweet = create_payload ( "tweet_old" , author_id, "stale tweet" ) ;
357+ old_tweet. created_at = Utc :: now ( ) - Duration :: days ( 8 ) ;
358+ repo. upsert_many ( & vec ! [ old_tweet] ) . await . unwrap ( ) ;
359+
360+ let newest_tweet_id = repo. get_newest_tweet_id ( ) . await . unwrap ( ) ;
361+ assert_eq ! (
362+ newest_tweet_id, None ,
363+ "Should return None when no tweet is within the last 7 days"
364+ ) ;
365+ }
366+
367+ #[ tokio:: test]
368+ async fn test_get_newest_tweet_id_returns_some_when_recent_tweet_exists ( ) {
369+ let ( repo, author_repo) = setup_test_repository ( ) . await ;
370+ let author_id = "author_recent" ;
371+ seed_author ( & author_repo, author_id, "active_user" ) . await ;
372+
373+ let mut recent_tweet = create_payload ( "tweet_recent" , author_id, "recent tweet" ) ;
374+ recent_tweet. created_at = Utc :: now ( ) - Duration :: days ( 2 ) ;
375+ repo. upsert_many ( & vec ! [ recent_tweet] ) . await . unwrap ( ) ;
376+
377+ let newest_tweet_id = repo. get_newest_tweet_id ( ) . await . unwrap ( ) ;
378+ assert_eq ! (
379+ newest_tweet_id,
380+ Some ( "tweet_recent" . to_string( ) ) ,
381+ "Should return the newest tweet id when at least one tweet is within the last 7 days"
382+ ) ;
383+ }
350384}
0 commit comments