-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_test.go
More file actions
318 lines (267 loc) · 9.1 KB
/
parser_test.go
File metadata and controls
318 lines (267 loc) · 9.1 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestDecodeProjectPath(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "standard path",
input: "-Users-name-project",
expected: "/Users/name/project",
},
{
name: "deep nested path",
input: "-Users-cengiz-han-workspace-code-claude-code-logs",
expected: "/Users/cengiz/han/workspace/code/claude/code/logs",
},
{
name: "root marker",
input: "-",
expected: "/",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "no leading dash",
input: "some-folder",
expected: "some-folder",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DecodeProjectPath(tt.input)
if result != tt.expected {
t.Errorf("DecodeProjectPath(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestDiscoverProjects(t *testing.T) {
// Create temporary directory structure
tmpDir := t.TempDir()
// Create test project directories
testProjects := []string{
"-Users-test-project1",
"-Users-test-project2",
".hidden-folder", // Should be skipped
}
for _, p := range testProjects {
if err := os.MkdirAll(filepath.Join(tmpDir, p), 0755); err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}
}
// Create a non-directory file (should be skipped)
if err := os.WriteFile(filepath.Join(tmpDir, "some-file.txt"), []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Test discovery
projects, err := DiscoverProjects(tmpDir)
if err != nil {
t.Fatalf("DiscoverProjects failed: %v", err)
}
if len(projects) != 2 {
t.Errorf("Expected 2 projects, got %d", len(projects))
}
// Check project paths are decoded
for _, p := range projects {
if p.Path == "" {
t.Errorf("Project path should not be empty")
}
if p.FolderName == "" {
t.Errorf("Project folder name should not be empty")
}
}
}
func TestDiscoverProjects_EmptyDirectory(t *testing.T) {
tmpDir := t.TempDir()
projects, err := DiscoverProjects(tmpDir)
if err != nil {
t.Fatalf("DiscoverProjects failed on empty dir: %v", err)
}
if len(projects) != 0 {
t.Errorf("Expected 0 projects for empty directory, got %d", len(projects))
}
}
func TestDiscoverProjects_NonExistentDirectory(t *testing.T) {
_, err := DiscoverProjects("/non/existent/path")
if err == nil {
t.Error("Expected error for non-existent directory, got nil")
}
}
func TestParseSession(t *testing.T) {
// Create temporary JSONL file
tmpDir := t.TempDir()
sessionPath := filepath.Join(tmpDir, "test-session.jsonl")
jsonlContent := `{"type":"summary","summary":"Test Session","leafUuid":"abc123"}
{"type":"user","uuid":"msg1","parentUuid":null,"timestamp":"2025-12-29T10:00:00.000Z","message":{"role":"user","content":"Hello, Claude!"}}
{"type":"assistant","uuid":"msg2","parentUuid":"msg1","timestamp":"2025-12-29T10:00:05.000Z","message":{"role":"assistant","content":[{"type":"text","text":"Hello! How can I help you today?"}]}}
{"type":"file-history-snapshot","messageId":"msg1","snapshot":{}}
`
if err := os.WriteFile(sessionPath, []byte(jsonlContent), 0644); err != nil {
t.Fatalf("Failed to create test JSONL file: %v", err)
}
session, err := ParseSession(sessionPath, "test-session")
if err != nil {
t.Fatalf("ParseSession failed: %v", err)
}
// Check summary
if session.Summary != "Test Session" {
t.Errorf("Expected summary 'Test Session', got %q", session.Summary)
}
// Check messages
if len(session.Messages) != 2 {
t.Errorf("Expected 2 messages, got %d", len(session.Messages))
}
// Check first message (user)
if session.Messages[0].Role != "user" {
t.Errorf("Expected first message role 'user', got %q", session.Messages[0].Role)
}
// Check second message (assistant)
if session.Messages[1].Role != "assistant" {
t.Errorf("Expected second message role 'assistant', got %q", session.Messages[1].Role)
}
// Check threading
if session.Messages[1].ParentUUID != "msg1" {
t.Errorf("Expected parent UUID 'msg1', got %q", session.Messages[1].ParentUUID)
}
}
func TestParseSession_WithToolCalls(t *testing.T) {
tmpDir := t.TempDir()
sessionPath := filepath.Join(tmpDir, "test-session.jsonl")
jsonlContent := `{"type":"summary","summary":"Tool Test"}
{"type":"assistant","uuid":"msg1","parentUuid":null,"timestamp":"2025-12-29T10:00:00.000Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"tool1","name":"Read","input":{"file_path":"/test/file.txt"}}]}}
{"type":"assistant","uuid":"msg2","parentUuid":"msg1","timestamp":"2025-12-29T10:00:01.000Z","message":{"role":"assistant","content":[{"type":"tool_result","tool_use_id":"tool1","content":"file contents here"}]}}
`
if err := os.WriteFile(sessionPath, []byte(jsonlContent), 0644); err != nil {
t.Fatalf("Failed to create test JSONL file: %v", err)
}
session, err := ParseSession(sessionPath, "test-session")
if err != nil {
t.Fatalf("ParseSession failed: %v", err)
}
if len(session.Messages) != 2 {
t.Fatalf("Expected 2 messages, got %d", len(session.Messages))
}
// Check tool_use block
toolUseBlock := session.Messages[0].Content[0]
if toolUseBlock.Type != "tool_use" {
t.Errorf("Expected type 'tool_use', got %q", toolUseBlock.Type)
}
if toolUseBlock.ToolName != "Read" {
t.Errorf("Expected tool name 'Read', got %q", toolUseBlock.ToolName)
}
// Check tool_result block
toolResultBlock := session.Messages[1].Content[0]
if toolResultBlock.Type != "tool_result" {
t.Errorf("Expected type 'tool_result', got %q", toolResultBlock.Type)
}
if toolResultBlock.ToolUseID != "tool1" {
t.Errorf("Expected tool_use_id 'tool1', got %q", toolResultBlock.ToolUseID)
}
}
func TestParseSession_MalformedJSON(t *testing.T) {
tmpDir := t.TempDir()
sessionPath := filepath.Join(tmpDir, "test-session.jsonl")
jsonlContent := `{"type":"summary","summary":"Test"}
{this is not valid json}
{"type":"user","uuid":"msg1","timestamp":"2025-12-29T10:00:00.000Z","message":{"role":"user","content":"Hello"}}
`
if err := os.WriteFile(sessionPath, []byte(jsonlContent), 0644); err != nil {
t.Fatalf("Failed to create test JSONL file: %v", err)
}
session, err := ParseSession(sessionPath, "test-session")
if err != nil {
t.Fatalf("ParseSession should not fail on malformed lines: %v", err)
}
// Should have summary and one message (malformed line skipped)
if session.Summary != "Test" {
t.Errorf("Expected summary 'Test', got %q", session.Summary)
}
if len(session.Messages) != 1 {
t.Errorf("Expected 1 message (malformed line skipped), got %d", len(session.Messages))
}
}
func TestParseSession_EmptyFile(t *testing.T) {
tmpDir := t.TempDir()
sessionPath := filepath.Join(tmpDir, "test-session.jsonl")
if err := os.WriteFile(sessionPath, []byte(""), 0644); err != nil {
t.Fatalf("Failed to create test JSONL file: %v", err)
}
session, err := ParseSession(sessionPath, "test-session")
if err != nil {
t.Fatalf("ParseSession failed on empty file: %v", err)
}
if len(session.Messages) != 0 {
t.Errorf("Expected 0 messages for empty file, got %d", len(session.Messages))
}
}
func TestListSessions(t *testing.T) {
tmpDir := t.TempDir()
// Create project directory
projectDir := filepath.Join(tmpDir, "-Users-test-project")
if err := os.MkdirAll(projectDir, 0755); err != nil {
t.Fatalf("Failed to create project directory: %v", err)
}
// Create test sessions with different timestamps
sessions := []struct {
id string
timestamp time.Time
}{
{"session1", time.Date(2025, 12, 29, 10, 0, 0, 0, time.UTC)},
{"session2", time.Date(2025, 12, 29, 11, 0, 0, 0, time.UTC)},
{"session3", time.Date(2025, 12, 29, 9, 0, 0, 0, time.UTC)},
}
for _, s := range sessions {
content := `{"type":"summary","summary":"Session ` + s.id + `"}
{"type":"user","uuid":"msg1","timestamp":"` + s.timestamp.Format(time.RFC3339) + `","message":{"role":"user","content":"test"}}
`
path := filepath.Join(projectDir, s.id+".jsonl")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create session file: %v", err)
}
}
// Also create a non-jsonl file (should be skipped)
if err := os.WriteFile(filepath.Join(projectDir, "readme.txt"), []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create non-jsonl file: %v", err)
}
project := &Project{
FolderName: "-Users-test-project",
Path: "/Users/test/project",
}
sessionList, err := ListSessions(tmpDir, project)
if err != nil {
t.Fatalf("ListSessions failed: %v", err)
}
if len(sessionList) != 3 {
t.Errorf("Expected 3 sessions, got %d", len(sessionList))
}
// Check sessions are sorted by creation date (newest first)
if len(sessionList) >= 2 {
if !sessionList[0].CreatedAt.After(sessionList[1].CreatedAt) {
t.Errorf("Sessions should be sorted newest first")
}
}
}
func TestDefaultClaudeProjectsPath(t *testing.T) {
path, err := DefaultClaudeProjectsPath()
if err != nil {
t.Fatalf("DefaultClaudeProjectsPath failed: %v", err)
}
if path == "" {
t.Error("Expected non-empty path")
}
// Should end with .claude/projects
if !filepath.IsAbs(path) {
t.Errorf("Expected absolute path, got %q", path)
}
}