diff --git a/core/autocomplete/context/root-path-context/test/testUtils.ts b/core/autocomplete/context/root-path-context/test/testUtils.ts index 7a5c22aba..207b1d6d4 100644 --- a/core/autocomplete/context/root-path-context/test/testUtils.ts +++ b/core/autocomplete/context/root-path-context/test/testUtils.ts @@ -4,7 +4,7 @@ import path from "path"; import Parser from "web-tree-sitter"; import { Position } from "../../../.."; -import { testIde } from "../../../../test/util/fixtures"; +import { testIde } from "../../../../test/fixtures"; import { getAst, getTreePathAtCursor } from "../../../util/ast"; import { ImportDefinitionsService } from "../../ImportDefinitionsService"; import { RootPathContextService } from "../RootPathContextService"; diff --git a/core/autocomplete/filtering/test/filter.test.ts b/core/autocomplete/filtering/test/filter.test.ts index e6c07b951..3f81962ca 100644 --- a/core/autocomplete/filtering/test/filter.test.ts +++ b/core/autocomplete/filtering/test/filter.test.ts @@ -1,4 +1,7 @@ -import { setUpTestDir, tearDownTestDir } from "../../../test/util/testDir"; +import { + setUpTestDir, + tearDownTestDir, +} from "../../../indexing/test/testWorkspaceDir"; import { TEST_CASES_WITH_DIFF, TEST_CASES_WITHOUT_DIFF } from "./testCases"; import { diff --git a/core/autocomplete/filtering/test/util.ts b/core/autocomplete/filtering/test/util.ts index 8adaa101a..a052aace2 100644 --- a/core/autocomplete/filtering/test/util.ts +++ b/core/autocomplete/filtering/test/util.ts @@ -2,7 +2,7 @@ import fs from "node:fs"; import path from "node:path"; import Mock from "../../../llm/llms/Mock"; -import { testConfigHandler, testIde } from "../../../test/util/fixtures"; +import { testConfigHandler, testIde } from "../../../test/fixtures"; import { CompletionProvider } from "../../CompletionProvider"; import { AutocompleteInput } from "../../util/types"; diff --git a/core/config/ConfigHandler.test.ts b/core/config/ConfigHandler.test.ts index 26de42ec7..90e07ba5c 100644 --- a/core/config/ConfigHandler.test.ts +++ b/core/config/ConfigHandler.test.ts @@ -1,8 +1,8 @@ import fs from "node:fs"; import path from "node:path"; -import { testConfigHandler } from "../test/util/fixtures"; -import { TEST_DIR } from "../test/util/testDir"; +import { testConfigHandler } from "../test/fixtures"; +import { TEST_DIR } from "../indexing/test/testWorkspaceDir"; import { getConfigTsPath } from "../util/paths"; import { defaultConfig } from "./default"; diff --git a/core/test/context/providers/index.skip.ts b/core/context/providers/_context-providers.test.ts similarity index 83% rename from core/test/context/providers/index.skip.ts rename to core/context/providers/_context-providers.test.ts index 1d4e9f1b1..8379083a9 100644 --- a/core/test/context/providers/index.skip.ts +++ b/core/context/providers/_context-providers.test.ts @@ -4,12 +4,12 @@ import { ContextProviderExtras, ContextProviderWithParams, IContextProvider, -} from "../../.."; -import { ConfigHandler } from "../../../config/ConfigHandler"; -import { contextProviderClassFromName } from "../../../context/providers"; -import { ControlPlaneClient } from "../../../control-plane/client"; -import FileSystemIde from "../../../util/filesystem"; -import { TEST_DIR } from "../../util/testDir"; +} from "../.."; +import { ConfigHandler } from "../../config/ConfigHandler"; +import { contextProviderClassFromName } from "."; +import { ControlPlaneClient } from "../../control-plane/client"; +import FileSystemIde from "../../util/filesystem"; +import { TEST_DIR } from "../../indexing/test/testWorkspaceDir"; const CONTEXT_PROVIDERS_TO_TEST: ContextProviderWithParams[] = [ { name: "diff", params: {} }, diff --git a/core/indexing/CodeSnippetsIndex.test.ts b/core/indexing/CodeSnippetsIndex.test.ts index 97d086d23..814927725 100644 --- a/core/indexing/CodeSnippetsIndex.test.ts +++ b/core/indexing/CodeSnippetsIndex.test.ts @@ -1,11 +1,11 @@ import { jest } from "@jest/globals"; -import { testIde } from "../test/util/fixtures"; +import { testIde } from "../test/fixtures"; import { insertMockChunks, mockPathAndCacheKey, updateIndexAndAwaitGenerator, -} from "../test/util/indexing"; +} from "./test/indexing"; import { CodeSnippetsCodebaseIndex } from "./CodeSnippetsIndex"; import { DatabaseConnection, SqliteDb } from "./refreshIndex"; diff --git a/core/indexing/CodebaseIndexer.test.ts b/core/indexing/CodebaseIndexer.test.ts index ee2f108f7..edd654dc7 100644 --- a/core/indexing/CodebaseIndexer.test.ts +++ b/core/indexing/CodebaseIndexer.test.ts @@ -5,13 +5,13 @@ import path from "node:path"; import { jest } from "@jest/globals"; import { ContinueServerClient } from "../continueServer/stubs/client.js"; -import { testConfigHandler, testIde } from "../test/util/fixtures.js"; +import { testConfigHandler, testIde } from "../test/fixtures.js"; import { addToTestDir, setUpTestDir, tearDownTestDir, TEST_DIR, -} from "../test/util/testDir.js"; +} from "./test/testWorkspaceDir.js"; import { getIndexSqlitePath } from "../util/paths.js"; import { CodebaseIndexer, PauseToken } from "./CodebaseIndexer.js"; @@ -76,8 +76,8 @@ describe("CodebaseIndexer", () => { setUpTestDir(); execSync("git init", { cwd: TEST_DIR }); - execSync("git config user.email \"test@example.com\"", { cwd: TEST_DIR }); - execSync("git config user.name \"Test\"", { cwd: TEST_DIR }); + execSync('git config user.email "test@example.com"', { cwd: TEST_DIR }); + execSync('git config user.name "Test"', { cwd: TEST_DIR }); }); afterAll(async () => { @@ -100,9 +100,7 @@ describe("CodebaseIndexer", () => { async function refreshIndexFiles(files: string[]) { const updates = []; - for await (const update of codebaseIndexer.refreshFiles( - files, - )) { + for await (const update of codebaseIndexer.refreshFiles(files)) { updates.push(update); } return updates; diff --git a/core/indexing/FullTextSearchCodebaseIndex.test.ts b/core/indexing/FullTextSearchCodebaseIndex.test.ts index d46e47fd4..3aefafaaa 100644 --- a/core/indexing/FullTextSearchCodebaseIndex.test.ts +++ b/core/indexing/FullTextSearchCodebaseIndex.test.ts @@ -4,7 +4,7 @@ import { insertMockChunks, mockPathAndCacheKey, updateIndexAndAwaitGenerator, -} from "../test/util/indexing"; +} from "./test/indexing"; import { FullTextSearchCodebaseIndex } from "./FullTextSearchCodebaseIndex"; import { DatabaseConnection, SqliteDb } from "./refreshIndex"; diff --git a/core/indexing/LanceDbIndex.test.ts b/core/indexing/LanceDbIndex.test.ts index f0c711e93..1d15117fd 100644 --- a/core/indexing/LanceDbIndex.test.ts +++ b/core/indexing/LanceDbIndex.test.ts @@ -1,13 +1,13 @@ import { jest } from "@jest/globals"; import lance from "vectordb"; -import { testConfigHandler, testIde } from "../test/util/fixtures"; +import { testConfigHandler, testIde } from "../test/fixtures"; import { mockPathAndCacheKey, mockTag, testContinueServerClient, updateIndexAndAwaitGenerator, -} from "../test/util/indexing"; +} from "./test/indexing"; import { getLanceDbPath } from "../util/paths"; import { LanceDbIndex } from "./LanceDbIndex"; diff --git a/core/indexing/chunk/ChunkCodebaseIndex.test.ts b/core/indexing/chunk/ChunkCodebaseIndex.test.ts index c2f95d560..b5f373648 100644 --- a/core/indexing/chunk/ChunkCodebaseIndex.test.ts +++ b/core/indexing/chunk/ChunkCodebaseIndex.test.ts @@ -1,14 +1,14 @@ import { jest } from "@jest/globals"; -import { testIde } from "../../test/util/fixtures"; +import { testIde } from "../../test/fixtures"; import { mockFileContents, mockFilename, mockPathAndCacheKey, testContinueServerClient, updateIndexAndAwaitGenerator, -} from "../../test/util/indexing"; -import { addToTestDir } from "../../test/util/testDir"; +} from "../test/indexing"; +import { addToTestDir } from "../test/testWorkspaceDir"; import { DatabaseConnection, SqliteDb } from "../refreshIndex"; import { IndexResultType } from "../types"; diff --git a/core/indexing/docs/DocsCrawler.skip.ts b/core/indexing/docs/DocsCrawler.skip.ts index 57d712fed..97226c296 100644 --- a/core/indexing/docs/DocsCrawler.skip.ts +++ b/core/indexing/docs/DocsCrawler.skip.ts @@ -3,7 +3,7 @@ import os from "os"; import { jest } from "@jest/globals"; import { ContinueConfig } from "../.."; -import { testConfigHandler } from "../../test/util/fixtures"; +import { testConfigHandler } from "../../test/fixtures"; import FileSystemIde from "../../util/filesystem"; import DocsCrawler, { ChromiumInstaller, type PageData } from "./DocsCrawler"; diff --git a/core/test/util/indexing.ts b/core/indexing/test/indexing.ts similarity index 84% rename from core/test/util/indexing.ts rename to core/indexing/test/indexing.ts index 16137236a..6bc643e66 100644 --- a/core/test/util/indexing.ts +++ b/core/indexing/test/indexing.ts @@ -2,19 +2,12 @@ import { jest } from "@jest/globals"; import { IndexTag } from "../.."; import { IContinueServerClient } from "../../continueServer/interface"; -import { ChunkCodebaseIndex } from "../../indexing/chunk/ChunkCodebaseIndex"; -import { tagToString } from "../../indexing/refreshIndex"; -import { - CodebaseIndex, - PathAndCacheKey, - RefreshIndexResults, -} from "../../indexing/types"; - -import { testIde } from "./fixtures"; -import { addToTestDir, TEST_DIR } from "./testDir"; - - +import { ChunkCodebaseIndex } from "../chunk/ChunkCodebaseIndex"; +import { tagToString } from "../refreshIndex"; +import { CodebaseIndex, PathAndCacheKey, RefreshIndexResults } from "../types"; +import { testIde } from "../../test/fixtures"; +import { addToTestDir, TEST_DIR } from "./testWorkspaceDir"; export const mockFilename = "test.py"; export const mockPathAndCacheKey = { diff --git a/core/test/util/testDir.ts b/core/indexing/test/testWorkspaceDir.ts similarity index 92% rename from core/test/util/testDir.ts rename to core/indexing/test/testWorkspaceDir.ts index 62cbbb02c..0eae7ad0b 100644 --- a/core/test/util/testDir.ts +++ b/core/indexing/test/testWorkspaceDir.ts @@ -3,7 +3,7 @@ import os from "os"; import path from "path"; // Want this outside of the git repository so we can change branches in tests -export const TEST_DIR = path.join(os.tmpdir(), "testDir"); +export const TEST_DIR = path.join(os.tmpdir(), "testWorkspaceDir"); export function setUpTestDir() { if (fs.existsSync(TEST_DIR)) { diff --git a/core/indexing/walkDir.test.ts b/core/indexing/walkDir.test.ts index 748e958e9..9819de571 100644 --- a/core/indexing/walkDir.test.ts +++ b/core/indexing/walkDir.test.ts @@ -6,7 +6,7 @@ import { setUpTestDir, tearDownTestDir, addToTestDir, -} from "../test/util/testDir"; +} from "./test/testWorkspaceDir"; import FileSystemIde from "../util/filesystem"; const ide = new FileSystemIde(TEST_DIR); diff --git a/core/jest.setup-after-env.ts b/core/jest.setup-after-env.ts index d92f171dd..53f94ecea 100644 --- a/core/jest.setup-after-env.ts +++ b/core/jest.setup-after-env.ts @@ -1,5 +1,4 @@ -// https://github.com/mswjs/msw/issues/1576#issuecomment-1482643055 - +import fs from "fs"; import { TextDecoder, TextEncoder } from "util"; import { jest } from "@jest/globals"; @@ -12,23 +11,20 @@ if (process.env.DEBUG === "jest") { const globalThis = global as any; globalThis.jest = jest; + +// https://github.com/mswjs/msw/issues/1576#issuecomment-1482643055 globalThis.fetch = fetch; globalThis.Request = Request; globalThis.Response = Response; globalThis.TextEncoder = TextEncoder; globalThis.TextDecoder = TextDecoder; -// Trying to delete this when for some reason SQLite is still in use causes tests to fail -// globalThis.beforeAll(() => { -// // We can ignore TS warnings here since we set this explicitly in `./jest.global-setup.ts` +// TODO - currently causing tests to fail because sqlite is still running for some reason +// const clearTestDirectory = () => { // if (fs.existsSync(process.env.CONTINUE_GLOBAL_DIR!)) { // fs.rmSync(process.env.CONTINUE_GLOBAL_DIR!, { recursive: true }); // } -// }); +// }; -// globalThis.afterAll(() => { -// // We can ignore TS warnings here since we set this explicitly in `./jest.global-setup.ts` -// if (fs.existsSync(process.env.CONTINUE_GLOBAL_DIR!)) { -// fs.rmSync(process.env.CONTINUE_GLOBAL_DIR!, { recursive: true }); -// } -// }); +// globalThis.beforeAll(clearTestDirectory); +// globalThis.afterAll(clearTestDirectory); diff --git a/core/test/llm.test.ts b/core/llm/llm.test.ts similarity index 95% rename from core/test/llm.test.ts rename to core/llm/llm.test.ts index cf42e128a..cb2218d1d 100644 --- a/core/test/llm.test.ts +++ b/core/llm/llm.test.ts @@ -1,10 +1,8 @@ import * as dotenv from "dotenv"; -import { CompletionOptions } from "../"; -import { BaseLLM } from "../llm/"; -import OpenAI from "../llm/llms/OpenAI"; - -// jest.setTimeout(100_000); +import { CompletionOptions } from ".."; +import { BaseLLM } from "."; +import OpenAI from "./llms/OpenAI"; dotenv.config(); diff --git a/core/package.json b/core/package.json index 8aa6f9f9c..c18847ecf 100644 --- a/core/package.json +++ b/core/package.json @@ -4,6 +4,7 @@ "description": "The Continue Core contains functionality that can be shared across web, VS Code, or Node.js", "scripts": { "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest", + "test:coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage && open ./coverage/lcov-report/index.html", "tsc:check": "tsc -p ./ --noEmit", "build:npm": "tsc -p ./tsconfig.npm.json", "lint": "eslint . --ext ts", diff --git a/core/test/.gitignore b/core/test/.gitignore deleted file mode 100644 index 227f3fc4f..000000000 --- a/core/test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -scratch.test.ts \ No newline at end of file diff --git a/core/test/editme.py b/core/test/editme.py deleted file mode 100644 index c6077fb3b..000000000 --- a/core/test/editme.py +++ /dev/null @@ -1,2 +0,0 @@ -def average(nums: list) -> float: - pass diff --git a/core/test/util/fixtures.ts b/core/test/fixtures.ts similarity index 60% rename from core/test/util/fixtures.ts rename to core/test/fixtures.ts index e0aa06d14..8b75de75d 100644 --- a/core/test/util/fixtures.ts +++ b/core/test/fixtures.ts @@ -1,8 +1,8 @@ -import { ConfigHandler } from "../../config/ConfigHandler"; -import { ControlPlaneClient } from "../../control-plane/client"; -import FileSystemIde from "../../util/filesystem"; +import { ConfigHandler } from "../config/ConfigHandler"; +import { ControlPlaneClient } from "../control-plane/client"; +import FileSystemIde from "../util/filesystem"; -import { TEST_DIR } from "./testDir"; +import { TEST_DIR } from "../indexing/test/testWorkspaceDir"; export const testIde = new FileSystemIde(TEST_DIR); diff --git a/core/test/_testEnv.test.ts b/core/test/testEnv.test.ts similarity index 100% rename from core/test/_testEnv.test.ts rename to core/test/testEnv.test.ts diff --git a/core/test/util/dedentAndGetCommonWhitespace.test.ts b/core/test/util/dedentAndGetCommonWhitespace.test.ts deleted file mode 100644 index 5f3c6a4be..000000000 --- a/core/test/util/dedentAndGetCommonWhitespace.test.ts +++ /dev/null @@ -1,76 +0,0 @@ -// File generated by Continue -import { dedentAndGetCommonWhitespace } from "../../util"; - -describe("dedentAndGetCommonWhitespace", () => { - let originalString: string; - - beforeEach(() => { - // Setup any global variables or states if needed - originalString = " line1\n line2\n line3"; - }); - - afterEach(() => { - // Tear down any changes to global variables or states if needed - originalString = ""; - }); - - test("should dedent and return common whitespace for a simple case", () => { - const input = " line1\n line2\n line3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["line1\nline2\nline3", " "]); - }); - - test("should handle empty string", () => { - const input = ""; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["", ""]); - }); - - test("should handle string with only whitespace", () => { - const input = " "; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["", ""]); - }); - - test("should handle string with mixed whitespace and content", () => { - const input = " line1\n line2\n line3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual([" line1\nline2\n line3", " "]); - }); - - test("should handle string with no common leading whitespace", () => { - const input = "line1\n line2\n line3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual([input, ""]); - }); - - test("should handle string with empty lines", () => { - const input = " line1\n\n line3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["line1\n\nline3", " "]); - }); - - test("should handle string with only empty lines", () => { - const input = "\n\n"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["\n\n", ""]); - }); - - test("should handle string with tabs as whitespace", () => { - const input = "\tline1\n\tline2\n\tline3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["line1\nline2\nline3", "\t"]); - }); - - test("should handle string with mixed tabs and spaces", () => { - const input = "\t line1\n\t line2\n\t line3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual(["line1\nline2\nline3", "\t "]); - }); - - test("should handle string with different leading whitespace lengths", () => { - const input = " line1\n line2\n line3"; - const output = dedentAndGetCommonWhitespace(input); - expect(output).toEqual([" line1\nline2\n line3", " "]); - }); -}); diff --git a/core/test/util/getLastNPathParts.test.ts b/core/test/util/getLastNPathParts.test.ts deleted file mode 100644 index ba72b4520..000000000 --- a/core/test/util/getLastNPathParts.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -// File generated by Continue -import { getLastNPathParts } from "../../util"; - -describe("getLastNPathParts", () => { - const SEP_REGEX = /[\\/]+/; - - beforeAll(() => { - // Setup code if needed - }); - - afterAll(() => { - // Teardown code if needed - }); - - test("returns the last N parts of a filepath with forward slashes", () => { - const filepath = "home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 2)).toBe("project/file.txt"); - }); - - test("returns the last N parts of a filepath with backward slashes", () => { - const filepath = "C:\\home\\user\\documents\\project\\file.txt"; - expect(getLastNPathParts(filepath, 3)).toBe("documents/project/file.txt"); - }); - - test("returns the last part if N is 1", () => { - const filepath = "/home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 1)).toBe("file.txt"); - }); - - test("returns the entire path if N is greater than the number of parts", () => { - const filepath = "home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 10)).toBe( - "home/user/documents/project/file.txt", - ); - }); - - test("returns an empty string if N is 0", () => { - const filepath = "home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 0)).toBe(""); - }); - - test("handles paths with mixed forward and backward slashes", () => { - const filepath = "home\\user/documents\\project/file.txt"; - expect(getLastNPathParts(filepath, 3)).toBe("documents/project/file.txt"); - }); - - test("handles edge case with empty filepath", () => { - const filepath = ""; - expect(getLastNPathParts(filepath, 2)).toBe(""); - }); -}); diff --git a/core/test/util/lcs.test.ts b/core/test/util/lcs.test.ts deleted file mode 100644 index da10cdf90..000000000 --- a/core/test/util/lcs.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -// This file is generated by Continue -import { longestCommonSubsequence } from "../../util/lcs"; - -describe("longestCommonSubsequence", () => { - beforeAll(() => { - // Setup if needed - }); - - afterAll(() => { - // Teardown if needed - }); - - test("should return the longest common subsequence for non-empty strings", () => { - expect(longestCommonSubsequence("abcde", "ace")).toBe("ace"); - expect(longestCommonSubsequence("abc", "abc")).toBe("abc"); - expect(longestCommonSubsequence("abc", "def")).toBe(""); - }); - - test("should handle cases where one or both strings are empty", () => { - expect(longestCommonSubsequence("", "abc")).toBe(""); - expect(longestCommonSubsequence("abc", "")).toBe(""); - expect(longestCommonSubsequence("", "")).toBe(""); - }); - - test("should handle cases with no common subsequence", () => { - expect(longestCommonSubsequence("abc", "def")).toBe(""); - expect(longestCommonSubsequence("xyz", "abc")).toBe(""); - }); - - test("should handle cases with special characters", () => { - expect(longestCommonSubsequence("a!@#b$c", "!@#$")).toBe("!@#$"); - expect(longestCommonSubsequence("a!@#b$c", "xyz")).toBe(""); - }); - - test("should handle long strings efficiently", () => { - const str1 = "a".repeat(1000) + "b".repeat(1000); - const str2 = "a".repeat(1000) + "c".repeat(1000); - expect(longestCommonSubsequence(str1, str2)).toBe("a".repeat(1000)); - }); -}); diff --git a/core/test/util/merge.test.ts b/core/test/util/merge.test.ts deleted file mode 100644 index cf7ba99d5..000000000 --- a/core/test/util/merge.test.ts +++ /dev/null @@ -1,100 +0,0 @@ -// File generated by Continue -import { mergeJson } from "../../util/merge"; - -describe("mergeJson", () => { - it("should merge two simple JSON objects", () => { - const first = { a: 1, b: 2 }; - const second = { b: 3, c: 4 }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: 1, b: 3, c: 4 }); - }); - - it("should overwrite values when mergeBehavior is \"overwrite\"", () => { - const first = { a: 1, b: 2 }; - const second = { b: 3, c: 4 }; - const result = mergeJson(first, second, "overwrite"); - expect(result).toEqual({ a: 1, b: 3, c: 4 }); - }); - - it("should merge nested objects", () => { - const first = { a: { b: 1 } }; - const second = { a: { c: 2 } }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: { b: 1, c: 2 } }); - }); - - it("should merge arrays without mergeKeys", () => { - const first = { a: [1, 2] }; - const second = { a: [3, 4] }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: [1, 2, 3, 4] }); - }); - - it("should merge arrays with mergeKeys", () => { - const first = { - a: [ - { id: 1, value: "first" }, - { id: 2, value: "second" }, - ], - }; - const second = { - a: [ - { id: 2, value: "updated" }, - { id: 3, value: "third" }, - ], - }; - const mergeKeys = { - a: (item1: any, item2: any) => item1.id === item2.id, - }; - const result = mergeJson(first, second, undefined, mergeKeys); - expect(result).toEqual({ - a: [ - { id: 1, value: "first" }, - { id: 2, value: "updated" }, - { id: 3, value: "third" }, - ], - }); - }); - - it("should handle non-object values correctly", () => { - const first = { a: 1, b: "string", c: true }; - const second = { a: 2, b: "new string", c: false }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: 2, b: "new string", c: false }); - }); - - it("should handle null and undefined values correctly", () => { - const first = { a: 1, b: null }; - const second = { b: 2, c: undefined }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: 1, b: 2, c: undefined }); - }); - - it("should handle empty objects", () => { - const first = {}; - const second = { a: 1 }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: 1 }); - }); - - it("should handle empty second object", () => { - const first = { a: 1 }; - const second = {}; - const result = mergeJson(first, second); - expect(result).toEqual({ a: 1 }); - }); - - it("should handle errors gracefully", () => { - const first = { a: 1 }; - const second = { b: () => {} }; // Functions are not serializable in JSON - const result = mergeJson(first, second); - expect(result).toEqual({ a: 1, b: expect.any(Function) }); - }); - - it("should merge deeply nested objects", () => { - const first = { a: { b: { c: 1 } } }; - const second = { a: { b: { d: 2 } } }; - const result = mergeJson(first, second); - expect(result).toEqual({ a: { b: { c: 1, d: 2 } } }); - }); -}); diff --git a/core/test/util/ranges/getRangeInString.test.ts b/core/test/util/ranges/getRangeInString.test.ts deleted file mode 100644 index e0a4df67a..000000000 --- a/core/test/util/ranges/getRangeInString.test.ts +++ /dev/null @@ -1,105 +0,0 @@ -// This file is generated by Continue - -import { getRangeInString } from "../../../util/ranges"; - -describe.skip("getRangeInString", () => { - let content: string; - - beforeEach(() => { - content = `Line 1 -Line 2 -Line 3 -Line 4 -Line 5`; - }); - - afterEach(() => { - content = ""; - }); - - test("should return substring within the same line", () => { - const range = { - start: { line: 1, character: 0 }, - end: { line: 1, character: 4 }, - }; - expect(getRangeInString(content, range)).toBe("Line"); - }); - - test("should return substring spanning multiple lines", () => { - const range = { - start: { line: 1, character: 2 }, - end: { line: 3, character: 3 }, - }; - const expected = `ne 2 -Line 3 -Line 4`; - expect(getRangeInString(content, range)).toBe(expected); - }); - - test("should handle range that starts and ends at the same character", () => { - const range = { - start: { line: 2, character: 0 }, - end: { line: 2, character: 0 }, - }; - expect(getRangeInString(content, range)).toBe("L"); - }); - - test("should handle range that spans entire content", () => { - const range = { - start: { line: 0, character: 0 }, - end: { line: 4, character: 5 }, - }; - expect(getRangeInString(content, range)).toBe(content); - }); - - test("should handle range that spans to the end of the last line", () => { - const range = { - start: { line: 3, character: 2 }, - end: { line: 4, character: 5 }, - }; - const expected = `ne 4 -Line 5`; - expect(getRangeInString(content, range)).toBe(expected); - }); - - test("should handle empty content", () => { - content = ""; - const range = { - start: { line: 0, character: 0 }, - end: { line: 0, character: 0 }, - }; - expect(getRangeInString(content, range)).toBe(""); - }); - - test("should handle invalid range (start line > end line)", () => { - const range = { - start: { line: 3, character: 0 }, - end: { line: 2, character: 0 }, - }; - expect(getRangeInString(content, range)).toBe(""); - }); - - test("should handle invalid range (start character > end character on same line)", () => { - const range = { - start: { line: 1, character: 5 }, - end: { line: 1, character: 2 }, - }; - expect(getRangeInString(content, range)).toBe(""); - }); - - test("should handle range that starts and ends at the same line but different characters", () => { - const range = { - start: { line: 1, character: 2 }, - end: { line: 1, character: 6 }, - }; - expect(getRangeInString(content, range)).toBe("ne 2"); - }); - - test("should handle range that starts and ends at the same line and same characters", () => { - const range = { - start: { line: 1, character: 2 }, - end: { line: 1, character: 2 }, - }; - expect(getRangeInString(content, range)).toBe("n"); - }); -}); diff --git a/core/test/util/ranges/intersection.test.ts b/core/test/util/ranges/intersection.test.ts deleted file mode 100644 index 397dbc1a8..000000000 --- a/core/test/util/ranges/intersection.test.ts +++ /dev/null @@ -1,123 +0,0 @@ -// This file is generated by Continue - -import { intersection } from "../../../util/ranges"; - -interface Position { - line: number; - character: number; -} - -interface Range { - start: Position; - end: Position; -} - -describe("intersection", () => { - let rangeA: Range; - let rangeB: Range; - - beforeEach(() => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 3, character: 5 }, - }; - - rangeB = { - start: { line: 2, character: 2 }, - end: { line: 4, character: 0 }, - }; - }); - - test("returns correct intersection for overlapping ranges", () => { - const result = intersection(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 2, character: 2 }, - end: { line: 3, character: 5 }, - }); - }); - - test("returns null for non-overlapping ranges", () => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 2, character: 0 }, - }; - - rangeB = { - start: { line: 3, character: 0 }, - end: { line: 4, character: 0 }, - }; - - const result = intersection(rangeA, rangeB); - expect(result).toBeNull(); - }); - - // TODO - test.skip("returns correct intersection for single line overlap", () => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 1, character: 5 }, - }; - - rangeB = { - start: { line: 1, character: 3 }, - end: { line: 2, character: 0 }, - }; - - const result = intersection(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 1, character: 3 }, - end: { line: 1, character: 5 }, - }); - }); - - test("returns null for single line non-overlapping ranges", () => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 1, character: 2 }, - }; - - rangeB = { - start: { line: 1, character: 3 }, - end: { line: 1, character: 5 }, - }; - - const result = intersection(rangeA, rangeB); - expect(result).toBeNull(); - }); - - test("returns correct intersection when one range is fully within another", () => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 4, character: 5 }, - }; - - rangeB = { - start: { line: 2, character: 2 }, - end: { line: 3, character: 3 }, - }; - - const result = intersection(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 2, character: 2 }, - end: { line: 3, character: 3 }, - }); - }); - - test("returns correct intersection when ranges touch at the edge", () => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 2, character: 0 }, - }; - - rangeB = { - start: { line: 2, character: 0 }, - end: { line: 3, character: 0 }, - }; - - const result = intersection(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 2, character: 0 }, - end: { line: 2, character: 0 }, - }); - }); -}); diff --git a/core/test/util/ranges/maxMinPosition.test.ts b/core/test/util/ranges/maxMinPosition.test.ts deleted file mode 100644 index 0468df1cd..000000000 --- a/core/test/util/ranges/maxMinPosition.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { maxPosition, minPosition } from "../../../util/ranges"; - -// This file is generated by Continue -interface Position { - line: number; - character: number; -} - -describe("Position comparison functions", () => { - let pos1: Position; - let pos2: Position; - let pos3: Position; - let pos4: Position; - - beforeAll(() => { - // Setup: Initializing positions - pos1 = { line: 1, character: 5 }; - pos2 = { line: 2, character: 3 }; - pos3 = { line: 1, character: 7 }; - pos4 = { line: 2, character: 3 }; // Same as pos2 to test equality - }); - - describe("maxPosition", () => { - it("should return the position with the greater line number", () => { - expect(maxPosition(pos1, pos2)).toEqual(pos2); - }); - - it("should return the position with the greater character number when lines are equal", () => { - expect(maxPosition(pos1, pos3)).toEqual(pos3); - }); - - it("should return the first position when both positions are equal", () => { - expect(maxPosition(pos2, pos4)).toEqual(pos2); - }); - - it("should handle positions with negative line numbers", () => { - const posNegative = { line: -1, character: 0 }; - expect(maxPosition(posNegative, pos1)).toEqual(pos1); - }); - - it("should handle positions with negative character numbers", () => { - const posNegativeChar = { line: 1, character: -1 }; - expect(maxPosition(posNegativeChar, pos1)).toEqual(pos1); - }); - }); - - describe("minPosition", () => { - it("should return the position with the lesser line number", () => { - expect(minPosition(pos1, pos2)).toEqual(pos1); - }); - - it("should return the position with the lesser character number when lines are equal", () => { - expect(minPosition(pos1, pos3)).toEqual(pos1); - }); - - it("should return the first position when both positions are equal", () => { - expect(minPosition(pos2, pos4)).toEqual(pos2); - }); - - it("should handle positions with negative line numbers", () => { - const posNegative = { line: -1, character: 0 }; - expect(minPosition(posNegative, pos1)).toEqual(posNegative); - }); - - it("should handle positions with negative character numbers", () => { - const posNegativeChar = { line: 1, character: -1 }; - expect(minPosition(posNegativeChar, pos1)).toEqual(posNegativeChar); - }); - }); -}); diff --git a/core/test/util/ranges/union.test.ts b/core/test/util/ranges/union.test.ts deleted file mode 100644 index 9d06a97b4..000000000 --- a/core/test/util/ranges/union.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { union } from "../../../util/ranges"; - -// This file is generated by Continue -interface Position { - line: number; - character: number; -} - -interface Range { - start: Position; - end: Position; -} - -describe("union", () => { - let rangeA: Range; - let rangeB: Range; - - beforeEach(() => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 3, character: 5 }, - }; - - rangeB = { - start: { line: 2, character: 3 }, - end: { line: 4, character: 2 }, - }; - }); - - test("should return the union of two overlapping ranges", () => { - const result = union(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 1, character: 0 }, - end: { line: 4, character: 2 }, - }); - }); - - test("should return the union when ranges are identical", () => { - const result = union(rangeA, rangeA); - expect(result).toEqual(rangeA); - }); - - test("should return the union when ranges do not overlap but are adjacent", () => { - rangeB = { - start: { line: 3, character: 5 }, - end: { line: 4, character: 2 }, - }; - const result = union(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 1, character: 0 }, - end: { line: 4, character: 2 }, - }); - }); - - test("should return the union when one range is completely within the other", () => { - rangeB = { - start: { line: 2, character: 1 }, - end: { line: 2, character: 4 }, - }; - const result = union(rangeA, rangeB); - expect(result).toEqual(rangeA); - }); - - test("should return the union when ranges do not overlap and are not adjacent", () => { - rangeB = { - start: { line: 4, character: 3 }, - end: { line: 5, character: 1 }, - }; - const result = union(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 1, character: 0 }, - end: { line: 5, character: 1 }, - }); - }); - - // TODO - test.skip("should handle edge case where start and end lines are the same", () => { - rangeA = { - start: { line: 1, character: 0 }, - end: { line: 1, character: 5 }, - }; - rangeB = { - start: { line: 1, character: 3 }, - end: { line: 1, character: 7 }, - }; - const result = union(rangeA, rangeB); - expect(result).toEqual({ - start: { line: 1, character: 0 }, - end: { line: 1, character: 7 }, - }); - }); -}); diff --git a/core/util/chatDescriber.ts b/core/util/chatDescriber.ts index b52fd7696..e44ec85a0 100644 --- a/core/util/chatDescriber.ts +++ b/core/util/chatDescriber.ts @@ -4,20 +4,7 @@ import { removeQuotesAndEscapes } from "."; import type { FromCoreProtocol, ToCoreProtocol } from "../protocol"; import type { IMessenger } from "./messenger"; - -/** - * Removes code blocks from a message. - * - * Return modified message text. - */ -function removeCodeBlocksAndTrim(msgText: string): string { - const codeBlockRegex = /```[\s\S]*?```/g; - - // Remove code blocks from the message text - const textWithoutCodeBlocks = msgText.replace(codeBlockRegex, ""); - - return textWithoutCodeBlocks.trim(); -} +import { removeCodeBlocksAndTrim } from "./markdown"; export class ChatDescriber { static prompt: string | undefined = diff --git a/core/test/util/history.test.ts b/core/util/history.test.ts similarity index 96% rename from core/test/util/history.test.ts rename to core/util/history.test.ts index 270e81248..048087564 100644 --- a/core/test/util/history.test.ts +++ b/core/util/history.test.ts @@ -1,8 +1,8 @@ import { v4 as uuidv4 } from "uuid"; -import { Session } from "../.."; -import historyManager from "../../util/history"; -import { getSessionFilePath } from "../../util/paths"; +import { Session } from ".."; +import historyManager from "./history"; +import { getSessionFilePath } from "./paths"; const sessionId = uuidv4(); const testSession: Session = { diff --git a/core/util/markdown.ts b/core/util/markdown.ts new file mode 100644 index 000000000..f1cb8929c --- /dev/null +++ b/core/util/markdown.ts @@ -0,0 +1,13 @@ +/** + * Removes code blocks from a message. + * + * Return modified message text. + */ +export function removeCodeBlocksAndTrim(text: string): string { + const codeBlockRegex = /```[\s\S]*?```/g; + + // Remove code blocks from the message text + const textWithoutCodeBlocks = text.replace(codeBlockRegex, ""); + + return textWithoutCodeBlocks.trim(); +} diff --git a/core/util/tts.ts b/core/util/tts.ts index efae302c2..bf62c6e29 100644 --- a/core/util/tts.ts +++ b/core/util/tts.ts @@ -1,6 +1,8 @@ import { exec, ChildProcess } from "child_process"; import os from "node:os"; +import { removeCodeBlocksAndTrim } from "./markdown"; + import type { IMessenger } from "./messenger"; import type { FromCoreProtocol, ToCoreProtocol } from "../protocol"; @@ -8,27 +10,13 @@ import type { FromCoreProtocol, ToCoreProtocol } from "../protocol"; // a zombie after executing .kill() const ttsKillTimeout: number = 5000; -/** - * Removes code blocks from a message. - * - * Return modified message text. - */ -function removeCodeBlocks(msgText: string): string { - const codeBlockRegex = /```[\s\S]*?```/g; - - // Remove code blocks from the message text - const textWithoutCodeBlocks = msgText.replace(codeBlockRegex, ""); - - return textWithoutCodeBlocks.trim(); -} - /** * Cleans a message text to safely be used in 'exec' context on host. * * Return modified message text. */ -function sanitizeMessageForTTS(message: string): string { - message = removeCodeBlocks(message); +export function sanitizeMessageForTTS(message: string): string { + message = removeCodeBlocksAndTrim(message); // Remove or replace problematic characters message = message diff --git a/core/util/withExponentialBackoff.test.ts b/core/util/withExponentialBackoff.test.ts deleted file mode 100644 index 07298dfe7..000000000 --- a/core/util/withExponentialBackoff.test.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { - withExponentialBackoff, - RETRY_AFTER_HEADER, -} from "./withExponentialBackoff"; -const MAX_RETRIES = 4; -const INITIAL_DELAY = 0.01; // so the tests run fast - -describe("withExponentialBackoff", () => { - // beforeEach(() => { - // jest.useFakeTimers(); - // jest.clearAllMocks(); - // }); - - // afterEach(() => { - // jest.useRealTimers(); - // }); - - it("should return result on first successful API call", async () => { - const apiCall = jest.fn().mockResolvedValue("success"); - - const result = await withExponentialBackoff(apiCall, MAX_RETRIES); - - expect(result).toBe("success"); - expect(apiCall).toHaveBeenCalledTimes(1); - }); - it("should retry on API rate limit and succeed", async () => { - const apiCall = jest - .fn() - .mockRejectedValueOnce({ - response: { - status: 429, - headers: new Map([[RETRY_AFTER_HEADER, "1"]]), - }, - }) - .mockResolvedValue("success"); - - const start = Date.now(); - - const result = await withExponentialBackoff(apiCall, MAX_RETRIES); - - const duration = (Date.now() - start) / 1000; - - expect(result).toBe("success"); - expect(apiCall).toHaveBeenCalledTimes(2); - expect(duration).toBeGreaterThanOrEqual(1); - }); - it("should stop retrying after maxRetries and throw error", async () => { - const apiCall = jest.fn().mockRejectedValue({ - response: { - status: 429, - headers: new Map(), - }, - }); - - await expect( - withExponentialBackoff(apiCall, MAX_RETRIES, INITIAL_DELAY), - ).rejects.toThrow("Failed to make API call after multiple retries"); - expect(apiCall).toHaveBeenCalledTimes(MAX_RETRIES); - }); - - it("should throw on non-rate limit errors", async () => { - const apiCall = jest.fn().mockRejectedValue(new Error("Server Error")); - - await expect(withExponentialBackoff(apiCall, MAX_RETRIES)).rejects.toThrow( - "Server Error", - ); - expect(apiCall).toHaveBeenCalledTimes(1); - }); -});