From 6dc366fe47096380a28b084cc56827c55aa2cf95 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Fri, 24 Nov 2023 16:07:54 -0800 Subject: [PATCH] Add test for presaving while offline --- client/client_test.go | 44 ++++++++++++++++++- .../testPresavingOffline-query-missing | 1 + .../testPresavingOffline-query-present | 2 + 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 client/lib/goldens/testPresavingOffline-query-missing create mode 100644 client/lib/goldens/testPresavingOffline-query-present diff --git a/client/client_test.go b/client/client_test.go index 288b675..a1f0473 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -94,6 +94,7 @@ func TestParam(t *testing.T) { t.Run("testCustomColumns/"+tester.ShellName(), func(t *testing.T) { testCustomColumns(t, tester) }) t.Run("testUninstall/"+tester.ShellName(), func(t *testing.T) { testUninstall(t, tester) }) t.Run("testPresaving/"+tester.ShellName(), func(t *testing.T) { testPresaving(t, tester, tester.ShellName()) }) + t.Run("testPresavingOffline/"+tester.ShellName(), func(t *testing.T) { testPresavingOffline(t, tester) }) t.Run("testPresavingDisabled/"+tester.ShellName(), func(t *testing.T) { testPresavingDisabled(t, tester) }) t.Run("testControlR/online/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester, tester.ShellName(), Online) }) t.Run("testControlR/offline/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester, tester.ShellName(), Offline) }) @@ -2179,6 +2180,48 @@ func testPresavingDisabled(t *testing.T, tester shellTester) { } } +func testPresavingOffline(t *testing.T, tester shellTester) { + // Setup + defer testutils.BackupAndRestore(t)() + defer testutils.BackupAndRestoreEnv("HISHTORY_SIMULATE_NETWORK_ERROR")() + userSecret := installHishtory(t, tester, "") + + // Enable the presaving feature + require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get presaving`))) + tester.RunInteractiveShell(t, `hishtory config-set presaving true`) + require.Equal(t, "true", strings.TrimSpace(tester.RunInteractiveShell(t, `hishtory config-get presaving`))) + + // Simulate a network error when presaving + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "1") + require.NoError(t, os.Chdir("/")) + require.NoError(t, tester.RunInteractiveShellBackground(t, `sleep 13371336`)) + + // Check the exported data locally + tester.RunInteractiveShell(t, ` hishtory config-set displayed-columns CWD Runtime Command`) + out := tester.RunInteractiveShell(t, ` hishtory query sleep -tquery -query`) + testutils.CompareGoldens(t, out, "testPresavingOffline-query-present") + + // And check it on another device where it isn't yet available + os.Setenv("HISHTORY_SIMULATE_NETWORK_ERROR", "") + restoreDevice1 := testutils.BackupAndRestoreWithId(t, "device1") + installHishtory(t, tester, userSecret) + tester.RunInteractiveShell(t, ` hishtory config-set displayed-columns CWD Runtime Command`) + out = tester.RunInteractiveShell(t, ` hishtory query sleep -tquery -query`) + testutils.CompareGoldens(t, out, "testPresavingOffline-query-missing") + + // Then go back to the first device and restore the internet connection so that it uploads the presaved entry + restoreDevice2 := testutils.BackupAndRestoreWithId(t, "device2") + restoreDevice1() + tester.RunInteractiveShell(t, `echo any_command_to_trigger_reupload`) + out = tester.RunInteractiveShell(t, ` hishtory query sleep -tquery -query`) + testutils.CompareGoldens(t, out, "testPresavingOffline-query-present") + + // And check that it is now present on the second device + restoreDevice2() + out = tester.RunInteractiveShell(t, ` hishtory query sleep -tquery -query`) + testutils.CompareGoldens(t, out, "testPresavingOffline-query-present") +} + func testPresaving(t *testing.T, tester shellTester, shellName string) { // Setup defer testutils.BackupAndRestore(t)() @@ -2660,4 +2703,3 @@ func TestAugmentedIsOfflineError(t *testing.T) { } // TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed -// TODO: Tests for deleting presaved entries while offline diff --git a/client/lib/goldens/testPresavingOffline-query-missing b/client/lib/goldens/testPresavingOffline-query-missing new file mode 100644 index 0000000..93cb4bc --- /dev/null +++ b/client/lib/goldens/testPresavingOffline-query-missing @@ -0,0 +1 @@ +CWD Runtime Command diff --git a/client/lib/goldens/testPresavingOffline-query-present b/client/lib/goldens/testPresavingOffline-query-present new file mode 100644 index 0000000..d745cc6 --- /dev/null +++ b/client/lib/goldens/testPresavingOffline-query-present @@ -0,0 +1,2 @@ +CWD Runtime Command +/ N/A sleep 13371336