| 1 | import * as fs from "fs/promises"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import { |
| 5 | cleanupTestContext, |
| 6 | createTestContext, |
| 7 | runCLI, |
| 8 | } from "../test-helpers/cli-helpers.js"; |
| 9 | import { |
| 10 | setupMockLLMTest, |
| 11 | cleanupMockLLMServer, |
| 12 | type MockLLMServer, |
| 13 | } from "../test-helpers/mock-llm-server.js"; |
| 14 | |
| 15 | describe("E2E: Resume Flag", () => { |
| 16 | let context: any; |
| 17 | let mockServer: MockLLMServer; |
| 18 | |
| 19 | beforeEach(async () => { |
| 20 | context = await createTestContext(); |
| 21 | mockServer = await setupMockLLMTest(context, { |
| 22 | response: "Hello! Nice to meet you.", |
| 23 | }); |
| 24 | }); |
| 25 | |
| 26 | afterEach(async () => { |
| 27 | await cleanupMockLLMServer(mockServer); |
| 28 | await cleanupTestContext(context); |
| 29 | }); |
| 30 | |
| 31 | it("should resume from previous session when using --resume flag", async () => { |
| 32 | // First, run a chat session with a single message |
| 33 | const firstResult = await runCLI(context, { |
| 34 | args: ["-p", "--config", context.configPath, "hello"], |
| 35 | env: { |
| 36 | // Use a fixed session ID so both CLI calls use the same session |
| 37 | CONTINUE_CLI_TEST_SESSION_ID: "test-session-123", |
| 38 | CONTINUE_GLOBAL_DIR: path.join(context.testDir, ".continue"), |
| 39 | }, |
| 40 | timeout: 15000, |
| 41 | }); |
| 42 | |
| 43 | // Verify the first session completed successfully |
| 44 | expect(firstResult.exitCode).toBe(0); |
| 45 | expect(firstResult.stdout).toContain("Hello! Nice to meet you."); |
| 46 | |
| 47 | // Verify that a session file was created |
| 48 | const sessionDir = path.join(context.testDir, ".continue", "sessions"); |
| 49 | |
| 50 | // Ensure session directory exists |
| 51 | try { |
| 52 | await fs.mkdir(sessionDir, { recursive: true }); |
| 53 | } catch (error) { |
| 54 | // Directory already exists, continue |
| 55 | } |
| 56 | |
| 57 | const sessionFiles = (await fs.readdir(sessionDir)).filter( |
| 58 | (f) => f.endsWith(".json") && f !== "sessions.json", |
| 59 | ); |
| 60 | expect(sessionFiles).toHaveLength(1); |
| 61 | |
| 62 | // Read the session file to verify it contains our conversation |
| 63 | const sessionFile = sessionFiles[0]; |
| 64 | const sessionPath = path.join(sessionDir, sessionFile); |
| 65 | const sessionContent = await fs.readFile(sessionPath, "utf-8"); |
| 66 | const sessionData = JSON.parse(sessionContent); |
| 67 | |
| 68 | expect(sessionData.history).toBeDefined(); |
| 69 | expect(sessionData.history.length).toBeGreaterThan(0); |
| 70 | |
| 71 | // Should contain the user message and assistant response |
| 72 | const userMessage = sessionData.history.find( |
| 73 | (msg: any) => msg.message?.role === "user", |
| 74 | ); |
| 75 | const assistantMessage = sessionData.history.find( |
| 76 | (msg: any) => msg.message?.role === "assistant", |
| 77 | ); |
| 78 | |
| 79 | expect(userMessage?.message?.content).toBe("hello"); |
| 80 | expect(assistantMessage?.message?.content).toBe("Hello! Nice to meet you."); |
| 81 | |
| 82 | // Now run with --resume flag using the same session ID |
| 83 | const resumeResult = await runCLI(context, { |
| 84 | args: ["--resume", "--config", context.configPath], |
| 85 | env: { |
| 86 | CONTINUE_CLI_TEST_SESSION_ID: "test-session-123", |
| 87 | CONTINUE_GLOBAL_DIR: path.join(context.testDir, ".continue"), |
| 88 | }, |
| 89 | timeout: 15000, |
| 90 | }); |
| 91 | |
| 92 | // The resume command should complete successfully |
| 93 | expect(resumeResult.exitCode).toBe(0); |
| 94 | |
| 95 | // In headless mode with no new prompt, it should just show existing messages |
| 96 | // This is a simplification for the test - the behavior may vary based on implementation |
| 97 | }, 30000); |
| 98 | |
| 99 | it("should handle --resume when no previous session exists", async () => { |
| 100 | // Try to resume without any previous session |
| 101 | const result = await runCLI(context, { |
| 102 | args: ["--resume", "--config", context.configPath], |
| 103 | env: { |
| 104 | CONTINUE_CLI_TEST_SESSION_ID: "no-session-456", |
| 105 | CONTINUE_GLOBAL_DIR: path.join(context.testDir, ".continue"), |
| 106 | }, |
| 107 | timeout: 15000, |
| 108 | }); |
| 109 | |
| 110 | // Should handle gracefully (either start new session or show appropriate message) |
| 111 | expect(result.exitCode).toBe(0); |
| 112 | }, 20000); |
| 113 | |
| 114 | it("should resume and allow new messages to be added", async () => { |
| 115 | // Setup mock to return different responses for different calls |
| 116 | let responseCount = 0; |
| 117 | |
| 118 | // Clean up current server first |
| 119 | await cleanupMockLLMServer(mockServer); |
| 120 | |
| 121 | // Wait a bit to ensure cleanup |
| 122 | await new Promise((resolve) => setTimeout(resolve, 100)); |
| 123 | |
| 124 | // Create new server with dynamic response |
| 125 | mockServer = await setupMockLLMTest(context, { |
| 126 | response: (prompt: string) => { |
| 127 | responseCount++; |
| 128 | if (responseCount === 1) { |
| 129 | return "First response"; |
| 130 | } else { |
| 131 | return "Second response"; |
| 132 | } |
| 133 | }, |
| 134 | }); |
| 135 | |
| 136 | const sessionId = "test-session-789"; |
| 137 | |
| 138 | // First session |
| 139 | const firstResult = await runCLI(context, { |
| 140 | args: ["-p", "--config", context.configPath, "first message"], |
| 141 | env: { |
| 142 | CONTINUE_CLI_TEST_SESSION_ID: sessionId, |
| 143 | CONTINUE_GLOBAL_DIR: path.join(context.testDir, ".continue"), |
| 144 | }, |
| 145 | timeout: 15000, |
| 146 | }); |
| 147 | |
| 148 | expect(firstResult.exitCode).toBe(0); |
| 149 | expect(firstResult.stdout).toContain("First response"); |
| 150 | |
| 151 | // Verify the first session was saved correctly |
| 152 | const sessionDir = path.join(context.testDir, ".continue", "sessions"); |
| 153 | |
| 154 | // Ensure session directory exists |
| 155 | try { |
| 156 | await fs.mkdir(sessionDir, { recursive: true }); |
| 157 | } catch (error) { |
| 158 | // Directory already exists, continue |
| 159 | } |
| 160 | |
| 161 | let sessionFiles = (await fs.readdir(sessionDir)).filter( |
| 162 | (f) => f.endsWith(".json") && f !== "sessions.json", |
| 163 | ); |
| 164 | expect(sessionFiles).toHaveLength(1); |
| 165 | |
| 166 | let sessionFile = sessionFiles[0]; |
| 167 | let sessionPath = path.join(sessionDir, sessionFile); |
| 168 | let sessionContent = await fs.readFile(sessionPath, "utf-8"); |
| 169 | let sessionData = JSON.parse(sessionContent); |
| 170 | |
| 171 | // Resume and add a new message with the SAME session ID |
| 172 | const resumeResult = await runCLI(context, { |
| 173 | args: [ |
| 174 | "--resume", |
| 175 | "-p", |
| 176 | "--config", |
| 177 | context.configPath, |
| 178 | "second message", |
| 179 | ], |
| 180 | env: { |
| 181 | CONTINUE_CLI_TEST_SESSION_ID: sessionId, |
| 182 | CONTINUE_GLOBAL_DIR: path.join(context.testDir, ".continue"), |
| 183 | }, |
| 184 | timeout: 15000, |
| 185 | }); |
| 186 | |
| 187 | expect(resumeResult.exitCode).toBe(0); |
| 188 | // Should show the new response |
| 189 | expect(resumeResult.stdout).toContain("Second response"); |
| 190 | |
| 191 | // Verify both requests were made to the server |
| 192 | expect(mockServer.requests).toHaveLength(2); |
| 193 | |
| 194 | // Check that the session file contains both messages |
| 195 | sessionFiles = (await fs.readdir(sessionDir)).filter( |
| 196 | (f) => f.endsWith(".json") && f !== "sessions.json", |
| 197 | ); |
| 198 | sessionFile = sessionFiles[0]; |
| 199 | sessionPath = path.join(sessionDir, sessionFile); |
| 200 | sessionContent = await fs.readFile(sessionPath, "utf-8"); |
| 201 | sessionData = JSON.parse(sessionContent); |
| 202 | |
| 203 | const userMessages = sessionData.history.filter( |
| 204 | (msg: any) => msg.message?.role === "user", |
| 205 | ); |
| 206 | const assistantMessages = sessionData.history.filter( |
| 207 | (msg: any) => msg.message?.role === "assistant", |
| 208 | ); |
| 209 | |
| 210 | expect(userMessages).toHaveLength(2); |
| 211 | expect(assistantMessages).toHaveLength(2); |
| 212 | expect(userMessages[0].message?.content).toBe("first message"); |
| 213 | expect(userMessages[1].message?.content).toBe("second message"); |
| 214 | expect(assistantMessages[0].message?.content).toBe("First response"); |
| 215 | expect(assistantMessages[1].message?.content).toBe("Second response"); |
| 216 | }, 30000); |
| 217 | }); |