sabiql 1.15.1

A fast, driver-less TUI for browsing and editing PostgreSQL databases
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
452
453
454
455
use super::*;
use harness::connected_state;
use sabiql_app::model::app_state::AppState;
use sabiql_app::model::shared::confirm_dialog::ConfirmIntent;

fn make_update_preview(diff: Vec<ColumnDiff>, sql: String) -> WritePreview {
    make_update_preview_with_key(diff, sql, "1")
}

fn make_update_preview_with_key(diff: Vec<ColumnDiff>, sql: String, id: &str) -> WritePreview {
    WritePreview {
        operation: WriteOperation::Update,
        sql,
        target_summary: TargetSummary {
            schema: "public".to_string(),
            table: "users".to_string(),
            key_values: vec![("id".to_string(), id.to_string())],
        },
        diff,
        guardrail: GuardrailDecision {
            risk_level: RiskLevel::Low,
            blocked: false,
            reason: None,
            target_summary: None,
        },
    }
}

fn open_write_confirm(state: &mut AppState, title: &str, sql: &str) {
    state.modal.set_mode(InputMode::ConfirmDialog);
    state.confirm_dialog.open(
        title,
        "",
        ConfirmIntent::ExecuteWrite {
            sql: sql.to_string(),
            blocked: false,
        },
    );
}

