use revw::app::{App, FormatMode};
#[test]
fn test_card_vertical_scroll() {
let json_input = r#"{
"outside": [
{
"name": "test",
"context": "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8",
"url": "http://example.com",
"percentage": 50
}
],
"inside": []
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
assert_eq!(app.relf_entries.len(), 1);
assert_eq!(app.hscroll, 0);
app.hscroll = 1;
assert_eq!(app.hscroll, 1);
app.hscroll = 0;
assert_eq!(app.hscroll, 0);
}
#[test]
fn test_card_scroll_reset_on_navigation() {
let json_input = r#"{
"outside": [
{
"name": "first",
"context": "First entry with long context",
"url": "http://example.com",
"percentage": 50
},
{
"name": "second",
"context": "Second entry with long context",
"url": "http://example2.com",
"percentage": 75
}
],
"inside": []
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
assert_eq!(app.relf_entries.len(), 2);
app.selected_entry_index = 0;
app.hscroll = 10;
assert_eq!(app.hscroll, 10);
app.selected_entry_index = 1;
app.hscroll = 0;
assert_eq!(app.hscroll, 0);
}
#[test]
fn test_card_scroll_reset_on_move_up() {
let json_input = r#"{
"outside": [
{
"name": "first",
"context": "First entry",
"url": "http://example.com",
"percentage": 50
},
{
"name": "second",
"context": "Second entry",
"url": "http://example2.com",
"percentage": 75
}
],
"inside": []
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
app.selected_entry_index = 1;
app.hscroll = 15;
assert_eq!(app.hscroll, 15);
app.selected_entry_index = 0;
app.hscroll = 0;
assert_eq!(app.hscroll, 0);
}
#[test]
fn test_multiple_cards_independent_scroll() {
let json_input = r#"{
"outside": [],
"inside": [
{
"date": "2025-01-01",
"context": "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7"
},
{
"date": "2025-01-01",
"context": "Another 1\nAnother 2\nAnother 3\nAnother 4\nAnother 5\nAnother 6"
}
]
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
assert_eq!(app.relf_entries.len(), 2);
app.selected_entry_index = 0;
app.hscroll = 0;
app.hscroll = 2;
assert_eq!(app.hscroll, 2);
app.selected_entry_index = 1;
app.hscroll = 0; assert_eq!(app.hscroll, 0);
app.hscroll = 1;
assert_eq!(app.hscroll, 1);
}
#[test]
fn test_hscroll_bounds() {
let json_input = r#"{
"outside": [
{
"name": "test",
"context": "Short",
"url": "http://example.com",
"percentage": 50
}
],
"inside": []
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
assert_eq!(app.hscroll, 0);
app.relf_hscroll_by(-10);
assert_eq!(app.hscroll, 0);
}
#[test]
fn test_card_full_height_display() {
let json_input = r#"{
"outside": [
{
"name": "test",
"context": "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\nLine 11\nLine 12\nLine 13\nLine 14\nLine 15",
"url": "http://example.com",
"percentage": 50
}
],
"inside": []
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
app.visible_height = 20;
app.max_visible_cards = 5;
assert_eq!(app.relf_entries.len(), 1);
let entry = &app.relf_entries[0];
assert!(entry.context.is_some());
let context = entry.context.as_ref().unwrap();
let lines: Vec<&str> = context.lines().collect();
assert!(lines.len() > 5, "Context should have more than 5 lines");
let expected_visible = (app.visible_height as usize / app.max_visible_cards).saturating_sub(2);
let max_scroll = lines.len().saturating_sub(expected_visible);
let calculated_max = app.relf_max_hscroll() as usize;
assert_eq!(calculated_max, max_scroll, "Max scroll should be calculated correctly");
app.hscroll = 0;
assert_eq!(app.hscroll, 0);
app.hscroll = 3;
assert_eq!(app.hscroll, 3);
app.hscroll = (calculated_max + 10) as u16;
assert!(calculated_max < lines.len(), "Max scroll should be less than total lines");
}
#[test]
fn test_card_scroll_with_newlines() {
let json_input = r#"{
"inside": [
{
"date": "2025-01-01",
"context": "First line\\nSecond line\\nThird line\\nFourth line\\nFifth line\\nSixth line\\nSeventh line"
}
]
}"#;
let mut app = App::new(FormatMode::View);
app.json_input = json_input.to_string();
app.format_mode = FormatMode::View;
app.convert_json();
app.visible_height = 20;
app.max_visible_cards = 5;
assert_eq!(app.relf_entries.len(), 1);
let entry = &app.relf_entries[0];
assert!(entry.context.is_some());
let context = entry.context.as_ref().unwrap();
assert!(context.contains("\\n"), "Context should contain literal \\n from JSON");
let context_with_newlines = context.replace("\\n", "\n");
let lines: Vec<&str> = context_with_newlines.lines().collect();
assert_eq!(lines.len(), 7, "Should have 7 lines after converting \\n to newlines");
app.hscroll = 0;
assert_eq!(app.hscroll, 0);
app.hscroll = 2;
assert_eq!(app.hscroll, 2);
}