返回 DeepSeek-Reasonix
pinned_context_store_test.go
根目录 / desktop / pinned_context_store_test.go
1 package main
2
3 import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "reflect"
8 "testing"
9
10 "reasonix/internal/agent"
11 "reasonix/internal/store"
12 )
13
14 func TestPinnedContextSidecarRoundTripAndCopy(t *testing.T) {
15 dir := t.TempDir()
16 source := filepath.Join(dir, "source.jsonl")
17 target := filepath.Join(dir, "target.jsonl")
18 files := []string{"docs/b.md", "docs/a.md", "docs/b.md"}
19 if err := savePinnedContextState(source, files); err != nil {
20 t.Fatalf("save source: %v", err)
21 }
22 state, err := loadPinnedContextState(source)
23 if err != nil {
24 t.Fatalf("load source: %v", err)
25 }
26 want := []string{"docs/a.md", "docs/b.md"}
27 if !reflect.DeepEqual(state.Files, want) {
28 t.Fatalf("source files = %v, want stable sorted order %v", state.Files, want)
29 }
30 if state.SchemaVersion != pinnedContextSchemaVersion || state.SessionID != agent.BranchID(source) {
31 t.Fatalf("source state = %+v", state)
32 }
33 if err := copyPinnedContextState(source, target); err != nil {
34 t.Fatalf("copy sidecar: %v", err)
35 }
36 copied, err := loadPinnedContextState(target)
37 if err != nil {
38 t.Fatalf("load copied sidecar: %v", err)
39 }
40 if copied.SessionID != agent.BranchID(target) || !reflect.DeepEqual(copied.Files, want) {
41 t.Fatalf("copied state = %+v", copied)
42 }
43 }
44
45 func TestCopyPinnedContextStateCopiesEmptySourceSemantics(t *testing.T) {
46 dir := t.TempDir()
47 source := filepath.Join(dir, "source.jsonl")
48 target := filepath.Join(dir, "target.jsonl")
49 if err := savePinnedContextState(target, []string{"stale.md"}); err != nil {
50 t.Fatal(err)
51 }
52 if err := copyPinnedContextState(source, target); err != nil {
53 t.Fatal(err)
54 }
55 state, err := loadPinnedContextState(target)
56 if err != nil {
57 t.Fatal(err)
58 }
59 if len(state.Files) != 0 {
60 t.Fatalf("empty source left stale target pins: %v", state.Files)
61 }
62 }
63
64 func TestPinnedContextSidecarRejectsCorruptionAndUnknownSchema(t *testing.T) {
65 path := filepath.Join(t.TempDir(), "session.jsonl")
66 sidecar := store.SessionPinnedContext(path)
67 for _, test := range []struct {
68 name string
69 raw string
70 }{
71 {name: "corrupt", raw: "{"},
72 {name: "unknown schema", raw: `{"schemaVersion":2,"sessionId":"session","files":[]}`},
73 {name: "wrong session", raw: `{"schemaVersion":1,"sessionId":"other","files":[]}`},
74 } {
75 t.Run(test.name, func(t *testing.T) {
76 if err := os.WriteFile(sidecar, []byte(test.raw), 0o600); err != nil {
77 t.Fatal(err)
78 }
79 if _, err := loadPinnedContextState(path); err == nil {
80 t.Fatal("invalid sidecar was accepted")
81 }
82 })
83 }
84 }
85
86 func TestLegacyPinnedFilesMigrateOnlyWhenSidecarAbsent(t *testing.T) {
87 path := filepath.Join(t.TempDir(), "session.jsonl")
88 if err := savePinnedContextState(path, []string{}); err != nil {
89 t.Fatal(err)
90 }
91 state, err := loadOrMigratePinnedContextState(path, []string{"legacy.md"})
92 if err != nil {
93 t.Fatal(err)
94 }
95 if len(state.Files) != 0 {
96 t.Fatalf("legacy pins overwrote existing sidecar: %v", state.Files)
97 }
98 raw, err := os.ReadFile(store.SessionPinnedContext(path))
99 if err != nil {
100 t.Fatal(err)
101 }
102 var persisted pinnedContextState
103 if err := json.Unmarshal(raw, &persisted); err != nil {
104 t.Fatal(err)
105 }
106 if len(persisted.Files) != 0 {
107 t.Fatalf("persisted files = %v, want empty", persisted.Files)
108 }
109 }
110
111 func TestFailedLegacyMigrationRemainsRoundTrippable(t *testing.T) {
112 root := t.TempDir()
113 blockedParent := filepath.Join(root, "blocked")
114 if err := os.WriteFile(blockedParent, []byte("not a directory"), 0o600); err != nil {
115 t.Fatal(err)
116 }
117 sessionPath := filepath.Join(blockedParent, "session.jsonl")
118 tab := &WorkspaceTab{ID: "tab", Scope: "project", SessionPath: sessionPath}
119 restoreTabPinnedContext(tab, []string{"legacy.md"})
120 if got := tab.GetPinnedFiles(); !reflect.DeepEqual(got, []string{"legacy.md"}) {
121 t.Fatalf("visible legacy pins = %v", got)
122 }
123 app := &App{tabs: map[string]*WorkspaceTab{"tab": tab}, tabOrder: []string{"tab"}, activeTabID: "tab"}
124 _, entries, _, _ := app.saveTabsCollectLocked()
125 if len(entries) != 1 || !reflect.DeepEqual(entries[0].PinnedFiles, []string{"legacy.md"}) {
126 t.Fatalf("persisted tab entries = %#v, want pending legacy pins", entries)
127 }
128
129 if err := os.Remove(blockedParent); err != nil {
130 t.Fatal(err)
131 }
132 if err := os.MkdirAll(blockedParent, 0o755); err != nil {
133 t.Fatal(err)
134 }
135 migratePendingLegacyPinnedFiles(tab, sessionPath)
136 if pending := tab.pendingLegacyPinnedFilesForPersistence(); len(pending) != 0 {
137 t.Fatalf("pending legacy pins after migration = %v", pending)
138 }
139 state, err := loadPinnedContextState(sessionPath)
140 if err != nil || !reflect.DeepEqual(state.Files, []string{"legacy.md"}) {
141 t.Fatalf("migrated sidecar = %+v, err = %v", state, err)
142 }
143 }
144
145 func TestPathlessLegacyPinsRemainRoundTrippable(t *testing.T) {
146 tab := &WorkspaceTab{ID: "tab", Scope: "project"}
147 restoreTabPinnedContext(tab, []string{"legacy.md"})
148 if got := tab.pendingLegacyPinnedFilesForPersistence(); !reflect.DeepEqual(got, []string{"legacy.md"}) {
149 t.Fatalf("pending pathless legacy pins = %v", got)
150 }
151 sessionPath := filepath.Join(t.TempDir(), "session.jsonl")
152 prepareStartupPinnedContext(tab, sessionPath, "")
153 state, err := loadPinnedContextState(sessionPath)
154 if err != nil || !reflect.DeepEqual(state.Files, []string{"legacy.md"}) {
155 t.Fatalf("startup migration state = %+v, err = %v", state, err)
156 }
157 if pending := tab.pendingLegacyPinnedFilesForPersistence(); len(pending) != 0 {
158 t.Fatalf("pending legacy pins after startup migration = %v", pending)
159 }
160 }
161
161 lines GO