991 lines
32 KiB
Rust
991 lines
32 KiB
Rust
use rundown_converter::{detect, export, model::*, pipeline, reader, time};
|
|
use std::{collections::BTreeMap, io::Write, sync::atomic::AtomicBool};
|
|
|
|
fn table(rows: &[&[&str]]) -> Table {
|
|
Table {
|
|
name: "Show".into(),
|
|
rows: rows
|
|
.iter()
|
|
.map(|r| {
|
|
r.iter()
|
|
.map(|v| Cell {
|
|
text: (*v).into(),
|
|
..Cell::default()
|
|
})
|
|
.collect()
|
|
})
|
|
.collect(),
|
|
merges: vec![],
|
|
}
|
|
}
|
|
fn analyze(t: &Table, p: &Profile) -> Analysis {
|
|
pipeline::analyze(t, p, &AtomicBool::new(false), |_, _| {})
|
|
}
|
|
fn simple() -> Table {
|
|
table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["11:45", "15 min", "Morning"],
|
|
&["13:30", "20 min", "Afternoon"],
|
|
])
|
|
}
|
|
#[test]
|
|
fn independent_anchors_and_implicit_lunch_gap() {
|
|
let a = analyze(&simple(), &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert!(r.entries.iter().all(|entry| !entry.inferred_duration));
|
|
assert!(r.entries.iter().all(|entry| entry.inferred_end));
|
|
assert_eq!(r.gaps.len(), 1);
|
|
assert_eq!(r.gaps[0].start_ms, Some(43_200_000));
|
|
assert_eq!(r.gaps[0].end_ms, Some(48_600_000));
|
|
assert!(!r.gaps[0].display_timer);
|
|
let json = export::ontime(&r).unwrap();
|
|
let entries = json["rundowns"]["default"]["entries"].as_object().unwrap();
|
|
assert_eq!(entries.len(), 2);
|
|
assert!(
|
|
entries
|
|
.values()
|
|
.all(|e| e["linkStart"] == false && e["endAction"] == "none")
|
|
);
|
|
assert!(entries.values().any(|e| e["timeStart"] == 48_600_000));
|
|
}
|
|
|
|
#[test]
|
|
fn ir_records_inferred_start_end_and_duration_provenance() {
|
|
let t = table(&[
|
|
&["Start", "End", "Duration", "Title"],
|
|
&["09:00", "", "10 min", "Opening"],
|
|
&["", "09:30", "", "Continuation"],
|
|
]);
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let entries = a.rundown.unwrap().entries;
|
|
assert!(entries[0].explicit_start);
|
|
assert!(entries[0].inferred_end);
|
|
assert!(!entries[0].inferred_duration);
|
|
assert!(!entries[1].explicit_start);
|
|
assert!(!entries[1].inferred_end);
|
|
assert!(entries[1].inferred_duration);
|
|
}
|
|
#[test]
|
|
fn named_untimed_break_occupies_time_without_timer() {
|
|
let t = table(&[
|
|
&["Heure", "Durée", "Déroulement"],
|
|
&["11h45", "15 min", "Talk"],
|
|
&["", "", "Déjeuner"],
|
|
&["13h30", "30 min", "Restart"],
|
|
]);
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries.len(), 2);
|
|
assert_eq!(r.gaps.len(), 1);
|
|
assert_eq!(r.gaps[0].source_rows[0].row, 3);
|
|
}
|
|
#[test]
|
|
fn start_only_rows_do_not_invent_breaks_or_durations() {
|
|
let t = table(&[
|
|
&["Start", "Title"],
|
|
&["11:45", "Talk"],
|
|
&["13:30", "Restart"],
|
|
]);
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.iter().any(|d| d.code == "MISSING_DURATION"));
|
|
assert_eq!(a.completed_stage, 2);
|
|
}
|
|
#[test]
|
|
fn all_cells_must_fit_and_duplicate_timer_columns_are_errors() {
|
|
let mut t = simple();
|
|
t.rows[2][0].text = "after lunch".into();
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(
|
|
a.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "INCONSISTENT_COLUMN_MEANING" && d.location.contains("A3"))
|
|
);
|
|
let t = table(&[
|
|
&["Heure", "Timer", "Timer", "Title"],
|
|
&["09h00", "20 min", "2 min", "A"],
|
|
]);
|
|
assert!(
|
|
analyze(&t, &Profile::default())
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "AMBIGUOUS_COLUMN")
|
|
);
|
|
let p = Profile {
|
|
columns: BTreeMap::from([("duration".into(), "B".into())]),
|
|
..Profile::default()
|
|
};
|
|
assert!(analyze(&t, &p).diagnostics.is_empty());
|
|
}
|
|
#[test]
|
|
fn duration_units_inferred_from_other_rows_and_not_per_cell() {
|
|
let t = table(&[
|
|
&["Start", "End", "Duration", "Title"],
|
|
&["09:00", "09:30", "00:30", "A"],
|
|
&["09:30", "10:30", "1h", "B"],
|
|
]);
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
assert_eq!(a.mapping.unwrap().duration_unit.as_deref(), Some("hh:mm"));
|
|
let t = table(&[&["Start", "Duration", "Title"], &["09:00", "15", "A"]]);
|
|
assert!(!analyze(&t, &Profile::default()).diagnostics.is_empty());
|
|
let p = Profile {
|
|
duration_unit: Some("minutes".into()),
|
|
..Profile::default()
|
|
};
|
|
assert!(analyze(&t, &p).diagnostics.is_empty());
|
|
}
|
|
#[test]
|
|
fn infer_roles_from_data_without_keyword_headers() {
|
|
let t = table(&[
|
|
&["When?", "Length?", "Title"],
|
|
&["09h00", "10 min", "A"],
|
|
&["10h00", "20 min", "B"],
|
|
]);
|
|
let p = Profile {
|
|
header_row: Some(1),
|
|
..Profile::default()
|
|
};
|
|
let a = analyze(&t, &p);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
assert_eq!(a.mapping.unwrap().duration, Some(1));
|
|
}
|
|
#[test]
|
|
fn independent_validation_rejects_conflicts_backward_and_negative_times() {
|
|
for (rows, code) in [
|
|
(
|
|
vec![
|
|
vec!["Start", "End", "Duration", "Title"],
|
|
vec!["09:00", "09:10", "20 min", "A"],
|
|
],
|
|
"CONFLICTING_TIMES",
|
|
),
|
|
(
|
|
vec![
|
|
vec!["Start", "Duration", "Title"],
|
|
vec!["10:00", "10 min", "A"],
|
|
vec!["09:00", "10 min", "B"],
|
|
],
|
|
"BACKWARD_TIME",
|
|
),
|
|
(
|
|
vec![vec!["Start", "End", "Title"], vec!["23:00", "01:00", "A"]],
|
|
"NEGATIVE_DURATION",
|
|
),
|
|
(
|
|
vec![
|
|
vec!["Start", "Duration", "Title"],
|
|
vec!["10:00", "-2 min", "A"],
|
|
],
|
|
"NEGATIVE_DURATION",
|
|
),
|
|
] {
|
|
let refs: Vec<&[&str]> = rows.iter().map(Vec::as_slice).collect();
|
|
let a = analyze(&table(&refs), &Profile::default());
|
|
assert!(
|
|
a.diagnostics.iter().any(|d| d.code == code),
|
|
"{code}: {:?}",
|
|
a.diagnostics
|
|
);
|
|
}
|
|
}
|
|
#[test]
|
|
fn merged_shared_duration_is_one_timer_with_all_source_rows() {
|
|
let mut t = table(&[
|
|
&["Heure", "Durée", "Title"],
|
|
&["09h00-09h20", "20 min", "First speaker"],
|
|
&["", "", "Second speaker"],
|
|
&["09h20", "5 min", "Next"],
|
|
]);
|
|
t.merges = vec![
|
|
Merge {
|
|
first_row: 1,
|
|
last_row: 2,
|
|
first_col: 0,
|
|
last_col: 0,
|
|
},
|
|
Merge {
|
|
first_row: 1,
|
|
last_row: 2,
|
|
first_col: 1,
|
|
last_col: 1,
|
|
},
|
|
];
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries.len(), 2);
|
|
assert_eq!(r.entries[0].source_rows.len(), 2);
|
|
}
|
|
#[test]
|
|
fn merged_window_with_individual_durations_leaves_slack() {
|
|
let mut t = table(&[
|
|
&["Heure", "Durée", "Title"],
|
|
&["09h00-09h10", "3 min", "A"],
|
|
&["", "3 min", "B"],
|
|
&["", "3 min", "C"],
|
|
&["09h10", "5 min", "Next"],
|
|
]);
|
|
t.merges = vec![Merge {
|
|
first_row: 1,
|
|
last_row: 3,
|
|
first_col: 0,
|
|
last_col: 0,
|
|
}];
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries.len(), 4);
|
|
assert_eq!(r.entries[1].start_ms, 32_580_000);
|
|
assert_eq!(r.gaps.len(), 1);
|
|
assert_eq!(
|
|
r.gaps[0].end_ms.unwrap() - r.gaps[0].start_ms.unwrap(),
|
|
60_000
|
|
);
|
|
t.rows[3][1].text = "5 min".into();
|
|
assert!(
|
|
analyze(&t, &Profile::default())
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "GROUP_OVERFLOW")
|
|
);
|
|
}
|
|
#[test]
|
|
fn notes_and_markers_require_explicit_choices() {
|
|
let t = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["09:00", "10 min", "A"],
|
|
&["", "", "Stage movement"],
|
|
&["09:10", "", "Thanks"],
|
|
]);
|
|
let unresolved = analyze(&t, &Profile::default());
|
|
let error = unresolved
|
|
.diagnostics
|
|
.iter()
|
|
.find(|d| d.code == "UNTIMED_ROW")
|
|
.unwrap();
|
|
assert!(
|
|
error
|
|
.message
|
|
.contains("Row preview:\nC (Title): Stage movement")
|
|
);
|
|
assert!(!error.message.contains("Thanks"));
|
|
let p = Profile {
|
|
untimed_as_notes: true,
|
|
row_kinds: BTreeMap::from([(4, "marker".into())]),
|
|
..Profile::default()
|
|
};
|
|
let a = analyze(&t, &p);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries[0].source_rows.len(), 2);
|
|
assert_eq!(r.entries[1].kind, "marker");
|
|
assert_eq!(
|
|
export::ontime(&r).unwrap()["rundowns"]["default"]["order"]
|
|
.as_array()
|
|
.unwrap()
|
|
.len(),
|
|
1
|
|
);
|
|
}
|
|
#[test]
|
|
fn explicit_corrections_preserve_originals() {
|
|
let t = table(&[
|
|
&["Start", "End", "Duration", "Title"],
|
|
&["09:00", "10:00", "TBC", "A"],
|
|
]);
|
|
let p = Profile {
|
|
titles: BTreeMap::from([(2, "Corrected title".into())]),
|
|
starts: BTreeMap::from([(2, "09:15".into())]),
|
|
ends: BTreeMap::from([(2, "09:20".into())]),
|
|
durations: BTreeMap::from([(2, "5 min".into())]),
|
|
..Profile::default()
|
|
};
|
|
let a = analyze(&t, &p);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries[0].start_ms, 33_300_000);
|
|
assert_eq!(r.entries[0].end_ms, 33_600_000);
|
|
assert_eq!(r.entries[0].duration_ms, 300_000);
|
|
assert_eq!(r.entries[0].title, "Corrected title");
|
|
assert_eq!(r.entries[0].source_rows[0].cells["A"].text, "09:00");
|
|
assert_eq!(r.entries[0].source_rows[0].cells["B"].text, "10:00");
|
|
assert_eq!(r.entries[0].source_rows[0].cells["C"].text, "TBC");
|
|
assert_eq!(r.entries[0].source_rows[0].cells["D"].text, "A");
|
|
|
|
for (profile, code) in [
|
|
(
|
|
Profile {
|
|
starts: BTreeMap::from([(2, "morning".into())]),
|
|
durations: BTreeMap::from([(2, "5 min".into())]),
|
|
..Profile::default()
|
|
},
|
|
"INVALID_START_OVERRIDE",
|
|
),
|
|
(
|
|
Profile {
|
|
ends: BTreeMap::from([(2, "later".into())]),
|
|
durations: BTreeMap::from([(2, "5 min".into())]),
|
|
..Profile::default()
|
|
},
|
|
"INVALID_END_OVERRIDE",
|
|
),
|
|
] {
|
|
assert!(
|
|
analyze(&t, &profile)
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == code)
|
|
);
|
|
}
|
|
|
|
let overlap = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["09:00", "1 h", "Opening"],
|
|
&["09:30", "30 min", "Next"],
|
|
]);
|
|
assert!(
|
|
analyze(&overlap, &Profile::default())
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "OVERLAP")
|
|
);
|
|
let corrected = analyze(
|
|
&overlap,
|
|
&Profile {
|
|
starts: BTreeMap::from([(3, "10:00".into())]),
|
|
..Profile::default()
|
|
},
|
|
);
|
|
assert!(
|
|
corrected.diagnostics.is_empty(),
|
|
"{:?}",
|
|
corrected.diagnostics
|
|
);
|
|
assert_eq!(corrected.rundown.unwrap().entries[1].start_ms, 36_000_000);
|
|
|
|
let missing_title = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["09:00", "5 min", "Opening"],
|
|
&["09:05", "5 min", ""],
|
|
]);
|
|
assert!(
|
|
analyze(&missing_title, &Profile::default())
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "MISSING_TITLE")
|
|
);
|
|
let titled = analyze(
|
|
&missing_title,
|
|
&Profile {
|
|
titles: BTreeMap::from([(3, "Closing".into())]),
|
|
..Profile::default()
|
|
},
|
|
);
|
|
assert!(titled.diagnostics.is_empty(), "{:?}", titled.diagnostics);
|
|
assert_eq!(titled.rundown.unwrap().entries[1].title, "Closing");
|
|
}
|
|
#[test]
|
|
fn atomic_publication_preserves_source_and_existing_output() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let input = dir.path().join("source.csv");
|
|
std::fs::write(&input, "source").unwrap();
|
|
let output = dir.path().join("out.json");
|
|
let a = analyze(&simple(), &Profile::default());
|
|
let cancel = AtomicBool::new(false);
|
|
export::write(&a, &input, &output, false, false, &cancel).unwrap();
|
|
let old = std::fs::read(&output).unwrap();
|
|
assert!(export::write(&a, &input, &output, true, false, &cancel).is_err());
|
|
assert_eq!(std::fs::read(&output).unwrap(), old);
|
|
assert!(export::write(&a, &input, &input, false, true, &cancel).is_err());
|
|
assert_eq!(std::fs::read_to_string(&input).unwrap(), "source");
|
|
assert!(export::write(&a, &input, &output, true, true, &AtomicBool::new(true)).is_err());
|
|
assert_eq!(std::fs::read(&output).unwrap(), old);
|
|
export::write(&a, &input, &output, true, true, &cancel).unwrap();
|
|
assert_eq!(
|
|
serde_json::from_slice::<serde_json::Value>(&std::fs::read(output).unwrap()).unwrap()["schema_version"],
|
|
1
|
|
);
|
|
}
|
|
#[test]
|
|
fn invalid_or_cancelled_analysis_never_exports() {
|
|
let a = pipeline::analyze(
|
|
&simple(),
|
|
&Profile::default(),
|
|
&AtomicBool::new(true),
|
|
|_, _| panic!("cancel first"),
|
|
);
|
|
assert_eq!(a.diagnostics[0].code, "CANCELLED");
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let p = dir.path().join("out.json");
|
|
assert!(export::write(&a, &p, &p, false, false, &AtomicBool::new(false)).is_err());
|
|
assert!(!p.exists());
|
|
let mut r = analyze(&simple(), &Profile::default()).rundown.unwrap();
|
|
r.entries[0].duration_ms = -1;
|
|
assert!(export::ontime(&r).is_err());
|
|
}
|
|
#[test]
|
|
fn clock_units_and_column_roundtrips() {
|
|
for (s, expected) in [
|
|
("9h", 32_400_000),
|
|
("9H30", 34_200_000),
|
|
("2:30 PM", 52_200_000),
|
|
("12:00 AM", 0),
|
|
("09:30:01", 34_201_000),
|
|
] {
|
|
assert_eq!(
|
|
time::clock(&Cell {
|
|
text: s.into(),
|
|
..Cell::default()
|
|
}),
|
|
Some(expected)
|
|
);
|
|
}
|
|
for s in ["24:00", "13:00 PM", "09:90", "9", "tomorrow"] {
|
|
assert!(
|
|
time::clock(&Cell {
|
|
text: s.into(),
|
|
..Cell::default()
|
|
})
|
|
.is_none()
|
|
);
|
|
}
|
|
assert_eq!(
|
|
time::duration(
|
|
&Cell {
|
|
text: "1h10".into(),
|
|
..Cell::default()
|
|
},
|
|
None
|
|
),
|
|
Some(4_200_000)
|
|
);
|
|
assert_eq!(
|
|
time::duration(
|
|
&Cell {
|
|
text: "1,5 min".into(),
|
|
..Cell::default()
|
|
},
|
|
None
|
|
),
|
|
Some(90_000)
|
|
);
|
|
for i in [0, 25, 26, 701, 16383] {
|
|
assert_eq!(column_index(&column_name(i)), Some(i));
|
|
}
|
|
assert_eq!(column_index("A1"), None);
|
|
}
|
|
#[test]
|
|
fn csv_bom_quotes_semicolon_and_unsupported_pdf() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("show.csv");
|
|
std::fs::write(
|
|
&path,
|
|
"\u{feff}Start;Duration;Title\n09:00;2 min;\"A; B\"\n",
|
|
)
|
|
.unwrap();
|
|
let t = reader::read(&path).unwrap();
|
|
assert_eq!(t[0].rows[1][2].text, "A; B");
|
|
assert!(analyze(&t[0], &Profile::default()).diagnostics.is_empty());
|
|
let pdf = dir.path().join("show.pdf");
|
|
std::fs::write(&pdf, "%PDF").unwrap();
|
|
assert!(
|
|
reader::read(&pdf)
|
|
.unwrap_err()
|
|
.to_string()
|
|
.contains("PDF_REQUIRES_REVIEW")
|
|
);
|
|
}
|
|
fn workbook(path: &std::path::Path) {
|
|
let mut z = zip::ZipWriter::new(std::fs::File::create(path).unwrap());
|
|
let parts = [
|
|
(
|
|
"xl/workbook.xml",
|
|
r#"<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Demo" r:id="rId1"/></sheets></workbook>"#,
|
|
),
|
|
(
|
|
"xl/_rels/workbook.xml.rels",
|
|
r#"<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>"#,
|
|
),
|
|
(
|
|
"xl/styles.xml",
|
|
r#"<styleSheet><cellXfs><xf numFmtId="0"/><xf numFmtId="20"/></cellXfs></styleSheet>"#,
|
|
),
|
|
(
|
|
"xl/sharedStrings.xml",
|
|
r#"<sst><si><t>Start</t></si><si><t>Duration</t></si><si><t>Title</t></si></sst>"#,
|
|
),
|
|
(
|
|
"xl/worksheets/sheet1.xml",
|
|
r#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c></row><row r="2"><c r="A2" s="1"><v>0.5</v></c><c r="B2" s="1"><v>0.006944444444444444</v></c><c r="C2" t="inlineStr"><is><t>Talk</t></is></c></row><row r="3"><c r="C3" t="inlineStr"><is><t>Continuation</t></is></c></row></sheetData><mergeCells><mergeCell ref="A2:A3"/><mergeCell ref="B2:B3"/></mergeCells></worksheet>"#,
|
|
),
|
|
];
|
|
for (name, text) in parts {
|
|
z.start_file(name, zip::write::SimpleFileOptions::default())
|
|
.unwrap();
|
|
z.write_all(text.as_bytes()).unwrap();
|
|
}
|
|
z.finish().unwrap();
|
|
}
|
|
#[test]
|
|
fn xlsx_typed_times_and_merges_without_private_fixtures() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("show.xlsx");
|
|
workbook(&path);
|
|
let tables = reader::read(&path).unwrap();
|
|
assert_eq!(tables[0].merges.len(), 2);
|
|
let a = analyze(&tables[0], &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries[0].duration_ms, 600_000);
|
|
assert_eq!(r.entries[0].source_rows.len(), 2);
|
|
}
|
|
#[test]
|
|
fn profile_and_empty_sheet_errors_are_actionable() {
|
|
assert!(detect::infer(&table(&[]), &Profile::default()).is_err());
|
|
for profile in [
|
|
Profile {
|
|
header_row: Some(0),
|
|
..Profile::default()
|
|
},
|
|
Profile {
|
|
columns: BTreeMap::from([("start".into(), "ZZ".into())]),
|
|
..Profile::default()
|
|
},
|
|
Profile {
|
|
duration_unit: Some("guess".into()),
|
|
..Profile::default()
|
|
},
|
|
] {
|
|
assert!(!analyze(&simple(), &profile).diagnostics.is_empty());
|
|
}
|
|
}
|
|
#[test]
|
|
fn formula_without_cached_value_is_not_silently_skipped() {
|
|
let mut t = simple();
|
|
t.rows[1][0].text.clear();
|
|
t.rows[1][0].formula = Some("A3".into());
|
|
assert!(
|
|
analyze(&t, &Profile::default())
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "FORMULA_WITHOUT_VALUE")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn full_width_merged_lunch_is_a_gap_not_a_discarded_heading() {
|
|
let mut t = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["11:00", "60 min", "Morning"],
|
|
&["Lunch", "", ""],
|
|
&["13:30", "10 min", "Restart"],
|
|
]);
|
|
t.merges.push(Merge {
|
|
first_row: 2,
|
|
last_row: 2,
|
|
first_col: 0,
|
|
last_col: 2,
|
|
});
|
|
let map = detect::infer(&t, &Profile::default()).unwrap();
|
|
let gaps = pipeline::identify_gaps(&t, &map, &Profile::default());
|
|
assert_eq!(gaps.len(), 1);
|
|
assert_eq!(gaps[0].title, "Lunch");
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
assert_eq!(a.rundown.unwrap().gaps[0].end_ms, Some(48_600_000));
|
|
}
|
|
#[test]
|
|
fn stage_two_identifies_an_unnamed_gap_before_constructing_events() {
|
|
let t = simple();
|
|
let map = detect::infer(&t, &Profile::default()).unwrap();
|
|
let gaps = pipeline::identify_gaps(&t, &map, &Profile::default());
|
|
assert_eq!(gaps.len(), 1);
|
|
assert_eq!(gaps[0].start_ms, Some(43_200_000));
|
|
}
|
|
#[test]
|
|
fn negative_subhour_duration_does_not_lose_its_sign() {
|
|
assert_eq!(
|
|
time::duration(
|
|
&Cell {
|
|
text: "-00:30".into(),
|
|
..Cell::default()
|
|
},
|
|
Some("hh:mm")
|
|
),
|
|
Some(-1_800_000)
|
|
);
|
|
assert_eq!(
|
|
time::duration(
|
|
&Cell {
|
|
text: "-1h30".into(),
|
|
..Cell::default()
|
|
},
|
|
None
|
|
),
|
|
Some(-5_400_000)
|
|
);
|
|
assert!(
|
|
time::duration(
|
|
&Cell {
|
|
text: "1h-30m".into(),
|
|
..Cell::default()
|
|
},
|
|
None
|
|
)
|
|
.is_none()
|
|
);
|
|
}
|
|
#[test]
|
|
fn stale_profiles_and_invalid_row_classifications_fail_early() {
|
|
let t = simple();
|
|
for p in [
|
|
Profile {
|
|
expected_headers: BTreeMap::from([("A".into(), "Something else".into())]),
|
|
..Profile::default()
|
|
},
|
|
Profile {
|
|
row_kinds: BTreeMap::from([(2, "guessed".into())]),
|
|
..Profile::default()
|
|
},
|
|
] {
|
|
assert_eq!(analyze(&t, &p).completed_stage, 0);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_event_is_not_downgraded_by_general_note_policy() {
|
|
let t = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["09:00", "10 min", "A"],
|
|
&["", "", "Needs a timer"],
|
|
]);
|
|
let p = Profile {
|
|
untimed_as_notes: true,
|
|
row_kinds: BTreeMap::from([(3, "event".into())]),
|
|
..Profile::default()
|
|
};
|
|
assert!(
|
|
analyze(&t, &p)
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "MISSING_DURATION")
|
|
);
|
|
}
|
|
#[test]
|
|
fn explicit_duration_can_replace_a_missing_formula_result() {
|
|
let mut t = simple();
|
|
t.rows[1][1].text.clear();
|
|
t.rows[1][1].formula = Some("B3".into());
|
|
let p = Profile {
|
|
durations: BTreeMap::from([(2, "15 min".into())]),
|
|
..Profile::default()
|
|
};
|
|
assert!(analyze(&t, &p).diagnostics.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn metadata_and_ignored_columns_control_inference_and_ontime_notes() {
|
|
let t = table(&[
|
|
&[
|
|
"Start", "Timer", "Timer", "Title", "Speaker", "Private", "Notes",
|
|
],
|
|
&[
|
|
"09:00",
|
|
"5 min",
|
|
"Operator reference",
|
|
"Opening",
|
|
"Alice",
|
|
"SECRET ONE",
|
|
"Microphone 1",
|
|
],
|
|
&[
|
|
"",
|
|
"",
|
|
"",
|
|
"Stage direction",
|
|
"Bob",
|
|
"SECRET TWO",
|
|
"Move lectern",
|
|
],
|
|
&["", "", "", "", "", "SECRET ONLY ROW", ""],
|
|
&[
|
|
"09:05",
|
|
"5 min",
|
|
"Backup reference",
|
|
"Closing",
|
|
"Carol",
|
|
"SECRET THREE",
|
|
"Microphone 2",
|
|
],
|
|
]);
|
|
let p = Profile {
|
|
column_usage: BTreeMap::from([
|
|
("C".into(), "metadata".into()),
|
|
("E".into(), "metadata".into()),
|
|
("f".into(), "ignore".into()),
|
|
("G".into(), "metadata".into()),
|
|
]),
|
|
row_kinds: BTreeMap::from([(3, "note".into())]),
|
|
..Profile::default()
|
|
};
|
|
let a = analyze(&t, &p);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.mapping.duration, Some(1));
|
|
assert_eq!(r.mapping.column_usage["F"], "ignore");
|
|
assert_eq!(r.entries.len(), 2);
|
|
assert_eq!(r.entries[0].source_rows[0].cells["F"].text, "SECRET ONE");
|
|
assert_eq!(r.entries[0].source_rows[1].cells["F"].text, "SECRET TWO");
|
|
let json = export::ontime(&r).unwrap();
|
|
let note = json["rundowns"]["default"]["entries"][&r.entries[0].id]["note"]
|
|
.as_str()
|
|
.unwrap();
|
|
for text in [
|
|
"Operator reference",
|
|
"Alice",
|
|
"Bob",
|
|
"Microphone 1",
|
|
"Move lectern",
|
|
] {
|
|
assert!(note.contains(text), "Missing metadata {text}");
|
|
}
|
|
assert!(!json.to_string().contains("SECRET"));
|
|
let restored: Rundown = serde_json::from_str(&serde_json::to_string(&r).unwrap()).unwrap();
|
|
assert!(
|
|
!export::ontime(&restored)
|
|
.unwrap()
|
|
.to_string()
|
|
.contains("SECRET")
|
|
);
|
|
let restored_profile: Profile =
|
|
serde_json::from_str(&serde_json::to_string(&p).unwrap()).unwrap();
|
|
assert_eq!(restored_profile.usage(5), Some("ignore"));
|
|
}
|
|
|
|
#[test]
|
|
fn column_usage_excludes_data_candidates_and_rejects_conflicting_profiles() {
|
|
let t = table(&[
|
|
&["Start", "End", "Reference", "Title"],
|
|
&["09:00", "09:05", "1 min", "Opening"],
|
|
&["09:05", "09:10", "2 min", "Closing"],
|
|
]);
|
|
for usage in ["metadata", "ignore"] {
|
|
let p = Profile {
|
|
column_usage: BTreeMap::from([("C".into(), usage.into())]),
|
|
..Default::default()
|
|
};
|
|
let a = analyze(&t, &p);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
assert_eq!(a.mapping.unwrap().duration, None);
|
|
assert_eq!(a.rundown.unwrap().entries[0].duration_ms, 300_000);
|
|
}
|
|
for usages in [
|
|
vec![("Z", "ignore")],
|
|
vec![("C", "unknown")],
|
|
vec![("C", "ignore"), ("c", "metadata")],
|
|
] {
|
|
let p = Profile {
|
|
column_usage: usages
|
|
.into_iter()
|
|
.map(|(k, v)| (k.into(), v.into()))
|
|
.collect(),
|
|
..Default::default()
|
|
};
|
|
assert!(
|
|
analyze(&t, &p)
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "INVALID_COLUMN_USAGE")
|
|
);
|
|
}
|
|
let p = Profile {
|
|
columns: BTreeMap::from([("start".into(), "A".into())]),
|
|
column_usage: BTreeMap::from([("A".into(), "ignore".into())]),
|
|
..Default::default()
|
|
};
|
|
assert!(
|
|
analyze(&t, &p)
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "INVALID_MAPPING")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn gap_choices_adjust_adjacent_timers_and_can_be_reversed() {
|
|
for named in [false, true] {
|
|
let t = if named {
|
|
table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["11:45", "15 min", "Morning"],
|
|
&["", "", "Lunch break"],
|
|
&["13:30", "20 min", "Afternoon"],
|
|
])
|
|
} else {
|
|
simple()
|
|
};
|
|
let baseline = analyze(&t, &Profile::default()).rundown.unwrap();
|
|
let key = baseline.gaps[0].key();
|
|
for action in ["keep", "extend_previous", "add_to_next"] {
|
|
let mut profile = Profile::default();
|
|
profile.gap_resolutions.insert(key.clone(), action.into());
|
|
// Profiles saved by either interface preserve the choice.
|
|
let profile: Profile =
|
|
serde_json::from_str(&serde_json::to_string(&profile).unwrap()).unwrap();
|
|
let a = analyze(&t, &profile);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries.len(), 2);
|
|
assert_eq!(r.gaps.len(), usize::from(action == "keep"));
|
|
assert_eq!(r.resolved_gaps.len(), usize::from(action != "keep"));
|
|
assert_eq!(r.entries[0].start_ms, baseline.entries[0].start_ms);
|
|
assert_eq!(r.entries[1].end_ms, baseline.entries[1].end_ms);
|
|
if action == "extend_previous" {
|
|
assert_eq!(r.entries[0].end_ms, baseline.entries[1].start_ms);
|
|
assert_eq!(r.entries[1].start_ms, baseline.entries[1].start_ms);
|
|
} else if action == "add_to_next" {
|
|
assert_eq!(r.entries[1].start_ms, baseline.entries[0].end_ms);
|
|
assert_eq!(r.entries[0].end_ms, baseline.entries[0].end_ms);
|
|
assert!(!r.entries[1].explicit_start);
|
|
}
|
|
let exported = export::ontime(&r).unwrap();
|
|
for e in &r.entries {
|
|
let timer = &exported["rundowns"]["default"]["entries"][&e.id];
|
|
assert_eq!(timer["duration"], e.end_ms - e.start_ms);
|
|
assert_eq!(timer["timeStart"], e.start_ms);
|
|
}
|
|
assert_eq!(
|
|
serde_json::to_value(&r).unwrap(),
|
|
serde_json::to_value(analyze(&t, &profile).rundown.unwrap()).unwrap()
|
|
);
|
|
}
|
|
let restored = analyze(&t, &Profile::default()).rundown.unwrap();
|
|
assert_eq!(
|
|
serde_json::to_value(restored).unwrap(),
|
|
serde_json::to_value(baseline).unwrap()
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_or_unavailable_gap_choices_block_export() {
|
|
let t = simple();
|
|
let baseline = analyze(&t, &Profile::default()).rundown.unwrap();
|
|
let mut profile = Profile::default();
|
|
profile
|
|
.gap_resolutions
|
|
.insert(baseline.gaps[0].key(), "unknown".into());
|
|
assert!(
|
|
analyze(&t, &profile)
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "INVALID_GAP_RESOLUTION")
|
|
);
|
|
profile.gap_resolutions = [("stale".into(), "extend_previous".into())]
|
|
.into_iter()
|
|
.collect();
|
|
assert!(
|
|
analyze(&t, &profile)
|
|
.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "INVALID_GAP_RESOLUTION")
|
|
);
|
|
let t = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["09:00", "30 min", "Lunch break"],
|
|
&["09:30", "20 min", "Opening"],
|
|
]);
|
|
profile = Profile::default();
|
|
profile.row_kinds.insert(2, "gap".into());
|
|
let baseline = analyze(&t, &profile).rundown.unwrap();
|
|
profile.gap_resolutions = [(baseline.gaps[0].key(), "extend_previous".into())]
|
|
.into_iter()
|
|
.collect();
|
|
let a = analyze(&t, &profile);
|
|
assert!(
|
|
a.diagnostics
|
|
.iter()
|
|
.any(|d| d.code == "GAP_RESOLUTION_UNAVAILABLE")
|
|
);
|
|
assert_eq!(a.rundown.unwrap().gaps.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn excel_scientific_notation_durations_reach_export_as_milliseconds() {
|
|
let mut t = simple();
|
|
t.rows[1][1] = Cell {
|
|
text: "6.9444444444444441E-3".into(),
|
|
numeric: true,
|
|
time_style: true,
|
|
..Default::default()
|
|
};
|
|
let a = analyze(&t, &Profile::default());
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries[0].duration_ms, 600_000);
|
|
let json = export::ontime(&r).unwrap();
|
|
assert_eq!(
|
|
json["rundowns"]["default"]["entries"][&r.entries[0].id]["duration"],
|
|
600_000
|
|
);
|
|
t.rows[1][1].text = "-6.9444444444444441E-3".into();
|
|
assert_eq!(time::duration(&t.rows[1][1], None), Some(-600_000));
|
|
assert!(!analyze(&t, &Profile::default()).diagnostics.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn warning_policy_excludes_rows_rechecks_and_can_be_disabled() {
|
|
let t = table(&[
|
|
&["Start", "Duration", "Title"],
|
|
&["09:00", "5 min", "Opening"],
|
|
&["", "", "Stage direction"],
|
|
&["09:10", "5 min", "Closing"],
|
|
]);
|
|
let profile = Profile {
|
|
ignore_warnings: true,
|
|
..Default::default()
|
|
};
|
|
let a = analyze(&t, &profile);
|
|
assert!(a.diagnostics.is_empty(), "{:?}", a.diagnostics);
|
|
assert_eq!(a.completed_stage, 4);
|
|
assert_eq!(a.automatically_ignored_rows, [3]);
|
|
let r = a.rundown.unwrap();
|
|
assert_eq!(r.entries.len(), 2);
|
|
assert_eq!(r.gaps.len(), 1);
|
|
assert!(export::ontime(&r).is_ok());
|
|
assert!(profile.row_kinds.is_empty());
|
|
assert_eq!(
|
|
analyze(&t, &Profile::default()).diagnostics[0].code,
|
|
"UNTIMED_ROW"
|
|
);
|
|
let both = Profile {
|
|
untimed_as_notes: true,
|
|
..profile.clone()
|
|
};
|
|
let a = analyze(&t, &both);
|
|
assert!(a.diagnostics.is_empty());
|
|
assert!(a.automatically_ignored_rows.is_empty());
|
|
assert_eq!(a.rundown.unwrap().entries[0].source_rows.len(), 2);
|
|
let mut overlap = t.clone();
|
|
overlap.rows[3][0].text = "09:03".into();
|
|
let a = analyze(&overlap, &profile);
|
|
assert_eq!(a.automatically_ignored_rows, [3]);
|
|
assert!(a.diagnostics.iter().any(|d| d.code == "OVERLAP"));
|
|
assert!(export::ontime(&a.rundown.unwrap()).is_err());
|
|
let mut ambiguity = t.clone();
|
|
ambiguity.rows[0][2].text = "Timer".into();
|
|
let a = analyze(&ambiguity, &profile);
|
|
assert!(a.mapping.is_none());
|
|
assert!(a.automatically_ignored_rows.is_empty());
|
|
let explicit = Profile {
|
|
row_kinds: [(3, "event".into())].into_iter().collect(),
|
|
..profile
|
|
};
|
|
let a = analyze(&t, &explicit);
|
|
assert!(!a.diagnostics.is_empty());
|
|
assert!(a.automatically_ignored_rows.is_empty());
|
|
}
|