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