75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package history
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestAppendAndList(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "history.json")
|
|
s := New(path, 10)
|
|
|
|
s.Append(Entry{Text: "first", Origin: "a", Ts: 1})
|
|
s.Append(Entry{Text: "second", Origin: "b", Ts: 2})
|
|
|
|
got := s.List()
|
|
if len(got) != 2 {
|
|
t.Fatalf("len = %d, want 2", len(got))
|
|
}
|
|
if got[0].Text != "first" || got[1].Text != "second" {
|
|
t.Errorf("order wrong: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestAppendSkipsDuplicate(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "history.json")
|
|
s := New(path, 10)
|
|
|
|
s.Append(Entry{Text: "dup", Origin: "a", Ts: 1})
|
|
s.Append(Entry{Text: "dup", Origin: "a", Ts: 2})
|
|
|
|
if len(s.List()) != 1 {
|
|
t.Errorf("len = %d, want 1 (duplicate skipped)", len(s.List()))
|
|
}
|
|
}
|
|
|
|
func TestTrimToMax(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "history.json")
|
|
s := New(path, 3)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
s.Append(Entry{Text: string(rune('a' + i)), Origin: "a", Ts: int64(i)})
|
|
}
|
|
|
|
got := s.List()
|
|
if len(got) != 3 {
|
|
t.Fatalf("len = %d, want 3", len(got))
|
|
}
|
|
if got[0].Text != "h" {
|
|
t.Errorf("oldest kept = %q, want \"h\"", got[0].Text)
|
|
}
|
|
}
|
|
|
|
func TestPersistence(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "history.json")
|
|
s := New(path, 10)
|
|
s.Append(Entry{Text: "persisted", Origin: "a", Ts: 42})
|
|
|
|
// A new store reading the same path should reload the entry.
|
|
s2 := New(path, 10)
|
|
got := s2.List()
|
|
if len(got) != 1 || got[0].Text != "persisted" {
|
|
t.Errorf("reloaded = %+v, want persisted entry", got)
|
|
}
|
|
}
|
|
|
|
func TestDefaultPath(t *testing.T) {
|
|
p, err := DefaultPath()
|
|
if err != nil {
|
|
t.Fatalf("DefaultPath: %v", err)
|
|
}
|
|
if p == "" {
|
|
t.Error("DefaultPath returned empty string")
|
|
}
|
|
}
|