| 1 | import { Server } from "http"; |
| 2 | |
| 3 | import express from "express"; |
| 4 | import type { ChatCompletionMessageParam } from "openai/resources.mjs"; |
| 5 | import { describe, expect, test } from "vitest"; |
| 6 | |
| 7 | export interface MockRemoteServerConfig { |
| 8 | port?: number; |
| 9 | responses?: string[]; |
| 10 | } |
| 11 | |
| 12 | export class MockRemoteServer { |
| 13 | private server: Server | null = null; |
| 14 | private app: express.Application; |
| 15 | private messages: ChatCompletionMessageParam[] = []; |
| 16 | private responses: string[] = []; |
| 17 | private currentResponseIndex = 0; |
| 18 | private isProcessing = false; |
| 19 | private streamInterval: NodeJS.Timeout | null = null; |
| 20 | |
| 21 | constructor(config: MockRemoteServerConfig = {}) { |
| 22 | this.app = express(); |
| 23 | this.app.use(express.json()); |
| 24 | this.responses = config.responses || ["Mock response"]; |
| 25 | this.setupRoutes(); |
| 26 | } |
| 27 | |
| 28 | private setupRoutes() { |
| 29 | // GET /state - Return the current state |
| 30 | this.app.get("/state", (req, res) => { |
| 31 | res.json({ |
| 32 | session: { |
| 33 | history: this.messages.map((msg) => ({ |
| 34 | message: msg, |
| 35 | contextItems: [], |
| 36 | })), |
| 37 | id: "mock-session", |
| 38 | workspaceDirectory: "/mock/workspace", |
| 39 | }, |
| 40 | isProcessing: this.isProcessing, |
| 41 | messageQueueLength: 0, |
| 42 | pendingPermission: null, |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | // POST /message - Handle new messages |
| 47 | this.app.post("/message", (req, res) => { |
| 48 | const { message } = req.body; |
| 49 | |
| 50 | if (!message && message !== "") { |
| 51 | return res.status(400).json({ error: "Message field is required" }); |
| 52 | } |
| 53 | |
| 54 | // Add user message |
| 55 | if (message) { |
| 56 | this.messages.push({ |
| 57 | role: "user", |
| 58 | content: message, |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | // Start processing response |
| 63 | this.processResponse(); |
| 64 | |
| 65 | res.json({ |
| 66 | queued: true, |
| 67 | position: 1, |
| 68 | willInterrupt: false, |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | // POST /exit - Shutdown server |
| 73 | this.app.post("/exit", (req, res) => { |
| 74 | res.json({ |
| 75 | message: "Server shutting down", |
| 76 | success: true, |
| 77 | }); |
| 78 | |
| 79 | this.stop(); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | private processResponse() { |
| 84 | if (this.isProcessing) return; |
| 85 | |
| 86 | this.isProcessing = true; |
| 87 | |
| 88 | // Get the response to simulate |
| 89 | const response = |
| 90 | this.responses[this.currentResponseIndex % this.responses.length]; |
| 91 | this.currentResponseIndex++; |
| 92 | |
| 93 | // Simulate streaming response |
| 94 | let currentContent = ""; |
| 95 | let charIndex = 0; |
| 96 | |
| 97 | // Add initial streaming message |
| 98 | const streamingMessage: ChatCompletionMessageParam = { |
| 99 | role: "assistant", |
| 100 | content: "", |
| 101 | }; |
| 102 | this.messages.push(streamingMessage); |
| 103 | |
| 104 | // Simulate character-by-character streaming (but faster for tests) |
| 105 | this.streamInterval = setInterval(() => { |
| 106 | if (charIndex < response.length) { |
| 107 | currentContent += response[charIndex]; |
| 108 | streamingMessage.content = currentContent; |
| 109 | charIndex++; |
| 110 | } else { |
| 111 | // Finish streaming |
| 112 | this.isProcessing = false; |
| 113 | |
| 114 | if (this.streamInterval) { |
| 115 | clearInterval(this.streamInterval); |
| 116 | this.streamInterval = null; |
| 117 | } |
| 118 | } |
| 119 | }, 10); // Fast streaming for tests |
| 120 | } |
| 121 | |
| 122 | public async start(port: number = 0): Promise<number> { |
| 123 | return new Promise((resolve, reject) => { |
| 124 | this.server = this.app.listen(port, (err?: Error) => { |
| 125 | if (err) { |
| 126 | reject(err); |
| 127 | } else { |
| 128 | const actualPort = (this.server!.address() as any)?.port || port; |
| 129 | resolve(actualPort); |
| 130 | } |
| 131 | }); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | public async stop(): Promise<void> { |
| 136 | return new Promise((resolve) => { |
| 137 | if (this.streamInterval) { |
| 138 | clearInterval(this.streamInterval); |
| 139 | this.streamInterval = null; |
| 140 | } |
| 141 | |
| 142 | if (this.server) { |
| 143 | this.server.close(() => { |
| 144 | this.server = null; |
| 145 | resolve(); |
| 146 | }); |
| 147 | } else { |
| 148 | resolve(); |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | public getMessages(): ChatCompletionMessageParam[] { |
| 154 | return [...this.messages]; |
| 155 | } |
| 156 | |
| 157 | public clearMessages(): void { |
| 158 | this.messages = []; |
| 159 | } |
| 160 | |
| 161 | public setResponses(responses: string[]): void { |
| 162 | this.responses = responses; |
| 163 | this.currentResponseIndex = 0; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Add a dummy test to satisfy Vitest requirement |
| 168 | describe("MockRemoteServer", () => { |
| 169 | test("should be defined", () => { |
| 170 | expect(MockRemoteServer).toBeDefined(); |
| 171 | }); |
| 172 | }); |