zippa-db 0.1.0

A fast, lightweight, cross-platform database client for PostgreSQL, MySQL, and SQLite.
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//! Connections in tabs of their own, and the toolbar over them.

use super::*;

#[gpui_kit::test]
fn each_connection_opens_in_a_tab_of_its_own(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "the window should start on the connection manager"
    );

    let first = connect(cx, &handle);
    assert_eq!(
        titles(cx, &handle),
        [first.config().display_name()],
        "connecting should turn the tab it was opened from into the session"
    );

    click(cx, &handle, "new-connection");
    assert_eq!(
        titles(cx, &handle),
        [first.config().display_name(), "New connection".to_string()]
    );
    assert_eq!(active(cx, &handle), 1, "a new tab comes to the front");

    let second = connect(cx, &handle);
    assert_eq!(
        titles(cx, &handle),
        [
            first.config().display_name(),
            second.config().display_name()
        ],
        "both connections should stay open"
    );
}

#[gpui_kit::test]
fn a_connection_tab_names_the_connection_and_its_database(cx: &mut TestAppContext) {
    let handle = workspace(cx);

    // An unnamed SQLite connection takes the file's name as its own, so the
    // one line it draws already says which database it is on.
    let database = connect(cx, &handle);
    let unnamed = crate::db::file_name(&database.config().database);
    assert_eq!(
        labels(cx, &handle),
        [unnamed.as_str()],
        "an unnamed connection should not repeat its database under its name"
    );

    // Named, the two are different things and both are worth a line.
    click(cx, &handle, "new-connection");
    let named = connect_named(cx, &handle, "Reporting");
    let named_file = crate::db::file_name(&named.config().database);
    assert_eq!(
        labels(cx, &handle),
        [unnamed, format!("Reporting · {named_file}")],
        "a named connection should name the database it is on"
    );
}

#[gpui_kit::test]
fn the_connection_bar_only_shows_with_more_than_one_tab(cx: &mut TestAppContext) {
    let handle = workspace(cx);

    let bar = |cx: &mut TestAppContext, expect: bool| {
        let found = cx
            .update_window(handle.window.into(), |_, window, cx| {
                window.render_frame(cx);
                window.try_find("connection-bar").is_some()
            })
            .unwrap();
        assert_eq!(found, expect, "the connection bar's visibility");
    };

    // A single connection — the manager included — needs no strip.
    bar(cx, false);

    let _database = connect(cx, &handle);
    bar(cx, false);

    click(cx, &handle, "new-connection");
    bar(cx, true);
}

#[gpui_kit::test]
fn the_shortcut_opens_the_editor_on_the_welcome_screen(cx: &mut TestAppContext) {
    let handle = workspace(cx);

    // Nothing has been clicked yet, so the keystroke has only the launcher's
    // own context to travel through.
    cx.update_window(handle.window.into(), |_, window, cx| {
        window.draw(cx).clear(cx);
        window.press("secondary-n", cx);
    })
    .unwrap();

    let welcome = handle
        .update(cx, |workspace, _, _| workspace.active_welcome_for_test())
        .unwrap()
        .expect("the active tab should be showing the connection manager");

    assert!(
        welcome.update(cx, |welcome, _| welcome.editor_open_for_test()),
        "the new-connection shortcut should open the editor on the welcome screen"
    );
    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "opening the editor should not add another connection tab"
    );
}

