| 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 | }, |
| 39 | timeout: 15000, |
| 40 | }); |
| 41 | |
| 42 | // Verify the first session completed successfully |
| 43 | expect(firstResult.exitCode).toBe(0); |
| 44 | expect(firstResult.stdout).toContain("Hello! Nice to meet you."); |
| 45 | |
| 46 | // Verify that a session file was created |
| 47 | const sessionDir = path.join(context.testDir, ".continue", "sessions"); |
| 48 | const sessionFiles = (await fs.readdir(sessionDir)).filter( |
| 49 | f => f.endsWith('.json') && f !== 'sessions.json' |
| 50 | ); |
| 51 | expect(sessionFiles).toHaveLength(1); |
| 52 | |
| 53 | // Read the session file to verify it contains our conversation |
| 54 | const sessionFile = sessionFiles[0]; |
| 55 | const sessionPath = path.join(sessionDir, sessionFile); |
| 56 | const sessionContent = await fs.readFile(sessionPath, "utf-8"); |
| 57 | const sessionData = JSON.parse(sessionContent); |
| 58 | |
| 59 | expect(sessionData.history).toBeDefined(); |
| 60 | expect(sessionData.history.length).toBeGreaterThan(0); |
| 61 | |
| 62 | // Should contain the user message and assistant response |
| 63 | const userMessage = sessionData.history.find( |
| 64 | (msg: any) => msg.message?.role === "user", |
| 65 | ); |
| 66 | const assistantMessage = sessionData.history.find( |
| 67 | (msg: any) => msg.message?.role === "assistant", |
| 68 | ); |
| 69 | |
| 70 | expect(userMessage?.message?.content).toBe("hello"); |
| 71 | expect(assistantMessage?.message?.content).toBe("Hello! Nice to meet you."); |
| 72 | |
| 73 | // Now run with --resume flag using the same session ID |
| 74 | const resumeResult = await runCLI(context, { |
| 75 | args: ["--resume", "--config", context.configPath], |
| 76 | env: { |
| 77 | CONTINUE_CLI_TEST_SESSION_ID: "test-session-123", |
| 78 | }, |
| 79 | timeout: 15000, |
| 80 | }); |
| 81 | |
| 82 | // The resume command should complete successfully |
| 83 | expect(resumeResult.exitCode).toBe(0); |
| 84 | |
| 85 | // In headless mode with no new prompt, it should just show existing messages |
| 86 | // This is a simplification for the test - the behavior may vary based on implementation |
| 87 | }, 30000); |
| 88 | |
| 89 | it("should handle --resume when no previous session exists", async () => { |
| 90 | // Try to resume without any previous session |
| 91 | const result = await runCLI(context, { |
| 92 | args: ["--resume", "--config", context.configPath], |
| 93 | env: { |
| 94 | CONTINUE_CLI_TEST_SESSION_ID: "no-session-456", |
| 95 | }, |
| 96 | timeout: 15000, |
| 97 | }); |
| 98 | |
| 99 | // Should handle gracefully (either start new session or show appropriate message) |
| 100 | expect(result.exitCode).toBe(0); |
| 101 | }, 20000); |
| 102 | |
| 103 | it("should resume and allow new messages to be added", async () => { |
| 104 | // Setup mock to return different responses for different calls |
| 105 | let responseCount = 0; |
| 106 | |
| 107 | // Clean up current server first |
| 108 | await cleanupMockLLMServer(mockServer); |
| 109 | |
| 110 | // Wait a bit to ensure cleanup |
| 111 | await new Promise((resolve) => setTimeout(resolve, 100)); |
| 112 | |
| 113 | // Create new server with dynamic response |
| 114 | mockServer = await setupMockLLMTest(context, { |
| 115 | response: (prompt: string) => { |
| 116 | responseCount++; |
| 117 | if (responseCount === 1) { |
| 118 | return "First response"; |
| 119 | } else { |
| 120 | return "Second response"; |
| 121 | } |
| 122 | }, |
| 123 | }); |
| 124 | |
| 125 | const sessionId = "test-session-789"; |
| 126 | |
| 127 | // First session |
| 128 | const firstResult = await runCLI(context, { |
| 129 | args: ["-p", "--config", context.configPath, "first message"], |
| 130 | env: { |
| 131 | CONTINUE_CLI_TEST_SESSION_ID: sessionId, |
| 132 | }, |
| 133 | timeout: 15000, |
| 134 | }); |
| 135 | |
| 136 | expect(firstResult.exitCode).toBe(0); |
| 137 | expect(firstResult.stdout).toContain("First response"); |
| 138 | |
| 139 | // Verify the first session was saved correctly |
| 140 | const sessionDir = path.join(context.testDir, ".continue", "sessions"); |
| 141 | let sessionFiles = (await fs.readdir(sessionDir)).filter( |
| 142 | f => f.endsWith('.json') && f !== 'sessions.json' |
| 143 | ); |
| 144 | expect(sessionFiles).toHaveLength(1); |
| 145 | |
| 146 | let sessionFile = sessionFiles[0]; |
| 147 | let sessionPath = path.join(sessionDir, sessionFile); |
| 148 | let sessionContent = await fs.readFile(sessionPath, "utf-8"); |
| 149 | let sessionData = JSON.parse(sessionContent); |
| 150 | |
| 151 | // Resume and add a new message with the SAME session ID |
| 152 | const resumeResult = await runCLI(context, { |
| 153 | args: [ |
| 154 | "--resume", |
| 155 | "-p", |
| 156 | "--config", |
| 157 | context.configPath, |
| 158 | "second message", |
| 159 | ], |
| 160 | env: { |
| 161 | CONTINUE_CLI_TEST_SESSION_ID: sessionId, |
| 162 | }, |
| 163 | timeout: 15000, |
| 164 | }); |
| 165 | |
| 166 | expect(resumeResult.exitCode).toBe(0); |
| 167 | // Should show the new response |
| 168 | expect(resumeResult.stdout).toContain("Second response"); |
| 169 | |
| 170 | // Verify both requests were made to the server |
| 171 | expect(mockServer.requests).toHaveLength(2); |
| 172 | |
| 173 | // Check that the session file contains both messages |
| 174 | sessionFiles = (await fs.readdir(sessionDir)).filter( |
| 175 | f => f.endsWith('.json') && f !== 'sessions.json' |
| 176 | ); |
| 177 | sessionFile = sessionFiles[0]; |
| 178 | sessionPath = path.join(sessionDir, sessionFile); |
| 179 | sessionContent = await fs.readFile(sessionPath, "utf-8"); |
| 180 | sessionData = JSON.parse(sessionContent); |
| 181 | |
| 182 | const userMessages = sessionData.history.filter( |
| 183 | (msg: any) => msg.message?.role === "user", |
| 184 | ); |
| 185 | const assistantMessages = sessionData.history.filter( |
| 186 | (msg: any) => msg.message?.role === "assistant", |
| 187 | ); |
| 188 | |
| 189 | expect(userMessages).toHaveLength(2); |
| 190 | expect(assistantMessages).toHaveLength(2); |
| 191 | expect(userMessages[0].message?.content).toBe("first message"); |
| 192 | expect(userMessages[1].message?.content).toBe("second message"); |
| 193 | expect(assistantMessages[0].message?.content).toBe("First response"); |
| 194 | expect(assistantMessages[1].message?.content).toBe("Second response"); |
| 195 | }, 30000); |
| 196 | }); |