| 1 | import { SerializedContinueConfig } from "core"; |
| 2 | // import Mock from "core/llm/llms/Mock.js"; |
| 3 | import { FromIdeProtocol, ToIdeProtocol } from "core/protocol/index.js"; |
| 4 | import { IMessenger } from "core/protocol/messenger"; |
| 5 | import FileSystemIde from "core/util/filesystem"; |
| 6 | import fs from "fs"; |
| 7 | import { |
| 8 | ChildProcessWithoutNullStreams, |
| 9 | execSync, |
| 10 | spawn, |
| 11 | } from "node:child_process"; |
| 12 | import path from "path"; |
| 13 | import { |
| 14 | CoreBinaryMessenger, |
| 15 | CoreBinaryTcpMessenger, |
| 16 | } from "../src/IpcMessenger"; |
| 17 | |
| 18 | // jest.setTimeout(100_000); |
| 19 | |
| 20 | const USE_TCP = false; |
| 21 | |
| 22 | function autodetectPlatformAndArch() { |
| 23 | const platform = { |
| 24 | aix: "linux", |
| 25 | darwin: "darwin", |
| 26 | freebsd: "linux", |
| 27 | linux: "linux", |
| 28 | openbsd: "linux", |
| 29 | sunos: "linux", |
| 30 | win32: "win32", |
| 31 | android: "linux", |
| 32 | cygwin: "win32", |
| 33 | netbsd: "linux", |
| 34 | haiku: "linux", |
| 35 | }[process.platform]; |
| 36 | const arch = { |
| 37 | arm: "arm64", |
| 38 | arm64: "arm64", |
| 39 | ia32: "x64", |
| 40 | loong64: "arm64", |
| 41 | mips: "arm64", |
| 42 | mipsel: "arm64", |
| 43 | ppc: "x64", |
| 44 | ppc64: "x64", |
| 45 | riscv64: "arm64", |
| 46 | s390: "x64", |
| 47 | s390x: "x64", |
| 48 | x64: "x64", |
| 49 | }[process.arch]; |
| 50 | return [platform, arch]; |
| 51 | } |
| 52 | |
| 53 | const CONTINUE_GLOBAL_DIR = path.join(__dirname, "..", ".continue"); |
| 54 | if (fs.existsSync(CONTINUE_GLOBAL_DIR)) { |
| 55 | fs.rmSync(CONTINUE_GLOBAL_DIR, { recursive: true, force: true }); |
| 56 | } |
| 57 | fs.mkdirSync(CONTINUE_GLOBAL_DIR); |
| 58 | |
| 59 | describe("Test Suite", () => { |
| 60 | let messenger: IMessenger<ToIdeProtocol, FromIdeProtocol>; |
| 61 | let subprocess: ChildProcessWithoutNullStreams; |
| 62 | |
| 63 | beforeAll(async () => { |
| 64 | const [platform, arch] = autodetectPlatformAndArch(); |
| 65 | const binaryDir = path.join(__dirname, "..", "bin", `${platform}-${arch}`); |
| 66 | const exe = platform === "win32" ? ".exe" : ""; |
| 67 | const binaryPath = path.join(binaryDir, `continue-binary${exe}`); |
| 68 | const expectedItems = [ |
| 69 | `continue-binary${exe}`, |
| 70 | `esbuild${exe}`, |
| 71 | "index.node", |
| 72 | "package.json", |
| 73 | "build/Release/node_sqlite3.node", |
| 74 | ]; |
| 75 | expectedItems.forEach((item) => { |
| 76 | expect(fs.existsSync(path.join(binaryDir, item))).toBe(true); |
| 77 | }); |
| 78 | |
| 79 | // Set execute permissions and remove quarantine attribute if on macOS |
| 80 | if (platform !== "win32") { |
| 81 | try { |
| 82 | fs.chmodSync(binaryPath, 0o755); |
| 83 | console.log("Execute permissions set for the binary"); |
| 84 | |
| 85 | if (platform === "darwin") { |
| 86 | const indexNodePath = path.join(binaryDir, "index.node"); |
| 87 | const filesToUnquarantine = [binaryPath, indexNodePath]; |
| 88 | |
| 89 | for (const file of filesToUnquarantine) { |
| 90 | try { |
| 91 | execSync(`xattr -d com.apple.quarantine "${file}"`, { |
| 92 | stdio: "ignore", |
| 93 | }); |
| 94 | console.log( |
| 95 | `Quarantine attribute removed from ${path.basename(file)}`, |
| 96 | ); |
| 97 | } catch (error) { |
| 98 | console.warn( |
| 99 | `Failed to remove quarantine attribute from ${path.basename(file)}:`, |
| 100 | error, |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } catch (error) { |
| 106 | console.error( |
| 107 | "Error setting permissions or removing quarantine:", |
| 108 | error, |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (USE_TCP) { |
| 114 | messenger = new CoreBinaryTcpMessenger<ToIdeProtocol, FromIdeProtocol>(); |
| 115 | } else { |
| 116 | try { |
| 117 | subprocess = spawn(binaryPath, { |
| 118 | env: { ...process.env, CONTINUE_GLOBAL_DIR }, |
| 119 | }); |
| 120 | console.log("Successfully spawned subprocess"); |
| 121 | } catch (error) { |
| 122 | console.error("Error spawning subprocess:", error); |
| 123 | throw error; |
| 124 | } |
| 125 | messenger = new CoreBinaryMessenger<ToIdeProtocol, FromIdeProtocol>( |
| 126 | subprocess, |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | const testDir = path.join(__dirname, "..", ".test"); |
| 131 | if (!fs.existsSync(testDir)) { |
| 132 | fs.mkdirSync(testDir); |
| 133 | } |
| 134 | const ide = new FileSystemIde(testDir); |
| 135 | // const reverseIde = new ReverseMessageIde(messenger.on.bind(messenger), ide); |
| 136 | |
| 137 | // Wait for core to set itself up |
| 138 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 139 | }); |
| 140 | |
| 141 | afterAll(async () => { |
| 142 | // Wait for the subprocess to exit |
| 143 | if (USE_TCP) { |
| 144 | ( |
| 145 | messenger as CoreBinaryTcpMessenger<ToIdeProtocol, FromIdeProtocol> |
| 146 | ).close(); |
| 147 | } else { |
| 148 | subprocess.kill(); |
| 149 | await new Promise((resolve) => subprocess.on("close", resolve)); |
| 150 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | it("should respond to ping with pong", async () => { |
| 155 | const resp = await messenger.request("ping", "ping"); |
| 156 | expect(resp).toBe("pong"); |
| 157 | }); |
| 158 | |
| 159 | it("should create .continue directory at the specified location with expected files", async () => { |
| 160 | expect(fs.existsSync(CONTINUE_GLOBAL_DIR)).toBe(true); |
| 161 | |
| 162 | // Many of the files are only created when trying to load the config |
| 163 | const config = await messenger.request( |
| 164 | "config/getSerializedProfileInfo", |
| 165 | undefined, |
| 166 | ); |
| 167 | |
| 168 | const expectedFiles = [ |
| 169 | "config.json", |
| 170 | "config.ts", |
| 171 | "package.json", |
| 172 | "logs/core.log", |
| 173 | "index/autocompleteCache.sqlite", |
| 174 | "types/core/index.d.ts", |
| 175 | ]; |
| 176 | |
| 177 | const missingFiles = expectedFiles.filter((file) => { |
| 178 | const filePath = path.join(CONTINUE_GLOBAL_DIR, file); |
| 179 | return !fs.existsSync(filePath); |
| 180 | }); |
| 181 | |
| 182 | expect(missingFiles).toEqual([]); |
| 183 | if (missingFiles.length > 0) { |
| 184 | console.log("Missing files:", missingFiles); |
| 185 | } |
| 186 | }); |
| 187 | |
| 188 | it("should return valid config object", async () => { |
| 189 | const { result } = await messenger.request( |
| 190 | "config/getSerializedProfileInfo", |
| 191 | undefined, |
| 192 | ); |
| 193 | const { config } = result; |
| 194 | expect(config).toHaveProperty("models"); |
| 195 | expect(config).toHaveProperty("embeddingsProvider"); |
| 196 | expect(config).toHaveProperty("contextProviders"); |
| 197 | expect(config).toHaveProperty("slashCommands"); |
| 198 | }); |
| 199 | |
| 200 | it("should properly handle history requests", async () => { |
| 201 | const sessionId = "test-session-id"; |
| 202 | await messenger.request("history/save", { |
| 203 | history: [], |
| 204 | sessionId, |
| 205 | title: "test-title", |
| 206 | |
| 207 | workspaceDirectory: "test-workspace-directory", |
| 208 | }); |
| 209 | const sessions = await messenger.request("history/list", {}); |
| 210 | expect(sessions.length).toBeGreaterThan(0); |
| 211 | |
| 212 | const session = await messenger.request("history/load", { |
| 213 | id: sessionId, |
| 214 | }); |
| 215 | expect(session).toHaveProperty("history"); |
| 216 | |
| 217 | await messenger.request("history/delete", { |
| 218 | id: sessionId, |
| 219 | }); |
| 220 | const sessionsAfterDelete = await messenger.request("history/list", {}); |
| 221 | expect(sessionsAfterDelete.length).toBe(sessions.length - 1); |
| 222 | }); |
| 223 | |
| 224 | it("should add and delete a model from config.json", async () => { |
| 225 | const model: SerializedContinueConfig["models"][number] = { |
| 226 | title: "Test Model", |
| 227 | provider: "openai", |
| 228 | model: "gpt-3.5-turbo", |
| 229 | underlyingProviderName: "openai", |
| 230 | }; |
| 231 | await messenger.request("config/addModel", { |
| 232 | model, |
| 233 | }); |
| 234 | const { |
| 235 | result: { config }, |
| 236 | } = await messenger.request("config/getSerializedProfileInfo", undefined); |
| 237 | |
| 238 | expect(config!.modelsByRole.chat.some((m) => m.title === model.title)).toBe( |
| 239 | true, |
| 240 | ); |
| 241 | |
| 242 | await messenger.request("config/deleteModel", { title: model.title }); |
| 243 | const { |
| 244 | result: { config: configAfterDelete }, |
| 245 | } = await messenger.request("config/getSerializedProfileInfo", undefined); |
| 246 | expect( |
| 247 | configAfterDelete!.modelsByRole.chat.some((m) => m.title === model.title), |
| 248 | ).toBe(false); |
| 249 | }); |
| 250 | |
| 251 | it("should make an LLM completion", async () => { |
| 252 | const model: SerializedContinueConfig["models"][number] = { |
| 253 | title: "Test Model", |
| 254 | provider: "mock", |
| 255 | model: "gpt-3.5-turbo", |
| 256 | underlyingProviderName: "mock", |
| 257 | }; |
| 258 | await messenger.request("config/addModel", { |
| 259 | model, |
| 260 | }); |
| 261 | |
| 262 | const resp = await messenger.request("llm/complete", { |
| 263 | prompt: "Say 'Hello' and nothing else", |
| 264 | completionOptions: {}, |
| 265 | title: "Test Model", |
| 266 | }); |
| 267 | expect(resp).toBe("Test Completion"); |
| 268 | }); |
| 269 | }); |