summaryrefslogtreecommitdiff
path: root/internal/ui/table_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-08 15:44:30 +0300
committerPaul Buetow <paul@buetow.org>2026-04-08 16:14:11 +0300
commit4855df91e6ef9b7cc3e8278e2cb349f8e0d36dad (patch)
tree3eedc25419021c4221e3c6be745737406dd1a5e1 /internal/ui/table_test.go
parente2394dab62d54a3ae40044c06a7fec0b953a78e0 (diff)
Fix task 2: description editor temp-file cleanup
Diffstat (limited to 'internal/ui/table_test.go')
-rw-r--r--internal/ui/table_test.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go
index 3d3601e..95f9b1b 100644
--- a/internal/ui/table_test.go
+++ b/internal/ui/table_test.go
@@ -12,6 +12,8 @@ import (
tea "charm.land/bubbletea/v2"
"github.com/charmbracelet/x/ansi"
+
+ "codeberg.org/snonux/tasksamurai/internal/task"
)
func TestAnnotateHotkey(t *testing.T) {
@@ -136,6 +138,96 @@ func TestReplaceAnnotationHotkey(t *testing.T) {
}
}
+func TestHandleDescEditDoneUpdatesDescriptionAndRemovesTempFile(t *testing.T) {
+ tmp := t.TempDir()
+ taskPath := filepath.Join(tmp, "task")
+ logFile := filepath.Join(tmp, "log.txt")
+
+ script := "#!/bin/sh\n" +
+ "if echo \"$@\" | grep -q export; then\n" +
+ " echo '{\"id\":1,\"uuid\":\"x\",\"description\":\"old description\",\"status\":\"pending\",\"entry\":\"\",\"priority\":\"\",\"urgency\":0}'\n" +
+ " exit 0\n" +
+ "fi\n" +
+ "printf '%s\\n' \"$@\" >> " + logFile + "\n"
+
+ if err := os.WriteFile(taskPath, []byte(script), 0o755); err != nil {
+ t.Fatal(err)
+ }
+
+ origPath := os.Getenv("PATH")
+ os.Setenv("PATH", tmp+":"+origPath)
+ t.Cleanup(func() { os.Setenv("PATH", origPath) })
+
+ os.Setenv("TASKDATA", tmp)
+ os.Setenv("TASKRC", "/dev/null")
+ t.Cleanup(func() {
+ os.Unsetenv("TASKDATA")
+ os.Unsetenv("TASKRC")
+ })
+
+ m, err := New(nil, "firefox")
+ if err != nil {
+ t.Fatalf("New: %v", err)
+ }
+ m.currentTaskDetail = &task.Task{ID: 1, Description: "old description"}
+ m.showTaskDetail = true
+ m.detailDescEditing = true
+
+ tempFile := filepath.Join(tmp, "desc.txt")
+ if err := os.WriteFile(tempFile, []byte("updated description\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ mv, cmd := (&m).handleDescEditDone(descEditDoneMsg{tempFile: tempFile})
+ m = *mv.(*Model)
+
+ if m.detailDescEditing {
+ t.Fatalf("description editor state was not cleared")
+ }
+ if cmd == nil {
+ t.Fatalf("handleDescEditDone did not return a reload command")
+ }
+ if _, err := os.Stat(tempFile); !os.IsNotExist(err) {
+ t.Fatalf("temp file still exists after handler: %v", err)
+ }
+
+ logData, err := os.ReadFile(logFile)
+ if err != nil {
+ t.Fatalf("read log: %v", err)
+ }
+ if !strings.Contains(string(logData), "1\nmodify\ndescription:updated description") {
+ t.Fatalf("description update did not reach task command: %s", logData)
+ }
+}
+
+func TestHandleDescEditDoneRemovesTempFileOnEditorError(t *testing.T) {
+ tmp := t.TempDir()
+ tempFile := filepath.Join(tmp, "desc.txt")
+ if err := os.WriteFile(tempFile, []byte("ignored"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ m := Model{detailViewState: detailViewState{detailDescEditing: true}}
+ mv, cmd := (&m).handleDescEditDone(descEditDoneMsg{
+ err: fmt.Errorf("editor failed"),
+ tempFile: tempFile,
+ })
+ m = *mv.(*Model)
+
+ if m.detailDescEditing {
+ t.Fatalf("description editor state was not cleared after error")
+ }
+ if cmd == nil {
+ t.Fatalf("error path did not return a status-clear command")
+ }
+ if _, err := os.Stat(tempFile); !os.IsNotExist(err) {
+ t.Fatalf("temp file still exists after error path: %v", err)
+ }
+ if !strings.Contains(m.statusMsg, "Edit error: editor failed") {
+ t.Fatalf("unexpected status message: %q", m.statusMsg)
+ }
+}
+
func TestDoneHotkey(t *testing.T) {
tmp := t.TempDir()
taskPath := filepath.Join(tmp, "task")