xonecas.io
6119
My

mysis / files

mysis / internal / store / compression_test.go

4.8 KB
1package store
2
3import (
4	"testing"
5
6	"github.com/xonecas/mysis/internal/provider"
7)
8
9func TestCompressHistory_NoCompression(t *testing.T) {
10	messages := []provider.Message{
11		{Role: "user", Content: "hello"},
12		{Role: "assistant", Content: "hi there"},
13	}
14
15	compressed := CompressHistory(messages, 10)
16
17	if len(compressed) != len(messages) {
18		t.Errorf("expected no compression, got %d messages from %d", len(compressed), len(messages))
19	}
20}
21
22func TestCompressHistory_StateQueries(t *testing.T) {
23	messages := []provider.Message{
24		// Turn 1
25		{Role: "user", Content: "what's my status?"},
26		{Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
27			{ID: "call1", Name: "get_status"},
28		}},
29		{Role: "tool", Content: "OLD STATUS DATA: very long result here...", ToolCallID: "call1"},
30		{Role: "assistant", Content: "You're doing well"},
31
32		// Turn 2
33		{Role: "user", Content: "status again?"},
34		{Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
35			{ID: "call2", Name: "get_status"},
36		}},
37		{Role: "tool", Content: "NEW STATUS DATA: latest result", ToolCallID: "call2"},
38		{Role: "assistant", Content: "Here's the update"},
39
40		// Turn 3 (recent)
41		{Role: "user", Content: "navigate somewhere"},
42		{Role: "assistant", Content: "navigating..."},
43	}
44
45	// Keep last 2 turns full, compress older
46	compressed := CompressHistory(messages, 2)
47
48	// Find the old get_status result
49	var oldStatusResult *provider.Message
50	for i := range compressed {
51		if compressed[i].Role == "tool" && compressed[i].ToolCallID == "call1" {
52			oldStatusResult = &compressed[i]
53			break
54		}
55	}
56
57	if oldStatusResult == nil {
58		t.Fatal("old status result not found in compressed history")
59	}
60
61	if oldStatusResult.Content != compressedToolResult {
62		t.Errorf("old status result not compressed, got: %s", oldStatusResult.Content)
63	}
64
65	// Find the new get_status result
66	var newStatusResult *provider.Message
67	for i := range compressed {
68		if compressed[i].Role == "tool" && compressed[i].ToolCallID == "call2" {
69			newStatusResult = &compressed[i]
70			break
71		}
72	}
73
74	if newStatusResult == nil {
75		t.Fatal("new status result not found in compressed history")
76	}
77
78	if newStatusResult.Content != "NEW STATUS DATA: latest result" {
79		t.Errorf("new status result was compressed when it shouldn't be, got: %s", newStatusResult.Content)
80	}
81}
82
83func TestCompressHistory_AuthToolsPreserved(t *testing.T) {
84	messages := []provider.Message{
85		// Turn 1 (old)
86		{Role: "user", Content: "login"},
87		{Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
88			{ID: "call1", Name: "login"},
89		}},
90		{Role: "tool", Content: "Login successful with credentials", ToolCallID: "call1"},
91		{Role: "assistant", Content: "Logged in"},
92
93		// Turn 2
94		{Role: "user", Content: "status?"},
95		{Role: "assistant", Content: "", ToolCalls: []provider.ToolCall{
96			{ID: "call2", Name: "get_status"},
97		}},
98		{Role: "tool", Content: "Some status data", ToolCallID: "call2"},
99		{Role: "assistant", Content: "Here's your status"},
100
101		// Turn 3 (recent)
102		{Role: "user", Content: "navigate"},
103		{Role: "assistant", Content: "navigating..."},
104	}
105
106	// Keep last 1 turn full, compress older
107	compressed := CompressHistory(messages, 1)
108
109	// Find the login result
110	var loginResult *provider.Message
111	for i := range compressed {
112		if compressed[i].Role == "tool" && compressed[i].ToolCallID == "call1" {
113			loginResult = &compressed[i]
114			break
115		}
116	}
117
118	if loginResult == nil {
119		t.Fatal("login result not found in compressed history")
120	}
121
122	// Auth tools should NEVER be compressed
123	if loginResult.Content != "Login successful with credentials" {
124		t.Errorf("login result was compressed, got: %s", loginResult.Content)
125	}
126}
127
128func TestEstimateTokenCount(t *testing.T) {
129	messages := []provider.Message{
130		{Role: "user", Content: "hello world"},               // ~3 tokens + 4 overhead
131		{Role: "assistant", Content: "hi there how are you"}, // ~5 tokens + 4 overhead
132	}
133
134	tokens := EstimateTokenCount(messages)
135
136	// Should be roughly (12 + 20) / 4 + 8 = ~16
137	if tokens < 10 || tokens > 20 {
138		t.Errorf("token estimate seems off: %d", tokens)
139	}
140}
141
142func TestIsStateQueryTool(t *testing.T) {
143	tests := []struct {
144		name string
145		want bool
146	}{
147		{"get_status", true},
148		{"get_ship", true},
149		{"get_map", true},
150		{"GET_STATUS", true}, // case insensitive
151		{"login", false},
152		{"travel", false},
153		{"mine", false},
154	}
155
156	for _, tt := range tests {
157		got := isStateQueryTool(tt.name)
158		if got != tt.want {
159			t.Errorf("isStateQueryTool(%q) = %v, want %v", tt.name, got, tt.want)
160		}
161	}
162}
163
164func TestIsAuthTool(t *testing.T) {
165	tests := []struct {
166		name string
167		want bool
168	}{
169		{"login", true},
170		{"register", true},
171		{"logout", true},
172		{"LOGIN", true}, // case insensitive
173		{"get_status", false},
174		{"travel", false},
175	}
176
177	for _, tt := range tests {
178		got := isAuthTool(tt.name)
179		if got != tt.want {
180			t.Errorf("isAuthTool(%q) = %v, want %v", tt.name, got, tt.want)
181		}
182	}
183}