From b9645c99110bfbd6cd786756e7f05ca7b30f1081 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 19 Jul 2025 14:41:11 -0700 Subject: [PATCH] e2e tests --- src/e2e.test.ts | 86 ----------------------- src/e2e/auth.test.ts | 21 +----- src/e2e/basic-commands.test.ts | 34 ++++----- src/e2e/headless-minimal.test.ts | 33 ++++----- src/e2e/headless-simple.test.ts | 112 ++++++------------------------ src/test-helpers/adapter-mocks.ts | 64 +++++------------ src/test-helpers/cli-helpers.ts | 12 ++-- 7 files changed, 75 insertions(+), 287 deletions(-) delete mode 100644 src/e2e.test.ts diff --git a/src/e2e.test.ts b/src/e2e.test.ts deleted file mode 100644 index f185b4238..000000000 --- a/src/e2e.test.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { execaNode } from "execa"; -import path from "path"; -import { getVersion } from "./version.js"; - -describe("CLI E2E Tests", () => { - const $ = (args?: string[] | undefined) => - execaNode(path.resolve("dist/index.js"), args); - - beforeAll(() => { - // Ensure the CLI is built before running E2E tests - // This should be handled by the npm test script or CI pipeline - }); - - describe("--version flag", () => { - it("should display version information", async () => { - const result = await $(["--version"]); - - expect(result.exitCode).toBe(0); - expect(result.stdout).toBe(getVersion()); - }); - }); - - describe("--help flag", () => { - it("should display help information", async () => { - const result = await $(["--help"]); - - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("Continue CLI"); - expect(result.stdout).toContain("AI-powered development assistant"); - expect(result.stdout).toContain("Options:"); - expect(result.stdout).toContain("-p, --print"); - }); - - it("should show available commands", async () => { - const result = await $(["--help"]); - - expect(result.stdout).toContain("Commands:"); - expect(result.stdout).toContain("login"); - expect(result.stdout).toContain("logout"); - }); - }); - - describe("invalid commands", () => { - it("should show error for unknown command", async () => { - try { - // Add explicit timeout for Windows CI - const result = await execaNode( - path.resolve("dist/index.js"), - ["unknown-command"], - { - timeout: 5000, // 5 second timeout instead of default 10 - reject: true, - windowsHide: true, // Prevent console window on Windows - } - ); - fail("Should have thrown an error"); - } catch (error: any) { - expect(error.exitCode).not.toBe(0); - // Check if error message exists in any of the possible locations - const errorOutput = error.message || error.stderr || error.stdout || ""; - expect(errorOutput.length).toBeGreaterThan(0); - } - }, 8000); // Increase test timeout to 8 seconds - - it("should show help on invalid flag", async () => { - try { - // Add explicit timeout for Windows CI - const result = await execaNode( - path.resolve("dist/index.js"), - ["--invalid-flag"], - { - timeout: 5000, // 5 second timeout instead of default 10 - reject: true, - windowsHide: true, // Prevent console window on Windows - } - ); - fail("Should have thrown an error"); - } catch (error: any) { - expect(error.exitCode).not.toBe(0); - // Commander.js usually shows help or error message for invalid flags - const errorOutput = error.message || error.stderr || error.stdout || ""; - expect(errorOutput.length).toBeGreaterThan(0); - } - }, 8000); // Increase test timeout to 8 seconds - }); -}); diff --git a/src/e2e/auth.test.ts b/src/e2e/auth.test.ts index 9eb4bda10..28ef5d87d 100644 --- a/src/e2e/auth.test.ts +++ b/src/e2e/auth.test.ts @@ -37,7 +37,7 @@ describe("E2E: Authentication", () => { const result = await runCLI(context, { args: ["logout"] }); expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("logged out"); + expect(result.stdout).toContain("Successfully logged out"); // Check auth file is removed const authExists = await fs @@ -51,24 +51,7 @@ describe("E2E: Authentication", () => { const result = await runCLI(context, { args: ["logout"] }); expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("logged out"); - }); - }); - - describe("auth state in chat", () => { - it("should prompt for login when using authenticated features", async () => { - // This tests that the CLI properly handles unauthenticated state - const result = await runCLI(context, { - args: ["-p", "Hello"], - env: { - OPENAI_API_KEY: "", // No API key - }, - timeout: 5000, - expectError: true, - }); - - // Should fail or prompt for configuration - expect(result.exitCode).not.toBe(0); + expect(result.stdout).toContain("No active session found"); }); }); }); diff --git a/src/e2e/basic-commands.test.ts b/src/e2e/basic-commands.test.ts index d52505790..d2e99ff44 100644 --- a/src/e2e/basic-commands.test.ts +++ b/src/e2e/basic-commands.test.ts @@ -1,4 +1,8 @@ -import { createTestContext, cleanupTestContext, runCLI } from "../test-helpers/cli-helpers.js"; +import { + cleanupTestContext, + createTestContext, + runCLI, +} from "../test-helpers/cli-helpers.js"; import { getVersion } from "../version.js"; describe("E2E: Basic Commands", () => { @@ -15,7 +19,7 @@ describe("E2E: Basic Commands", () => { describe("--version flag", () => { it("should display version information", async () => { const result = await runCLI(context, { args: ["--version"] }); - + expect(result.exitCode).toBe(0); expect(result.stdout).toBe(getVersion()); expect(result.stderr).toBe(""); @@ -25,7 +29,7 @@ describe("E2E: Basic Commands", () => { describe("--help flag", () => { it("should display help information", async () => { const result = await runCLI(context, { args: ["--help"] }); - + expect(result.exitCode).toBe(0); expect(result.stdout).toContain("Continue CLI"); expect(result.stdout).toContain("AI-powered development assistant"); @@ -40,7 +44,7 @@ describe("E2E: Basic Commands", () => { it("should show available commands", async () => { const result = await runCLI(context, { args: ["--help"] }); - + expect(result.stdout).toContain("Commands:"); expect(result.stdout).toContain("login"); expect(result.stdout).toContain("logout"); @@ -50,38 +54,28 @@ describe("E2E: Basic Commands", () => { describe("subcommand help", () => { it("should show help for login command", async () => { const result = await runCLI(context, { args: ["login", "--help"] }); - + expect(result.exitCode).toBe(0); expect(result.stdout).toContain("Authenticate with Continue"); }); it("should show help for logout command", async () => { const result = await runCLI(context, { args: ["logout", "--help"] }); - + expect(result.exitCode).toBe(0); expect(result.stdout).toContain("Log out from Continue"); }); }); describe("invalid commands", () => { - it("should show error for unknown command", async () => { - const result = await runCLI(context, { - args: ["unknown-command"], - expectError: true - }); - - expect(result.exitCode).not.toBe(0); - expect(result.stderr || result.stdout).toContain("error"); - }); - it("should show error for invalid flag", async () => { - const result = await runCLI(context, { + const result = await runCLI(context, { args: ["--invalid-flag"], - expectError: true + expectError: true, }); - + expect(result.exitCode).not.toBe(0); expect(result.stderr || result.stdout).toContain("error"); }); }); -}); \ No newline at end of file +}); diff --git a/src/e2e/headless-minimal.test.ts b/src/e2e/headless-minimal.test.ts index 5cc00fbcd..80efbac49 100644 --- a/src/e2e/headless-minimal.test.ts +++ b/src/e2e/headless-minimal.test.ts @@ -1,6 +1,10 @@ -import { createTestContext, cleanupTestContext, runCLI, createTestConfig } from "../test-helpers/cli-helpers.js"; import fs from "fs/promises"; import path from "path"; +import { + cleanupTestContext, + createTestContext, + runCLI, +} from "../test-helpers/cli-helpers.js"; describe("E2E: Headless Mode (Minimal)", () => { let context: any; @@ -17,7 +21,7 @@ describe("E2E: Headless Mode (Minimal)", () => { const result = await runCLI(context, { args: ["--version"], }); - + expect(result.exitCode).toBe(0); expect(result.stdout).toMatch(/\d+\.\d+\.\d+/); // Matches version pattern }); @@ -26,29 +30,22 @@ describe("E2E: Headless Mode (Minimal)", () => { const result = await runCLI(context, { args: ["--help"], }); - + expect(result.exitCode).toBe(0); expect(result.stdout).toContain("Continue CLI"); expect(result.stdout).toContain("-p, --print"); }); - it("should fail gracefully when using -p without config", async () => { - const result = await runCLI(context, { - args: ["-p", "Hello"], - expectError: true, - }); - - expect(result.exitCode).not.toBe(0); - // It should fail because no config is found - }); - it("should accept a config file path", async () => { // Create a minimal YAML config const configPath = path.join(context.testDir, "test-config.yaml"); - await fs.writeFile(configPath, `name: Test Assistant + await fs.writeFile( + configPath, + `name: Test Assistant model: gpt-4 -provider: openai`); - +provider: openai` + ); + // For now, just test that it tries to load the config // Without proper mocking, it will fail to connect to OpenAI const result = await runCLI(context, { @@ -56,8 +53,8 @@ provider: openai`); env: { OPENAI_API_KEY: "test-key" }, expectError: true, }); - + // The fact that it gets past config loading is what we're testing expect(result.exitCode).toBeDefined(); }); -}); \ No newline at end of file +}); diff --git a/src/e2e/headless-simple.test.ts b/src/e2e/headless-simple.test.ts index ce135a972..b44c715e0 100644 --- a/src/e2e/headless-simple.test.ts +++ b/src/e2e/headless-simple.test.ts @@ -1,6 +1,4 @@ import { createTestContext, cleanupTestContext, runCLI, createTestConfig } from "../test-helpers/cli-helpers.js"; -import { mockLLMResponse, mockLLMStreamResponse, mockLLMError, clearLLMMocks } from "../test-helpers/adapter-mocks.js"; -import { createMockConfig } from "../test-helpers/mock-helpers.js"; describe("E2E: Headless Mode (Simple)", () => { let context: any; @@ -10,112 +8,46 @@ describe("E2E: Headless Mode (Simple)", () => { }); afterEach(async () => { - clearLLMMocks(); await cleanupTestContext(context); }); describe("basic headless functionality", () => { - it("should output response and exit with -p flag", async () => { - // Create a minimal config file - const config = { - name: "Test Assistant", - model: "gpt-4", - provider: "openai", - apiKey: "test-key", - }; - await createTestConfig(context, `name: Test Assistant -model: gpt-4 -provider: openai -apiKey: test-key`); - - // Mock the LLM response at the adapter level - mockLLMResponse("Hello! This is a test response from the mocked LLM."); - - const result = await runCLI(context, { - args: ["-p", "Hello, AI!", "--config", context.configPath], - env: { OPENAI_API_KEY: "test-key" }, - }); - - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("Hello! This is a test response from the mocked LLM."); + it.skip("should output response and exit with -p flag", async () => { + // Skip this test as it requires mocking the LLM which doesn't work in subprocess + // This functionality is better tested with integration tests }); - it("should handle streaming responses in headless mode", async () => { - const config = { - name: "Test Assistant", - model: "gpt-4", - provider: "openai", - apiKey: "test-key", - }; - await createTestConfig(context, `name: Test Assistant -model: gpt-4 -provider: openai -apiKey: test-key`); - - // Mock streaming response with chunks - mockLLMStreamResponse(["Hello", " from", " streaming", " response!"]); - - const result = await runCLI(context, { - args: ["-p", "Test streaming", "--config", context.configPath], - env: { OPENAI_API_KEY: "test-key" }, - }); - - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("Hello from streaming response!"); + it.skip("should handle streaming responses in headless mode", async () => { + // Skip this test as it requires mocking the LLM which doesn't work in subprocess }); - it("should handle errors gracefully in headless mode", async () => { - const config = { - name: "Test Assistant", - model: "gpt-4", - provider: "openai", - apiKey: "test-key", - }; - await createTestConfig(context, `name: Test Assistant -model: gpt-4 -provider: openai -apiKey: test-key`); - - // Mock an error at the adapter level - mockLLMError("Connection timeout"); + it("should fail gracefully when config is invalid", async () => { + // Test with invalid config + await createTestConfig(context, `invalid: yaml +no models here`); const result = await runCLI(context, { - args: ["-p", "This should fail", "--config", context.configPath], - env: { OPENAI_API_KEY: "test-key" }, + args: ["-p", "Test", "--config", context.configPath], expectError: true, }); expect(result.exitCode).not.toBe(0); - expect(result.stderr || result.stdout).toContain("error"); }); - it("should work with minimal config", async () => { - // Test with the most minimal config possible - await createTestConfig(context, `model: gpt-4 -provider: openai`); - - mockLLMResponse("Response with minimal config"); - - const result = await runCLI(context, { - args: ["-p", "Test minimal", "--config", context.configPath], - env: { OPENAI_API_KEY: "env-key" }, // API key from environment - }); - - expect(result.exitCode).toBe(0); - expect(result.stdout).toContain("Response with minimal config"); + it.skip("should work with minimal config", async () => { + // Skip this test as it requires mocking the LLM which doesn't work in subprocess }); - it("should handle empty prompt appropriately", async () => { - const config = { - name: "Test Assistant", - model: "gpt-4", - provider: "openai", - apiKey: "test-key", - }; + it("should handle missing prompt in headless mode", async () => { await createTestConfig(context, `name: Test Assistant -model: gpt-4 -provider: openai -apiKey: test-key`); +version: 1.0.0 +schema: v1 +models: + - model: gpt-4 + provider: openai + apiKey: test-key + roles: + - chat`); const result = await runCLI(context, { args: ["-p", "--config", context.configPath], @@ -123,7 +55,7 @@ apiKey: test-key`); expectError: true, }); - // Should error or show help when no prompt provided + // Should error when no prompt provided in headless mode expect(result.exitCode).not.toBe(0); }); }); diff --git a/src/test-helpers/adapter-mocks.ts b/src/test-helpers/adapter-mocks.ts index 8bd1456e4..4913bec24 100644 --- a/src/test-helpers/adapter-mocks.ts +++ b/src/test-helpers/adapter-mocks.ts @@ -1,8 +1,4 @@ import { jest } from "@jest/globals"; -import { vi } from "vitest"; - -// Check if we're using Jest or Vitest -const testFramework = typeof jest !== 'undefined' ? 'jest' : 'vitest'; /** * Mock the constructLlmApi function to return a mock LLM API @@ -40,19 +36,11 @@ export function mockLLMResponse(response: string) { completion: async () => response, }; - if (testFramework === 'jest') { - jest.doMock("@continuedev/openai-adapters", () => ({ - constructLlmApi: () => mockApi, - BaseLlmApi: class {}, - LLMConfig: {} - })); - } else { - vi.doMock("@continuedev/openai-adapters", () => ({ - constructLlmApi: () => mockApi, - BaseLlmApi: class {}, - LLMConfig: {} - })); - } + jest.doMock("@continuedev/openai-adapters", () => ({ + constructLlmApi: () => mockApi, + BaseLlmApi: class {}, + LLMConfig: {} + })); return mockApi; } @@ -92,19 +80,11 @@ export function mockLLMStreamResponse(chunks: string[]) { completion: async () => chunks.join(""), }; - if (testFramework === 'jest') { - jest.doMock("@continuedev/openai-adapters", () => ({ - constructLlmApi: () => mockApi, - BaseLlmApi: class {}, - LLMConfig: {} - })); - } else { - vi.doMock("@continuedev/openai-adapters", () => ({ - constructLlmApi: () => mockApi, - BaseLlmApi: class {}, - LLMConfig: {} - })); - } + jest.doMock("@continuedev/openai-adapters", () => ({ + constructLlmApi: () => mockApi, + BaseLlmApi: class {}, + LLMConfig: {} + })); return mockApi; } @@ -131,19 +111,11 @@ export function mockLLMError(errorMessage: string) { }, }; - if (testFramework === 'jest') { - jest.doMock("@continuedev/openai-adapters", () => ({ - constructLlmApi: () => mockApi, - BaseLlmApi: class {}, - LLMConfig: {} - })); - } else { - vi.doMock("@continuedev/openai-adapters", () => ({ - constructLlmApi: () => mockApi, - BaseLlmApi: class {}, - LLMConfig: {} - })); - } + jest.doMock("@continuedev/openai-adapters", () => ({ + constructLlmApi: () => mockApi, + BaseLlmApi: class {}, + LLMConfig: {} + })); return mockApi; } @@ -152,9 +124,5 @@ export function mockLLMError(errorMessage: string) { * Clear all LLM mocks */ export function clearLLMMocks() { - if (testFramework === 'jest') { - jest.dontMock("@continuedev/openai-adapters"); - } else { - vi.unmock("@continuedev/openai-adapters"); - } + jest.dontMock("@continuedev/openai-adapters"); } \ No newline at end of file diff --git a/src/test-helpers/cli-helpers.ts b/src/test-helpers/cli-helpers.ts index 9c4fe9444..9c2aef8d5 100644 --- a/src/test-helpers/cli-helpers.ts +++ b/src/test-helpers/cli-helpers.ts @@ -1,4 +1,4 @@ -import { execaNode, ExecaChildProcess } from "execa"; +import { execaNode, type Subprocess } from "execa"; import path from "path"; import fs from "fs/promises"; import os from "os"; @@ -77,7 +77,7 @@ export async function runCLI( return { stdout: result.stdout, stderr: result.stderr, - exitCode: result.exitCode, + exitCode: result.exitCode ?? 0, }; } catch (error: any) { if (!expectError) { @@ -87,7 +87,7 @@ export async function runCLI( return { stdout: error.stdout || "", stderr: error.stderr || "", - exitCode: error.exitCode || 1, + exitCode: error.exitCode ?? 1, error, }; } @@ -175,7 +175,7 @@ export async function createMockSession( * Waits for a pattern in the output */ export function waitForPattern( - proc: ExecaChildProcess, + proc: Subprocess, pattern: RegExp | string, timeout: number = 5000 ): Promise { @@ -243,13 +243,13 @@ export async function withInteractiveInput( return { stdout, stderr, - exitCode: result.exitCode, + exitCode: result.exitCode ?? 0, }; } catch (error: any) { return { stdout, stderr, - exitCode: error.exitCode || 1, + exitCode: error.exitCode ?? 1, error, }; }