#[test]
fn confirm_dialog() {
    let mut state = create_test_state();
    let mut terminal = create_test_terminal();

    state.modal.set_mode(InputMode::ConfirmDialog);
    state.confirm_dialog.open(
        "Confirm",
        "No connection configured.\nAre you sure you want to quit?",
        ConfirmIntent::QuitNoConnection,
    );

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);
    state.modal.set_mode(InputMode::ConfirmDialog);
    state.confirm_dialog.open(
        "Confirm UPDATE: users",
        "email: \"bob@example.com\" -> \"new@example.com\"\n\nUPDATE \"public\".\"users\"\nSET \"email\" = 'new@example.com'\nWHERE \"id\" = '2';",
        ConfirmIntent::ExecuteWrite {
            sql: "UPDATE \"public\".\"users\"\nSET \"email\" = 'new@example.com'\nWHERE \"id\" = '2';".to_string(),
            blocked: false,
        },
    );

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_rich() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let sql = "UPDATE \"public\".\"users\"\nSET \"email\" = 'new@example.com'\nWHERE \"id\" = '2';"
        .to_string();
    state
        .result_interaction
        .set_write_preview(make_update_preview_with_key(
            vec![ColumnDiff {
                column: "email".to_string(),
                before: "bob@example.com".to_string(),
                after: "new@example.com".to_string(),
                json_diff: None,
            }],
            sql.clone(),
            "2",
        ));
    state.modal.set_mode(InputMode::ConfirmDialog);
    state.confirm_dialog.open(
        "Confirm UPDATE: users",
        "email: \"bob@example.com\" -> \"new@example.com\"",
        ConfirmIntent::ExecuteWrite {
            sql,
            blocked: false,
        },
    );

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_delete_preview_low_risk() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let sql = "DELETE FROM \"public\".\"users\"\nWHERE \"id\" = '3';".to_string();
    state.result_interaction.set_write_preview(WritePreview {
        operation: WriteOperation::Delete,
        sql: sql.clone(),
        target_summary: TargetSummary {
            schema: "public".to_string(),
            table: "users".to_string(),
            key_values: vec![("id".to_string(), "3".to_string())],
        },
        diff: vec![],
        guardrail: GuardrailDecision {
            risk_level: RiskLevel::Low,
            blocked: false,
            reason: None,
            target_summary: None,
        },
    });
    open_write_confirm(&mut state, "Confirm DELETE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_long_jsonb() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let long_before = r#"{"industries": ["tech", "finance", "healthcare"], "company_size": "enterprise", "preferences": {"notifications": true, "theme": "dark"}}"#;
    let long_after = r#"{"industries": ["tech", "retail"], "company_size": "startup", "preferences": {"notifications": false, "theme": "light", "language": "ja"}}"#;

    let sql = format!(
        "UPDATE \"public\".\"users\"\nSET \"metadata\" = '{long_after}'\nWHERE \"id\" = '1';"
    );
    let json_diff = compute_json_diff(long_before, long_after, 1);
    assert!(
        json_diff.is_some(),
        "expected structured JSON diff for long_jsonb snapshot"
    );
    state
        .result_interaction
        .set_write_preview(make_update_preview(
            vec![ColumnDiff {
                column: "metadata".to_string(),
                before: long_before.to_string(),
                after: long_after.to_string(),
                json_diff,
            }],
            sql.clone(),
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_jsonb_key_order_normalized() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    // before: PostgreSQL key order (industries first, spaces)
    // after: serde_json key order (alphabetical, compact)
    // Only actual change is company_size value: "500+" → "600+"
    let pg_before =
        r#"{"industries": ["technology", "finance"], "company_size": ["100-500", "500+"]}"#;
    let serde_after =
        r#"{"company_size":["100-500","600+"],"industries":["technology","finance"]}"#;

    let sql = format!(
        "UPDATE \"public\".\"users\"\nSET \"target_audience\" = '{serde_after}'\nWHERE \"id\" = '1';"
    );
    // Apply normalize_for_diff to mirror the real build_update_preview path
    let before = normalize_for_diff(pg_before);
    let after = normalize_for_diff(serde_after);
    let json_diff = compute_json_diff(&before, &after, 1);
    assert!(
        json_diff.is_some(),
        "expected structured JSON diff for key_order_normalized snapshot"
    );
    state
        .result_interaction
        .set_write_preview(make_update_preview(
            vec![ColumnDiff {
                column: "target_audience".to_string(),
                before,
                after,
                json_diff,
            }],
            sql.clone(),
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    // Both before and after should show the same key order after normalization
    // (serde_json alphabetical: company_size before industries)
    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_scrollable() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let sql = "UPDATE \"public\".\"users\"\nSET \"a\" = '1', \"b\" = '2', \"c\" = '3', \"d\" = '4', \"e\" = '5'\nWHERE \"id\" = '1';".to_string();
    state
        .result_interaction
        .set_write_preview(make_update_preview(
            vec![
                ColumnDiff {
                    column: "a".to_string(),
                    before: "old_a".to_string(),
                    after: "new_a".to_string(),
                    json_diff: None,
                },
                ColumnDiff {
                    column: "b".to_string(),
                    before: "old_b".to_string(),
                    after: "new_b".to_string(),
                    json_diff: None,
                },
                ColumnDiff {
                    column: "c".to_string(),
                    before: "old_c".to_string(),
                    after: "new_c".to_string(),
                    json_diff: None,
                },
                ColumnDiff {
                    column: "d".to_string(),
                    before: "old_d".to_string(),
                    after: "new_d".to_string(),
                    json_diff: None,
                },
                ColumnDiff {
                    column: "e".to_string(),
                    before: "old_e".to_string(),
                    after: "new_e".to_string(),
                    json_diff: None,
                },
            ],
            sql.clone(),
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    assert!(
        output.contains("Enter: Confirm │ Esc: Cancel"),
        "Scrollable preview should keep only primary actions in the hint"
    );
    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_narrow_terminal() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal_sized(40, 12);

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let long_before = r#"{"industries": ["tech", "finance"], "company_size": "enterprise"}"#;
    let long_after = r#"{"industries": ["tech"], "company_size": "startup"}"#;

    let sql = format!(
        "UPDATE \"public\".\"users\"\nSET \"metadata\" = '{long_after}'\nWHERE \"id\" = '1';"
    );
    let json_diff = compute_json_diff(long_before, long_after, 1);
    assert!(
        json_diff.is_some(),
        "expected structured JSON diff for narrow_terminal snapshot"
    );
    state
        .result_interaction
        .set_write_preview(make_update_preview(
            vec![ColumnDiff {
                column: "metadata".to_string(),
                before: long_before.to_string(),
                after: long_after.to_string(),
                json_diff,
            }],
            sql.clone(),
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_multi_column() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let sql = "UPDATE \"public\".\"users\"\nSET \"email\" = 'new@example.com', \"name\" = 'New Name'\nWHERE \"id\" = '2';".to_string();
    state
        .result_interaction
        .set_write_preview(make_update_preview_with_key(
            vec![
                ColumnDiff {
                    column: "email".to_string(),
                    before: "bob@example.com".to_string(),
                    after: "new@example.com".to_string(),
                    json_diff: None,
                },
                ColumnDiff {
                    column: "name".to_string(),
                    before: "Bob".to_string(),
                    after: "New Name".to_string(),
                    json_diff: None,
                },
            ],
            sql.clone(),
            "2",
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_jsonb_structured_diff_with_ellipsis() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    // Large nested JSON where only one deep value changes, forcing ellipsis
    let before = r#"{"alpha": 1, "beta": 2, "gamma": 3, "delta": 4, "epsilon": 5, "zeta": {"nested_a": "unchanged", "nested_b": "old_value", "nested_c": "unchanged"}, "eta": 7, "theta": 8}"#;
    let after = r#"{"alpha": 1, "beta": 2, "gamma": 3, "delta": 4, "epsilon": 5, "zeta": {"nested_a": "unchanged", "nested_b": "new_value", "nested_c": "unchanged"}, "eta": 7, "theta": 8}"#;

    let sql =
        format!("UPDATE \"public\".\"users\"\nSET \"config\" = '{after}'\nWHERE \"id\" = '1';");
    let json_diff = compute_json_diff(before, after, 1);
    assert!(
        json_diff.is_some(),
        "expected structured JSON diff for ellipsis snapshot"
    );
    state
        .result_interaction
        .set_write_preview(make_update_preview(
            vec![ColumnDiff {
                column: "config".to_string(),
                before: before.to_string(),
                after: after.to_string(),
                json_diff,
            }],
            sql.clone(),
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    assert!(
        output.contains("..."),
        "large JSON diff should contain ellipsis"
    );
    insta::assert_snapshot!(output);
}

#[test]
fn confirm_dialog_update_preview_jsonb_and_string_mixed() {
    let mut state = connected_state();
    let mut terminal = create_test_terminal();

    let _ = state
        .session
        .set_table_detail(fixtures::sample_table_detail(), 0);

    let json_before = r#"{"status": "active", "role": "admin"}"#;
    let json_after = r#"{"status": "inactive", "role": "admin"}"#;
    let json_diff = compute_json_diff(json_before, json_after, 1);
    assert!(
        json_diff.is_some(),
        "expected structured JSON diff for mixed snapshot"
    );

    let sql = "UPDATE \"public\".\"users\"\nSET \"metadata\" = '{...}', \"email\" = 'new@example.com'\nWHERE \"id\" = '1';".to_string();
    state
        .result_interaction
        .set_write_preview(make_update_preview(
            vec![
                ColumnDiff {
                    column: "metadata".to_string(),
                    before: json_before.to_string(),
                    after: json_after.to_string(),
                    json_diff,
                },
                ColumnDiff {
                    column: "email".to_string(),
                    before: "old@example.com".to_string(),
                    after: "new@example.com".to_string(),
                    json_diff: None,
                },
            ],
            sql.clone(),
        ));
    open_write_confirm(&mut state, "Confirm UPDATE: users", &sql);

    let output = render_to_string(&mut terminal, &mut state);

    insta::assert_snapshot!(output);
}