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}