use vantage_dataset::traits::ReadableValueSet;
use super::Probe;
use crate::{CborValue, Page, Vista};
pub(super) async fn leading_into(vista: &Vista, probe: &mut Probe) {
let Ok(listed) = vista.list_values().await else {
return;
};
let mut limits = vec![1, listed.len().saturating_sub(1).max(1), listed.len() + 1];
limits.dedup();
for limit in limits {
let page = match vista.fetch_window(0, limit).await {
Ok(page) => page,
Err(e) => {
probe.fail(format!(
"`list` works but fetch_window(0, {limit}) fails: {e}; every shell that \
lists answers the leading window"
));
continue;
}
};
let expected = limit.min(listed.len());
if page.rows.len() > limit {
probe.fail(format!(
"fetch_window(0, {limit}) returns {} rows: the limit is not applied",
page.rows.len()
));
continue;
}
if page.rows.len() != expected {
probe.fail(format!(
"fetch_window(0, {limit}) returns {} rows of a set listing {}; it must \
return {expected}",
page.rows.len(),
listed.len()
));
continue;
}
if let Some((id, _)) = page.rows.iter().find(|(id, _)| !listed.contains_key(id)) {
probe.fail(format!(
"fetch_window(0, {limit}) returns row {id}, which the set doesn't list"
));
continue;
}
if let Some((column, _)) = &vista.query().order {
let want: Vec<Option<&CborValue>> = listed
.values()
.take(expected)
.map(|r| r.get(column))
.collect();
let got = column_values(&page, column);
if got != want {
probe.fail(format!(
"ordered by `{column}`, fetch_window(0, {limit}) returns {got:?}, not the \
first values in the set's order {want:?}"
));
}
}
}
}
fn column_values<'a>(page: &'a Page, column: &str) -> Vec<Option<&'a CborValue>> {
page.rows.iter().map(|(_, r)| r.get(column)).collect()
}