go-session/search_test.go
Snider f40caaa593 test: add comprehensive Phase 0 test suite — 67 tests, 90.9% coverage
Add parser_test.go (22 tests), search_test.go (9 tests), html_test.go (6 tests),
video_test.go (12 tests), and bench_test.go (4 benchmarks) covering all Phase 0
TODO items:

- ParseTranscript: minimal JSONL, all 7 tool types, errors, truncated/malformed
  input, large sessions (1100+ events), nested array/map results, mixed content
- ListSessions: empty dir, single/multi sorted, non-JSONL ignored, modtime fallback
- extractToolInput: all tool types plus nil, invalid JSON, unknown tool fallback
- extractResultContent: string, array, map, and other types
- Search: empty dir, no matches, multi-match, case insensitive, output matching
- RenderHTML: basic, empty, errors, XSS escaping, label types, invalid path
- generateTape/extractCommand: all event types, empty/failed commands, truncation
- Benchmarks: 2.2MB and 11MB ParseTranscript, ListSessions, Search with b.Loop()

go vet ./... clean, go test -race ./... clean.

Co-Authored-By: Virgil <virgil@lethean.io>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 05:01:28 +00:00

146 lines
4.1 KiB
Go

// SPDX-Licence-Identifier: EUPL-1.2
package session
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSearch_EmptyDir_Good(t *testing.T) {
dir := t.TempDir()
results, err := Search(dir, "anything")
require.NoError(t, err)
assert.Empty(t, results)
}
func TestSearch_NoMatches_Good(t *testing.T) {
dir := t.TempDir()
writeJSONL(t, dir, "session.jsonl",
toolUseEntry(ts(0), "Bash", "tool-1", map[string]interface{}{
"command": "ls -la",
}),
toolResultEntry(ts(1), "tool-1", "total 42", false),
)
results, err := Search(dir, "nonexistent-query")
require.NoError(t, err)
assert.Empty(t, results)
}
func TestSearch_SingleMatch_Good(t *testing.T) {
dir := t.TempDir()
writeJSONL(t, dir, "session.jsonl",
toolUseEntry(ts(0), "Bash", "tool-1", map[string]interface{}{
"command": "go test ./...",
}),
toolResultEntry(ts(1), "tool-1", "PASS ok mypackage 0.5s", false),
)
results, err := Search(dir, "go test")
require.NoError(t, err)
require.Len(t, results, 1)
assert.Equal(t, "session", results[0].SessionID)
assert.Equal(t, "Bash", results[0].Tool)
assert.Contains(t, results[0].Match, "go test")
}
func TestSearch_MultipleMatches_Good(t *testing.T) {
dir := t.TempDir()
writeJSONL(t, dir, "session1.jsonl",
toolUseEntry(ts(0), "Bash", "t1", map[string]interface{}{
"command": "go test ./...",
}),
toolResultEntry(ts(1), "t1", "PASS", false),
toolUseEntry(ts(2), "Bash", "t2", map[string]interface{}{
"command": "go test -race ./...",
}),
toolResultEntry(ts(3), "t2", "PASS", false),
)
writeJSONL(t, dir, "session2.jsonl",
toolUseEntry(ts(0), "Bash", "t3", map[string]interface{}{
"command": "go test -bench=.",
}),
toolResultEntry(ts(1), "t3", "PASS", false),
)
results, err := Search(dir, "go test")
require.NoError(t, err)
assert.Len(t, results, 3, "should find matches across both sessions")
}
func TestSearch_CaseInsensitive_Good(t *testing.T) {
dir := t.TempDir()
writeJSONL(t, dir, "session.jsonl",
toolUseEntry(ts(0), "Bash", "t1", map[string]interface{}{
"command": "GO TEST ./...",
}),
toolResultEntry(ts(1), "t1", "PASS", false),
)
results, err := Search(dir, "go test")
require.NoError(t, err)
assert.Len(t, results, 1, "search should be case-insensitive")
}
func TestSearch_MatchesInOutput_Good(t *testing.T) {
dir := t.TempDir()
writeJSONL(t, dir, "session.jsonl",
toolUseEntry(ts(0), "Bash", "t1", map[string]interface{}{
"command": "cat log.txt",
}),
toolResultEntry(ts(1), "t1", "ERROR: connection refused to database", false),
)
results, err := Search(dir, "connection refused")
require.NoError(t, err)
require.Len(t, results, 1, "should match against output text")
// Match field should contain the input (command) since it's non-empty
assert.Contains(t, results[0].Match, "cat log.txt")
}
func TestSearch_SkipsNonToolEvents_Good(t *testing.T) {
dir := t.TempDir()
writeJSONL(t, dir, "session.jsonl",
userTextEntry(ts(0), "Please search for something"),
assistantTextEntry(ts(1), "I will search for something"),
)
// "search" appears in user and assistant text, but Search only checks tool_use events
results, err := Search(dir, "search")
require.NoError(t, err)
assert.Empty(t, results, "should only match tool_use events, not user/assistant text")
}
func TestSearch_NonJSONLIgnored_Good(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "readme.md"), []byte("go test"), 0644))
results, err := Search(dir, "go test")
require.NoError(t, err)
assert.Empty(t, results, "non-JSONL files should be ignored")
}
func TestSearch_MalformedSessionSkipped_Bad(t *testing.T) {
dir := t.TempDir()
// One broken session and one valid session
writeJSONL(t, dir, "broken.jsonl",
`{not valid json at all`,
)
writeJSONL(t, dir, "valid.jsonl",
toolUseEntry(ts(0), "Bash", "t1", map[string]interface{}{
"command": "go test ./...",
}),
toolResultEntry(ts(1), "t1", "PASS", false),
)
results, err := Search(dir, "go test")
require.NoError(t, err)
assert.Len(t, results, 1, "should still find matches in valid sessions")
}