diff --git a/src/commands/serve.test.ts b/src/commands/serve.test.ts index ad1a2a1f4..fb752d306 100644 --- a/src/commands/serve.test.ts +++ b/src/commands/serve.test.ts @@ -1,10 +1,17 @@ -import { describe, it, expect, beforeEach, afterEach, jest } from "@jest/globals"; -import request from "supertest"; +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from "@jest/globals"; import type { Server } from "http"; +import request from "supertest"; // Mock fs to prevent file system checks jest.mock("fs", () => ({ - ...jest.requireActual("fs"), + ...(jest.requireActual("fs") as any), existsSync: jest.fn(() => false), })); @@ -17,16 +24,20 @@ jest.mock("../auth/workos.js", () => ({ })); jest.mock("../onboarding.js", () => ({ - runNormalFlow: jest.fn(() => Promise.resolve({ - config: { models: [] }, - llmApi: { chat: jest.fn() }, - model: { name: "test-model" }, - })), - runOnboardingFlow: jest.fn(() => Promise.resolve({ - config: { models: [] }, - llmApi: { chat: jest.fn() }, - model: { name: "test-model" }, - })), + runNormalFlow: jest.fn(() => + Promise.resolve({ + config: { models: [] }, + llmApi: { chat: jest.fn() }, + model: { name: "test-model" }, + }) + ), + runOnboardingFlow: jest.fn(() => + Promise.resolve({ + config: { models: [] }, + llmApi: { chat: jest.fn() }, + model: { name: "test-model" }, + }) + ), })); jest.mock("../session.js", () => ({ @@ -43,14 +54,14 @@ jest.mock("../telemetry/telemetryService.js", () => ({ startActiveTime: jest.fn(), stopActiveTime: jest.fn(), updateOrganization: jest.fn(), - } + }, })); jest.mock("../util/logger.js", () => ({ default: { error: jest.fn(), debug: jest.fn(), - } + }, })); // Mock chalk to prevent console output during tests @@ -60,7 +71,7 @@ jest.mock("chalk", () => ({ dim: (str: string) => str, yellow: (str: string) => str, red: (str: string) => str, - } + }, })); describe("serve command", () => { @@ -72,18 +83,18 @@ describe("serve command", () => { // Mock process.exit to prevent actual exit during tests originalProcessExit = process.exit; process.exit = jest.fn() as any; - + // Mock console.log to prevent output during tests - jest.spyOn(console, 'log').mockImplementation(() => {}); + jest.spyOn(console, "log").mockImplementation(() => {}); }); afterEach(async () => { // Restore original process.exit process.exit = originalProcessExit; - + // Restore console.log jest.restoreAllMocks(); - + // Clean up server if it's still running if (serverRef) { await new Promise((resolve) => { @@ -91,29 +102,29 @@ describe("serve command", () => { }); serverRef = null; } - + jest.clearAllMocks(); }); it.skip("should have /exit endpoint that returns success response", async () => { // Ensure mocks are properly set up jest.resetModules(); - + // Mock environment to avoid config file lookup const originalEnv = process.env.ANTHROPIC_API_KEY; delete process.env.ANTHROPIC_API_KEY; - + // Import serve after mocking dependencies const { serve } = await import("./serve.js"); // Start server with a random port to avoid conflicts const port = Math.floor(Math.random() * 10000) + 30000; - + // Create a promise to track when server is ready const serverReady = new Promise((resolve) => { - const originalListen = jest.spyOn(console, 'log'); + const originalListen = jest.spyOn(console, "log"); originalListen.mockImplementation((message: any) => { - if (typeof message === 'string' && message.includes('Server started')) { + if (typeof message === "string" && message.includes("Server started")) { resolve(); } }); @@ -121,7 +132,7 @@ describe("serve command", () => { // Start the server in the background serverPromise = serve(undefined, { port: port.toString() }); - + // Wait for server to be ready await serverReady; @@ -137,7 +148,7 @@ describe("serve command", () => { }); // Wait for process.exit to be called - await new Promise(resolve => setTimeout(resolve, 200)); + await new Promise((resolve) => setTimeout(resolve, 200)); // Verify process.exit was called expect(process.exit).toHaveBeenCalledWith(0); @@ -151,4 +162,4 @@ describe("serve command", () => { } } }, 20000); // Increased timeout to 20 seconds -}); \ No newline at end of file +});