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