| 1 | import { saveSession, hasSession, clearSession } from "./session.js"; |
| 2 | |
| 3 | describe("Session Management", () => { |
| 4 | let originalEnv: NodeJS.ProcessEnv; |
| 5 | |
| 6 | beforeEach(() => { |
| 7 | originalEnv = { ...process.env }; |
| 8 | |
| 9 | // Clear session-related environment variables |
| 10 | delete process.env.TMUX_PANE; |
| 11 | delete process.env.TERM_SESSION_ID; |
| 12 | delete process.env.SSH_TTY; |
| 13 | delete process.env.TMUX; |
| 14 | delete process.env.STY; |
| 15 | }); |
| 16 | |
| 17 | afterEach(() => { |
| 18 | process.env = originalEnv; |
| 19 | // Clean up any test sessions |
| 20 | try { |
| 21 | clearSession(); |
| 22 | } catch (error) { |
| 23 | // Ignore cleanup errors |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | describe("Environment Variable Session IDs", () => { |
| 28 | it("should create unique sessions for different TMUX_PANE values", () => { |
| 29 | // Test first session |
| 30 | process.env.TMUX_PANE = "%1"; |
| 31 | saveSession([{ role: "user", content: "test1" }]); |
| 32 | expect(hasSession()).toBe(true); |
| 33 | clearSession(); |
| 34 | |
| 35 | // Test second session with different TMUX_PANE |
| 36 | process.env.TMUX_PANE = "%2"; |
| 37 | saveSession([{ role: "user", content: "test2" }]); |
| 38 | expect(hasSession()).toBe(true); |
| 39 | clearSession(); |
| 40 | }); |
| 41 | |
| 42 | it("should use TERM_SESSION_ID when TMUX_PANE is not available", () => { |
| 43 | process.env.TERM_SESSION_ID = "w1t0s0:0.0"; |
| 44 | |
| 45 | saveSession([{ role: "user", content: "test" }]); |
| 46 | expect(hasSession()).toBe(true); |
| 47 | clearSession(); |
| 48 | }); |
| 49 | |
| 50 | it("should use SSH_TTY when other env vars are not available", () => { |
| 51 | process.env.SSH_TTY = "/dev/pts/0"; |
| 52 | |
| 53 | saveSession([{ role: "user", content: "test" }]); |
| 54 | expect(hasSession()).toBe(true); |
| 55 | clearSession(); |
| 56 | }); |
| 57 | |
| 58 | it("should clean special characters from session IDs", () => { |
| 59 | process.env.TMUX_PANE = "%1:0.0/special#chars@test!"; |
| 60 | |
| 61 | // Should not throw error despite special characters |
| 62 | expect(() => { |
| 63 | saveSession([{ role: "user", content: "test" }]); |
| 64 | }).not.toThrow(); |
| 65 | |
| 66 | expect(hasSession()).toBe(true); |
| 67 | clearSession(); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | describe("Test Mode", () => { |
| 72 | it("should use test session ID when provided", () => { |
| 73 | process.env.CONTINUE_CLI_TEST_SESSION_ID = "test-123"; |
| 74 | |
| 75 | saveSession([{ role: "user", content: "test" }]); |
| 76 | expect(hasSession()).toBe(true); |
| 77 | clearSession(); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | describe("Fallback Mechanisms", () => { |
| 82 | it("should create a session even when no environment variables are available", () => { |
| 83 | // No environment variables set |
| 84 | |
| 85 | // Should fall back to TTY path, process tree, or PID |
| 86 | expect(() => { |
| 87 | saveSession([{ role: "user", content: "fallback test" }]); |
| 88 | }).not.toThrow(); |
| 89 | |
| 90 | expect(hasSession()).toBe(true); |
| 91 | clearSession(); |
| 92 | }); |
| 93 | |
| 94 | it("should handle session persistence correctly", () => { |
| 95 | process.env.TMUX_PANE = "%test"; |
| 96 | |
| 97 | const testHistory = [ |
| 98 | { role: "user" as const, content: "Hello" }, |
| 99 | { role: "assistant" as const, content: "Hi there!" }, |
| 100 | ]; |
| 101 | |
| 102 | // Save session |
| 103 | saveSession(testHistory); |
| 104 | expect(hasSession()).toBe(true); |
| 105 | |
| 106 | // Clear and verify it's gone |
| 107 | clearSession(); |
| 108 | expect(hasSession()).toBe(false); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | describe("Error Handling", () => { |
| 113 | it("should handle filesystem errors gracefully", () => { |
| 114 | // Try to save to invalid session ID that might cause filesystem issues |
| 115 | process.env.CONTINUE_CLI_TEST_SESSION_ID = "test-with-various-chars"; |
| 116 | |
| 117 | expect(() => { |
| 118 | saveSession([{ role: "user", content: "test" }]); |
| 119 | }).not.toThrow(); |
| 120 | |
| 121 | clearSession(); |
| 122 | }); |
| 123 | }); |
| 124 | }); |