|
| 1 | +package session_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/sipeed/picoclaw/pkg/memory" |
| 8 | + "github.com/sipeed/picoclaw/pkg/providers" |
| 9 | + "github.com/sipeed/picoclaw/pkg/session" |
| 10 | +) |
| 11 | + |
| 12 | +// Compile-time interface satisfaction checks. |
| 13 | +var ( |
| 14 | + _ session.SessionStore = (*session.SessionManager)(nil) |
| 15 | + _ session.SessionStore = (*session.JSONLBackend)(nil) |
| 16 | +) |
| 17 | + |
| 18 | +func newBackend(t *testing.T) *session.JSONLBackend { |
| 19 | + t.Helper() |
| 20 | + store, err := memory.NewJSONLStore(t.TempDir()) |
| 21 | + if err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + t.Cleanup(func() { store.Close() }) |
| 25 | + return session.NewJSONLBackend(store) |
| 26 | +} |
| 27 | + |
| 28 | +func TestJSONLBackend_AddAndGetHistory(t *testing.T) { |
| 29 | + b := newBackend(t) |
| 30 | + |
| 31 | + b.AddMessage("s1", "user", "hello") |
| 32 | + b.AddMessage("s1", "assistant", "hi") |
| 33 | + |
| 34 | + history := b.GetHistory("s1") |
| 35 | + if len(history) != 2 { |
| 36 | + t.Fatalf("got %d messages, want 2", len(history)) |
| 37 | + } |
| 38 | + if history[0].Role != "user" || history[0].Content != "hello" { |
| 39 | + t.Errorf("msg[0] = %+v", history[0]) |
| 40 | + } |
| 41 | + if history[1].Role != "assistant" || history[1].Content != "hi" { |
| 42 | + t.Errorf("msg[1] = %+v", history[1]) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestJSONLBackend_AddFullMessage(t *testing.T) { |
| 47 | + b := newBackend(t) |
| 48 | + |
| 49 | + msg := providers.Message{ |
| 50 | + Role: "assistant", |
| 51 | + Content: "done", |
| 52 | + ToolCalls: []providers.ToolCall{ |
| 53 | + {ID: "tc1", Function: &providers.FunctionCall{Name: "read_file", Arguments: `{"path":"x"}`}}, |
| 54 | + }, |
| 55 | + } |
| 56 | + b.AddFullMessage("s1", msg) |
| 57 | + |
| 58 | + history := b.GetHistory("s1") |
| 59 | + if len(history) != 1 { |
| 60 | + t.Fatalf("got %d, want 1", len(history)) |
| 61 | + } |
| 62 | + if len(history[0].ToolCalls) != 1 || history[0].ToolCalls[0].ID != "tc1" { |
| 63 | + t.Errorf("tool calls = %+v", history[0].ToolCalls) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestJSONLBackend_Summary(t *testing.T) { |
| 68 | + b := newBackend(t) |
| 69 | + |
| 70 | + if got := b.GetSummary("s1"); got != "" { |
| 71 | + t.Errorf("got %q, want empty", got) |
| 72 | + } |
| 73 | + |
| 74 | + b.SetSummary("s1", "test summary") |
| 75 | + if got := b.GetSummary("s1"); got != "test summary" { |
| 76 | + t.Errorf("got %q, want %q", got, "test summary") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestJSONLBackend_TruncateAndSave(t *testing.T) { |
| 81 | + b := newBackend(t) |
| 82 | + |
| 83 | + for i := 0; i < 10; i++ { |
| 84 | + b.AddMessage("s1", "user", fmt.Sprintf("msg %d", i)) |
| 85 | + } |
| 86 | + b.TruncateHistory("s1", 3) |
| 87 | + |
| 88 | + history := b.GetHistory("s1") |
| 89 | + if len(history) != 3 { |
| 90 | + t.Fatalf("got %d, want 3", len(history)) |
| 91 | + } |
| 92 | + if history[0].Content != "msg 7" { |
| 93 | + t.Errorf("got %q, want %q", history[0].Content, "msg 7") |
| 94 | + } |
| 95 | + |
| 96 | + // Save triggers compaction. |
| 97 | + if err := b.Save("s1"); err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + |
| 101 | + // Messages still accessible after compaction. |
| 102 | + history = b.GetHistory("s1") |
| 103 | + if len(history) != 3 { |
| 104 | + t.Fatalf("after save: got %d, want 3", len(history)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestJSONLBackend_SetHistory(t *testing.T) { |
| 109 | + b := newBackend(t) |
| 110 | + b.AddMessage("s1", "user", "old") |
| 111 | + |
| 112 | + b.SetHistory("s1", []providers.Message{ |
| 113 | + {Role: "user", Content: "new1"}, |
| 114 | + {Role: "assistant", Content: "new2"}, |
| 115 | + }) |
| 116 | + |
| 117 | + history := b.GetHistory("s1") |
| 118 | + if len(history) != 2 { |
| 119 | + t.Fatalf("got %d, want 2", len(history)) |
| 120 | + } |
| 121 | + if history[0].Content != "new1" { |
| 122 | + t.Errorf("got %q, want %q", history[0].Content, "new1") |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestJSONLBackend_EmptySession(t *testing.T) { |
| 127 | + b := newBackend(t) |
| 128 | + |
| 129 | + history := b.GetHistory("nonexistent") |
| 130 | + if history == nil { |
| 131 | + t.Fatal("got nil, want empty slice") |
| 132 | + } |
| 133 | + if len(history) != 0 { |
| 134 | + t.Errorf("got %d, want 0", len(history)) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestJSONLBackend_SessionIsolation(t *testing.T) { |
| 139 | + b := newBackend(t) |
| 140 | + b.AddMessage("s1", "user", "session1") |
| 141 | + b.AddMessage("s2", "user", "session2") |
| 142 | + |
| 143 | + h1 := b.GetHistory("s1") |
| 144 | + h2 := b.GetHistory("s2") |
| 145 | + |
| 146 | + if len(h1) != 1 || h1[0].Content != "session1" { |
| 147 | + t.Errorf("s1: %+v", h1) |
| 148 | + } |
| 149 | + if len(h2) != 1 || h2[0].Content != "session2" { |
| 150 | + t.Errorf("s2: %+v", h2) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestJSONLBackend_SummarizeFlow(t *testing.T) { |
| 155 | + // Simulates the real summarization flow in the agent loop: |
| 156 | + // SetSummary → TruncateHistory → Save |
| 157 | + b := newBackend(t) |
| 158 | + |
| 159 | + for i := 0; i < 20; i++ { |
| 160 | + b.AddMessage("s1", "user", fmt.Sprintf("msg %d", i)) |
| 161 | + } |
| 162 | + |
| 163 | + b.SetSummary("s1", "conversation about testing") |
| 164 | + b.TruncateHistory("s1", 4) |
| 165 | + if err := b.Save("s1"); err != nil { |
| 166 | + t.Fatal(err) |
| 167 | + } |
| 168 | + |
| 169 | + if got := b.GetSummary("s1"); got != "conversation about testing" { |
| 170 | + t.Errorf("summary = %q", got) |
| 171 | + } |
| 172 | + history := b.GetHistory("s1") |
| 173 | + if len(history) != 4 { |
| 174 | + t.Fatalf("got %d messages, want 4", len(history)) |
| 175 | + } |
| 176 | + if history[0].Content != "msg 16" { |
| 177 | + t.Errorf("first message = %q, want %q", history[0].Content, "msg 16") |
| 178 | + } |
| 179 | +} |
0 commit comments