|
| 1 | +use crate::dashboard::{DashboardBlob, DashboardTemplate}; |
| 2 | +use askama::Template; |
| 3 | +use scraper::{Html, Selector}; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn should_render_empty_dashboard() { |
| 7 | + let dashboard = DashboardTemplate { |
| 8 | + total_blobs: 0, |
| 9 | + total_size_bytes: 0, |
| 10 | + blobs: vec![], |
| 11 | + }; |
| 12 | + let html = dashboard.render().unwrap(); |
| 13 | + let parsed = Html::parse_document(&html); |
| 14 | + |
| 15 | + assert_has_text(&parsed, "#total-blobs > td > code", "0"); |
| 16 | + assert_has_text(&parsed, "#total-size > td > code", "0"); |
| 17 | + assert!( |
| 18 | + parsed |
| 19 | + .select(&Selector::parse("#blobs").unwrap()) |
| 20 | + .next() |
| 21 | + .is_none(), |
| 22 | + "expected no blobs section" |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +#[test] |
| 27 | +fn should_render_blobs() { |
| 28 | + let dashboard = DashboardTemplate { |
| 29 | + total_blobs: 2, |
| 30 | + total_size_bytes: 300, |
| 31 | + blobs: vec![ |
| 32 | + DashboardBlob { |
| 33 | + hash: "98ac5cfe873a7b42b7c98a1b3fbeff2255e340deb9c80aa9eb0bd0ba3a0d2a99" |
| 34 | + .to_string(), |
| 35 | + uploader: "principal-1".to_string(), |
| 36 | + size: 100, |
| 37 | + inserted_at_ns: 1_700_000_000_000_000_000, |
| 38 | + tags: vec!["ledger".to_string(), "u256".to_string()], |
| 39 | + }, |
| 40 | + DashboardBlob { |
| 41 | + hash: "3d33f9edeae50572a42378d1eaaa29f5149543ec16268797b058156b1b575a04" |
| 42 | + .to_string(), |
| 43 | + uploader: "principal-2".to_string(), |
| 44 | + size: 200, |
| 45 | + inserted_at_ns: 1_700_000_001_000_000_000, |
| 46 | + tags: vec![], |
| 47 | + }, |
| 48 | + ], |
| 49 | + }; |
| 50 | + let html = dashboard.render().unwrap(); |
| 51 | + let parsed = Html::parse_document(&html); |
| 52 | + |
| 53 | + assert_has_text(&parsed, "#total-blobs > td > code", "2"); |
| 54 | + assert_has_text(&parsed, "#total-size > td > code", "300"); |
| 55 | + |
| 56 | + assert_row_contains( |
| 57 | + &parsed, |
| 58 | + 1, |
| 59 | + "98ac5cfe873a7b42b7c98a1b3fbeff2255e340deb9c80aa9eb0bd0ba3a0d2a99", |
| 60 | + "principal-1", |
| 61 | + "100", |
| 62 | + &["ledger", "u256"], |
| 63 | + ); |
| 64 | + assert_row_contains( |
| 65 | + &parsed, |
| 66 | + 2, |
| 67 | + "3d33f9edeae50572a42378d1eaaa29f5149543ec16268797b058156b1b575a04", |
| 68 | + "principal-2", |
| 69 | + "200", |
| 70 | + &[], |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +fn assert_has_text(html: &Html, selector: &str, expected: &str) { |
| 75 | + let sel = Selector::parse(selector).unwrap(); |
| 76 | + let element = html |
| 77 | + .select(&sel) |
| 78 | + .next() |
| 79 | + .unwrap_or_else(|| panic!("selector '{selector}' not found")); |
| 80 | + let text: String = element.text().collect(); |
| 81 | + assert_eq!(text, expected, "selector '{selector}'"); |
| 82 | +} |
| 83 | + |
| 84 | +fn assert_row_contains( |
| 85 | + html: &Html, |
| 86 | + row: usize, |
| 87 | + hash: &str, |
| 88 | + uploader: &str, |
| 89 | + size: &str, |
| 90 | + tags: &[&str], |
| 91 | +) { |
| 92 | + let row_sel = |
| 93 | + Selector::parse(&format!("#blobs + table > tbody > tr:nth-child({row})")).unwrap(); |
| 94 | + let row_el = html |
| 95 | + .select(&row_sel) |
| 96 | + .next() |
| 97 | + .unwrap_or_else(|| panic!("row {row} not found")); |
| 98 | + let cells: Vec<String> = row_el |
| 99 | + .select(&Selector::parse("td").unwrap()) |
| 100 | + .map(|td| td.text().collect::<String>()) |
| 101 | + .collect(); |
| 102 | + assert_eq!(cells[0], hash, "hash mismatch in row {row}"); |
| 103 | + assert_eq!(cells[1], uploader, "uploader mismatch in row {row}"); |
| 104 | + assert_eq!(cells[2], size, "size mismatch in row {row}"); |
| 105 | + // cells[3] is the formatted timestamp, just check it's not empty |
| 106 | + assert!(!cells[3].is_empty(), "timestamp missing in row {row}"); |
| 107 | + let tag_sel = Selector::parse(".tag").unwrap(); |
| 108 | + let actual_tags: Vec<String> = row_el |
| 109 | + .select(&tag_sel) |
| 110 | + .map(|el| el.text().collect::<String>()) |
| 111 | + .collect(); |
| 112 | + assert_eq!( |
| 113 | + actual_tags, |
| 114 | + tags.iter().map(|s| s.to_string()).collect::<Vec<_>>(), |
| 115 | + "tags mismatch in row {row}" |
| 116 | + ); |
| 117 | +} |
0 commit comments