From 07d3bd4282a66683c75c8740c54b8482a35bdef2 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 5 Dec 2024 18:56:06 -0800 Subject: [PATCH] global context and core test iterations --- .continue/prompts/core-unit-test.prompt | 5 +- core/package-lock.json | 111 ++++++++-- core/package.json | 4 +- core/test/jest.setup-after-env.ts | 3 +- core/util/GlobalContext.test.ts | 262 +++++++++--------------- 5 files changed, 202 insertions(+), 183 deletions(-) diff --git a/.continue/prompts/core-unit-test.prompt b/.continue/prompts/core-unit-test.prompt index 88c95ff06..581c59045 100644 --- a/.continue/prompts/core-unit-test.prompt +++ b/.continue/prompts/core-unit-test.prompt @@ -8,7 +8,7 @@ Use best practices. Be clear and consise. Aim for 100% code coverage where reasonable. Multiple tests can be written, split up tests for best clarity and readability. Only use typescript, and if the file/code is not typescript, warn the user. -Use ESM to import modules, do NOT use `require` +IMPORTANT Use ESM to import modules, do NOT use `require` anywhere Tests are to be described in an adjacent file with a path identical except for a `.test.ts` rather than a `.ts` file extension Use double quotes (or backticks if needed) for strings @@ -31,7 +31,8 @@ Jest testing setup includes Do NOT write tests for any files in `core/test`, only use them as helpers for testing other files. If no other files are provided, warn the user and write no tests. -IMPORTANT: Avoid using pure mocks when possible; perform actual operations and use `jest.spyOn` so that functions are actually invoked. +IMPORTANT: Do NOT mock the fixtures above other than using `jest.spyOn`. DO mock 3rd party modules, etc. when sensible. +When using jest.fn, always declare the type, e.g. jest.fn>().mockResolvedValue("hi") Instead, generate actual mock files and data for operations Pure mocks should only be used to emulate specific network responses/error or hard-to-duplicate errors, or to prevent long-duration tests diff --git a/core/package-lock.json b/core/package-lock.json index 41aeca5cb..694e4aab2 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -77,7 +77,7 @@ "@google/generative-ai": "^0.11.4", "@types/diff": "^5.2.2", "@types/follow-redirects": "^1.14.4", - "@types/jest": "^29.5.12", + "@types/jest": "^29.5.14", "@types/jquery": "^3.5.29", "@types/jsdom": "^21.1.6", "@types/mozilla-readability": "^0.2.1", @@ -96,7 +96,7 @@ "myers-diff": "^2.1.0", "onnxruntime-common": "1.14.0", "onnxruntime-web": "1.14.0", - "ts-jest": "^29.1.1" + "ts-jest": "^29.2.5" }, "engines": { "node": ">=20.11.0" @@ -5529,9 +5529,9 @@ } }, "node_modules/@types/jest": { - "version": "29.5.12", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.12.tgz", - "integrity": "sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==", + "version": "29.5.14", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.14.tgz", + "integrity": "sha512-ZN+4sdnLUbo8EVvVc2ao0GFW6oVrQRPn4K2lglySj7APvSrgzxHiNNK99us4WDMi57xxA2yggblIAMNhXOotLQ==", "dev": true, "dependencies": { "expect": "^29.0.0", @@ -6403,6 +6403,12 @@ "node": ">=4" } }, + "node_modules/async": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", + "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==", + "dev": true + }, "node_modules/async-mutex": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz", @@ -7925,6 +7931,21 @@ "resolved": "https://registry.npmjs.org/eight-colors/-/eight-colors-1.3.0.tgz", "integrity": "sha512-hVoK898cR71ADj7L1LZWaECLaSkzzPtqGXIaKv4K6Pzb72QgjLVsQaNI+ELDQQshzFvgp5xTPkaYkPGqw3YR+g==" }, + "node_modules/ejs": { + "version": "3.1.10", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.10.tgz", + "integrity": "sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA==", + "dev": true, + "dependencies": { + "jake": "^10.8.5" + }, + "bin": { + "ejs": "bin/cli.js" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/electron-to-chromium": { "version": "1.4.776", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.776.tgz", @@ -8845,6 +8866,27 @@ "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" }, + "node_modules/filelist": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz", + "integrity": "sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==", + "dev": true, + "dependencies": { + "minimatch": "^5.0.1" + } + }, + "node_modules/filelist/node_modules/minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", @@ -10352,6 +10394,46 @@ "@pkgjs/parseargs": "^0.11.0" } }, + "node_modules/jake": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/jake/-/jake-10.9.2.tgz", + "integrity": "sha512-2P4SQ0HrLQ+fw6llpLnOaGAvN2Zu6778SJMrCUwns4fOoG9ayrTiZk3VV8sCPkVZF8ab0zksVpS8FDY5pRCNBA==", + "dev": true, + "dependencies": { + "async": "^3.2.3", + "chalk": "^4.0.2", + "filelist": "^1.0.4", + "minimatch": "^3.1.2" + }, + "bin": { + "jake": "bin/cli.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/jake/node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/jake/node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, "node_modules/jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/jest/-/jest-29.7.0.tgz", @@ -15095,19 +15177,20 @@ } }, "node_modules/ts-jest": { - "version": "29.1.3", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.1.3.tgz", - "integrity": "sha512-6L9qz3ginTd1NKhOxmkP0qU3FyKjj5CPoY+anszfVn6Pmv/RIKzhiMCsH7Yb7UvJR9I2A64rm4zQl531s2F1iw==", + "version": "29.2.5", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.5.tgz", + "integrity": "sha512-KD8zB2aAZrcKIdGk4OwpJggeLcH1FgrICqDSROWqlnJXGCXK4Mn6FcdK2B6670Xr73lHMG1kHw8R87A0ecZ+vA==", "dev": true, "dependencies": { - "bs-logger": "0.x", - "fast-json-stable-stringify": "2.x", + "bs-logger": "^0.2.6", + "ejs": "^3.1.10", + "fast-json-stable-stringify": "^2.1.0", "jest-util": "^29.0.0", "json5": "^2.2.3", - "lodash.memoize": "4.x", - "make-error": "1.x", - "semver": "^7.5.3", - "yargs-parser": "^21.0.1" + "lodash.memoize": "^4.1.2", + "make-error": "^1.3.6", + "semver": "^7.6.3", + "yargs-parser": "^21.1.1" }, "bin": { "ts-jest": "cli.js" diff --git a/core/package.json b/core/package.json index 79559a524..ba35b0cfd 100644 --- a/core/package.json +++ b/core/package.json @@ -19,7 +19,7 @@ "@google/generative-ai": "^0.11.4", "@types/diff": "^5.2.2", "@types/follow-redirects": "^1.14.4", - "@types/jest": "^29.5.12", + "@types/jest": "^29.5.14", "@types/jquery": "^3.5.29", "@types/jsdom": "^21.1.6", "@types/mozilla-readability": "^0.2.1", @@ -38,7 +38,7 @@ "myers-diff": "^2.1.0", "onnxruntime-common": "1.14.0", "onnxruntime-web": "1.14.0", - "ts-jest": "^29.1.1" + "ts-jest": "^29.2.5" }, "dependencies": { "@aws-sdk/client-bedrock-runtime": "^3.620.1", diff --git a/core/test/jest.setup-after-env.ts b/core/test/jest.setup-after-env.ts index 53f94ecea..0d8d904fb 100644 --- a/core/test/jest.setup-after-env.ts +++ b/core/test/jest.setup-after-env.ts @@ -1,9 +1,10 @@ -import fs from "fs"; import { TextDecoder, TextEncoder } from "util"; import { jest } from "@jest/globals"; import fetch, { Request, Response } from "node-fetch"; +console.log("Process debug", process.env.DEBUG); + if (process.env.DEBUG === "jest") { jest.setTimeout(5 * 60 * 1000); } diff --git a/core/util/GlobalContext.test.ts b/core/util/GlobalContext.test.ts index d52ecc5e1..6014a1d1d 100644 --- a/core/util/GlobalContext.test.ts +++ b/core/util/GlobalContext.test.ts @@ -1,206 +1,140 @@ // core/util/GlobalContext.test.ts +import { GlobalContext, GlobalContextType } from "./GlobalContext"; import fs from "node:fs"; -import path from "node:path"; - -import { GlobalContext } from "./GlobalContext"; +import path from "path"; import { getGlobalContextFilePath } from "./paths"; -import { TEST_DIR } from "../test/testDir"; describe("GlobalContext", () => { - const globalContextFilePath = path.join(TEST_DIR, "globalContext.json"); - - beforeAll(() => { - // Ensure the test directory exists - if (!fs.existsSync(TEST_DIR)) { - fs.mkdirSync(TEST_DIR, { recursive: true }); - } - }); + let globalContext: GlobalContext; + const globalContextFilePath = getGlobalContextFilePath(); beforeEach(() => { - // Mock getGlobalContextFilePath to return the test path - jest - .spyOn(require("./paths"), "getGlobalContextFilePath") - .mockReturnValue(globalContextFilePath); - }); - - afterEach(() => { - // Clean up the test global context file after each test + // Remove the global context file if it exists if (fs.existsSync(globalContextFilePath)) { fs.unlinkSync(globalContextFilePath); } - jest.restoreAllMocks(); + globalContext = new GlobalContext(); }); - test("update writes data when file does not exist", () => { - const globalContext = new GlobalContext(); - globalContext.update("indexingPaused", true); - - const data = fs.readFileSync(globalContextFilePath, "utf-8"); - const parsed = JSON.parse(data); - expect(parsed).toEqual({ indexingPaused: true }); - }); - - test("update updates data when file exists", () => { - const globalContext = new GlobalContext(); - // First write initial data - globalContext.update("indexingPaused", true); - // Then update another property - globalContext.update("selectedTabAutocompleteModel", "model-1"); - - const data = fs.readFileSync(globalContextFilePath, "utf-8"); - const parsed = JSON.parse(data); - expect(parsed).toEqual({ - indexingPaused: true, - selectedTabAutocompleteModel: "model-1", - }); - }); - - test("get returns correct data when file exists", () => { - const globalContext = new GlobalContext(); - // First write data - globalContext.update("indexingPaused", true); - globalContext.update("selectedTabAutocompleteModel", "model-1"); - - const indexingPaused = globalContext.get("indexingPaused"); - const selectedModel = globalContext.get("selectedTabAutocompleteModel"); - - expect(indexingPaused).toBe(true); - expect(selectedModel).toBe("model-1"); - }); - - test("get returns undefined when file does not exist", () => { - const globalContext = new GlobalContext(); - - const indexingPaused = globalContext.get("indexingPaused"); - expect(indexingPaused).toBeUndefined(); - }); - - test("get returns undefined when key does not exist", () => { - const globalContext = new GlobalContext(); - globalContext.update("indexingPaused", true); - - const nonExistentValue = globalContext.get("nonExistentKey" as any); - - expect(nonExistentValue).toBeUndefined(); - }); - - test("update handles invalid JSON in existing file", () => { - const globalContext = new GlobalContext(); - // Write invalid JSON to the file - fs.writeFileSync(globalContextFilePath, "{ invalid json"); - - // Mock console.warn to capture warning messages - const consoleWarnMock = jest - .spyOn(console, "warn") - .mockImplementation(() => {}); - - // Attempt to update - globalContext.update("indexingPaused", true); - - // Expect console.warn to have been called - expect(consoleWarnMock).toHaveBeenCalledWith( - expect.stringMatching(/Error updating global context: .*/), - ); - + afterEach(() => { // Clean up - consoleWarnMock.mockRestore(); + if (fs.existsSync(globalContextFilePath)) { + fs.unlinkSync(globalContextFilePath); + } }); - test("get handles invalid JSON in existing file", () => { - const globalContext = new GlobalContext(); - // Write invalid JSON to the file - fs.writeFileSync(globalContextFilePath, "{ invalid json"); - - // Mock console.warn to capture warning messages - const consoleWarnMock = jest - .spyOn(console, "warn") - .mockImplementation(() => {}); - - // Attempt to get + it("should update and get a boolean value correctly", () => { + globalContext.update("indexingPaused", true); const value = globalContext.get("indexingPaused"); - - // Expect value to be undefined - expect(value).toBeUndefined(); - - // Expect console.warn to have been called - expect(consoleWarnMock).toHaveBeenCalledWith( - expect.stringMatching(/Error parsing global context: .*/), - ); - - // Clean up - consoleWarnMock.mockRestore(); + expect(value).toBe(true); }); - test("update handles existing file with non-JSON content", () => { - const globalContext = new GlobalContext(); - // Write non-JSON content to the file - fs.writeFileSync(globalContextFilePath, "Just some text"); + it("should update and get a string value correctly", () => { + globalContext.update("selectedTabAutocompleteModel", "test-model"); + const value = globalContext.get("selectedTabAutocompleteModel"); + expect(value).toBe("test-model"); + }); - // Mock console.warn to capture warning messages - const consoleWarnMock = jest - .spyOn(console, "warn") - .mockImplementation(() => {}); + it("should update and get an object value correctly", () => { + const workspaceIdentifier = "workspace-1"; + globalContext.update("lastSelectedProfileForWorkspace", { + [workspaceIdentifier]: "profile-1", + }); + + const value = globalContext.get("lastSelectedProfileForWorkspace"); + expect(value).toEqual({ "workspace-1": "profile-1" }); + }); + + it("should handle non-existent global context file gracefully on get", () => { + const value = globalContext.get("indexingPaused"); + expect(value).toBeUndefined(); + }); + + it("should handle non-existent global context file gracefully on update", () => { + globalContext.update("indexingPaused", true); + const value = globalContext.get("indexingPaused"); + expect(value).toBe(true); + }); + + it("should overwrite existing values", () => { + globalContext.update("indexingPaused", true); + expect(globalContext.get("indexingPaused")).toBe(true); - // Attempt to update globalContext.update("indexingPaused", false); - - // Expect console.warn to have been called - expect(consoleWarnMock).toHaveBeenCalledWith( - expect.stringMatching(/Error updating global context: .*/), - ); - - // Verify that the file now contains the updated key - const data = fs.readFileSync(globalContextFilePath, "utf-8"); - const parsed = JSON.parse(data); - expect(parsed).toEqual({ indexingPaused: false }); - - // Clean up - consoleWarnMock.mockRestore(); + expect(globalContext.get("indexingPaused")).toBe(false); }); - test("get handles existing file with non-JSON content", () => { - const globalContext = new GlobalContext(); - // Write non-JSON content to the file - fs.writeFileSync(globalContextFilePath, "Just some text"); + it("should handle JSON parsing errors gracefully on get", () => { + // Write invalid JSON to the file + fs.writeFileSync(globalContextFilePath, "{ invalid json }"); - // Mock console.warn to capture warning messages const consoleWarnMock = jest .spyOn(console, "warn") .mockImplementation(() => {}); - // Attempt to get const value = globalContext.get("indexingPaused"); - - // Expect value to be undefined expect(value).toBeUndefined(); - - // Expect console.warn to have been called expect(consoleWarnMock).toHaveBeenCalledWith( - expect.stringMatching(/Error parsing global context: .*/), + expect.stringContaining("Error parsing global context") ); // Clean up consoleWarnMock.mockRestore(); }); - test("update writes complex data types", () => { - const globalContext = new GlobalContext(); - const complexData = { workspace1: "profile1", workspace2: "profile2" }; - globalContext.update("lastSelectedProfileForWorkspace", complexData); + it("should handle JSON parsing errors gracefully on update", () => { + // Write invalid JSON to the file + fs.writeFileSync(globalContextFilePath, "{ invalid json }"); - const data = fs.readFileSync(globalContextFilePath, "utf-8"); - const parsed = JSON.parse(data); - expect(parsed).toEqual({ - lastSelectedProfileForWorkspace: complexData, - }); + const consoleWarnMock = jest + .spyOn(console, "warn") + .mockImplementation(() => {}); + + // Attempt to update + globalContext.update("indexingPaused", true); + + // The update should have been skipped due to JSON parsing error + // So attempting to get the value should return undefined + const value = globalContext.get("indexingPaused"); + expect(value).toBeUndefined(); + + expect(consoleWarnMock).toHaveBeenCalledWith( + expect.stringContaining("Error updating global context") + ); + + // Clean up + consoleWarnMock.mockRestore(); }); - test("get retrieves complex data types", () => { - const globalContext = new GlobalContext(); - const complexData = { workspace1: "profile1", workspace2: "profile2" }; - globalContext.update("lastSelectedProfileForWorkspace", complexData); + it("should update and retrieve multiple values correctly", () => { + globalContext.update("indexingPaused", true); + globalContext.update("selectedTabAutocompleteModel", "test-model"); + globalContext.update("hasDismissedConfigTsNoticeJetBrains", false); - const retrievedData = globalContext.get("lastSelectedProfileForWorkspace"); - expect(retrievedData).toEqual(complexData); + expect(globalContext.get("indexingPaused")).toBe(true); + expect(globalContext.get("selectedTabAutocompleteModel")).toBe( + "test-model" + ); + expect(globalContext.get("hasDismissedConfigTsNoticeJetBrains")).toBe( + false + ); }); -}); + + it("should handle updating hasAlreadyCreatedAPromptFile correctly", () => { + globalContext.update("hasAlreadyCreatedAPromptFile", true); + expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(true); + }); + + it("should handle curEmbeddingsProviderId correctly", () => { + globalContext.update("curEmbeddingsProviderId", "test-provider-id"); + expect(globalContext.get("curEmbeddingsProviderId")).toBe( + "test-provider-id" + ); + }); + + it("should not crash or throw when getting a key that hasn't been set", () => { + expect( + globalContext.get("hasAlreadyCreatedAPromptFile") + ).toBeUndefined(); + }); +}); \ No newline at end of file