Lines
95.08 %
Functions
100 %
Branches
//! The category CSV pane: `x` fetches it, `y` copies it by OSC 52, `s` saves it locally.
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{key, press};
use cli_core::report_form::csv_call;
use cli_core::transaction::submit_call;
use forms::{FormValues, REPORT_CATEGORY, escape_str, write_payload};
use sqlx::types::Uuid;
use super::report_chart::{EMPTY, echoed, pending, rendered, text};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::modal::{ConfirmAction, Modal};
use crate::route::RouteCtx;
use crate::tabs::reports::ReportKind;
use crate::view::Tab;
use ratatui::crossterm::event::KeyCode;
const CSV: &str = "name,amount\nfood,10\nrent,20\n";
fn category() -> FormValues {
let mut values = FormValues::default();
values.set("from", "2026-01-01");
values.set("to", "2026-02-01");
values
}
fn with_category() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.switch_tab(Tab::Reports);
app.submit_report(
app.window_of(crate::view::ViewId::Reports),
ReportKind::Breakdown,
&category(),
);
let [run] = pending(&app, |ctx| matches!(ctx, RouteCtx::Reports { .. }))[..] else {
panic!("one report run");
};
app.route_reply(run, EMPTY);
(app, sent)
fn csv_requests(app: &App) -> Vec<i64> {
pending(app, |ctx| matches!(ctx, RouteCtx::ReportCsv { .. }))
fn csv_wire(id: i64, csv: &str) -> String {
let quoted = csv.replace('\\', "\\\\").replace('"', "\\\"");
format!("(:id {id} :value \"{quoted}\")")
/// An app showing the CSV pane for [`CSV`].
fn with_pane() -> App {
let (mut app, _) = with_category();
press(&mut app, key(KeyCode::Char('x')));
let [id] = csv_requests(&app)[..] else {
panic!("one csv request");
app.route_reply(id, &csv_wire(id, CSV));
app
fn scratch_file(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("tui-csv-{}", Uuid::new_v4()));
std::fs::create_dir_all(&dir).expect("scratch dir");
dir.join(name)
fn path_values(path: &std::path::Path) -> FormValues {
values.set("path", &path.display().to_string());
#[tokio::test]
async fn x_without_a_report_says_so() {
assert_eq!(app.status, "no report run yet");
assert!(csv_requests(&app).is_empty());
async fn x_on_another_report_is_refused() {
app.attach_echo();
ReportKind::Balance,
&FormValues::default(),
assert_eq!(app.status, "CSV is only for the category report");
async fn the_csv_request_carries_the_payload_the_report_ran_with() {
let (mut app, sent) = with_category();
assert_eq!(csv_requests(&app).len(), 1);
let echoed = echoed(&mut app, &sent).await;
let payload = escape_str(&write_payload(&REPORT_CATEGORY, &category()));
assert!(echoed.contains(&csv_call(&REPORT_CATEGORY, &category())));
assert!(echoed.contains(&format!("(report-csv {payload})")));
assert!(echoed.contains(&submit_call(&REPORT_CATEGORY, &category())));
async fn the_reply_opens_a_read_only_pane_with_the_csv() {
let app = with_pane();
let Some(Modal::Csv(csv)) = app.overlays.top() else {
panic!("csv pane on top");
assert_eq!(csv.text, CSV);
assert_eq!(csv.file_name, "category-2026-01-01-2026-02-01.csv");
let shown = text(&rendered(&app, 100, 20));
assert!(shown.contains("name,amount"), "{shown}");
assert!(shown.contains("rent,20"), "{shown}");
async fn the_pane_scrolls_and_closes() {
let mut app = with_pane();
crate::test_support::draw_once(&app, 80, 8);
press(&mut app, key(KeyCode::Down));
assert_eq!(csv.scroll.top(), 1);
press(&mut app, key(KeyCode::Esc));
assert!(app.overlays.is_empty());
async fn y_queues_the_osc52_sequence_for_the_terminal() {
press(&mut app, key(KeyCode::Char('y')));
assert_eq!(
app.take_outbound(),
vec![b"\x1b]52;c;bmFtZSxhbW91bnQKZm9vZCwxMApyZW50LDIwCg==\x07".to_vec()]
assert!(app.take_outbound().is_empty());
assert_eq!(app.status, "CSV copied to the clipboard");
async fn y_over_the_size_limit_copies_nothing() {
app.route_reply(id, &csv_wire(id, &"x".repeat(100_000)));
assert!(app.status.contains("too large"), "{}", app.status);
async fn saving_is_refused_over_ssh() {
assert!(!app.local_files);
press(&mut app, key(KeyCode::Char('s')));
assert_eq!(app.status, "save is not available over ssh");
assert!(matches!(app.overlays.top(), Some(Modal::Csv(_))));
let path = scratch_file("refused.csv");
app.save_csv(&path_values(&path));
assert!(!path.exists());
async fn s_locally_asks_for_the_default_path() {
app.local_files = true;
let Some(Modal::Form(form)) = app.overlays.top() else {
panic!("save prompt on top");
form.values().text("path"),
Some("category-2026-01-01-2026-02-01.csv")
async fn saving_writes_the_file_and_asks_before_overwriting() {
let path = scratch_file("out.csv");
assert_eq!(std::fs::read_to_string(&path).expect("written"), CSV);
std::fs::write(&path, "old").expect("seed");
assert_eq!(std::fs::read_to_string(&path).expect("kept"), "old");
assert!(matches!(
app.overlays.top(),
Some(Modal::Confirm {
action: ConfirmAction::OverwriteCsv(_),
..
})
));
apply(&mut app, Intent::ConfirmYes);
assert_eq!(std::fs::read_to_string(&path).expect("replaced"), CSV);
let leftovers = std::fs::read_dir(path.parent().expect("dir"))
.expect("list")
.count();
assert_eq!(leftovers, 1);
async fn a_server_error_goes_to_the_status_and_keeps_the_table() {
assert!(text(&rendered(&app, 100, 20)).contains("(no data)"));
app.route_reply(
id,
&format!(r#"(:id {id} :error (:code args :message "bad"))"#),
assert!(app.status.contains("bad"), "{}", app.status);
assert!(shown.contains("(no data)"), "{shown}");
assert!(!shown.contains("[error]"), "{shown}");
async fn an_older_csv_reply_is_dropped() {
let [first] = csv_requests(&app)[..] else {
app.route_reply(first, &csv_wire(first, CSV));