1package core
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/patriceckhart/zot/internal/provider"
10)
11
12// TestSessionExportImportRoundTrip writes a few messages to a live
13// session, exports it, imports the export under a different cwd,
14// and verifies OpenSession on the imported file yields the same
15// message payloads.
16func TestSessionExportImportRoundTrip(t *testing.T) {
17 root := t.TempDir()
18 originalCWD := "/path/to/project"
19 sess, err := NewSession(root, originalCWD, "anthropic", "claude-opus-4-7", "0.0.0-test")
20 if err != nil {
21 t.Fatal(err)
22 }
23 _ = sess.AppendMessage(provider.Message{
24 Role: provider.RoleUser,
25 Content: []provider.Content{provider.TextBlock{Text: "hello from the exporter"}},
26 })
27 _ = sess.AppendMessage(provider.Message{
28 Role: provider.RoleAssistant,
29 Content: []provider.Content{provider.TextBlock{Text: "hi — reply from the assistant"}},
30 })
31 _ = sess.Close()
32
33 // Export to a directory — helper should build a name inside it.
34 exportDir := t.TempDir()
35 exportPath, err := ExportSession(sess.Path, exportDir)
36 if err != nil {
37 t.Fatalf("ExportSession: %v", err)
38 }
39 if !strings.HasSuffix(exportPath, PortableExt) {
40 t.Errorf("exported path should end in %s, got %q", PortableExt, exportPath)
41 }
42 if _, err := os.Stat(exportPath); err != nil {
43 t.Fatalf("exported file doesn't exist: %v", err)
44 }
45
46 // Import into a different root + cwd.
47 root2 := t.TempDir()
48 cwd2 := "/some/other/project"
49 importedPath, err := ImportSession(exportPath, root2, cwd2, "0.0.0-test")
50 if err != nil {
51 t.Fatalf("ImportSession: %v", err)
52 }
53 if filepath.Dir(importedPath) != SessionsDir(root2, cwd2) {
54 t.Errorf("imported file should land in SessionsDir, got %q", importedPath)
55 }
56
57 // Reopen and verify message round-trip.
58 imported, msgs, err := OpenSession(importedPath)
59 if err != nil {
60 t.Fatalf("OpenSession: %v", err)
61 }
62 defer imported.Close()
63 if imported.Meta.CWD != cwd2 {
64 t.Errorf("meta cwd: want %q, got %q", cwd2, imported.Meta.CWD)
65 }
66 if imported.Meta.ID == sess.ID {
67 t.Errorf("imported session kept the original id %q; must be rotated", sess.ID)
68 }
69 if imported.Meta.Model != "claude-opus-4-7" {
70 t.Errorf("model not preserved: %q", imported.Meta.Model)
71 }
72 if len(msgs) != 2 {
73 t.Fatalf("want 2 messages, got %d", len(msgs))
74 }
75 // Text should round-trip.
76 if extractText(msgs[0]) != "hello from the exporter" {
77 t.Errorf("msg 0 mismatch: %q", extractText(msgs[0]))
78 }
79 if extractText(msgs[1]) != "hi — reply from the assistant" {
80 t.Errorf("msg 1 mismatch: %q", extractText(msgs[1]))
81 }
82}
83
84// TestExportToFilePath writes to an explicit file path (no
85// directory guessing) and checks the .zotsession extension is
86// appended when missing.
87func TestExportToFilePath(t *testing.T) {
88 root := t.TempDir()
89 sess, err := NewSession(root, "/cwd", "anthropic", "claude-opus-4-7", "0.0.0-test")
90 if err != nil {
91 t.Fatal(err)
92 }
93 _ = sess.AppendMessage(provider.Message{
94 Role: provider.RoleUser,
95 Content: []provider.Content{provider.TextBlock{Text: "x"}},
96 })
97 _ = sess.Close()
98
99 // No extension — should add .zotsession.
100 dst := filepath.Join(t.TempDir(), "mysession")
101 out, err := ExportSession(sess.Path, dst)
102 if err != nil {
103 t.Fatal(err)
104 }
105 if !strings.HasSuffix(out, PortableExt) {
106 t.Errorf("want .zotsession suffix on %q", out)
107 }
108}
109
110// TestExportStripsCWDFromMeta verifies the exported meta no longer
111// carries the source user's cwd (not useful to the recipient).
112func TestExportStripsCWDFromMeta(t *testing.T) {
113 root := t.TempDir()
114 sess, err := NewSession(root, "/original/cwd", "anthropic", "claude-opus-4-7", "0.0.0-test")
115 if err != nil {
116 t.Fatal(err)
117 }
118 _ = sess.AppendMessage(provider.Message{
119 Role: provider.RoleUser,
120 Content: []provider.Content{provider.TextBlock{Text: "x"}},
121 })
122 _ = sess.Close()
123
124 out, err := ExportSession(sess.Path, filepath.Join(t.TempDir(), "x"+PortableExt))
125 if err != nil {
126 t.Fatal(err)
127 }
128 b, _ := os.ReadFile(out)
129 if strings.Contains(string(b), "/original/cwd") {
130 t.Errorf("exported file leaks the source cwd: %s", string(b))
131 }
132}
133
134// TestBranchSessionCopiesPrefix writes several messages to a
135// session, branches at message index 2 (first user + first
136// assistant), and verifies the new session has exactly those two
137// messages with parent + fork_point meta set.
138func TestBranchSessionCopiesPrefix(t *testing.T) {
139 root := t.TempDir()
140 cwd := "/project"
141 parent, err := NewSession(root, cwd, "anthropic", "claude-opus-4-7", "0.0.0-test")
142 if err != nil {
143 t.Fatal(err)
144 }
145 _ = parent.AppendMessage(provider.Message{
146 Role: provider.RoleUser,
147 Content: []provider.Content{provider.TextBlock{Text: "first"}},
148 })
149 _ = parent.AppendMessage(provider.Message{
150 Role: provider.RoleAssistant,
151 Content: []provider.Content{provider.TextBlock{Text: "first reply"}},
152 })
153 _ = parent.AppendMessage(provider.Message{
154 Role: provider.RoleUser,
155 Content: []provider.Content{provider.TextBlock{Text: "second"}},
156 })
157 _ = parent.Close()
158
159 // Branch at the first user+assistant pair (upToMessageIdx=2).
160 branchPath, err := BranchSession(parent.Path, root, cwd, "0.0.0-test", 2)
161 if err != nil {
162 t.Fatalf("BranchSession: %v", err)
163 }
164 branch, msgs, err := OpenSession(branchPath)
165 if err != nil {
166 t.Fatal(err)
167 }
168 defer branch.Close()
169
170 if len(msgs) != 2 {
171 t.Errorf("want 2 copied messages, got %d", len(msgs))
172 }
173 if branch.Meta.Parent != parent.Meta.ID {
174 t.Errorf("parent id: want %q, got %q", parent.Meta.ID, branch.Meta.Parent)
175 }
176 if branch.Meta.ForkPoint != 2 {
177 t.Errorf("fork_point: want 2, got %d", branch.Meta.ForkPoint)
178 }
179 if branch.Meta.ID == parent.Meta.ID {
180 t.Errorf("branch kept parent id; must rotate")
181 }
182}
183
184// TestBuildSessionTree verifies parent/child edges are rebuilt
185// from meta + sibling-scan.
186func TestBuildSessionTree(t *testing.T) {
187 root := t.TempDir()
188 cwd := "/project"
189 parent, _ := NewSession(root, cwd, "anthropic", "claude-opus-4-7", "0.0.0-test")
190 _ = parent.AppendMessage(provider.Message{
191 Role: provider.RoleUser,
192 Content: []provider.Content{provider.TextBlock{Text: "x"}},
193 })
194 _ = parent.Close()
195
196 childA, err := BranchSession(parent.Path, root, cwd, "0.0.0-test", 1)
197 if err != nil {
198 t.Fatal(err)
199 }
200 childB, err := BranchSession(parent.Path, root, cwd, "0.0.0-test", 1)
201 if err != nil {
202 t.Fatal(err)
203 }
204 _ = childA
205 _ = childB
206
207 tree := BuildSessionTree(root, cwd)
208 if len(tree) != 1 {
209 t.Fatalf("want 1 root, got %d", len(tree))
210 }
211 rootNode := tree[0]
212 if rootNode.Meta.ID != parent.Meta.ID {
213 t.Errorf("root should be parent %q, got %q", parent.Meta.ID, rootNode.Meta.ID)
214 }
215 if len(rootNode.Children) != 2 {
216 t.Errorf("want 2 children, got %d", len(rootNode.Children))
217 }
218}