#[cfg(test)]
mod tests {
use crate::config::AppConfig;
use crate::state::AppState;
use crate::view::dashboard::WidthMode;
use iced_test::core::Size;
use knotra_vcs::{
ConflictStatus, Project, ProjectStatus, RemoteStatus, RepositoryIdentity, VcsKind,
WorkingTreeStatus, Workspace, WorkspaceStatus,
};
fn install_one_project_per_status(state: &mut AppState) {
state.config.dashboard_all_set_collapsed = false;
let base_status = |project: &Project| ProjectStatus {
project_id: project.id.clone(),
identity: RepositoryIdentity {
path: project.path.clone(),
vcs_kind: VcsKind::Git,
},
context: None,
remote: RemoteStatus::default(),
working_tree: WorkingTreeStatus::default(),
conflict: ConflictStatus::default(),
refreshed_at: jiff::Timestamp::now(),
read_error: None,
};
let mut all_projects = Vec::new();
let mut all_statuses = Vec::new();
for n in 1..=1 {
let project = Project::new(format!("Needs Help Co {n}"), format!("/tmp/needs-{n}"));
let mut status = base_status(&project);
status.conflict = ConflictStatus {
has_conflict: true,
conflict_count: Some(2),
detection_unavailable: false,
};
all_projects.push(project);
all_statuses.push(status);
}
for n in 1..=2 {
let project = Project::new(format!("Unsaved Work Co {n}"), format!("/tmp/dirty-{n}"));
let mut status = base_status(&project);
status.working_tree = WorkingTreeStatus {
uncommitted_count: 3,
untracked_count: 0,
};
all_projects.push(project);
all_statuses.push(status);
}
for n in 1..=3 {
let project = Project::new(format!("Behind Co {n}"), format!("/tmp/behind-{n}"));
let mut status = base_status(&project);
status.remote = RemoteStatus {
ahead: 0,
behind: 2,
upstream: Some("origin/main".to_owned()),
};
all_projects.push(project);
all_statuses.push(status);
}
for n in 1..=4 {
let project = Project::new(format!("Ahead Co {n}"), format!("/tmp/ahead-{n}"));
let mut status = base_status(&project);
status.remote = RemoteStatus {
ahead: 2,
behind: 0,
upstream: Some("origin/main".to_owned()),
};
all_projects.push(project);
all_statuses.push(status);
}
for n in 1..=5 {
let project = Project::new(format!("All Set Co {n}"), format!("/tmp/allset-{n}"));
let status = base_status(&project);
all_projects.push(project);
all_statuses.push(status);
}
let mut workspace = Workspace::new("Demo");
workspace.projects = all_projects;
state.all_workspaces = vec![workspace];
state.active_workspace_idx = 0;
state.workspace = state.all_workspaces.first().cloned();
state.workspace_status = Some(WorkspaceStatus {
projects: all_statuses,
last_refresh: None,
});
}
const REFERENCE_SIZE: Size = Size {
width: 4000.0,
height: 2000.0,
};
fn reference_simulator(state: &AppState) -> iced_test::Simulator<'_, crate::message::Message> {
crate::headless::simulator(crate::view::app_view(state), REFERENCE_SIZE)
}
fn check_label(
ui: &mut iced_test::Simulator<'_, crate::message::Message>,
reference: &mut iced_test::Simulator<'_, crate::message::Message>,
label: &str,
viewport_width: f32,
context: &str,
) -> Result<(), String> {
let actual = ui
.find(label)
.map_err(|e| format!("{context}: label {label:?} not found: {e}"))?
.bounds();
let natural = reference
.find(label)
.map_err(|e| {
format!("{context}: label {label:?} not found at the reference size: {e}")
})?
.bounds();
let mut problems = Vec::new();
if (actual.width - natural.width).abs() > 0.5 {
problems.push(format!(
"width {:.2} != natural {:.2} (clamped)",
actual.width, natural.width
));
}
if actual.height != natural.height {
problems.push(format!(
"height {:.2} != natural {:.2}",
actual.height, natural.height
));
}
if actual.x + actual.width > viewport_width + 0.5 {
problems.push(format!(
"right edge {:.2} exceeds the viewport's {:.2}",
actual.x + actual.width,
viewport_width
));
}
if problems.is_empty() {
Ok(())
} else {
Err(format!(
"{context}: label {label:?} {actual:?} (natural {natural:?}): {}",
problems.join("; ")
))
}
}
#[test]
#[cfg_attr(
not(target_os = "linux"),
ignore = "the pinned faces (Noto Sans, Noto Sans CJK JP) are installed only on the \
Linux CI runner (Task 108 A1 §4) — the widths this guard measures would be \
some other font's on macOS/Windows"
)]
fn every_label_fits_its_control_across_the_matrix() {
let mut checked = 0usize;
let mut failures: Vec<String> = Vec::new();
for &locale in &[knotra_ui::i18n::Locale::En, knotra_ui::i18n::Locale::Ja] {
for &zoom_percent in &[100.0_f32, 150.0_f32] {
for &width in &[800.0_f32, 1280.0_f32] {
for selection_mode in [false, true] {
let mut state = AppState::new(AppConfig::default());
state.catalog = knotra_ui::i18n::Catalog::for_locale(locale);
install_one_project_per_status(&mut state);
state.selection_mode = selection_mode;
let logical = crate::headless::logical_size_at_zoom(
Size::new(width, 720.0),
zoom_percent,
);
state.width_mode = WidthMode::from_width(logical.width);
let context = format!(
"locale={locale:?} zoom={zoom_percent}% width={width} \
selection_mode={selection_mode} width_mode={:?}",
state.width_mode
);
let mut ui =
crate::headless::simulator(crate::view::app_view(&state), logical);
let mut reference = reference_simulator(&state);
let mut check = |label: &str, failures: &mut Vec<String>| {
if let Err(e) =
check_label(&mut ui, &mut reference, label, logical.width, &context)
{
failures.push(e);
}
};
let chip_keys: &[(&str, &str)] = &[
("filter.needs_help", "1"),
("filter.dirty", "2"),
("filter.behind", "3"),
("filter.ahead", "4"),
("filter.all_set", "5"),
];
for (key, count) in chip_keys {
let label = state.t(key).to_owned();
check(&label, &mut failures);
check(count, &mut failures);
checked += 2;
}
let arrange = state.t("dashboard.arrange").to_owned();
check(&arrange, &mut failures);
checked += 1;
let select = state.t("plain.selection.enter").to_owned();
check(&select, &mut failures);
checked += 1;
let resolve = state.t("dashboard.resolve").to_owned();
check(&resolve, &mut failures);
checked += 1;
if selection_mode {
for key in [
"plain.check_for_updates",
"plain.get_latest",
"plain.save_release_point",
"plain.change_work_area",
"plain.exit_selection",
] {
let label = state.t(key).to_owned();
check(&label, &mut failures);
checked += 1;
}
}
}
}
}
}
assert!(checked > 0, "the matrix must check at least one label");
eprintln!("label_overflow_guard: checked {checked} label/control pairs");
assert!(
failures.is_empty(),
"{} of {checked} label/control pairs were clamped or off-screen:\n{}",
failures.len(),
failures.join("\n")
);
}
#[test]
#[cfg_attr(
not(target_os = "linux"),
ignore = "the pinned faces (Noto Sans, Noto Sans CJK JP) are installed only on the \
Linux CI runner (Task 108 A1 §4) — the widths this guard measures would be \
some other font's on macOS/Windows"
)]
fn every_detail_panel_action_label_fits_its_button() {
use crate::state::detail_panel::DetailPanelState;
fn reference_bounds(label: &str) -> iced_test::core::Rectangle {
let button: iced::widget::Button<'static, (), iced::Theme, iced::Renderer> =
iced::widget::button(knotra_ui::widget::centered_button_label(
iced::widget::text(label.to_owned()),
))
.height(iced::Length::Fixed(knotra_ui::widget::BUTTON_HEIGHT))
.padding([0.0, 18.0]);
let mut sim = crate::headless::simulator(button, REFERENCE_SIZE);
sim.find(label)
.unwrap_or_else(|e| panic!("reference label {label:?} not found: {e}"))
.bounds()
}
let mut checked = 0usize;
let mut failures: Vec<String> = Vec::new();
for &locale in &[knotra_ui::i18n::Locale::En, knotra_ui::i18n::Locale::Ja] {
for &zoom_percent in &[100.0_f32, 150.0_f32] {
for &width in &[800.0_f32, 1280.0_f32] {
let mut state = AppState::new(AppConfig::default());
state.catalog = knotra_ui::i18n::Catalog::for_locale(locale);
install_one_project_per_status(&mut state);
let project_id = state
.workspace
.as_ref()
.and_then(|ws| ws.projects.first())
.map(|p| p.id.clone())
.expect("install_one_project_per_status adds at least one project");
state.detail_panel = DetailPanelState {
open_project_id: Some(project_id),
..Default::default()
};
let logical = crate::headless::logical_size_at_zoom(
Size::new(width, 720.0),
zoom_percent,
);
state.width_mode = WidthMode::from_width(logical.width);
let context = format!(
"locale={locale:?} zoom={zoom_percent}% width={width} (detail panel)"
);
let panel = crate::view::detail_panel::view(&state)
.expect("detail_panel.open_project_id is set above");
let mut ui = crate::headless::simulator(panel, logical);
for key in [
"detail.refresh",
"detail.fetch",
"detail.remove_from_workspace",
] {
let label = state.t(key).to_owned();
let natural = reference_bounds(&label);
let actual = ui
.find(label.as_str())
.unwrap_or_else(|e| panic!("{context}: label {label:?} not found: {e}"))
.bounds();
let mut problems = Vec::new();
if (actual.width - natural.width).abs() > 0.5 {
problems.push(format!(
"width {:.2} != natural {:.2} (clamped)",
actual.width, natural.width
));
}
if actual.height != natural.height {
problems.push(format!(
"height {:.2} != natural {:.2}",
actual.height, natural.height
));
}
if actual.x + actual.width > logical.width + 0.5 {
problems.push(format!(
"right edge {:.2} exceeds the viewport's {:.2}",
actual.x + actual.width,
logical.width
));
}
if !problems.is_empty() {
failures.push(format!(
"{context}: label {label:?} {actual:?} (natural {natural:?}): {}",
problems.join("; ")
));
}
checked += 1;
}
}
}
}
assert!(checked > 0, "the matrix must check at least one label");
eprintln!("label_overflow_guard: checked {checked} detail-panel label/control pairs");
assert!(
failures.is_empty(),
"{} of {checked} detail-panel label/control pairs were clamped or off-screen:\n{}",
failures.len(),
failures.join("\n")
);
}
}