From fcf05ac21a986afaa16340a88972bbac84aa722f Mon Sep 17 00:00:00 2001 From: David Dworken Date: Thu, 26 Oct 2023 17:42:50 -0700 Subject: [PATCH] Add tests for errors in testTui --- client/client_test.go | 29 ++++++++++++++++++++-- client/lib/goldens/TestTui-Offline | 29 ++++++++++++++++++++++ client/lib/goldens/TestTui-OfflineInvalid | 30 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 client/lib/goldens/TestTui-Offline create mode 100644 client/lib/goldens/TestTui-OfflineInvalid diff --git a/client/client_test.go b/client/client_test.go index 3fb3528..5a263eb 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -104,6 +104,7 @@ func TestParam(t *testing.T) { t.Run("testTui/resize", testTui_resize) t.Run("testTui/delete", testTui_delete) t.Run("testTui/color", testTui_color) + t.Run("testTui/errors", testTui_errors) // Assert there are no leaked connections assertNoLeakedConnections(t) @@ -1609,8 +1610,6 @@ func TestFish(t *testing.T) { testutils.CompareGoldens(t, out, "TestFish-table") } -// TODO(ddworken): Run TestTui in online and offline mode - func setupTestTui(t testing.TB, onlineStatus OnlineStatus) (shellTester, string, *gorm.DB) { tester := zshTester{} userSecret := installWithOnlineStatus(t, tester, onlineStatus) @@ -1906,6 +1905,32 @@ func testTui_general(t *testing.T, onlineStatus OnlineStatus) { assertNoLeakedConnections(t) } +func testTui_errors(t *testing.T) { + // Setup + defer testutils.BackupAndRestore(t)() + tester, _, _ := setupTestTui(t, Online) + + // Check the output when the device is offline + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1") + out := captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery ENTER"}) + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "") + if len(strings.Split(out, "hishtory tquery")) != 2 { + t.Fatalf("failed to split out=%#v", out) + } + out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) + testutils.CompareGoldens(t, out, "TestTui-Offline") + + // Check the output when the device is offline AND there is an invalid search + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1") + out = captureTerminalOutput(t, tester, []string{"hishtory SPACE tquery ENTER", "ls:"}) + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "") + if len(strings.Split(out, "hishtory tquery")) != 2 { + t.Fatalf("failed to split out=%#v", out) + } + out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1]) + testutils.CompareGoldens(t, out, "TestTui-OfflineInvalid") +} + func testControlR(t *testing.T, tester shellTester, shellName string, onlineStatus OnlineStatus) { // Setup defer testutils.BackupAndRestore(t)() diff --git a/client/lib/goldens/TestTui-Offline b/client/lib/goldens/TestTui-Offline new file mode 100644 index 0000000..5458114 --- /dev/null +++ b/client/lib/goldens/TestTui-Offline @@ -0,0 +1,29 @@ +Warning: failed to contact the hishtory backend (are you offline?), so some results may be stale + +Search Query: > ls + +┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ Hostname CWD Timestamp Runtime Exit Code Command │ +│────────────────────────────────────────────────────────────────────────────────────────────────────────│ +│ localhost /tmp/ Oct 17 2022 21:43:21 PDT 3s 2 echo 'aaaaaa bbbb' │ +│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────────────────────────────────────────────────────────────────────────────────────────┘ +hiSHtory: Search your shell history • ctrl+h help \ No newline at end of file diff --git a/client/lib/goldens/TestTui-OfflineInvalid b/client/lib/goldens/TestTui-OfflineInvalid new file mode 100644 index 0000000..c817fcf --- /dev/null +++ b/client/lib/goldens/TestTui-OfflineInvalid @@ -0,0 +1,30 @@ +Warning: failed to contact the hishtory backend (are you offline?), so some results may be stale +Warning: failed to search: search query contains unknown search atom 'ls' that doesn't match any column names + +Search Query: > ls: + +┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +│ Hostname CWD Timestamp Runtime Exit Code Command │ +│────────────────────────────────────────────────────────────────────────────────────────────────────────│ +│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +│ │ +└────────────────────────────────────────────────────────────────────────────────────────────────────────┘ +hiSHtory: Search your shell history • ctrl+h help \ No newline at end of file