use super::super::super::{row, seat};
use crate::budgets::BudgetSpend;
use crate::paint_probe;
use crate::spend::{Attribution, Cost, Figure};
use crate::ui_state::Panel;
const FRAMES: usize = 6;
const WINDOW: f32 = 1150.0;
fn wide_figure() -> Figure {
Figure {
tokens: BudgetSpend {
input_tokens: 1_234_567,
output_tokens: 987_654,
cache_read_tokens: 55_555_555,
cache_write_tokens: 4_444_444,
},
cost: Some(Cost {
micro_usd: 12_345_678,
unpriced_tokens: 987_654,
}),
attribution: Attribution::Workspace,
}
}
fn spend_row(ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.weak("spend:");
crate::spend::render(ui, &wide_figure());
});
}
fn column(window: (f32, f32), body: &mut dyn FnMut(&mut egui::Ui)) -> (Vec<f32>, egui::FullOutput) {
let (w, h) = window;
let ctx = egui::Context::default();
let mut widths = Vec::new();
let mut last = None;
for _ in 0..FRAMES {
let out = ctx.run(paint_probe::screen_sized(w, h), |ctx| {
egui::SidePanel::left("conversations")
.resizable(true)
.default_width(Panel::Conversations.default_size())
.width_range(Panel::Conversations.min_size()..=Panel::Conversations.max_size(w))
.show(ctx, |ui| {
row::bounded(ui);
seat(ui, &mut *body);
});
egui::CentralPanel::default().show(ctx, |_| {});
});
widths.push(
egui::containers::panel::PanelState::load(&ctx, egui::Id::new("conversations"))
.expect("the panel stores its rect")
.rect
.width(),
);
last = Some(out);
}
(widths, last.expect("FRAMES is not zero"))
}
#[test]
fn the_widest_board_row_cannot_ratchet_the_conversation_column_at_any_size() {
let mut report = Vec::new();
for (w, h) in super::super::SIZES {
let ceiling = Panel::Conversations.max_size(w);
let opened = Panel::Conversations.default_size().min(ceiling);
let (widths, out) = column((w, h), &mut spend_row);
if widths.iter().any(|width| (width - opened).abs() > 1.0) {
report.push(format!(
"at {w:.0}x{h:.0} the column left its {opened} pt opening: {widths:?}"
));
}
for seen in paint_probe::seen_of(&out) {
if seen.laid.right() > opened + 1.0 {
report.push(format!(
"at {w:.0}x{h:.0} `{}` is laid past the column's right edge at {}",
seen.text,
seen.laid.right()
));
}
if seen.text.trim() == "…" {
report.push(format!(
"at {w:.0}x{h:.0} a part of the figure was elided until nothing \
was left, at {:?}",
seen.laid
));
}
}
}
assert!(report.is_empty(), "{}", report.join("\n"));
}
#[test]
fn the_figure_under_test_is_wider_than_the_column_when_nothing_bounds_it() {
let painted = paint_probe::painted_settled(2000.0, 400.0, |ui| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
ui.horizontal(|ui| {
ui.weak("spend:");
for part in parts() {
ui.weak(part);
}
});
});
let widest = painted
.iter()
.fold(0.0_f32, |right, (_, rect)| right.max(rect.right()));
assert!(
widest > 2.0 * Panel::Conversations.default_size(),
"the fixture figure must not fit the column, or nothing here is being \
tested: it laid {widest} pt"
);
}
fn parts() -> Vec<String> {
let figure = wide_figure();
let spend = figure.tokens;
let mut parts = vec![
format!("budget {} tok", spend.total_tokens()),
format!(
"(in {} · out {} · cache r {} · w {})",
spend.input_tokens,
spend.output_tokens,
spend.cache_read_tokens,
spend.cache_write_tokens
),
];
if let Some(cost) = figure.cost {
parts.push(cost.usd());
parts.push(format!("+{} tok unpriced", cost.unpriced_tokens));
}
if let Some(note) = figure.attribution.note() {
parts.push(note.label.clone());
}
parts
}
#[test]
fn every_part_of_a_figure_is_painted_whole_in_the_centre_seat() {
let painted =
paint_probe::painted_settled(WINDOW - Panel::Conversations.default_size(), 600.0, |ui| {
row::bounded(ui);
crate::spend::render(ui, &wide_figure());
});
let said: Vec<&str> = painted.iter().map(|(text, _)| text.as_str()).collect();
for part in parts() {
assert!(
said.iter().any(|seen| *seen == part),
"`{part}` is not on the glass whole in the centre seat: {said:?}"
);
}
}