~kris/9p

llm9p

ref: f2d8604ad1f0dafbb375aa2f69d8633a28dcae0b llm9p/internal/llmfs/session_compact_test.go -rw-r--r-- 5.4 KiB
f2d8604a — pdfinn fix(llm): strip CLAUDECODE env var before spawning claude subprocess 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package llmfs

import (
	"io"
	"strings"
	"testing"

	"github.com/NERVsystems/llm9p/internal/llm"
)

// newTestSession creates a SessionManager with a mock backend and a fresh session.
// Returns the manager and the session ID.
func newTestSession() (*llm.SessionManager, int) {
	mock := NewMockBackend()
	mock.askResponse = "This is a compact summary of the conversation."
	sm := llm.NewSessionManager(mock)
	id := sm.Create()
	return sm, id
}

// ---- SessionCompactFile ----

func TestSessionCompactFile_Read(t *testing.T) {
	sm, id := newTestSession()
	f := NewSessionCompactFile(sm, id)

	buf := make([]byte, 100)
	n, err := f.Read(buf, 0)
	if err != nil {
		t.Fatalf("Read() error: %v", err)
	}
	content := string(buf[:n])
	if content == "" {
		t.Error("Read() returned empty content")
	}
}

func TestSessionCompactFile_Read_EOF(t *testing.T) {
	sm, id := newTestSession()
	f := NewSessionCompactFile(sm, id)

	buf := make([]byte, 100)
	n, err := f.Read(buf, 10000)
	if err != io.EOF {
		t.Errorf("Read(large offset) = %v, want io.EOF", err)
	}
	if n != 0 {
		t.Errorf("Read(large offset) n = %d, want 0", n)
	}
}

func TestSessionCompactFile_Write_NoOp_ShortHistory(t *testing.T) {
	sm, id := newTestSession()
	session := sm.Get(id)
	session.AddMessage("user", "hello")
	session.AddMessage("assistant", "hi")

	f := NewSessionCompactFile(sm, id)
	n, err := f.Write([]byte("compact"), 0)
	if err != nil {
		t.Fatalf("Write() error: %v", err)
	}
	if n != 7 {
		t.Errorf("Write() n = %d, want 7", n)
	}
	// Session with < 4 messages: compaction is a no-op (messages unchanged)
	msgs := session.Messages()
	if len(msgs) != 2 {
		t.Errorf("after no-op compact: messages = %d, want 2", len(msgs))
	}
}

func TestSessionCompactFile_Write_Compacts(t *testing.T) {
	sm, id := newTestSession()
	session := sm.Get(id)
	// Add 3 turns (6 messages) to pass the threshold
	for i := 0; i < 3; i++ {
		session.AddMessage("user", "question")
		session.AddMessage("assistant", "answer")
	}

	f := NewSessionCompactFile(sm, id)
	_, err := f.Write([]byte("compact"), 0)
	if err != nil {
		t.Fatalf("Write() error: %v", err)
	}

	// After compaction messages should be condensed
	msgs := session.Messages()
	if len(msgs) >= 6 {
		t.Errorf("after compact: messages = %d, expected fewer than 6", len(msgs))
	}
}

func TestSessionCompactFile_Stat(t *testing.T) {
	sm, id := newTestSession()
	f := NewSessionCompactFile(sm, id)
	stat := f.Stat()
	if stat.Length == 0 {
		t.Error("Stat().Length should be non-zero")
	}
}

// ---- SessionUsageFile ----

func TestSessionUsageFile_Read_Format(t *testing.T) {
	sm, id := newTestSession()
	session := sm.Get(id)
	// 400 chars → 100 estimated tokens
	session.AddMessage("user", strings.Repeat("x", 400))

	f := NewSessionUsageFile(sm, id)
	buf := make([]byte, 64)
	n, err := f.Read(buf, 0)
	if err != nil {
		t.Fatalf("Read() error: %v", err)
	}

	content := string(buf[:n])
	// Should be "100/200000\n"
	if content != "100/200000\n" {
		t.Errorf("Read() = %q, want %q", content, "100/200000\n")
	}
}

func TestSessionUsageFile_Read_EOF(t *testing.T) {
	sm, id := newTestSession()
	f := NewSessionUsageFile(sm, id)

	buf := make([]byte, 64)
	n, err := f.Read(buf, 10000)
	if err != io.EOF {
		t.Errorf("Read(large offset) = %v, want io.EOF", err)
	}
	if n != 0 {
		t.Errorf("Read(large offset) n = %d, want 0", n)
	}
}

func TestSessionUsageFile_Write_ReadOnly(t *testing.T) {
	sm, id := newTestSession()
	f := NewSessionUsageFile(sm, id)

	n, err := f.Write([]byte("anything"), 0)
	if err == nil {
		t.Error("Write() should return error (read-only)")
	}
	if n != 0 {
		t.Errorf("Write() n = %d, want 0", n)
	}
}

func TestSessionUsageFile_Stat_Length(t *testing.T) {
	sm, id := newTestSession()
	f := NewSessionUsageFile(sm, id)
	stat := f.Stat()
	// "0/200000\n" = 9 chars
	if stat.Length != 9 {
		t.Errorf("Stat().Length = %d, want 9 (for '0/200000\\n')", stat.Length)
	}
}

func TestSessionUsageFile_Reflects_Content(t *testing.T) {
	sm, id := newTestSession()
	session := sm.Get(id)
	f := NewSessionUsageFile(sm, id)

	buf := make([]byte, 64)

	// Initially empty
	n, _ := f.Read(buf, 0)
	if string(buf[:n]) != "0/200000\n" {
		t.Errorf("initial: got %q, want '0/200000\\n'", string(buf[:n]))
	}

	// Add 800 chars → 200 estimated tokens
	session.AddMessage("user", strings.Repeat("a", 800))

	n, _ = f.Read(buf, 0)
	if string(buf[:n]) != "200/200000\n" {
		t.Errorf("after add: got %q, want '200/200000\\n'", string(buf[:n]))
	}
}

// ---- SessionDir includes compact and usage ----

func TestSessionDir_HasCompactAndUsage(t *testing.T) {
	sm, id := newTestSession()
	dir := NewSessionDir(sm, id)

	children := dir.Children()
	names := make(map[string]bool)
	for _, f := range children {
		names[f.Stat().Name] = true
	}

	for _, want := range []string{"compact", "usage"} {
		if !names[want] {
			t.Errorf("SessionDir.Children() missing %q", want)
		}
	}
}

func TestSessionDir_LookupCompact(t *testing.T) {
	sm, id := newTestSession()
	dir := NewSessionDir(sm, id)

	f, err := dir.Lookup("compact")
	if err != nil {
		t.Fatalf("Lookup('compact') error: %v", err)
	}
	if _, ok := f.(*SessionCompactFile); !ok {
		t.Errorf("Lookup('compact') returned %T, want *SessionCompactFile", f)
	}
}

func TestSessionDir_LookupUsage(t *testing.T) {
	sm, id := newTestSession()
	dir := NewSessionDir(sm, id)

	f, err := dir.Lookup("usage")
	if err != nil {
		t.Fatalf("Lookup('usage') error: %v", err)
	}
	if _, ok := f.(*SessionUsageFile); !ok {
		t.Errorf("Lookup('usage') returned %T, want *SessionUsageFile", f)
	}
}