M1: raw per-session focus log with retention prune
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAppendFocusReplayRoundTrips(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "sessions")
|
||||
id := "session-x"
|
||||
events := []FocusEvent{
|
||||
{AtUnixMillis: 1000, Class: "code", Title: "a", Available: true},
|
||||
{AtUnixMillis: 2000, Class: "firefox", Title: "b", Available: true},
|
||||
{AtUnixMillis: 3000, Class: "", Title: "", Available: false, Reason: "no active window"},
|
||||
}
|
||||
for _, e := range events {
|
||||
if err := AppendFocus(dir, id, e); err != nil {
|
||||
t.Fatalf("append: %v", err)
|
||||
}
|
||||
}
|
||||
got, err := ReplaySession(dir, id)
|
||||
if err != nil {
|
||||
t.Fatalf("replay: %v", err)
|
||||
}
|
||||
if len(got) != len(events) {
|
||||
t.Fatalf("got %d events, want %d", len(got), len(events))
|
||||
}
|
||||
for i := range events {
|
||||
if got[i] != events[i] {
|
||||
t.Errorf("event %d: got %+v want %+v", i, got[i], events[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayMissingSessionIsEmpty(t *testing.T) {
|
||||
got, err := ReplaySession(filepath.Join(t.TempDir(), "sessions"), "nope")
|
||||
if err != nil {
|
||||
t.Fatalf("missing session should not error: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("missing session should be empty, got %d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneOlderThan(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "sessions")
|
||||
_ = AppendFocus(dir, "old", FocusEvent{AtUnixMillis: 1})
|
||||
_ = AppendFocus(dir, "new", FocusEvent{AtUnixMillis: 1})
|
||||
|
||||
now := time.Date(2026, 5, 31, 12, 0, 0, 0, time.UTC)
|
||||
oldTime := now.Add(-40 * 24 * time.Hour)
|
||||
if err := os.Chtimes(filepath.Join(dir, "old.jsonl"), oldTime, oldTime); err != nil {
|
||||
t.Fatalf("chtimes: %v", err)
|
||||
}
|
||||
|
||||
if err := PruneOlderThan(dir, 30*24*time.Hour, now); err != nil {
|
||||
t.Fatalf("prune: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "old.jsonl")); !os.IsNotExist(err) {
|
||||
t.Errorf("old session should be pruned")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "new.jsonl")); err != nil {
|
||||
t.Errorf("new session should survive: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneMissingDirIsNoop(t *testing.T) {
|
||||
if err := PruneOlderThan(filepath.Join(t.TempDir(), "nope"), time.Hour, time.Now()); err != nil {
|
||||
t.Fatalf("missing dir should be a no-op, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user