#[gpui_kit::test]
fn switching_connections_leaves_each_session_where_it_was(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let _first = connect(cx, &handle);

    let sql_of = |cx: &mut TestAppContext| {
        let session = handle
            .update(cx, |workspace, _, _| workspace.active_session_for_test())
            .unwrap()
            .expect("the active tab should be a session");
        session.update(cx, |session, cx| session.active_sql(cx))
    };
    let type_sql = |cx: &mut TestAppContext, sql: &str| {
        let session = handle
            .update(cx, |workspace, _, _| workspace.active_session_for_test())
            .unwrap()
            .expect("the active tab should be a session");
        handle
            .update(cx, |_, window, cx| {
                session.update(cx, |session, cx| {
                    session.prepare_active_editor_for_test(sql, window, cx)
                })
            })
            .unwrap();
    };

    type_sql(cx, "select 1");

    click(cx, &handle, "new-connection");
    let _second = connect(cx, &handle);
    type_sql(cx, "select 2");

    // Back one tab, and the first session is as it was left.
    cx.update_window(handle.window.into(), |_, window, cx| {
        window.draw(cx).clear(cx);
        window.press("secondary-shift-[", cx);
    })
    .unwrap();

    assert_eq!(
        active(cx, &handle),
        0,
        "the shortcut should step back a tab"
    );
    assert_eq!(sql_of(cx), "select 1");

    cx.update_window(handle.window.into(), |_, window, cx| {
        window.press("secondary-shift-]", cx);
    })
    .unwrap();

    assert_eq!(active(cx, &handle), 1);
    assert_eq!(sql_of(cx), "select 2");
}

#[gpui_kit::test]
fn closing_a_connection_tab_leaves_the_others(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let _first = connect(cx, &handle);
    click(cx, &handle, "new-connection");
    let second = connect(cx, &handle);

    click(cx, &handle, "close-connection-0");

    assert_eq!(
        titles(cx, &handle),
        [second.config().display_name()],
        "closing one connection should leave the other open"
    );
    assert_eq!(active(cx, &handle), 0);
}

#[gpui_kit::test]
fn closing_the_last_connection_leaves_the_manager(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let _database = connect(cx, &handle);

    // A lone connection has no strip to close it from, so the shortcut (and
    // the menu item behind it) is the way out.
    press_workspace(cx, &handle, "secondary-shift-w");

    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "the window always has a tab to open a connection from"
    );
}

#[gpui_kit::test]
fn closing_a_connection_tab_with_unsaved_changes_asks_first(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let first = connect(cx, &handle);

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the active tab should be a session");
    handle
        .update(cx, |_, window, cx| {
            session.update(cx, |session, cx| {
                session.prepare_active_editor_for_test("select 1", window, cx)
            })
        })
        .unwrap();

    press_workspace(cx, &handle, "secondary-shift-w");
    assert_eq!(
        titles(cx, &handle),
        [first.config().display_name()],
        "a connection with unsaved changes should not close until the question is answered"
    );

    // Saying no leaves the tab open, still connected.
    click(cx, &handle, "cancel");
    assert_eq!(titles(cx, &handle), [first.config().display_name()]);

    // Saying yes closes it despite the unsaved changes.
    press_workspace(cx, &handle, "secondary-shift-w");
    click(cx, &handle, "ok");
    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "confirming should close the connection"
    );
}

#[gpui_kit::test]
fn disconnecting_returns_the_tab_to_the_manager(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let first = connect(cx, &handle);
    click(cx, &handle, "new-connection");
    let _second = connect(cx, &handle);

    press_workspace(cx, &handle, "secondary-d");

    assert_eq!(
        titles(cx, &handle),
        [first.config().display_name(), "New connection".to_string()],
        "disconnecting should keep the tab and show the manager in it"
    );
    assert_eq!(
        active(cx, &handle),
        1,
        "the tab that was disconnected stays in front"
    );
}

