use crate::fetched_transcript::FetchedTranscript;
use crate::models::{FetchedTranscriptSnippet, TranslationLanguage};
use crate::transcript::Transcript;
use crate::transcript_list::TranscriptList;
use reqwest::Client;
use std::collections::HashMap;
use tokio::test;
fn create_client() -> Client {
Client::builder()
.user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
.build()
.unwrap()
}
#[allow(dead_code)]
const MOCK_VIDEO_ID: &str = "arj7oStGLkU";
#[allow(dead_code)]
fn create_mock_transcript() -> FetchedTranscript {
FetchedTranscript {
snippets: vec![
FetchedTranscriptSnippet {
text: "Hello, this is a test transcript.".to_string(),
start: 0.0,
duration: 2.5,
},
FetchedTranscriptSnippet {
text: "This is mocked data for CI testing.".to_string(),
start: 2.5,
duration: 3.0,
},
],
video_id: MOCK_VIDEO_ID.to_string(),
language: "English".to_string(),
language_code: "en".to_string(),
is_generated: false,
}
}
#[allow(dead_code)]
fn create_mock_transcript_list() -> TranscriptList {
let client = create_client();
let translation_languages = vec![
TranslationLanguage {
language: "English".to_string(),
language_code: "en".to_string(),
},
TranslationLanguage {
language: "French".to_string(),
language_code: "fr".to_string(),
},
];
let mut manually_created_transcripts = HashMap::new();
let english_transcript = Transcript::new(
client.clone(),
MOCK_VIDEO_ID.to_string(),
"https://mock.youtube.com/transcript/en".to_string(),
"English".to_string(),
"en".to_string(),
false,
translation_languages.clone(),
);
manually_created_transcripts.insert("en".to_string(), english_transcript);
TranscriptList::new(
MOCK_VIDEO_ID.to_string(),
manually_created_transcripts,
HashMap::new(),
translation_languages,
)
}
#[test]
async fn test_list_transcripts() {
let transcript_list = create_mock_transcript_list();
assert_eq!(transcript_list.video_id, MOCK_VIDEO_ID);
let mut found_transcripts = false;
for transcript in &transcript_list {
found_transcripts = true;
assert_eq!(transcript.video_id, MOCK_VIDEO_ID);
assert!(!transcript.language_code.is_empty());
assert!(!transcript.language.is_empty());
}
assert!(found_transcripts, "No transcripts found in the list");
}
#[test]
async fn test_find_transcript() {
let transcript_list = create_mock_transcript_list();
let transcript = transcript_list.find_transcript(&["en"]);
assert!(transcript.is_ok(), "Failed to find English transcript");
let transcript = transcript_list.find_transcript(&["non-existent", "en"]);
assert!(
transcript.is_ok(),
"Failed to find transcript with fallback languages"
);
let transcript = transcript_list.find_transcript(&["non-existent"]);
assert!(transcript.is_err(), "Found a non-existent transcript");
}
#[test]
async fn test_fetch_transcript() {
let transcript = create_mock_transcript();
assert_eq!(transcript.video_id, MOCK_VIDEO_ID);
assert!(
!transcript.snippets.is_empty(),
"Transcript has no snippets"
);
for snippet in &transcript.snippets {
assert!(!snippet.text.is_empty(), "Snippet has empty text");
assert!(snippet.start >= 0.0, "Snippet has negative start time");
assert!(
snippet.duration > 0.0,
"Snippet has zero or negative duration"
);
}
}
#[test]
async fn test_translate_transcript() {
let transcript_list = create_mock_transcript_list();
let found_translatable = transcript_list
.manually_created_transcripts
.values()
.any(|t| !t.translation_languages.is_empty());
assert!(found_translatable, "No translatable transcripts found");
}
#[test]
async fn test_preserve_formatting() {
let with_formatting = create_mock_transcript();
let without_formatting = create_mock_transcript();
assert_eq!(with_formatting.video_id, without_formatting.video_id);
assert_eq!(with_formatting.language, without_formatting.language);
assert_eq!(
with_formatting.language_code,
without_formatting.language_code
);
}
#[test]
async fn test_transcript_formatter() {
let transcript_list = create_mock_transcript_list();
let transcript_result = transcript_list.find_transcript(&["en"]);
assert!(
transcript_result.is_ok(),
"Failed to find English transcript"
);
let transcript = transcript_result.unwrap();
let transcript_str = format!("{}", transcript);
assert!(!transcript_str.is_empty());
assert!(transcript_str.contains("en"));
}
#[test]
async fn test_transcriptlist_formatter() {
let transcript_list = create_mock_transcript_list();
let transcript_list_str = format!("{}", transcript_list);
assert!(!transcript_list_str.is_empty());
assert!(transcript_list_str.contains("Available transcripts"));
}