| 1 | import fs from "fs"; |
| 2 | import os from "os"; |
| 3 | |
| 4 | import type { ChatHistoryItem, Session } from "core/index.js"; |
| 5 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | |
| 7 | import { |
| 8 | clearSession, |
| 9 | createSession, |
| 10 | getCurrentSession, |
| 11 | hasSession, |
| 12 | loadSession, |
| 13 | saveSession, |
| 14 | updateSessionHistory, |
| 15 | updateSessionTitle, |
| 16 | } from "./session.js"; |
| 17 | |
| 18 | // Mock dependencies first, before any imports |
| 19 | vi.mock("os", () => ({ |
| 20 | default: { |
| 21 | homedir: vi.fn(() => "/home/test"), |
| 22 | }, |
| 23 | homedir: vi.fn(() => "/home/test"), |
| 24 | })); |
| 25 | vi.mock("fs"); |
| 26 | vi.mock("uuid", () => ({ |
| 27 | v4: vi.fn(() => "test-uuid-123"), |
| 28 | })); |
| 29 | vi.mock("./util/logger.js", () => ({ |
| 30 | logger: { |
| 31 | debug: vi.fn(), |
| 32 | info: vi.fn(), |
| 33 | warn: vi.fn(), |
| 34 | error: vi.fn(), |
| 35 | }, |
| 36 | })); |
| 37 | vi.mock("../../core/util/history.js", () => ({ |
| 38 | default: { |
| 39 | save: vi.fn(), |
| 40 | load: vi.fn(() => ({ |
| 41 | sessionId: "test-session-id", |
| 42 | title: "Test Session", |
| 43 | workspaceDirectory: "/test/workspace", |
| 44 | history: [], |
| 45 | })), |
| 46 | list: vi.fn(() => []), |
| 47 | }, |
| 48 | })); |
| 49 | |
| 50 | const mockFs = vi.mocked(fs); |
| 51 | const mockOs = vi.mocked(os); |
| 52 | |
| 53 | describe("SessionManager", () => { |
| 54 | beforeEach(() => { |
| 55 | vi.clearAllMocks(); |
| 56 | |
| 57 | // Reset the singleton between tests |
| 58 | // @ts-ignore - accessing private static property for testing |
| 59 | const SessionManager = (globalThis as any).SessionManager; |
| 60 | if (SessionManager) { |
| 61 | SessionManager.instance = null; |
| 62 | } |
| 63 | |
| 64 | // Default mocks |
| 65 | mockOs.homedir.mockReturnValue("/home/test"); |
| 66 | mockFs.existsSync.mockReturnValue(true); |
| 67 | mockFs.mkdirSync.mockReturnValue(undefined); |
| 68 | mockFs.writeFileSync.mockReturnValue(undefined); |
| 69 | mockFs.readFileSync.mockReturnValue("[]"); |
| 70 | mockFs.readdirSync.mockReturnValue([]); |
| 71 | mockFs.statSync.mockReturnValue({ mtime: new Date() } as any); |
| 72 | }); |
| 73 | |
| 74 | describe("getCurrentSession", () => { |
| 75 | it("should create a new session if none exists", () => { |
| 76 | const session = getCurrentSession(); |
| 77 | |
| 78 | expect(session).toEqual({ |
| 79 | sessionId: "test-uuid-123", |
| 80 | title: "Untitled Session", |
| 81 | workspaceDirectory: process.cwd(), |
| 82 | history: [], |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | it("should return the same session on subsequent calls", () => { |
| 87 | const session1 = getCurrentSession(); |
| 88 | const session2 = getCurrentSession(); |
| 89 | |
| 90 | expect(session1).toBe(session2); // Same reference |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | describe("createSession", () => { |
| 95 | it("should create a new session with default values", () => { |
| 96 | const session = createSession(); |
| 97 | |
| 98 | expect(session).toEqual({ |
| 99 | sessionId: "test-uuid-123", |
| 100 | title: "Untitled Session", |
| 101 | workspaceDirectory: process.cwd(), |
| 102 | history: [], |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | it("should create a session with provided history", () => { |
| 107 | const history: ChatHistoryItem[] = [ |
| 108 | { |
| 109 | message: { |
| 110 | role: "user", |
| 111 | content: "Hello", |
| 112 | }, |
| 113 | contextItems: [], |
| 114 | }, |
| 115 | ]; |
| 116 | |
| 117 | const session = createSession(history); |
| 118 | |
| 119 | expect(session.history).toBe(history); |
| 120 | }); |
| 121 | |
| 122 | it("should set the created session as current", () => { |
| 123 | const session = createSession(); |
| 124 | const currentSession = getCurrentSession(); |
| 125 | |
| 126 | expect(currentSession).toBe(session); |
| 127 | }); |
| 128 | }); |
| 129 | |
| 130 | describe("updateSessionHistory", () => { |
| 131 | it("should update the current session's history", () => { |
| 132 | const session = getCurrentSession(); |
| 133 | const newHistory: ChatHistoryItem[] = [ |
| 134 | { |
| 135 | message: { |
| 136 | role: "user", |
| 137 | content: "New message", |
| 138 | }, |
| 139 | contextItems: [], |
| 140 | }, |
| 141 | ]; |
| 142 | |
| 143 | updateSessionHistory(newHistory); |
| 144 | |
| 145 | expect(session.history).toBe(newHistory); |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | describe("updateSessionTitle", () => { |
| 150 | it("should update the current session's title", () => { |
| 151 | const session = getCurrentSession(); |
| 152 | |
| 153 | updateSessionTitle("My New Title"); |
| 154 | |
| 155 | expect(session.title).toBe("My New Title"); |
| 156 | }); |
| 157 | }); |
| 158 | |
| 159 | describe("saveSession", () => { |
| 160 | it("should save the current session to file", () => { |
| 161 | const session = getCurrentSession(); |
| 162 | session.title = "Test Session"; |
| 163 | session.history = [ |
| 164 | { |
| 165 | message: { |
| 166 | role: "user", |
| 167 | content: "Hello", |
| 168 | }, |
| 169 | contextItems: [], |
| 170 | }, |
| 171 | ]; |
| 172 | |
| 173 | saveSession(); |
| 174 | |
| 175 | expect(mockFs.writeFileSync).toHaveBeenCalledWith( |
| 176 | expect.stringContaining("test-uuid-123.json"), |
| 177 | expect.stringContaining("Test Session"), |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | it("should filter out system messages except the first one", () => { |
| 182 | const session = getCurrentSession(); |
| 183 | session.history = [ |
| 184 | { |
| 185 | message: { |
| 186 | role: "system", |
| 187 | content: "System message 1", |
| 188 | }, |
| 189 | contextItems: [], |
| 190 | }, |
| 191 | { |
| 192 | message: { |
| 193 | role: "user", |
| 194 | content: "User message", |
| 195 | }, |
| 196 | contextItems: [], |
| 197 | }, |
| 198 | { |
| 199 | message: { |
| 200 | role: "system", |
| 201 | content: "System message 2", |
| 202 | }, |
| 203 | contextItems: [], |
| 204 | }, |
| 205 | ]; |
| 206 | |
| 207 | saveSession(); |
| 208 | |
| 209 | const savedData = JSON.parse( |
| 210 | (mockFs.writeFileSync as any).mock.calls[0][1], |
| 211 | ); |
| 212 | // After modification, system messages are filtered out |
| 213 | expect(savedData.history).toHaveLength(1); |
| 214 | expect(savedData.history[0].message.role).toBe("user"); |
| 215 | }); |
| 216 | }); |
| 217 | |
| 218 | describe("loadSession", () => { |
| 219 | it("should return null if session directory doesn't exist", () => { |
| 220 | mockFs.existsSync.mockReturnValue(false); |
| 221 | |
| 222 | const result = loadSession(); |
| 223 | |
| 224 | expect(result).toBeNull(); |
| 225 | }); |
| 226 | |
| 227 | it("should return null if no session files exist", () => { |
| 228 | mockFs.readdirSync.mockReturnValue([]); |
| 229 | |
| 230 | const result = loadSession(); |
| 231 | |
| 232 | expect(result).toBeNull(); |
| 233 | }); |
| 234 | |
| 235 | it("should load the most recent session", () => { |
| 236 | const mockSession: Session = { |
| 237 | sessionId: "loaded-session-id", |
| 238 | title: "Loaded Session", |
| 239 | workspaceDirectory: "/test/workspace", |
| 240 | history: [], |
| 241 | }; |
| 242 | |
| 243 | mockFs.readdirSync.mockReturnValue([ |
| 244 | "old-session.json" as any, |
| 245 | "sessions.json" as any, // Should be filtered out |
| 246 | "recent-session.json" as any, |
| 247 | ]); |
| 248 | |
| 249 | mockFs.statSync |
| 250 | .mockReturnValueOnce({ mtime: new Date("2023-01-01") } as any) |
| 251 | .mockReturnValueOnce({ mtime: new Date("2023-01-02") } as any); |
| 252 | |
| 253 | mockFs.readFileSync.mockReturnValue(JSON.stringify(mockSession)); |
| 254 | |
| 255 | const result = loadSession(); |
| 256 | |
| 257 | expect(result).toEqual(mockSession); |
| 258 | expect(getCurrentSession()).toEqual(mockSession); |
| 259 | }); |
| 260 | }); |
| 261 | |
| 262 | describe("clearSession", () => { |
| 263 | it("should delete session file if it exists", () => { |
| 264 | // Set up a current session |
| 265 | createSession(); |
| 266 | mockFs.existsSync.mockReturnValue(true); |
| 267 | mockFs.unlinkSync.mockReturnValue(undefined); |
| 268 | |
| 269 | clearSession(); |
| 270 | |
| 271 | expect(mockFs.unlinkSync).toHaveBeenCalledWith( |
| 272 | expect.stringContaining("test-uuid-123.json"), |
| 273 | ); |
| 274 | }); |
| 275 | |
| 276 | it("should not delete file if it doesn't exist", () => { |
| 277 | createSession(); |
| 278 | mockFs.existsSync.mockReturnValue(false); |
| 279 | |
| 280 | clearSession(); |
| 281 | |
| 282 | expect(mockFs.unlinkSync).not.toHaveBeenCalled(); |
| 283 | }); |
| 284 | |
| 285 | it("should clear the current session from memory", () => { |
| 286 | createSession(); |
| 287 | |
| 288 | clearSession(); |
| 289 | |
| 290 | // Getting session after clear should create a new one |
| 291 | const newSession = getCurrentSession(); |
| 292 | expect(newSession.sessionId).toBe("test-uuid-123"); // New UUID |
| 293 | }); |
| 294 | }); |
| 295 | |
| 296 | describe("hasSession", () => { |
| 297 | it("should return false if no current session exists", () => { |
| 298 | // Clear any existing session |
| 299 | clearSession(); |
| 300 | |
| 301 | const result = hasSession(); |
| 302 | |
| 303 | expect(result).toBe(false); |
| 304 | }); |
| 305 | |
| 306 | it("should return false if session file doesn't exist", () => { |
| 307 | createSession(); |
| 308 | mockFs.existsSync.mockReturnValue(false); |
| 309 | |
| 310 | const result = hasSession(); |
| 311 | |
| 312 | expect(result).toBe(false); |
| 313 | }); |
| 314 | |
| 315 | it("should return true if session exists and file exists", () => { |
| 316 | createSession(); |
| 317 | mockFs.existsSync.mockReturnValue(true); |
| 318 | |
| 319 | const result = hasSession(); |
| 320 | |
| 321 | expect(result).toBe(true); |
| 322 | }); |
| 323 | }); |
| 324 | |
| 325 | describe("singleton behavior", () => { |
| 326 | it("should maintain the same session across different function calls", () => { |
| 327 | const session1 = getCurrentSession(); |
| 328 | updateSessionTitle("Modified Title"); |
| 329 | const session2 = getCurrentSession(); |
| 330 | |
| 331 | expect(session1).toBe(session2); |
| 332 | expect(session2.title).toBe("Modified Title"); |
| 333 | }); |
| 334 | |
| 335 | it("should persist changes across session operations", () => { |
| 336 | createSession(); |
| 337 | updateSessionTitle("Test Title"); |
| 338 | updateSessionHistory([ |
| 339 | { |
| 340 | message: { |
| 341 | role: "user", |
| 342 | content: "Test message", |
| 343 | }, |
| 344 | contextItems: [], |
| 345 | }, |
| 346 | ]); |
| 347 | |
| 348 | const session = getCurrentSession(); |
| 349 | |
| 350 | expect(session.title).toBe("Test Title"); |
| 351 | expect(session.history).toHaveLength(1); |
| 352 | expect(session.history[0].message.content).toBe("Test message"); |
| 353 | }); |
| 354 | }); |
| 355 | }); |