#[gpui_kit::test]
fn disconnecting_with_unsaved_changes_asks_first(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let first = connect(cx, &handle);

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the active tab should be a session");
    handle
        .update(cx, |_, window, cx| {
            session.update(cx, |session, cx| {
                session.prepare_active_editor_for_test("select 1", window, cx)
            })
        })
        .unwrap();

    press_workspace(cx, &handle, "secondary-d");
    assert_eq!(
        titles(cx, &handle),
        [first.config().display_name()],
        "a connection with unsaved changes should not disconnect until the question is answered"
    );

    // Saying no leaves it connected.
    click(cx, &handle, "cancel");
    assert_eq!(titles(cx, &handle), [first.config().display_name()]);
    cx.run_until_parked();

    // The keystroke goes to whatever holds the focus, so put it back in the
    // session rather than depending on where the closed dialog leaves it.
    handle
        .update(cx, |_, window, cx| {
            session.update(cx, |session, cx| session.focus(window, cx));
        })
        .unwrap();

    // Saying yes disconnects it, keeping the tab for the next connection.
    press_workspace(cx, &handle, "secondary-d");
    click(cx, &handle, "ok");
    cx.run_until_parked();
    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "confirming should disconnect the connection"
    );
}

#[gpui_kit::test]
fn the_toolbar_buttons_are_disabled_without_a_connection(cx: &mut TestAppContext) {
    let handle = workspace(cx);

    // Disabled buttons still register a click; only their effect proves the
    // disabled state, since gpui-component's Button does not surface it to
    // the accessibility tree that the test harness reads.
    click(cx, &handle, "refresh");
    click(cx, &handle, "new-query");

    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "refresh and new-query should have no session to act on"
    );
}

#[gpui_kit::test]
fn the_toolbar_new_query_button_opens_a_tab(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let _database = connect(cx, &handle);

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the active tab should be a session");
    assert_eq!(
        session.read_with(cx, |session, cx| session.tab_titles(cx)),
        ["Query 1"]
    );

    click(cx, &handle, "new-query");

    assert_eq!(
        session.read_with(cx, |session, cx| session.tab_titles(cx)),
        ["Query 1", "Query 2"],
        "the toolbar button should behave like the session's own new-tab button"
    );
}

#[gpui_kit::test]
fn many_connection_tabs_scroll_inside_the_bar(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let _database = connect(cx, &handle);
    for _ in 0..20 {
        click(cx, &handle, "new-connection");
    }

    // The outer bar wraps every screen, so a connection tab strip that grew
    // with its tabs would push the session — and the toolbar above it — out of
    // the window.
    cx.update_window(handle.window.into(), |_, window, cx| {
        window.render_frame(cx);
        let viewport = Bounds {
            origin: Default::default(),
            size: size(px(WINDOW.0), px(WINDOW.1)),
        };
        for id in ["new-connection", "welcome-new-connection"] {
            let found = window.find(id);
            assert!(
                contains(viewport, found.bounds()),
                "{id} ran outside the window with 21 connection tabs open: {:?}",
                found.bounds()
            );
        }
    })
    .unwrap();
}

#[gpui_kit::test]
fn restored_connections_reopen_their_tabs_in_order(cx: &mut TestAppContext) {
    let database = runtime::block_on(TempDatabase::new());
    let config = database.config();
    let state = WorkspaceState {
        active: 0,
        sessions: vec![SessionState {
            connection: config.id,
            database: Some(config.database.clone()),
            active: 1,
            panels: vec![
                PanelState::Query {
                    title: "Query 1".into(),
                    sql: "select 1".into(),
                    file: None,
                },
                PanelState::Query {
                    title: "Query 2".into(),
                    sql: "select 2".into(),
                    file: None,
                },
            ],
        }],
        ..WorkspaceState::default()
    };

    let (_dir, handle) = restored_workspace(cx, state, vec![config.clone()]);
    cx.run_until_parked();

    assert_eq!(
        titles(cx, &handle),
        [config.display_name()],
        "the saved connection should reopen as a session tab"
    );
    assert_eq!(active(cx, &handle), 0);

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the restored connection should have become a session");
    assert_eq!(
        session.read_with(cx, |session, cx| session.tab_titles(cx)),
        ["Query 1", "Query 2"],
        "the tabs should come back in order, with no leftover empty editor"
    );
    assert_eq!(
        session.read_with(cx, |session, cx| session.active_sql(cx)),
        "select 2",
        "the saved active tab should be the one showing"
    );
}

