mathypad 0.1.14

A smart TUI calculator that understands units and makes complex calculations simple.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! UI snapshot tests using insta and ratatui TestBackend

use super::*;
use crate::ui::render::render_welcome_dialog_with_content;
use crate::{App, Mode};
use insta::assert_snapshot;
use ratatui::{Terminal, backend::TestBackend};

/// Helper function to create a test terminal with a fixed size
fn create_test_terminal() -> Terminal<TestBackend> {
    Terminal::new(TestBackend::new(120, 30)).unwrap()
}

/// Helper function to render app and return terminal output
fn render_app_to_string(app: &App) -> String {
    let mut terminal = create_test_terminal();
    terminal.draw(|frame| ui(frame, app)).unwrap();
    format!("{}", terminal.backend())
}

/// Helper function to create an app with some sample content
fn create_sample_app() -> App {
    let mut app = App::default();
    app.core.text_lines = vec![
        "5 + 3".to_string(),
        "10 kg to lb".to_string(),
        "line1 * 2".to_string(),
        "sin(30 degrees)".to_string(),
    ];
    app.core.results = vec![
        Some("8".to_string()),
        Some("22.046 lb".to_string()),
        Some("16".to_string()),
        Some("0.5".to_string()),
    ];
    app.core.cursor_line = 1;
    app.core.cursor_col = 5;

    // Add some variables
    app.core.variables.insert("x".to_string(), "42".to_string());
    app.core
        .variables
        .insert("rate".to_string(), "5.5".to_string());

    app
}

#[test]
fn test_basic_ui_layout() {
    let app = App::default();
    let output = render_app_to_string(&app);
    assert_snapshot!("basic_ui_layout", output);
}

#[test]
fn test_ui_with_content() {
    let app = create_sample_app();
    let output = render_app_to_string(&app);
    assert_snapshot!("ui_with_content", output);
}

#[test]
fn test_ui_normal_mode() {
    let mut app = create_sample_app();
    app.mode = Mode::Normal;
    let output = render_app_to_string(&app);
    assert_snapshot!("ui_normal_mode", output);
}

#[test]
fn test_ui_insert_mode() {
    let mut app = create_sample_app();
    app.mode = Mode::Insert;
    let output = render_app_to_string(&app);
    assert_snapshot!("ui_insert_mode", output);
}

#[test]
fn test_ui_with_unsaved_changes() {
    let mut app = create_sample_app();
    app.has_unsaved_changes = true;
    let output = render_app_to_string(&app);
    assert_snapshot!("ui_with_unsaved_changes", output);
}

#[test]
fn test_ui_different_separator_position() {
    let mut app = create_sample_app();
    app.separator_position = 60; // 60/40 split instead of default 80/20
    let output = render_app_to_string(&app);
    assert_snapshot!("ui_different_separator_position", output);
}

#[test]
fn test_text_area_rendering() {
    let mut terminal = create_test_terminal();
    let app = create_sample_app();

    terminal
        .draw(|frame| {
            let area = frame.area();
            render_text_area(frame, &app, area);
        })
        .unwrap();

    let output = format!("{}", terminal.backend());
    assert_snapshot!("text_area_rendering", output);
}

#[test]
fn test_results_panel_rendering() {
    let mut terminal = create_test_terminal();
    let app = create_sample_app();

    terminal
        .draw(|frame| {
            let area = frame.area();
            render_results_panel(frame, &app, area);
        })
        .unwrap();

    let output = format!("{}", terminal.backend());
    assert_snapshot!("results_panel_rendering", output);
}

#[test]
fn test_syntax_highlighting_numbers() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "123".to_string(),
        "3.14159".to_string(),
        "1,234,567.89".to_string(),
    ];
    app.core.results = vec![None, None, None];

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_numbers", output);
}

#[test]
fn test_syntax_highlighting_operators() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "5 + 3 - 2".to_string(),
        "10 * (4 / 2)".to_string(),
        "x = 42".to_string(),
    ];
    app.core.results = vec![None, None, None];

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_operators", output);
}

#[test]
fn test_syntax_highlighting_functions() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "sqrt(16) + 1".to_string(),
        "2^3 + sqrt(9)".to_string(),
        "sqrt(2 + 2) * 3".to_string(),
    ];
    app.core.results = vec![None, None, None];

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_functions", output);
}

