-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
204 lines (190 loc) · 4.68 KB
/
helpers_test.go
File metadata and controls
204 lines (190 loc) · 4.68 KB
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
package main
import (
"os"
"testing"
)
// TestGetEnvInt64 tests the getEnvInt64 function
func TestGetEnvInt64(t *testing.T) {
// Save original environment
originalValue := os.Getenv("TEST_INT64_ENV")
defer os.Setenv("TEST_INT64_ENV", originalValue)
tests := []struct {
name string
setEnvValue string
expectedVal int64
expectedErr bool
}{
{
name: "valid integer",
setEnvValue: "12345",
expectedVal: 12345,
expectedErr: false,
},
{
name: "zero value",
setEnvValue: "0",
expectedVal: 0,
expectedErr: false,
},
{
name: "negative value",
setEnvValue: "-100",
expectedVal: -100,
expectedErr: false,
},
{
name: "large value",
setEnvValue: "9223372036854775807",
expectedVal: 9223372036854775807,
expectedErr: false,
},
{
name: "not set",
setEnvValue: "",
expectedVal: 0,
expectedErr: true,
},
{
name: "invalid value",
setEnvValue: "not-a-number",
expectedVal: 0,
expectedErr: true,
},
{
name: "float value",
setEnvValue: "3.14",
expectedVal: 0,
expectedErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnvValue == "" {
os.Unsetenv("TEST_INT64_ENV")
} else {
os.Setenv("TEST_INT64_ENV", tt.setEnvValue)
}
val, err := getEnvInt64("TEST_INT64_ENV")
if tt.expectedErr && err == nil {
t.Error("expected error but got nil")
}
if !tt.expectedErr && err != nil {
t.Errorf("expected no error but got: %v", err)
}
if !tt.expectedErr && val != tt.expectedVal {
t.Errorf("expected %d, got %d", tt.expectedVal, val)
}
})
}
}
// TestShouldEnableMemoryTool tests the shouldEnableMemoryTool function
func TestShouldEnableMemoryTool(t *testing.T) {
// Save original environment
originalMemoryGroupID := os.Getenv("MEMORY_GROUP_ID")
originalOwnerID := os.Getenv("OWNER_ID")
defer func() {
os.Setenv("MEMORY_GROUP_ID", originalMemoryGroupID)
os.Setenv("OWNER_ID", originalOwnerID)
}()
tests := []struct {
name string
memoryGroupID string
ownerID string
chatID int64
userID int64
expected bool
}{
{
name: "in memory group",
memoryGroupID: "12345",
ownerID: "67890",
chatID: 12345,
userID: 11111,
expected: true,
},
{
name: "owner DM",
memoryGroupID: "12345",
ownerID: "67890",
chatID: 67890, // Same as userID (DM)
userID: 67890,
expected: true,
},
{
name: "not in memory group and not owner DM",
memoryGroupID: "12345",
ownerID: "67890",
chatID: 99999,
userID: 11111,
expected: false,
},
{
name: "in group but wrong user",
memoryGroupID: "12345",
ownerID: "67890",
chatID: 12345,
userID: 67890, // Owner but not in memory group
expected: true, // Should still be true because in memory group
},
{
name: "memory group ID not set",
memoryGroupID: "",
ownerID: "67890",
chatID: 12345,
userID: 11111,
expected: false,
},
{
name: "owner ID not set",
memoryGroupID: "12345",
ownerID: "",
chatID: 67890,
userID: 67890,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.memoryGroupID == "" {
os.Unsetenv("MEMORY_GROUP_ID")
} else {
os.Setenv("MEMORY_GROUP_ID", tt.memoryGroupID)
}
if tt.ownerID == "" {
os.Unsetenv("OWNER_ID")
} else {
os.Setenv("OWNER_ID", tt.ownerID)
}
result := shouldEnableMemoryTool(tt.chatID, tt.userID)
if result != tt.expected {
t.Errorf("shouldEnableMemoryTool(%d, %d) = %v, want %v",
tt.chatID, tt.userID, result, tt.expected)
}
})
}
}
// TestShouldEnableMemoryToolEdgeCases tests edge cases
func TestShouldEnableMemoryToolEdgeCases(t *testing.T) {
// Save original environment
originalMemoryGroupID := os.Getenv("MEMORY_GROUP_ID")
originalOwnerID := os.Getenv("OWNER_ID")
defer func() {
os.Setenv("MEMORY_GROUP_ID", originalMemoryGroupID)
os.Setenv("OWNER_ID", originalOwnerID)
}()
// Test with invalid memory group ID
os.Setenv("MEMORY_GROUP_ID", "invalid")
os.Setenv("OWNER_ID", "12345")
result := shouldEnableMemoryTool(12345, 67890)
if result {
t.Error("expected false with invalid memory group ID")
}
// Reset to valid
os.Setenv("MEMORY_GROUP_ID", "12345")
os.Setenv("OWNER_ID", "67890")
// Test with matching memory group ID (positive number)
result = shouldEnableMemoryTool(12345, 67890)
if !result {
t.Error("expected true for matching memory group ID")
}
}