#[gpui_kit::test]
fn a_restored_file_tab_reads_its_file_to_decide_dirty(cx: &mut TestAppContext) {
    let file_dir = ScratchDir::new();
    let file = file_dir.file("report.sql", "select 1;\n");

    let database = runtime::block_on(TempDatabase::new());
    let config = database.config();
    let state = WorkspaceState {
        sessions: vec![SessionState {
            connection: config.id,
            database: Some(config.database.clone()),
            active: 0,
            panels: vec![PanelState::Query {
                title: "report.sql".into(),
                // The saved buffer differs from what is on disk, so the tab is
                // unsaved work once the file has been read back.
                sql: "select 2;\n".into(),
                file: Some(file),
            }],
        }],
        ..WorkspaceState::default()
    };

    let (_config_dir, handle) = restored_workspace(cx, state, vec![config]);
    cx.run_until_parked();

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the restored connection should have become a session");
    assert!(
        session.read_with(cx, |session, cx| session.tab_is_dirty_for_test(0, cx)),
        "a restored buffer that differs from its file should show as unsaved"
    );
}

#[gpui_kit::test]
fn a_restored_table_tab_replaces_the_empty_editor(cx: &mut TestAppContext) {
    let database = runtime::block_on(TempDatabase::new());
    let config = database.config();
    let state = WorkspaceState {
        sessions: vec![SessionState {
            connection: config.id,
            database: Some(config.database.clone()),
            active: 0,
            panels: vec![PanelState::Table {
                object: DatabaseObject {
                    schema: None,
                    name: "items".into(),
                    kind: ObjectKind::Table,
                },
                filters: Vec::new(),
            }],
        }],
        ..WorkspaceState::default()
    };

    let (_dir, handle) = restored_workspace(cx, state, vec![config]);
    cx.run_until_parked();

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the restored connection should have become a session");
    assert_eq!(
        session.read_with(cx, |session, cx| session.tab_titles(cx)),
        ["items"],
        "a restored table should not leave the empty editor beside it"
    );
}

#[gpui_kit::test]
fn a_restore_with_no_tabs_keeps_the_empty_editor(cx: &mut TestAppContext) {
    let database = runtime::block_on(TempDatabase::new());
    let config = database.config();
    let state = WorkspaceState {
        sessions: vec![SessionState {
            connection: config.id,
            database: Some(config.database.clone()),
            active: 0,
            panels: Vec::new(),
        }],
        ..WorkspaceState::default()
    };

    let (_dir, handle) = restored_workspace(cx, state, vec![config]);
    cx.run_until_parked();

    let session = handle
        .update(cx, |workspace, _, _| workspace.active_session_for_test())
        .unwrap()
        .expect("the restored connection should have become a session");
    assert_eq!(
        session.read_with(cx, |session, cx| session.tab_titles(cx)),
        ["Query 1"],
        "a session always shows one editor"
    );
}

#[gpui_kit::test]
fn a_connection_deleted_since_the_last_run_is_skipped(cx: &mut TestAppContext) {
    let database = runtime::block_on(TempDatabase::new());
    let config = database.config();
    let state = WorkspaceState {
        sessions: vec![
            SessionState {
                connection: config.id,
                database: Some(config.database.clone()),
                active: 0,
                panels: vec![PanelState::Query {
                    title: "Query 1".into(),
                    sql: "select 1".into(),
                    file: None,
                }],
            },
            SessionState {
                connection: Uuid::new_v4(),
                database: None,
                active: 0,
                panels: vec![PanelState::Query {
                    title: "Query 1".into(),
                    sql: "select 2".into(),
                    file: None,
                }],
            },
        ],
        ..WorkspaceState::default()
    };

    let (_dir, handle) = restored_workspace(cx, state, vec![config.clone()]);
    cx.run_until_parked();

    assert_eq!(
        titles(cx, &handle),
        [config.display_name()],
        "a connection that no longer exists should not get a tab"
    );
}