#[test]
fn test_syntax_highlighting_units() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "100 kg".to_string(),
        "50 miles per hour".to_string(),
        "25 degrees celsius".to_string(),
        "1 GiB".to_string(),
    ];
    app.core.results = vec![None, None, None, None];

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_units", output);
}

#[test]
fn test_syntax_highlighting_keywords() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "100 kg to lb".to_string(),
        "50 miles in km".to_string(),
        "25% of 200".to_string(),
    ];
    app.core.results = vec![None, None, None];

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_keywords", output);
}

#[test]
fn test_syntax_highlighting_line_references() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "100".to_string(),
        "line1 + 50".to_string(),
        "line2 * 2".to_string(),
        "line1 + line2 + line3".to_string(),
    ];
    app.core.results = vec![
        Some("100".to_string()),
        Some("150".to_string()),
        Some("300".to_string()),
        Some("550".to_string()),
    ];

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_line_references", output);
}

#[test]
fn test_syntax_highlighting_line_references_with_operators() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "$40".to_string(),
        "line1/month * 3".to_string(),
        "line1+100".to_string(),
        "line1*rate-50".to_string(),
    ];
    app.core.results = vec![
        Some("40 $".to_string()),
        Some("120 $/month".to_string()),
        Some("140 $".to_string()),
        Some("170 $".to_string()),
    ];
    app.core
        .variables
        .insert("rate".to_string(), "5.5".to_string());

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_line_references_with_operators", output);
}

#[test]
fn test_syntax_highlighting_variables() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "x = 42".to_string(),
        "y = x * 2".to_string(),
        "result = x + y".to_string(),
    ];
    app.core.results = vec![None, None, None];
    app.core.variables.insert("x".to_string(), "42".to_string());
    app.core.variables.insert("y".to_string(), "84".to_string());
    app.core
        .variables
        .insert("result".to_string(), "126".to_string());

    let output = render_app_to_string(&app);
    assert_snapshot!("syntax_highlighting_variables", output);
}

#[test]
fn test_cursor_highlighting() {
    let mut app = App::default();
    app.core.text_lines = vec!["hello world".to_string(), "123 + 456".to_string()];
    app.core.results = vec![None, None];
    app.core.cursor_line = 0;
    app.core.cursor_col = 6; // Position cursor on 'w' in "world"

    let output = render_app_to_string(&app);
    assert_snapshot!("cursor_highlighting", output);
}

#[test]
fn test_cursor_at_end_of_line() {
    let mut app = App::default();
    app.core.text_lines = vec!["hello".to_string()];
    app.core.results = vec![None];
    app.core.cursor_line = 0;
    app.core.cursor_col = 5; // Position cursor at end of line

    let output = render_app_to_string(&app);
    assert_snapshot!("cursor_at_end_of_line", output);
}

#[test]
fn test_scrolled_content() {
    let mut app = App::default();
    // Create more lines than fit on screen
    app.core.text_lines = (0..50).map(|i| format!("line {} content", i + 1)).collect();
    app.core.results = vec![None; 50];
    app.scroll_offset = 10; // Scroll down 10 lines
    app.core.cursor_line = 15;
    app.core.cursor_col = 5;

    let output = render_app_to_string(&app);
    assert_snapshot!("scrolled_content", output);
}

#[test]
fn test_empty_results() {
    let mut app = App::default();
    app.core.text_lines = vec![
        "".to_string(),
        "invalid expression +++".to_string(),
        "".to_string(),
    ];
    app.core.results = vec![None, None, None];

    let output = render_app_to_string(&app);
    assert_snapshot!("empty_results", output);
}

#[test]
fn test_unsaved_dialog() {
    let mut app = create_sample_app();
    app.show_unsaved_dialog = true;
    app.has_unsaved_changes = true;

    let output = render_app_to_string(&app);
    assert_snapshot!("unsaved_dialog", output);
}

#[test]
fn test_save_as_dialog() {
    let mut app = create_sample_app();
    app.show_save_as_dialog = true;
    app.save_as_input = "my_notebook.pad".to_string();

    let output = render_app_to_string(&app);
    assert_snapshot!("save_as_dialog", output);
}

#[test]
fn test_save_as_dialog_placeholder() {
    let mut app = create_sample_app();
    app.show_save_as_dialog = true;
    app.save_as_input = ".pad".to_string(); // Default placeholder

    let output = render_app_to_string(&app);
    assert_snapshot!("save_as_dialog_placeholder", output);
}

