From 34166b6af9fc825338e14a449236b061b10d9549 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 28 May 2025 15:31:18 -0700 Subject: [PATCH] change to 'gitDiffCache' and add tests --- core/autocomplete/snippets/getAllSnippets.ts | 2 +- .../snippets/gitDiffCache.test.ts | 84 +++++++++++++++++++ .../{diffSnippetCache.ts => gitDiffCache.ts} | 3 + core/core.ts | 5 +- .../v1/slashCommandFromPromptFile.ts | 2 +- core/tools/implementations/viewDiff.ts | 2 +- manual-testing-sandbox/test.kt | 10 ++- 7 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 core/autocomplete/snippets/gitDiffCache.test.ts rename core/autocomplete/snippets/{diffSnippetCache.ts => gitDiffCache.ts} (95%) diff --git a/core/autocomplete/snippets/getAllSnippets.ts b/core/autocomplete/snippets/getAllSnippets.ts index d872c3842..8fa51a99b 100644 --- a/core/autocomplete/snippets/getAllSnippets.ts +++ b/core/autocomplete/snippets/getAllSnippets.ts @@ -3,7 +3,7 @@ import { findUriInDirs } from "../../util/uri"; import { ContextRetrievalService } from "../context/ContextRetrievalService"; import { GetLspDefinitionsFunction } from "../types"; import { HelperVars } from "../util/HelperVars"; -import { getDiffsFromCache } from "./diffSnippetCache"; +import { getDiffsFromCache } from "./gitDiffCache"; import { AutocompleteClipboardSnippet, diff --git a/core/autocomplete/snippets/gitDiffCache.test.ts b/core/autocomplete/snippets/gitDiffCache.test.ts new file mode 100644 index 000000000..ee7d338a8 --- /dev/null +++ b/core/autocomplete/snippets/gitDiffCache.test.ts @@ -0,0 +1,84 @@ +import { GitDiffCache } from "./gitDiffCache"; + +beforeEach(() => { + // Add this line to GitDiffCache class to make instance accessible + (GitDiffCache as any).instance = null; +}); + +test("GitDiffCache returns cached results within cache time", async () => { + const mockDiff = ["file1.ts", "file2.ts"]; + const getDiffFn = jest.fn().mockResolvedValue(mockDiff); + const cache = GitDiffCache.getInstance(getDiffFn, 1); // 1 second cache + + const result1 = await cache.get(); + const result2 = await cache.get(); + + expect(result1).toEqual(mockDiff); + expect(result2).toEqual(mockDiff); + expect(getDiffFn).toHaveBeenCalledTimes(1); +}); + +test("GitDiffCache refreshes cache after expiration", async () => { + const mockDiff = ["file1.ts"]; + const getDiffFn = jest.fn().mockResolvedValue(mockDiff); + const cache = GitDiffCache.getInstance(getDiffFn, 0.1); // 100ms cache + + const result1 = await cache.get(); + await new Promise((resolve) => setTimeout(resolve, 200)); // Wait for cache to expire + const result2 = await cache.get(); + + expect(getDiffFn).toHaveBeenCalledTimes(2); +}); + +test("GitDiffCache returns empty array on error", async () => { + const getDiffFn = jest.fn().mockRejectedValue(new Error("Git error")); + const cache = GitDiffCache.getInstance(getDiffFn); + + const result = await cache.get(); + expect(result).toEqual([]); +}); + +test("GitDiffCache reuses pending request", async () => { + const mockDiff = ["file1.ts"]; + let resolvePromise: (value: string[]) => void; + const getDiffFn = jest.fn().mockImplementation(() => { + return new Promise((resolve) => { + resolvePromise = resolve; + }); + }); + + const cache = GitDiffCache.getInstance(getDiffFn); + + const promise1 = cache.get(); + const promise2 = cache.get(); + + resolvePromise!(mockDiff); + + const [result1, result2] = await Promise.all([promise1, promise2]); + + expect(result1).toEqual(mockDiff); + expect(result2).toEqual(mockDiff); + expect(getDiffFn).toHaveBeenCalledTimes(1); +}); + +test("GitDiffCache invalidate clears cache", async () => { + const mockDiff = ["file1.ts"]; + const getDiffFn = jest.fn().mockResolvedValue(mockDiff); + const cache = GitDiffCache.getInstance(getDiffFn); + + await cache.get(); + cache.invalidate(); + await cache.get(); + + expect(getDiffFn).toHaveBeenCalledTimes(2); +}); + +test("GitDiffCache maintains singleton instance", () => { + const getDiffFn1 = jest.fn(); + const getDiffFn2 = jest.fn(); + + const instance1 = GitDiffCache.getInstance(getDiffFn1); + const instance2 = GitDiffCache.getInstance(getDiffFn2); + + expect(instance1).toBe(instance2); +}); diff --git a/core/autocomplete/snippets/diffSnippetCache.ts b/core/autocomplete/snippets/gitDiffCache.ts similarity index 95% rename from core/autocomplete/snippets/diffSnippetCache.ts rename to core/autocomplete/snippets/gitDiffCache.ts index b26bdcc99..3dfed8b20 100644 --- a/core/autocomplete/snippets/diffSnippetCache.ts +++ b/core/autocomplete/snippets/gitDiffCache.ts @@ -31,6 +31,9 @@ export class GitDiffCache { this.cachedDiff = diff; this.lastFetchTime = Date.now(); return this.cachedDiff; + } catch (e) { + console.error("Error fetching git diff:", e); + return []; } finally { this.pendingRequest = null; } diff --git a/core/core.ts b/core/core.ts index 32eb4a8bd..b27641673 100644 --- a/core/core.ts +++ b/core/core.ts @@ -45,10 +45,7 @@ import { } from "."; import { ConfigYaml } from "@continuedev/config-yaml"; -import { - getDiffFn, - GitDiffCache, -} from "./autocomplete/snippets/diffSnippetCache"; +import { getDiffFn, GitDiffCache } from "./autocomplete/snippets/gitDiffCache"; import { isLocalAssistantFile } from "./config/loadLocalAssistants"; import { setupBestConfig, diff --git a/core/promptFiles/v1/slashCommandFromPromptFile.ts b/core/promptFiles/v1/slashCommandFromPromptFile.ts index 461db9c19..22e626c75 100644 --- a/core/promptFiles/v1/slashCommandFromPromptFile.ts +++ b/core/promptFiles/v1/slashCommandFromPromptFile.ts @@ -1,5 +1,5 @@ import { ContinueSDK, SlashCommand } from "../.."; -import { getDiffsFromCache } from "../../autocomplete/snippets/diffSnippetCache"; +import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache"; import { renderChatMessage } from "../../util/messageContent"; import { getLastNPathParts } from "../../util/uri"; import { parsePromptFileV1V2 } from "../v2/parsePromptFileV1V2"; diff --git a/core/tools/implementations/viewDiff.ts b/core/tools/implementations/viewDiff.ts index d09f05944..fd1dfdb8a 100644 --- a/core/tools/implementations/viewDiff.ts +++ b/core/tools/implementations/viewDiff.ts @@ -1,5 +1,5 @@ import { ToolImpl } from "."; -import { getDiffsFromCache } from "../../autocomplete/snippets/diffSnippetCache"; +import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache"; export const viewDiffImpl: ToolImpl = async (args, extras) => { const diffs = await getDiffsFromCache(extras.ide); // const diffs = await extras.ide.getDiff(true); diff --git a/manual-testing-sandbox/test.kt b/manual-testing-sandbox/test.kt index 512c34c25..b665bba3f 100644 --- a/manual-testing-sandbox/test.kt +++ b/manual-testing-sandbox/test.kt @@ -29,8 +29,10 @@ class Test { result = 0.0 return this } + + + + + + } - - - -