#[gpui_kit::test]
fn a_failed_reconnect_keeps_its_tabs_for_retrying(cx: &mut TestAppContext) {
    // A SQLite file that is not there: `open` refuses to create it, so the
    // connection fails the way a server being down would.
    let mut config = ConnectionConfig::new(Engine::Sqlite);
    config.database = std::env::temp_dir()
        .join(format!("zippa-missing-{}.sqlite", Uuid::new_v4()))
        .to_string_lossy()
        .into_owned();
    let state = WorkspaceState {
        sessions: vec![SessionState {
            connection: config.id,
            database: Some(config.database.clone()),
            active: 0,
            panels: vec![PanelState::Query {
                title: "Query 1".into(),
                sql: "select 1".into(),
                file: None,
            }],
        }],
        ..WorkspaceState::default()
    };

    let (_dir, handle) = restored_workspace(cx, state.clone(), vec![config]);
    cx.run_until_parked();

    assert_eq!(
        titles(cx, &handle),
        ["New connection"],
        "a connection that cannot be reopened stays on the launcher"
    );
    assert_eq!(
        handle
            .view
            .read_with(cx, |workspace, cx| workspace.state_for_test(cx)),
        state,
        "the tabs should still be recorded while the connect is outstanding"
    );
}

#[gpui_kit::test]
fn a_restored_state_round_trips_through_snapshot(cx: &mut TestAppContext) {
    let database = runtime::block_on(TempDatabase::new());
    let config = database.config();
    let state = WorkspaceState {
        active: 0,
        sessions: vec![SessionState {
            connection: config.id,
            database: Some(config.database.clone()),
            active: 1,
            panels: vec![
                PanelState::Query {
                    title: "Query 1".into(),
                    sql: "select 1".into(),
                    file: None,
                },
                PanelState::Table {
                    object: DatabaseObject {
                        schema: None,
                        name: "items".into(),
                        kind: ObjectKind::Table,
                    },
                    filters: vec![FilterSpec {
                        column: "name".into(),
                        operator: Operator::Equals,
                        value: "alpha".into(),
                    }],
                },
            ],
        }],
        ..WorkspaceState::default()
    };

    let (_dir, handle) = restored_workspace(cx, state.clone(), vec![config]);
    cx.run_until_parked();

    let round_tripped = handle
        .view
        .read_with(cx, |workspace, cx| workspace.state_for_test(cx));
    assert_eq!(
        round_tripped, state,
        "a restored window should snapshot back to the state it came from, filters included"
    );
}

#[gpui_kit::test]
fn clicking_a_saved_connection_opens_it(cx: &mut TestAppContext) {
    let handle = workspace(cx);
    let (_database, id) = saved_connection(cx, &handle);
    let target = gpui_kit::SharedString::from(format!("connect-{id}"));

    // A card is the connect target now, so a single click opens it.
    cx.update_window(handle.window.into(), |_, window, cx| {
        window.draw(cx).clear(cx);
        window.click(target.clone(), cx);
    })
    .unwrap();
    cx.run_until_parked();

    assert!(
        handle
            .update(cx, |workspace, _, _| workspace
                .active_session_for_test()
                .is_some())
            .unwrap(),
        "a click on the card should open the connection"
    );
}

#[gpui_kit::test]
fn a_recorded_crash_opens_its_report(cx: &mut TestAppContext) {
    let dir = std::env::temp_dir().join(format!("zippa-db-crash-ui-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&dir).expect("could not create the scratch directory");
    crate::db::store::set_config_dir_for_test(dir.clone());
    std::fs::write(
        dir.join("crashes.log"),
        "=== Zippa DB crashed at 2026-09-26 ===\nmessage: boom\n",
    )
    .expect("could not write the crash log");

    let handle = workspace_from(cx, WorkspaceState::default());
    cx.run_until_parked();

    assert!(
        workspace_dialog_open(cx, &handle),
        "the recorded crash should open its report as a dialog"
    );
    let _ = std::fs::remove_dir_all(&dir);
}