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