Files
magpie/internal/sessions/search_db_test.go
T
yetoneandliubing 9935119f1d sessions: search finds Cursor and OpenCode sessions read from their stores, and a Cursor chat's system prompt isn't shown as said (#1743)
Follow-up to liubing's #1743 patch, which put cursor and opencode in
HasTranscript. That moved their search from magpie's recorded
transcript to SearchSession, whose byte prefilter (mayHold) reads
s.Path. For these agents s.Path isn't the conversation:
- OpenCode's JSON storage: the session info file, without messages,
  so every word missed;
- OpenCode's and Cursor's SQLite stores: the newest pages are in the
  -wal, so a live chat's latest turns missed.
linesFile now skips the prefilter for them (a Cursor .jsonl still
gets it).

cursorTranscript also handed the CLI store's system message ("You
are a coding agent.") to the transcript as a said part; system
messages are skipped, as the listing already does.

The patch's tests commit left TestSearchSessionWithoutATranscript
red (it used cursor as the agent without a transcript; now gemini).
sessions-empty-range.test.cjs runs in every language, de at 420px.

Run: go test -tags nogui -count=1 ./internal/sessions/ ok, and -race
ok; sessions-empty-range 10/10 (Chromium+WebKit, en zh zh-TW ja de);
sessions-today/overview/toolbar/search/manage/calendar-fill/paging
GUI tests pass.
Broken by hand: prefilter always on -> TestSearchOpenCodeFiles and
TestSearchCursorLiveChat red; system skip removed ->
TestSearchCursorLiveChat red. Also broke the patch itself: editor
files not appended, readOnly Delete guard, stats message dates, CLI
dedupe, HasTranscript entries, parseCursor readOnly dispatch, and
the "All sessions" button (via MAGPIE_TEST_APP_JS); each went red.

Co-authored-by: liubing <lb0806@foxmail.com>
2026-10-13 06:43:50 +08:00

68 lines
1.7 KiB
Go

package sessions
import (
"context"
"path/filepath"
"strings"
"testing"
)
// searchesEveryPart has SearchSession find each word of each text part of
// s's transcript: a session whose conversation isn't in the file its Path
// names (a database, OpenCode's session info) is searched all the same.
// The agent's own system prompt isn't part of it.
func searchesEveryPart(t *testing.T, s Session) {
t.Helper()
tr, err := TranscriptOf(s)
if err != nil || len(tr.Parts) == 0 {
t.Fatalf("%s %s: no transcript: %v", s.Agent, s.ID, err)
}
tried := 0
for _, p := range tr.Parts {
if p.Role == "system" {
t.Fatalf("%s %s: the agent's own prompt is shown as said: %q", s.Agent, s.ID, p.Text)
}
if p.Kind != "text" {
continue
}
for _, w := range strings.Fields(p.Text) {
w = strings.Trim(w, ".,!?\"'()")
if len(w) < 4 {
continue
}
tried++
if _, ok, err := SearchSession(context.Background(), s, NeedleOf(w)); !ok || err != nil {
t.Fatalf("%s %s: %q, said in %q, wasn't found: %v", s.Agent, s.ID, w, p.Text, err)
}
}
}
if tried == 0 {
t.Fatalf("%s %s: nothing to look for in %+v", s.Agent, s.ID, tr.Parts)
}
}
func TestSearchOpenCodeFiles(t *testing.T) {
setupMore(t)
searchesEveryPart(t, find(t, List(All), "opencode", ocMain))
}
func TestSearchOpenCodeDatabase(t *testing.T) {
data, _ := setupMore(t)
db := ocMakeV2(t, filepath.Join(data, "opencode", "opencode.db"))
defer db.Close()
searchesEveryPart(t, find(t, List(All), "opencode", ocMain))
}
func TestSearchCursorLiveChat(t *testing.T) {
dir := cursorSetup(t)
makeCursorChats(t, dir)
ss := List(0)
for _, id := range []string{curMain, curOther} {
s, ok := findSession(ss, "cursor", id)
if !ok {
t.Fatalf("%s isn't listed", id)
}
searchesEveryPart(t, s)
}
}