| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestColdCompatibilityReadersResolveOriginalGlobalDirectory(t *testing.T) { |
| 14 | app := historySliceTestApp(t) |
| 15 | t.Cleanup(app.closeHistoryReaders) |
| 16 | tab := newColdHistoryTab(t, app) |
| 17 | tab.WorkspaceRoot = globalWorkspaceRoot() |
| 18 | dir := config.SessionDir() |
| 19 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | tab.SessionPath = filepath.Join(dir, "global-before-workspaces.jsonl") |
| 23 | body := []byte("{\"role\":\"user\",\"content\":\"original global history\"}\n") |
| 24 | if err := os.WriteFile(tab.SessionPath, body, 0600); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | page := app.HistoryPageForTab(tab.ID, 0, 10) |
| 28 | if len(page.Messages) != 1 || page.Messages[0].Content != "original global history" { |
| 29 | t.Fatalf("legacy page lost its original source: %+v", page) |
| 30 | } |
| 31 | messages := app.HistoryForTab(tab.ID) |
| 32 | if len(messages) != 1 || messages[0].Content != "original global history" { |
| 33 | t.Fatalf("legacy history lost its original source: %+v", messages) |
| 34 | } |
| 35 | outside := filepath.Join(t.TempDir(), "outside.jsonl") |
| 36 | if err := os.WriteFile(outside, body, 0600); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | for _, source := range []string{outside, filepath.Join(dir, "escape.jsonl")} { |
| 40 | if source != outside { |
| 41 | if err := os.Symlink(outside, source); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | } |
| 45 | if _, _, err := app.historyReadSource(tabSessionDir(tab), source); err == nil { |
| 46 | t.Fatalf("accepted a source outside known roots: %s", source) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestHistorySliceColdPathBeforeControllerReady(t *testing.T) { |
| 52 | isolateDesktopUserDirs(t) |
| 53 | root := t.TempDir() |
| 54 | dir := desktopSessionDir(root) |
| 55 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | messages := make([]provider.Message, 0, 80) |
| 59 | for i := range 40 { |
| 60 | messages = append(messages, |
| 61 | provider.Message{Role: provider.RoleUser, Content: fmt.Sprintf("cold user turn %d large-session marker", i)}, |
| 62 | provider.Message{Role: provider.RoleAssistant, Content: fmt.Sprintf("cold assistant turn %d large-session marker", i)}, |
| 63 | ) |
| 64 | } |
| 65 | _, path := saveHistorySliceSession(t, dir, "cold-large.jsonl", messages) |
| 66 | app := NewApp() |
| 67 | tab := &WorkspaceTab{ |
| 68 | ID: "cold-large", |
| 69 | Scope: "project", |
| 70 | WorkspaceRoot: root, |
| 71 | SessionPath: path, |
| 72 | Ready: false, |
| 73 | Ctrl: nil, |
| 74 | } |
| 75 | app.mu.Lock() |
| 76 | app.tabs = map[string]*WorkspaceTab{tab.ID: tab} |
| 77 | app.activeTabID = tab.ID |
| 78 | app.mu.Unlock() |
| 79 | |
| 80 | page := app.HistorySliceForTab(tab.ID, HistorySliceRequest{Turns: 12}) |
| 81 | if page.Error != "" { |
| 82 | t.Fatalf("cold slice error = %q", page.Error) |
| 83 | } |
| 84 | if len(page.Entries) == 0 { |
| 85 | t.Fatal("cold slice returned no entries before controller ready") |
| 86 | } |
| 87 | if page.Source != "index" && page.Source != "scan" { |
| 88 | t.Fatalf("cold source = %q, want index|scan", page.Source) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestHistorySliceReportsErrorInsteadOfEmptySuccess(t *testing.T) { |
| 93 | isolateDesktopUserDirs(t) |
| 94 | app := NewApp() |
| 95 | tab := &WorkspaceTab{ |
| 96 | ID: "missing", |
| 97 | Scope: "project", |
| 98 | SessionPath: filepath.Join(t.TempDir(), "does-not-exist.jsonl"), |
| 99 | Ready: false, |
| 100 | } |
| 101 | app.mu.Lock() |
| 102 | app.tabs = map[string]*WorkspaceTab{tab.ID: tab} |
| 103 | app.activeTabID = tab.ID |
| 104 | app.mu.Unlock() |
| 105 | |
| 106 | page := app.HistorySliceForTab(tab.ID, HistorySliceRequest{}) |
| 107 | if page.Error == "" { |
| 108 | t.Fatal("missing session should report Error, not a silent empty success") |
| 109 | } |
| 110 | if len(page.Entries) != 0 { |
| 111 | t.Fatalf("entries = %d, want 0 on error", len(page.Entries)) |
| 112 | } |
| 113 | } |
| 114 |