#[test]
fn test_separator_indicator_hovering() {
    let mut app = create_sample_app();
    app.is_hovering_separator = true;

    let output = render_app_to_string(&app);
    assert_snapshot!("separator_indicator_hovering", output);
}

#[test]
fn test_separator_indicator_dragging() {
    let mut app = create_sample_app();
    app.is_dragging_separator = true;

    let output = render_app_to_string(&app);
    assert_snapshot!("separator_indicator_dragging", output);
}

/// Helper function to render welcome dialog with stable test content
fn render_welcome_dialog_to_string(
    app: &App,
    changelog_content: &str,
    current_version: &str,
    stored_version: Option<&str>,
) -> String {
    let mut terminal = create_test_terminal();
    terminal
        .draw(|frame| {
            render_welcome_dialog_with_content(
                frame,
                app,
                frame.area(),
                changelog_content,
                current_version,
                stored_version,
            )
        })
        .unwrap();
    format!("{}", terminal.backend())
}

#[test]
fn test_welcome_dialog_first_run() {
    let app = App {
        show_welcome_dialog: true,
        welcome_scroll_offset: 0,
        ..Default::default()
    };

    let mock_changelog = "## [1.0.0] - 2024-01-01\n\n### 🤖 AI Assisted\n\n- Add welcome screen for new versions\n- Implement scrollable changelog display\n- Add version tracking functionality\n\n### 👤 Artisanally Crafted\n\n- Fix UI layout bugs\n- Improve error handling\n- Update documentation";

    let output = render_welcome_dialog_to_string(&app, mock_changelog, "1.0.0", None);
    assert_snapshot!("welcome_dialog_first_run", output);
}

#[test]
fn test_welcome_dialog_upgrade() {
    let app = App {
        show_welcome_dialog: true,
        welcome_scroll_offset: 0,
        ..Default::default()
    };

    let mock_changelog = "## [1.1.0] - 2024-02-01\n\n### 🤖 AI Assisted\n\n- Add new calculation features\n- Improve unit conversion accuracy\n- Enhanced TUI responsiveness\n\n### 👤 Artisanally Crafted\n\n- Performance optimizations\n- Bug fixes in expression parser\n- Updated dependencies\n\n## [1.0.0] - 2024-01-01\n\n### 🤖 AI Assisted\n\n- Initial release\n- Basic calculator functionality";

    let output = render_welcome_dialog_to_string(&app, mock_changelog, "1.1.0", Some("1.0.0"));
    assert_snapshot!("welcome_dialog_upgrade", output);
}

#[test]
fn test_welcome_dialog_with_scrolling() {
    let app = App {
        show_welcome_dialog: true,
        welcome_scroll_offset: 3, // Scroll down a bit
        ..Default::default()
    };

    let mock_changelog = "## [1.2.0] - 2024-03-01\n\n### 🤖 AI Assisted\n\n- Major UI overhaul with new themes\n- Advanced calculation engine\n- Smart auto-completion\n- Real-time result preview\n- Enhanced error messages\n- Improved keyboard shortcuts\n- Better file handling\n- Performance monitoring\n- Extended unit support\n- Advanced graphing features\n\n### 👤 Artisanally Crafted\n\n- Critical security fixes\n- Memory usage optimizations\n- Cross-platform compatibility\n- Accessibility improvements\n- Test coverage expansion";

    let output = render_welcome_dialog_to_string(&app, mock_changelog, "1.2.0", Some("1.1.0"));
    assert_snapshot!("welcome_dialog_with_scrolling", output);
}

#[test]
fn test_welcome_dialog_minimal_content() {
    let app = App {
        show_welcome_dialog: true,
        welcome_scroll_offset: 0,
        ..Default::default()
    };

    let mock_changelog = "## [1.0.1] - 2024-01-02\n\n- Quick bug fix";

    let output = render_welcome_dialog_to_string(&app, mock_changelog, "1.0.1", Some("1.0.0"));
    assert_snapshot!("welcome_dialog_minimal_content", output);
}

#[test]
fn test_command_mode_rendering() {
    let mut app = App::default();
    app.core.text_lines = vec!["5 + 3".to_string(), "sqrt(16)".to_string()];
    app.core.results = vec![Some("8".to_string()), Some("4".to_string())];
    app.mode = Mode::Command;
    app.command_line = ":w test-file".to_string();
    app.command_cursor = 11;
    app.core.cursor_line = 0;
    app.core.cursor_col = 3;

    let output = render_app_to_string(&app);
    assert_snapshot!("command_mode_rendering", output);
}