返回 DeepSeek-Reasonix
session_target_history_test.go
根目录 / desktop / session_target_history_test.go
1 package main
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "reasonix/desktop/internal/workspacestate"
10 "reasonix/internal/agent"
11 "reasonix/internal/config"
12 "reasonix/internal/provider"
13 )
14
15 func writeTargetHistoryFixture(t *testing.T, dir, name, marker string) string {
16 t.Helper()
17 path := filepath.Join(dir, name+".jsonl")
18 session := agent.NewSession("")
19 for range 4 {
20 session.Add(provider.Message{Role: provider.RoleUser, Content: marker + " user"})
21 session.Add(provider.Message{Role: provider.RoleAssistant, Content: marker + " answer"})
22 }
23 if err := session.SaveSnapshot(path); err != nil {
24 t.Fatal(err)
25 }
26 if err := agent.UpdateBranchMeta(path, false, func(meta *agent.BranchMeta) error {
27 meta.Scope = "global"
28 meta.TopicID = name
29 return nil
30 }); err != nil {
31 t.Fatal(err)
32 }
33 return path
34 }
35
36 func TestHistorySliceForColdLegacyTargetDoesNotNavigateAndBindsCursor(t *testing.T) {
37 isolateDesktopUserDirs(t)
38 dir := config.SessionDir()
39 if err := os.MkdirAll(dir, 0o755); err != nil {
40 t.Fatal(err)
41 }
42 first := writeTargetHistoryFixture(t, dir, "first", "first")
43 second := writeTargetHistoryFixture(t, dir, "second", "second")
44 app := NewApp()
45 installSessionCatalogForTest(t, app, dir, "global", "")
46 app.tabs = map[string]*WorkspaceTab{"active": {ID: "active", TopicID: "unrelated", SessionID: "unrelated"}}
47 app.activeTabID = "active"
48
49 page, err := app.HistorySliceForTarget(SessionSelector{SessionPath: first}, HistorySliceRequest{Turns: 1, Entries: 2})
50 if err != nil {
51 t.Fatal(err)
52 }
53 if len(page.Entries) == 0 || !page.HasOlder || page.NextCursor == "" {
54 t.Fatalf("first page = %+v, want bounded page with older cursor", page)
55 }
56 if app.activeTabID != "active" || app.tabs["active"].TopicID != "unrelated" {
57 t.Fatal("cold target history changed active navigation")
58 }
59 if _, err := app.HistorySliceForTarget(SessionSelector{SessionPath: second}, HistorySliceRequest{Cursor: page.NextCursor, Turns: 1, Entries: 2}); err == nil ||
60 !strings.Contains(err.Error(), "session_operation:stale_cursor:") {
61 t.Fatalf("cross-target cursor = %v, want stale_cursor", err)
62 }
63 }
64
65 func TestCopySessionTargetPreservesColdLegacyHistory(t *testing.T) {
66 isolateDesktopUserDirs(t)
67 root := t.TempDir()
68 dir := config.SessionDir()
69 if err := os.MkdirAll(dir, 0o755); err != nil {
70 t.Fatal(err)
71 }
72 path := writeTargetHistoryFixture(t, dir, "legacy-copy", "legacy-copy")
73 app := NewApp()
74 t.Cleanup(app.closeSessionServices)
75 app.ctx = t.Context()
76 app.desktopSessions.root = filepath.Join(root, "desktop-sessions-v5", "by-id")
77 app.desktopSessions.workspaceState = workspacestate.NewStore(filepath.Join(root, "desktop", "workspace-state-v1.json"))
78 installSessionCatalogForTest(t, app, dir, "global", "")
79
80 result, err := app.CopySessionTarget(SessionSelector{SessionPath: path}, "legacy-copy-operation")
81 if err != nil {
82 t.Fatal(err)
83 }
84 if !result.Committed || result.Ref.SessionID == "" {
85 t.Fatalf("legacy copy result = %+v", result)
86 }
87 history, err := app.desktopSessionService("").Query().History(t.Context(), result.Ref)
88 if err != nil {
89 t.Fatal(err)
90 }
91 if len(history) != 8 || history[0].Content != "legacy-copy user" || history[7].Content != "legacy-copy answer" {
92 t.Fatalf("legacy copied history = %+v", history)
93 }
94 retry, err := app.CopySessionTarget(SessionSelector{SessionPath: path}, "legacy-copy-operation")
95 if err != nil {
96 t.Fatal(err)
97 }
98 if retry.Ref != result.Ref {
99 t.Fatalf("legacy retry created another copy: first=%+v retry=%+v", result.Ref, retry.Ref)
100 }
101 }
102
102 lines GO