Files
FWS-Kitty/kittens/file_manager/sync_test.go
T
BlackAngelTV 5d2e28b144 feat(file-manager): move the shell while browsing the panel
Opening a directory in the panel now hands it to the shell, so walking the tree
reads like cd and the two stay on the same directory. The request is still read
at the shell's next prompt, and quitting without validating withdraws a request
the shell has not read yet, so Esc keeps leaving the shell where it was.
2026-11-11 08:48:03 +01:00

151 lines
4.6 KiB
Go

// License: GPLv3 Copyright: 2026, FWS
package file_manager
import (
"os"
"path/filepath"
"testing"
)
func TestNewShellSyncNeedsOptionDirectoryAndWindow(t *testing.T) {
dir := t.TempDir()
if s := new_shell_sync(dir, "7", false); s != nil {
t.Fatal("the sync must stay off until the option is enabled")
}
if s := new_shell_sync("", "7", true); s != nil {
t.Fatal("the sync must be off without a session directory")
}
if s := new_shell_sync(dir, "", true); s != nil {
t.Fatal("the sync must be off without a shell window id")
}
if s := new_shell_sync(filepath.Join(dir, "missing"), "7", true); s != nil {
t.Fatal("the sync must be off when the session directory is missing")
}
if s := new_shell_sync(dir, "7", true); s == nil {
t.Fatal("the sync must be on with an option, a directory and a window id")
}
}
func TestPublishRequestIsReadableAndLeavesNoTemporaryFile(t *testing.T) {
dir := t.TempDir()
s := new_shell_sync(dir, "42", true)
if err := s.publish_request("/tmp/target dir"); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(filepath.Join(dir, "42.set"))
if err != nil {
t.Fatal(err)
}
if string(got) != "/tmp/target dir" {
t.Fatalf("unexpected request payload %q", got)
}
if _, err = os.Stat(filepath.Join(dir, "42.set.tmp")); err == nil {
t.Fatal("the temporary file must be renamed, not left behind")
}
}
func TestShellHasHookNeedsAPublishedDirectory(t *testing.T) {
dir := t.TempDir()
s := new_shell_sync(dir, "5", true)
if s.shell_has_hook() {
t.Fatal("the shell published nothing yet")
}
if err := s.publish_request(dir); err != nil {
t.Fatal(err)
}
if s.shell_has_hook() {
t.Fatal("a request must not be mistaken for a published directory")
}
if err := os.WriteFile(filepath.Join(dir, "5"), []byte(dir), 0o600); err != nil {
t.Fatal(err)
}
if !s.shell_has_hook() {
t.Fatal("a published directory means the shell has the hook")
}
}
func TestPublishBrowseNeedsTheShellHookAndCanBeWithdrawn(t *testing.T) {
dir := t.TempDir()
h := &Handler{sync: new_shell_sync(dir, "11", true), cwd: dir}
h.publish_browse()
if _, err := os.Stat(filepath.Join(dir, "11.set")); err == nil {
t.Fatal("the panel must not publish before the shell proved it has the hook")
}
if h.sync_published {
t.Fatal("nothing was published")
}
if err := os.WriteFile(filepath.Join(dir, "11"), []byte(dir), 0o600); err != nil {
t.Fatal(err)
}
h.publish_browse()
if got, err := os.ReadFile(filepath.Join(dir, "11.set")); err != nil || string(got) != dir {
t.Fatalf("unexpected request %q (%v)", got, err)
}
if !h.sync_published {
t.Fatal("the request must be remembered so that quitting can withdraw it")
}
h.withdraw_sync_request()
if _, err := os.Stat(filepath.Join(dir, "11.set")); err == nil {
t.Fatal("quitting must withdraw the pending request")
}
if h.sync_published {
t.Fatal("nothing is pending anymore")
}
h.withdraw_sync_request()
}
func TestShellCwdIgnoresUnusablePaths(t *testing.T) {
dir := t.TempDir()
s := new_shell_sync(dir, "9", true)
published := filepath.Join(dir, "9")
for _, content := range []string{"", " \n", "relative/path", filepath.Join(dir, "gone")} {
if err := os.WriteFile(published, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if got := s.shell_cwd(); got != "" {
t.Fatalf("%q must not be followed, got %q", content, got)
}
}
if got := s.shell_cwd(); got != "" {
t.Fatalf("a missing file must not be followed, got %q", got)
}
}
func TestNewTargetFollowsEachShellMoveOnce(t *testing.T) {
dir := t.TempDir()
s := new_shell_sync(dir, "3", true)
moved := t.TempDir()
s.last = dir
if got := s.new_target(dir); got != "" {
t.Fatalf("the shell has not moved yet, got %q", got)
}
if err := os.WriteFile(filepath.Join(dir, "3"), []byte(moved), 0o600); err != nil {
t.Fatal(err)
}
if got := s.new_target(dir); got != moved {
t.Fatalf("wanted %q, got %q", moved, got)
}
if got := s.new_target(dir); got != "" {
t.Fatalf("a shell move must only be followed once, got %q", got)
}
if err := os.WriteFile(filepath.Join(dir, "3"), []byte(dir), 0o600); err != nil {
t.Fatal(err)
}
if got := s.new_target(dir); got != "" {
t.Fatalf("moving to the directory already displayed must be a no-op, got %q", got)
}
}
func TestResultPayloadAdvertisesSyncOnlyWhenHandled(t *testing.T) {
h := &Handler{result_action: "cd", result_path: "/tmp"}
if payload := h.result_payload(); len(payload) != 2 {
t.Fatalf("the shell must be typed into when the sync did not handle the move: %#v", payload)
}
h.sync_handled = true
payload := h.result_payload()
if payload["sync"] != "1" || payload["path"] != "/tmp" {
t.Fatalf("unexpected payload: %#v", payload)
}
}