Persist undo/redo history across runs with a 20-entry cap.
Serialize create/update/delete commands to DataDir/history.json and reload them on startup via OpenHistory. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package command_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"task/internal/command"
|
||||
"task/internal/ticket"
|
||||
)
|
||||
|
||||
func TestHistoryPersistsAcrossOpen(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store := ticket.NewJSONStore(dir)
|
||||
path := command.HistoryPath(dir)
|
||||
|
||||
h1, err := command.OpenHistory(path, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := h1.Do(&command.CreateTicket{
|
||||
Store: store,
|
||||
Ticket: ticket.Ticket{
|
||||
Title: "Persist me", ProjectID: "inbox", Status: ticket.StatusTodo,
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Fatalf("history file missing: %v", err)
|
||||
}
|
||||
|
||||
h2, err := command.OpenHistory(path, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !h2.CanUndo() {
|
||||
t.Fatal("expected undo after reload")
|
||||
}
|
||||
if err := h2.Undo(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list, err := store.List("inbox")
|
||||
if err != nil || len(list) != 0 {
|
||||
t.Fatalf("after persistent undo: %v len=%d", err, len(list))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryCapsAtMax(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store := ticket.NewJSONStore(dir)
|
||||
h, err := command.OpenHistory(filepath.Join(dir, "history.json"), store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < command.MaxHistory+5; i++ {
|
||||
if err := h.Do(&command.CreateTicket{
|
||||
Store: store,
|
||||
Ticket: ticket.Ticket{
|
||||
Title: "t", ProjectID: "inbox", Status: ticket.StatusTodo,
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Count undos available
|
||||
n := 0
|
||||
for h.CanUndo() {
|
||||
if err := h.Undo(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n++
|
||||
if n > command.MaxHistory+1 {
|
||||
t.Fatal("undo stack larger than cap")
|
||||
}
|
||||
}
|
||||
if n != command.MaxHistory {
|
||||
t.Fatalf("undo count = %d, want %d", n, command.MaxHistory)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user